-1

I have this snippet.

SELECT 'SELECT * FROM ' + OBJECT_SCHEMA_NAME(o.object_id, DB_ID(${20:})) + '.' + name,
       *
FROM ${20/$/./}sys.all_objects o
WHERE name LIKE '%${10:hadr}%'
ORDER BY o.name;

And this is how it works:

gif of usage of snippet

When the user types something in the function DB_ID(), I hope the content the user typed appears before sys.all_objects AND append an additional .. It already works like this as it shown in the above gif. However, I also hope if the user types nothing in the function DB_ID(), don't add . before sys.all_objects. Is this possible?

3
  • If you have snippet code already, you should show what you have. Commented Jan 22, 2023 at 3:20
  • Please write a more descriptive title, maybe something like, "How do I get a snippet to insert a character only if the user typed something?" Check out How to Ask for tips on how to write a good title. Commented Jan 22, 2023 at 3:26
  • It'd help to make a minimal reproducible example. You can copy the one from my answer if you want. Commented Jan 22, 2023 at 3:33

2 Answers 2

2

No need to add the : in field 2:

DB_ID(${2})

Use field 2

${2/(.*)/$1${1:+.}/}
  • capture all the typed text: (.*)
  • replace it with all the typed text: $1
  • followed by a . if the typed text is not empty: ${1:+.}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use lookbehind to assert that there's something in that field: (?<=.)$. For a minimal example, let's say this is the original snippet:

foo($1); ${1/$/./}bar()

Change it to:

foo($1); ${1/(?<=.)$/./}bar()

If I type something, e.g. x, then press Tab, I get:

foo(x); x.bar()

If I don't type anything then press Tab, I get:

foo(); bar()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.