Skip to content

Commit bc47f5b

Browse files
jwortmannmichaelblyons
authored andcommitted
Clear string scope for subexpressions within strings
Use meta.interpolation and punctuation.section.interpolation scopes for $(...) subexpressions within strings and clear the string scope, to enable ST's brackets matching.
1 parent 7f9cb43 commit bc47f5b

File tree

2 files changed

+55
-50
lines changed

2 files changed

+55
-50
lines changed

PowerShell.sublime-syntax

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ contexts:
4242
- include: redirection
4343
- include: commands
4444
- include: variable
45-
- include: interpolated-string-content
45+
- include: group
4646
- include: function
4747
- include: attribute
4848
- include: using-directive
@@ -427,17 +427,16 @@ contexts:
427427
- match: \@"(?=$)
428428
scope: punctuation.definition.string.begin.powershell
429429
push:
430-
- meta_scope: string.quoted.double.heredoc.powershell
430+
- meta_scope: meta.string.powershell string.quoted.double.heredoc.powershell
431431
- match: ^"@
432432
scope: punctuation.definition.string.end.powershell
433433
pop: true
434-
- include: variable-no-property
435434
- include: escape-characters
436435
- include: interpolation
437436
- match: \@'(?=$)
438437
scope: punctuation.definition.string.begin.powershell
439438
push:
440-
- meta_scope: string.quoted.single.heredoc.powershell
439+
- meta_scope: meta.string.powershell string.quoted.single.heredoc.powershell
441440
- match: ^'@
442441
scope: punctuation.definition.string.end.powershell
443442
pop: true
@@ -448,7 +447,7 @@ contexts:
448447
- match: \'
449448
scope: punctuation.definition.string.begin.powershell
450449
push:
451-
- meta_scope: string.quoted.single.powershell
450+
- meta_scope: meta.string.powershell string.quoted.single.powershell
452451
- match: "''"
453452
scope: constant.character.escape.powershell
454453
- match: \'
@@ -459,16 +458,14 @@ contexts:
459458
- match: '"'
460459
scope: punctuation.definition.string.begin.powershell
461460
push:
462-
- meta_scope: string.quoted.double.powershell
461+
- meta_scope: meta.string.powershell string.quoted.double.powershell
463462
- match: '""'
464463
scope: constant.character.escape.powershell
465464
- include: escape-characters
466465
- match: '"'
467466
scope: punctuation.definition.string.end.powershell
468467
pop: true
469468
- match: '(?i)\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,64}\b'
470-
- include: variable-no-property
471-
- include: variable
472469
- include: interpolation
473470
- match: '`\s*$'
474471
scope: keyword.other.powershell
@@ -511,31 +508,34 @@ contexts:
511508
- include: script-block
512509
- include: main
513510

514-
interpolated-string-content:
511+
group:
515512
- match: \(
516513
scope: punctuation.section.group.begin.powershell
517514
push:
518-
- meta_content_scope: interpolated.simple.source.powershell
515+
- meta_scope: meta.group.powershell
519516
- match: \)
520517
scope: punctuation.section.group.end.powershell
521518
pop: true
522519
- include: main
523-
- include: interpolation
524-
- include: interpolated-string-content
525520

526521
interpolation:
527-
- match: (\$)(\()
528-
captures:
529-
1: punctuation.definition.variable.powershell
530-
2: punctuation.section.group.begin.powershell
522+
- match: \$\(
523+
scope: punctuation.section.interpolation.begin.powershell
531524
push:
532-
- meta_content_scope: interpolated.complex.source.powershell
525+
- clear_scopes: 1
526+
- meta_scope: meta.interpolation.powershell
533527
- match: \)
534-
scope: punctuation.section.group.end.powershell
528+
scope: punctuation.section.interpolation.end.powershell
535529
pop: true
536530
- include: main
537-
- include: interpolation
538-
- include: interpolated-string-content
531+
- match: (?=\$)
532+
push:
533+
- clear_scopes: 1
534+
- meta_scope: meta.interpolation.powershell
535+
- include: variable-no-property
536+
- include: variable
537+
- match: ''
538+
pop: true
539539

540540
numeric-constant:
541541
- match: \b((0[xX])[\h_]*\h({{integer_suffix}})?)({{bytes_unit}})?\b

Tests/syntax_test_PowerShell.ps1

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -163,28 +163,31 @@ $variable.Name.Method( )
163163
# In double-quoted strings, only the variable should be highlighted, not the property
164164
"This is my $variable.Name!"
165165
# <- punctuation.definition.string.begin
166-
#^^^^^^^^^^^^^^^^^^^^^^^^^^^ string.quoted.double
166+
#^^^^^^^^^^^ meta.string string.quoted.double
167+
# ^^^^^^^^^ meta.interpolation variable.other.readwrite - string
167168
# ^ punctuation.definition.variable
168-
# ^^^^^^^^ variable.other.readwrite
169-
# ^^^^^ - variable - punctuation
169+
# ^^^^^^ meta.string string.quoted.double - meta.interpolation - variable - punctuation
170170
# ^ punctuation.definition.string.end
171171

172172
# When used in a subexpression, both should be highlighted
173173
"This is my $($variable.Name)!"
174174
# <- punctuation.definition.string.begin
175-
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ string.quoted.double
175+
#^^^^^^^^^^^ meta.string string.quoted.double
176+
# ^^^^^^^^^^^^^^^^^ meta.string meta.interpolation - string
177+
# ^^ meta.string string.quoted.double - meta.interpolation
178+
# ^^ punctuation.section.interpolation.begin
179+
# ^^^^^^^^^ variable.other.readwrite
176180
# ^ punctuation.definition.variable
177-
# ^ punctuation.section.group.begin
178-
# ^ punctuation.section.group.end
179-
# ^^^^^^^^ variable.other.readwrite
180181
# ^ punctuation.accessor.dot
181182
# ^^^^ variable.other.member
183+
# ^ punctuation.section.interpolation.end
182184
# ^ punctuation.definition.string.end
183185

184186
# $ENV:ComputerName should be highlighted
185187
"This is the name of my computer: $ENV:ComputerName"
186188
# <- punctuation.definition.string.begin
187-
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ string.quoted.double
189+
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string string.quoted.double
190+
# ^^^^^^^^^^^^^^^^^ meta.string meta.interpolation - string
188191
# ^ punctuation.definition.variable
189192
# ^^^^ support.variable.drive
190193
# ^^^^^^^^^^^^ variable.other.readwrite
@@ -193,7 +196,8 @@ $variable.Name.Method( )
193196
# Here as well
194197
"This is the name of my computer: ${ENV:ComputerName}"
195198
# <- punctuation.definition.string.begin
196-
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ string.quoted.double
199+
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string string.quoted.double
200+
# ^^^^^^^^^^^^^^^^^^^ meta.string meta.interpolation - string
197201
# ^ punctuation.definition.variable
198202
# ^ punctuation.section.braces.begin
199203
# ^^^^ support.variable.drive
@@ -402,42 +406,43 @@ $a3[1..2]
402406

403407
# Single quoted strings
404408
'This is a single quoted string.'
405-
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ string.quoted.single
409+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string string.quoted.single
406410
'$This is a single ''quoted'' string.'
407-
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ string.quoted.single
411+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string string.quoted.single
408412
# ^^ constant.character.escape
409413
# ^^ constant.character.escape
410414
'This is a
411415
single quoted string.'
412-
# ^^^^^^^^^^^^^^^^^^^^^^ string.quoted.single
416+
# ^^^^^^^^^^^^^^^^^^^^^^ meta.string string.quoted.single
413417
'This #also'
414-
# ^^^^^^^^^^^^ string.quoted.single
418+
# ^^^^^^^^^^^^ meta.string string.quoted.single
415419
'$(Invoke-Something)'
416-
# ^^^^^^^^^^^^^^^^^^^^^ string.quoted.single - meta - variable - support
420+
# ^^^^^^^^^^^^^^^^^^^^^ meta.string string.quoted.single - meta.interpolation - variable - support
417421
'This "string" is nice.'
418-
# ^^^^^^^^^^^^^^^^^^^^^^^^ string.quoted.single
422+
# ^^^^^^^^^^^^^^^^^^^^^^^^ meta.string string.quoted.single
419423

420424
# Double quoted strings
421425
"This is a double quoted string."
422-
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ string.quoted.double
426+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string string.quoted.double
423427
"$This is a double ""quoted"" string."
424-
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ string.quoted.double
425-
# ^^^^^ variable.language
428+
# ^ meta.string string.quoted.double
429+
# ^^^^^ meta.string meta.interpolation variable.language - string
430+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string string.quoted.double - meta.interpolation
426431
# ^^ constant.character.escape
427432
# ^^ constant.character.escape
428433
"This is a
429434
double quoted string."
430-
# ^^^^^^^^^^^^^^^^^^^^^^ string.quoted.double
435+
# ^^^^^^^^^^^^^^^^^^^^^^ meta.string string.quoted.double
431436
"This #also"
432-
# ^^^^^^^^^^^^ string.quoted.double
437+
# ^^^^^^^^^^^^ meta.string string.quoted.double
433438
"$(Invoke-Something)"
434-
# ^^^^^^^^^^^^^^^^^^^^^ string.quoted.double
435-
# ^ punctuation.definition.variable
436-
# ^ punctuation.section.group.begin
437-
# ^ interpolated.complex.source support.function
438-
# ^ punctuation.section.group.end
439+
# ^ meta.string string.quoted.double
440+
# ^^^^^^^^^^^^^^^^^^^ meta.string meta.interpolation - string
441+
# ^^ punctuation.section.interpolation.begin
442+
# ^^^^^^^^^^^^^^^^ support.function
443+
# ^ punctuation.section.interpolation.end
439444
"This 'string' is nice."
440-
# ^^^^^^^^^^^^^^^^^^^^^^^^ string.quoted.double
445+
# ^^^^^^^^^^^^^^^^^^^^^^^^ meta.string string.quoted.double
441446

442447
# Double quoted here-string
443448
@"
@@ -1376,7 +1381,7 @@ $file = join-path $env:SystemDrive "$([System.io.path]::GetRandomFileName()).ps1
13761381
# ^ support.function
13771382
# ^ support.variable.drive
13781383
# ^ variable.other.readwrite
1379-
# ^ string.quoted.double punctuation.definition.variable
1384+
# ^^ meta.string meta.interpolation punctuation.section.interpolation.begin
13801385
# ^ storage.type
13811386
# @@@@@@@@@@@@@@@@@ reference
13821387
$ScriptBlock | Out-File $file -Force
@@ -1423,7 +1428,7 @@ get-thing | Out-WithYou > $null # destroy
14231428
# ^^ constant.character.escape
14241429
# ^^^^^^^^^^^^^^^^^^^ - constant
14251430
"When you call a method: $( get-number | %{ invoke-command $( [string]::format("Like (this{0})","what?") ) $var } )"
1426-
# ^ punctuation.definition.variable
1431+
# ^^ punctuation.section.interpolation.begin
14271432
# ^ keyword.operator.logical.pipe
14281433
# ^ meta.group.complex.subexpression punctuation.section.group.begin
14291434
# ^^^^^^ storage.type
@@ -1435,8 +1440,8 @@ get-thing | Out-WithYou > $null # destroy
14351440
# ^ meta.group.complex.subexpression punctuation.section.group.end
14361441
# ^ punctuation.definition.variable
14371442
"This is the DebugPreference variable: $DebugPreference"
1438-
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ string.quoted.double
1439-
# ^^^^^^^^^^^^^^^^ variable.language
1443+
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string string.quoted.double
1444+
# ^^^^^^^^^^^^^^^^ meta.string meta.interpolation variable.language - string
14401445

14411446
$ConfirmPreference $DebugPreference $ErrorActionPreference $ErrorView
14421447
#^ variable.language punctuation

0 commit comments

Comments
 (0)