Sublime Forum

[SOLVED] @Snippets: Is it possible to create an alternate placeholder for $SELECTION?

#1

Usage Case:

  • a snippet contains multiple placeholders, one of which is $SELECTION
  • in the event that no text is selected when the snippet is executed, the $SELECTION placeholder should display an alternate value which:
  • provides information as to its purpose
  • acts as a visual indicator so that the value is not accidentally left blank

 
Implementation:

  • returns $SELECTION if it exists
  • otherwise, returns __PLACEHOLDER__

 

 



 
This mostly works:

<snippet>

<tabTrigger>test_Selection_PlaceHolder</tabTrigger>
<description>test_Selection_PlaceHolder</description>

<content><![CDATA[
Selection_Or_PlaceHolder = "${1:$SELECTION}${1/(.+)|(^(?![\S\s]))/(?1)(?2__PLACEHOLDER__)/}"
]]></content>

</snippet>

 
The only issue is that __PLACEHOLDER__ is not inserted as a ( selected ) tab stop.
 



 
I also tried:

Selection_Or_PlaceHolder = "${1:$SELECTION}${1/(.+)|(^(?![\S\s]))/(?1)(?2${1:__PLACEHOLDER__})/}"

but it just returns the literal string: ${1__PLACEHOLDER__}
( which is missing the : after 1 )

 
It seems like it might be tricky to nest a placeholder within the substitution, since both of them reserve $ as a special character.
 



 
As a [ side note | feature request ]:

It would be pretty useful if we could just use something like:

${1:$SELECTION/__PLACEHOLDER__}
0 Likes

#2
Selection_Or_PlaceHolder = "${1:${SELECTION/(^$)/(?{1}$1:__PLACEHOLDER__)/}}"

Ideally, I would have used something like follows, but it doesn’t work:

Selection_Or_PlaceHolder = "${1:${SELECTION:__PLACEHOLDER__}}"
1 Like

#3

@FichteFoll

Thanks!  :grin:

That works great, and I managed to trim it down for its most basic implementation:

${1:${SELECTION/^$/__PLACEHOLDER__/}}

 



 
Here are a few examples for context:

<snippet>

<tabTrigger>test_Selection_PlaceHolder</tabTrigger>
<description>test_Selection_PlaceHolder</description>


<content><![CDATA[
Selection_Or_PlaceHolder = "${1:${SELECTION/^$/__PLACEHOLDER_1__/}}"
Selection_Or_PlaceHolder = "${2:${SELECTION/^$/__PLACEHOLDER_2__/}}"
Selection_Or_PlaceHolder = "${3:${SELECTION/(^$)|(This_Is_A_)/(?1__PLACEHOLDER_3__)(?2__REGEX_REPLACEMENT_2__ = )/}}"
]]></content>

</snippet>
1 Like

Is it possible to add this snippet feature?
#4

You could even combine it with settings taken from *.tmPreferences files, compare: sublimehq/Packages#131

1 Like