Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I get a snippet to insert a character only if the user typed something?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading