Skip to content

Commit

Permalink
[Regex Replace] Add syntax (sublimehq#2214)
Browse files Browse the repository at this point in the history
This commit adds the Regex Replace.sublime-syntax to highlight the
content of the replace input widget if the regexp mode is enabled.

The knowledge about the Regex Replace Widget.sublime-setting was
published by @keith-hall at Discord.

The syntax definition is sourced from PackageDev with some fixes and
scope name modifications being applied. Scoping of conditional
expressions and groups was removed as ST doesn't seem to support that.

Both file name and the `name` attribute use `Regex` as the widget
setting is named like that. The scope name on the other hand uses
`regexp` as this is what the general `Regular Expressions` syntax
defines.
  • Loading branch information
deathaxe authored Jul 14, 2020
1 parent fd58f07 commit 3bea839
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Regular Expressions/Regex Replace Widget.sublime-settings
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"syntax": "Regex Replace.sublime-syntax"
}
35 changes: 35 additions & 0 deletions Regular Expressions/Regex Replace.sublime-syntax
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
%YAML 1.2
---
# See https://www.sublimetext.com/docs/3/syntax.html
# https://www.boost.org/doc/libs/1_71_0/libs/regex/doc/html/boost_regex/format/boost_format_syntax.html
scope: source.regexp-replace
name: Regular Expression Replacement
hidden: true

contexts:
main:
- include: escapes
- include: variables

escapes:
- match: \\[1-9]
scope: variable.language.backref.regexp-replace
- match: \\[lLuUE]
scope: keyword.operator.case-conversion.regexp-replace
- match: \\[aefnrtv()]
scope: constant.character.escape.regexp-replace
- match: \\x(\h{2}|\{\h{4}\})
scope: constant.character.escape.regexp-replace
- match: \\.
scope: constant.character.escape.regexp-replace

variables:
- match: \$\$
scope: constant.character.escape.regexp-replace
- match: |-
\$(?x:
\d+ | [`'&] | \^N | \+(?: \{ \w+ \} )? |
\{ (?: \d+ | \^(?: MATCH | PREMATCH | POSTMATCH ) ) \} |
LAST_PAREN_MATCH | MATCH | PREMATCH | POSTMATCH | LAST_SUBMATCH_RESULT
)
scope: variable.language.match.regexp-replace
113 changes: 113 additions & 0 deletions Regular Expressions/syntax_test_regex_replace
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# SYNTAX TEST "Regex Replace.sublime-syntax"

# Placeholder Sequences
#######################

$
# ^ - constant - variable

$foo
# ^^^^ - constant - variable

$$ Outputs a literal $
# ^^ constant.character.escape.regexp-replace

$1 Outputs what matched the n'th sub-expression.
# ^^ variable.language.match.regexp-replace

${1} Outputs what matched the n'th sub-expression.
# ^^^^ variable.language.match.regexp-replace

$+{NAME} Outputs whatever matched the sub-expression named "NAME".
# ^^^^^^^^ variable.language.match.regexp-replace

$& Outputs what matched the whole expression.
# ^^ variable.language.match.regexp-replace

$MATCH As $&
# ^^^^^^ variable.language.match.regexp-replace

${^MATCH} As $&
# ^^^^^^^^^ variable.language.match.regexp-replace

$` Outputs the text between the end of the last match found (or the start of the text if no previous match was found), and the start of the current match.
# ^^ variable.language.match.regexp-replace

$PREMATCH As $`
# ^^^^^^^^^ variable.language.match.regexp-replace

${^PREMATCH} As $`
# ^^^^^^^^^^^^ variable.language.match.regexp-replace

$' Outputs all the text following the end of the current match.
# ^^ variable.language.match.regexp-replace

$POSTMATCH As $'
# ^^^^^^^^^^ variable.language.match.regexp-replace

${^POSTMATCH} As $'
# ^^^^^^^^^^^^^ variable.language.match.regexp-replace

$+ Outputs what matched the last marked sub-expression in the regular expression.
# ^^ variable.language.match.regexp-replace

$LAST_PAREN_MATCH As $+
# ^^^^^^^^^^^^^^^^^ variable.language.match.regexp-replace

$LAST_SUBMATCH_RESULT Outputs what matched the last sub-expression to be actually matched.
# ^^^^^^^^^^^^^^^^^^^^^ variable.language.match.regexp-replace

$^N As $LAST_SUBMATCH_RESULT
# ^^^ variable.language.match.regexp-replace


# Escape Sequences
##################

\a Outputs the bell character.
# ^^ constant.character.escape.regexp-replace

\e Outputs the ANSI escape character (code point 27).
# ^^ constant.character.escape.regexp-replace
\f Outputs a form feed character.
# ^^ constant.character.escape.regexp-replace

\n Outputs a newline character.
# ^^ constant.character.escape.regexp-replace

\r Outputs a carriage return character.
# ^^ constant.character.escape.regexp-replace

\t Outputs a tab character.
# ^^ constant.character.escape.regexp-replace

\v Outputs a vertical tab character.
# ^^ constant.character.escape.regexp-replace

\xDD Outputs the character whose hexadecimal code point is 0xDD
# ^^^^ constant.character.escape.regexp-replace

\x{DDDD} Outputs the character whose hexadecimal code point is 0xDDDDD
# ^^^^^^^^ constant.character.escape.regexp-replace

\cX Outputs the ANSI escape sequence "escape-X".
# ^^ constant.character.escape.regexp-replace
# ^ - constant

\2 If D is a decimal digit in the range 1-9, then outputs the text that matched sub-expression D.
# ^^ variable.language.backref.regexp-replace

\l Causes the next character to be outputted, to be output in lower case.
# ^^ keyword.operator.case-conversion.regexp-replace

\u Causes the next character to be outputted, to be output in upper case.
# ^^ keyword.operator.case-conversion.regexp-replace

\L Causes all subsequent characters to be output in lower case, until a \E is found.
# ^^ keyword.operator.case-conversion.regexp-replace

\U Causes all subsequent characters to be output in upper case, until a \E is found.
# ^^ keyword.operator.case-conversion.regexp-replace

\E Terminates a \L or \U sequence.
# ^^ keyword.operator.case-conversion.regexp-replace

0 comments on commit 3bea839

Please sign in to comment.