Background
Hi all, I'm wokring on a VScode extension for snippets. I highlighted all
$name
TextMate style variables like$CURRENT_YEAR
and$TM_FILENAME_BASE
.name
in$name
can be a integer, or a static variable.I would also like to highlight variables like
${<INTEGER>:<DEFAULT>}
,${<INTEGER>:<DEFAULT>|<OPTIONAL_OPTIONS>}
e.g.${2:placeholder}
and${1:true|false}
Moreover, highlight transformed variables
${<STATIC_VAR>\<MATCH_REGEX>\<TRANSFORM_TARGET>\}
. Example:${TM_FILENAME/[\\.]/_/
replaces the first.
with_
.All these example can be found in VScode documentation
I played Regex Golf a little, but I couldn't solve the "Balance" Chapter
Question
Is there a NEAT way to match all TextMate variables(see definition in chapter 7.2) with a Regular Expression (or other technique)?
In detail, I would want my Regex to match all $<NAME>
, ${<INTEGER>:<DEFAULT>|<OPTION>}
, and ${<STATIC_VAR>\<MATCH_REGEX>\<TRANSFORM_TARGET>\}
where
<NAME>
can be digits and some specific letters likeLINE_COMMENT
;<DEFAULT>
and can be any text.<STATIC_VAR>
is some specific letters likeLINE_COMMENT
<MATCH_REGEX>
A regular expression, I'm not going to check if it is legal.<TRANSFORM_TARGET>
The regular expression substitution output, not going to check.
My idea was to detect if there is a pair of brackets (curly parentheses). And the first word after parentheses opening is a <NAME>
.
TextMate uses Oniguruma regular expression library by K. Kosako
What I've done
- Match variable without
{}
with
match:"\\$(?:[0-9]+|TM_SELECTED_TEXT|<TYPED_ALL_POSSIBLE_NAMES>|TM_CURRENT_WORD|LINE_COMMENT)"
- Tried regex pattern generator (AI)
- In question Regular expression to match balanced parentheses is it said not possible in this answer
- Using a dirty work-around
\\$(?= (?=<ALL_NAMES>)|{(?=<ALL_NAMES>):.+} )
Where <ALL_NAMES>
represents all <NAME>
s joined with pipe char |