Sublime Forum

Logic in snippets

#1

I think this is a no-brainer.

Snippets are are one of the reasons why I made a switch to Sublime Text and I feel others feel the same. Sublime Text would be even cooler, and more people would be using it if you were able to do logic in snippets. it doesn’t have to be anything complex.

I think being able to store $1, $2… values in variables and then being able to run “if-then” logic off of those stored values would help 90% of users.

In my particular scenario, I created a documentation snippet where I am describing input and output variables of JavaScript code as such (pseudo code):

created by {$1:my name goes here} on {$2:date} (oh that's another thing, can we the ability for snippets to auto insert current date at the time of creation?) Inputs: {$3:var1} - {$4:var1 type} {$5:var2} - {$6:var2 type} {$7:var3} - {$8:var3 type} {$9:var4} - {$10:var4 type} ... ... ... Details: $3 - {$11: var1 details} $4 - {$12: var 2 details} ... ...

So this works great but if the code i’m documenting only has 1 input variable, I spend a lot of time deleting tabbed sections.
It would be nice if I could do: "if $5 == null/empty, ignore/skip asking for var 3 and var 4 data

0 Likes

Snippets with functions and multiple values - is that possible?
#2

Since snippets use the boost implementation of regular expressions (which supports conditionals!), you can actually do lots of stuff with some regex replacements.
I once made an example here: stackoverflow.com/a/25901738/1327727

Regarding the insertion of the current time/date: I can think of two ways to do this.

  1. Auto-generating tmPreferences files that define “shellVariables”, see docs.sublimetext.info/en/latest/ … f-settings
  2. (easier) Using a macro like github.com/FichteFoll/InsertDate#snippet-macros and utilizing “next_field” and “prev_field” commands.
1 Like

Snippets with functions and multiple values - is that possible?