To answer your questions in your answer about conditional transforms using this snippet body:
${TM_FILENAME_BASE/(\\w+)|(\\W+)/${1:/capitalize}${2:+ }/g}
The conditional replacements are explained here: snippets grammar.
| '${' int ':+' if '}'
| '${' int ':?' if ':' else '}'
| '${' int ':-' else '}' | '${' int ':' else '}'
${2:+ }
is an if
transform. If there is a group 2 insert a space.
${2: }
is one of the two else
transforms from the last line above. So ${2: }
means if there is a capture group 2 do nothing but else
(meaning there is no group 2) then insert a space.
The g
flag makes the transform run as many times as it finds a match. So whenever the (\\W+)
part of your regex matches that means there was no group 1 (since you are using an alternate |
in the regex and there was a group 2. So how does your snippet work if you use the ${2: }
form? You have to work through each match.
Using some-file-name
:
The first match will be capture group 1 some
and no capture group 2. Now the entire transform gets applied to that match. Group 1 gets capitalized and since there is no group 2 on the first match, the else
${2: }
is triggered and a space is inserted.
Then the next match will be no group 1 and a group 2 of -
. The replacement part of the transform will not capitalize anything (since there is no group 1). The ${2: }
will insert nothing (since there is a group 2). But it seems to be inserting group 2 - which is non-obvious to me and actually seems like a bug. It is operating as if it read: if group 2, insert group 2, else (no group 2) insert a space. That is a little surprising to me. In any case, it explains why the -
which is matched (in the 2nd/4th/6th/etc. match) inserts the group 2 -
.