Skip to content

Commit c735169

Browse files
michaelblyonskeith-halldeathaxe
authored
[C#] Add regex to argument strings in known functions (#4522)
Co-authored-by: Keith Hall <kingkeith+github@gmail.com> Co-authored-by: deathaxe <16542113+deathaxe@users.noreply.github.com>
1 parent 4487167 commit c735169

3 files changed

Lines changed: 119 additions & 2 deletions

File tree

C#/C#.sublime-syntax

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,6 +1789,20 @@ contexts:
17891789
scope: meta.generic.cs punctuation.definition.generic.end.cs
17901790
pop: 1
17911791
- include: type
1792+
- match: (?:(?:(?:(System)(\.))?(Text)(\.))?(RegularExpressions)(\.))?(Regex)(\.)(Match|IsMatch|Replace)(\()
1793+
scope: meta.function-call.cs
1794+
captures:
1795+
1: variable.other.cs
1796+
2: punctuation.accessor.dot.cs
1797+
3: variable.other.cs
1798+
4: punctuation.accessor.dot.cs
1799+
5: variable.other.cs
1800+
6: punctuation.accessor.dot.cs
1801+
7: support.type.cs
1802+
8: punctuation.accessor.dot.cs
1803+
9: variable.function.cs
1804+
10: meta.group.cs punctuation.section.group.begin.cs
1805+
push: [function_call_arguments, regex_arguments]
17921806
- match: '((?!{{reserved}}\b){{name}})\s*(\()'
17931807
scope: meta.function-call.cs
17941808
captures:
@@ -1986,6 +2000,20 @@ contexts:
19862000
pop: 1
19872001
- match: '(?=\S)'
19882002
push: line_of_code_in
2003+
- match: (?:(?:(?:(System)(\.))?(Text)(\.))?(RegularExpressions)(\.))?(Regex)\s*(\()
2004+
captures:
2005+
1: variable.other.cs
2006+
2: punctuation.accessor.dot.cs
2007+
3: variable.other.cs
2008+
4: punctuation.accessor.dot.cs
2009+
5: variable.other.cs
2010+
6: punctuation.accessor.dot.cs
2011+
7: support.type.cs
2012+
8: meta.group.cs punctuation.section.group.begin.cs
2013+
set: [constructor_arguments, regex_arguments]
2014+
- match: \(
2015+
scope: meta.group.cs punctuation.section.group.begin.cs
2016+
set: [constructor_arguments, arguments]
19892017
- match: (?:\s*((\()\s*(\)))\s*)?(\{)
19902018
captures:
19912019
1: meta.group.cs
@@ -2134,6 +2162,11 @@ contexts:
21342162
pop: 1
21352163
- include: immediately_pop
21362164

2165+
regex_arguments:
2166+
- match: (?=[@$]*")
2167+
push: regex_string
2168+
- include: arguments
2169+
21372170
arguments:
21382171
- match: (out)\s
21392172
captures:
@@ -2160,6 +2193,15 @@ contexts:
21602193
1: variable.parameter.cs
21612194
2: punctuation.separator.argument.value.cs
21622195
push: line_of_code_in
2196+
- include: semi_colon_before_close_paren
2197+
- match: (?=\S)
2198+
push:
2199+
- include: lambdas
2200+
- include: line_of_code_in_no_semicolon
2201+
- match: (?=[;)])
2202+
pop: 1
2203+
2204+
semi_colon_before_close_paren:
21632205
- match: ','
21642206
scope: punctuation.separator.argument.cs
21652207
- match: (?=\))
@@ -2516,15 +2558,15 @@ contexts:
25162558
regex_string:
25172559
- match: '"'
25182560
scope: meta.string.cs string.quoted.double.cs punctuation.definition.string.begin.cs
2519-
embed: scope:source.regexp
2561+
embed: scope:source.regexp.cs
25202562
embed_scope: meta.string.cs string.quoted.double.cs source.regexp
25212563
escape: '"'
25222564
escape_captures:
25232565
0: meta.string.cs string.quoted.double.cs punctuation.definition.string.end.cs
25242566
pop: 1
25252567
- match: '@"'
25262568
scope: meta.string.cs string.quoted.double.verbatim.cs punctuation.definition.string.begin.cs
2527-
embed: scope:source.regexp
2569+
embed: scope:source.regexp.cs
25282570
embed_scope: meta.string.cs string.quoted.double.verbatim.cs source.regexp
25292571
escape: '"'
25302572
escape_captures:
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
%YAML 1.2
2+
---
3+
name: Regular Expressions (C#)
4+
scope: source.regexp.cs
5+
version: 2
6+
hidden: true
7+
8+
extends: Packages/Regular Expressions/RegExp (Basic).sublime-syntax
9+
10+
variables:
11+
capture_group_name: (?:[a-zA-Z_]\w*)
12+
13+
14+
contexts:
15+
anchors:
16+
# Full replacement to un-match $ backrefs
17+
- match: \\[bBAZzG]|\^|\$(?![\d{])
18+
scope: keyword.control.anchor.regexp
19+
push: maybe-unexpected-quantifiers
20+
21+
group-start:
22+
- meta_prepend: true
23+
- match: \?(<)({{capture_group_name}})(>)
24+
scope: keyword.other.named-capture-group.regexp
25+
captures:
26+
1: punctuation.definition.capture-group-name.begin.regexp
27+
2: entity.name.capture-group.regexp
28+
3: punctuation.definition.capture-group-name.end.regexp
29+
30+
backrefs:
31+
- meta_prepend: true
32+
- match: \$(\d+)
33+
scope: keyword.other.backref-and-recursion.regexp
34+
captures:
35+
1: variable.other.backref-and-recursion.regexp
36+
- match: \${({{capture_group_name}})}
37+
scope: keyword.other.backref-and-recursion.regexp
38+
captures:
39+
1: variable.other.backref-and-recursion.regexp

C#/tests/syntax_test_Strings.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,39 @@ FROM some_table
169169
no sql here
170170
/// ^^^^^^^^^^^^ meta.string.cs string.quoted.double.block.cs - source.sql
171171
""";
172+
173+
Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b", RegexOptions.Compiled | RegexOptions.IgnoreCase);
174+
/// ^^^^^ meta.instance support.type
175+
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string source.regexp
176+
/// ^ meta.string punctuation.definition.string.end - source.regexp
177+
/// ^^ keyword.control.anchor
178+
Match m = Regex.Match(input, @"\ba\w*\b", RegexOptions.IgnoreCase);
179+
/// ^^^^^ meta.function-call support.type
180+
/// ^ meta.function-call punctuation.accessor.dot
181+
/// ^^^^^ meta.function-call variable.function
182+
/// ^ meta.group punctuation.section.group.begin
183+
/// ^^^^^^^^ meta.string source.regexp
184+
/// ^ meta.string string.quoted.double punctuation.definition.string.end - source.regexp
185+
186+
replaced = Regex.Replace(some_value, "(?!^)([A-Z])", " $1");
187+
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function-call
188+
/// ^^^^^ support.type
189+
/// ^ punctuation.accessor.dot
190+
/// ^^^^^^^ variable.function
191+
/// ^ punctuation.section.group.begin
192+
/// ^^^^^^^^^^ variable.other
193+
/// ^ punctuation.separator.argument
194+
/// ^ meta.string string.quoted.double punctuation.definition.string.begin
195+
/// ^^^^^^^^^^^^ meta.string source.regexp
196+
/// ^ meta.string string.quoted.double punctuation.definition.string.end - source.regexp
197+
/// ^ punctuation.separator.argument
198+
199+
Regex rx = new Regex(@"\bincomplete-missing-paren\b"
200+
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.instance
201+
/// ^^^ keyword.operator.new
202+
/// ^^^^^ support.type
203+
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string
204+
/// ^^ string.quoted.double punctuation.definition.string.begin
205+
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ source.regexp
206+
/// ^ punctuation.definition.string.end - source.regexp
207+
/// ^^ keyword.control.anchor

0 commit comments

Comments
 (0)