Skip to content

Commit 3f3a963

Browse files
committed
Improve variable matches
1 parent cf2f3c3 commit 3f3a963

File tree

3 files changed

+128
-108
lines changed

3 files changed

+128
-108
lines changed

PowerShell.sublime-syntax

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -546,79 +546,86 @@ contexts:
546546
###[ VARIABLES ]###############################################################
547547

548548
variables:
549-
- match: (\$){{var_constants}}
549+
- match: (\$|@){{var_constants}}
550550
scope: support.constant.variable.powershell
551551
captures:
552552
1: punctuation.definition.variable.powershell
553553
push: members
554-
- match: (\$){{var_language}}
554+
- match: (\$|@){{var_language}}
555555
scope: variable.language.powershell
556556
captures:
557557
1: punctuation.definition.variable.powershell
558558
push: members
559-
- match: ((\$|@)(?i:({{var_scope_mod}}):\w+))
559+
- match: (\$|@)({{var_scope_mod}})(:){{identifier_simple}}
560+
scope: variable.other.readwrite.powershell
560561
captures:
561-
1: variable.other.readwrite.powershell
562-
2: punctuation.definition.variable.powershell
563-
3: storage.modifier.scope.powershell
562+
1: punctuation.definition.variable.powershell
563+
2: storage.modifier.scope.powershell
564+
3: punctuation.separator.sequence.powershell
564565
push: members
565-
- match: ((\$)(\{)({{var_scope_mod}}):([^}]*[^}`])(\}))
566+
- match: (\$|@)(\{)({{var_scope_mod}})(:){{identifier_special}}(\})
567+
scope: variable.other.readwrite.powershell
566568
captures:
567-
1: variable.other.readwrite.powershell
568-
2: punctuation.definition.variable.powershell
569-
3: punctuation.section.braces.begin.powershell
570-
4: storage.modifier.scope.powershell
571-
6: punctuation.section.braces.end.powershell
569+
1: punctuation.definition.variable.powershell
570+
2: punctuation.section.interpolation.begin.powershell
571+
3: storage.modifier.scope.powershell
572+
4: punctuation.separator.sequence.powershell
573+
5: punctuation.section.interpolation.end.powershell
572574
push: members
573-
- match: ((?i:(\$|@)(\w+:)?\w+))
575+
- match: (\$|@)(?:({{identifier_simple}})(:))?{{identifier_simple}}
576+
scope: variable.other.readwrite.powershell
574577
captures:
575-
1: variable.other.readwrite.powershell
576-
2: punctuation.definition.variable.powershell
577-
3: support.variable.drive.powershell
578-
4: variable.other.member.powershell
578+
1: punctuation.definition.variable.powershell
579+
2: support.variable.drive.powershell
580+
3: punctuation.separator.sequence.powershell
579581
push: members
580-
- match: ((?i:(\$)(\{)(\w+:)?([^}]*[^}`])(\})))
582+
- match: (\$|@)(\{)(?:({{identifier_special}})(:))?{{identifier_special}}(\})
583+
scope: variable.other.readwrite.powershell
581584
captures:
582-
1: variable.other.readwrite.powershell
583-
2: punctuation.definition.variable.powershell
584-
3: punctuation.section.braces.begin.powershell
585-
4: support.variable.drive.powershell
586-
6: punctuation.section.braces.end.powershell
585+
1: punctuation.definition.variable.powershell
586+
2: punctuation.section.interpolation.begin.powershell
587+
3: support.variable.drive.powershell
588+
4: punctuation.separator.sequence.powershell
589+
5: punctuation.section.interpolation.end.powershell
587590
push: members
588591

589592
variables-without-property:
590-
- match: (\$){{var_constants}}
593+
- match: (\$|@){{var_constants}}
591594
scope: support.constant.variable.powershell
592595
captures:
593596
1: punctuation.definition.variable.powershell
594-
- match: (\$){{var_language}}
597+
- match: (\$|@){{var_language}}
595598
scope: variable.language.powershell
596599
captures:
597600
1: punctuation.definition.variable.powershell
598-
- match: (?i:(\$|@)({{var_scope_mod}}):(\w+))
601+
- match: (\$|@)({{var_scope_mod}})(:){{identifier_simple}}
602+
scope: variable.other.readwrite.powershell
599603
captures:
600604
1: punctuation.definition.variable.powershell
601605
2: storage.modifier.scope.powershell
602-
3: variable.other.readwrite.powershell
603-
- match: (?i:(\$)(\{)({{var_scope_mod}}):([^}]*[^}`])(\}))
606+
3: punctuation.separator.sequence.powershell
607+
- match: (\$|@)(\{)({{var_scope_mod}})(:){{identifier_special}}(\})
608+
scope: variable.other.readwrite.powershell
604609
captures:
605610
1: punctuation.definition.variable.powershell
606-
2: punctuation.section.braces.begin
611+
2: punctuation.section.interpolation.begin.powershell
607612
3: storage.modifier.scope.powershell
608-
4: variable.other.readwrite.powershell
609-
5: punctuation.section.braces.end
610-
- match: (?i:(\$)(\w+:)?\w+)
613+
4: punctuation.separator.sequence.powershell
614+
5: punctuation.section.interpolation.end.powershell
615+
- match: (\$|@)(?:({{identifier_simple}})(:))?{{identifier_simple}}
611616
scope: variable.other.readwrite.powershell
612617
captures:
613618
1: punctuation.definition.variable.powershell
614619
2: support.variable.drive.powershell
615-
- match: (?i:(\$)(\{)(\w+:)?([^}]*[^}`])(\}))
620+
3: punctuation.separator.sequence.powershell
621+
- match: (\$|@)(\{)(?:({{identifier_special}})(:))?{{identifier_special}}(\})
622+
scope: variable.other.readwrite.powershell
616623
captures:
617624
1: punctuation.definition.variable.powershell
618-
2: punctuation.section.braces.begin
625+
2: punctuation.section.interpolation.begin.powershell
619626
3: support.variable.drive.powershell
620-
4: variable.other.readwrite.powershell
621-
5: punctuation.section.braces.end
627+
4: punctuation.separator.sequence.powershell
628+
5: punctuation.section.interpolation.end.powershell
622629

623630
members:
624631
- match: (?=\.\.)
@@ -985,6 +992,10 @@ variables:
985992
unit_suffix: (?i:[kmgtp]b\b)
986993

987994
# Variables
995+
# https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_variables
996+
identifier_simple: '[\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Lo}\p{Nd}_?]+'
997+
identifier_special: '[^}]*[^}`]'
998+
# https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables
988999
var_constants: |-
9891000
\b(?xi:
9901001
(?:

Tests/syntax_test_PowerShell.ps1

Lines changed: 73 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -137,73 +137,80 @@ throw "Do not run this file!"
137137
# ^^^^^ constant.language.null.powershell
138138
# ^ punctuation.definition.variable.powershell
139139

140-
# Automatic variables
141-
$_, $$, $^, $?
142-
# <- punctuation.definition.variable
143-
#^ variable.language
144-
# ^ punctuation.definition.variable
145-
# ^^ variable.language
146-
# ^ punctuation.definition.variable
147-
# ^^ variable.language
148-
# ^ punctuation.definition.variable
149-
# ^^ variable.language
150-
$args
151-
# <- punctuation.definition.variable
152-
#^^^^ variable.language
153-
$error
154-
# <- punctuation.definition.variable
155-
# ^^^^ variable.language
156-
$home
157-
# <- punctuation.definition.variable
158-
# ^^^ support.constant.variable
159-
$foreach
160-
# <- punctuation.definition.variable
161-
#^^^^^^^ variable.language
140+
# Automatic variables
141+
$_, $$, $^, $?
142+
# ^^ variable.language.powershell
143+
# ^ punctuation.definition.variable.powershell
144+
# ^ punctuation.separator.sequence.powershell
145+
# ^^ variable.language.powershell
146+
# ^ punctuation.definition.variable.powershell
147+
# ^ punctuation.separator.sequence.powershell
148+
# ^^ variable.language.powershell
149+
# ^ punctuation.definition.variable.powershell
150+
# ^ punctuation.separator.sequence.powershell
151+
# ^^ variable.language.powershell
152+
# ^ punctuation.definition.variable.powershell
153+
$args
154+
# ^^^^^ variable.language.powershell
155+
# ^ punctuation.definition.variable.powershell
156+
$error
157+
# ^^^^^^ variable.language.powershell
158+
# ^ punctuation.definition.variable.powershell
159+
$home
160+
# ^^^^^ support.constant.variable.powershell
161+
# ^ punctuation.definition.variable.powershell
162+
$foreach
163+
# ^^^^^^^^ variable.language.powershell
164+
# ^ punctuation.definition.variable.powershell
162165

163-
# Normal variables
164-
$variable
165-
# <- punctuation.definition.variable
166-
# ^ variable.other.readwrite
167-
$script:variable
168-
# <- variable.other.readwrite punctuation.definition.variable
169-
# ^ storage.modifier.scope
170-
# ^ variable.other.readwrite
171-
$ENV:ComputerName
172-
# <- variable.other.readwrite punctuation.definition.variable
173-
# ^ support.variable.drive
174-
# ^ variable.other.readwrite
175-
${variable}
176-
# <- variable.other.readwrite punctuation.definition.variable
177-
# <- punctuation.section.braces.begin
178-
# ^^^^^^^^ variable.other.readwrite
179-
# ^ punctuation.section.braces.end
180-
${script:variable}
181-
# <- variable.other.readwrite punctuation.definition.variable
182-
# <- punctuation.section.braces.begin
183-
# ^ storage.modifier.scope
184-
# ^ variable.other.readwrite
185-
# ^ punctuation.section.braces.end
186166

187-
# Variable properties should be highlighted
188-
$variable.Name
189-
# <- variable.other.readwrite punctuation.definition.variable
190-
#^^^^^^^^ variable.other.readwrite
191-
# ^ punctuation.accessor.dot
192-
# ^^^^^ - variable.other.readwrite
193-
# ^^^^ variable.other.member
167+
# Normal variables
168+
$variable
169+
# ^^^^^^^^^ variable.other.readwrite.powershell
170+
# ^ punctuation.definition.variable.powershell
171+
$script:variable
172+
# ^^^^^^^^^^^^^^^^ variable.other.readwrite.powershell
173+
# ^ punctuation.definition.variable.powershell
174+
# ^^^^^^ storage.modifier.scope.powershell
175+
# ^ punctuation.separator.sequence.powershell
176+
$ENV:ComputerName
177+
# ^^^^^^^^^^^^^^^^^ variable.other.readwrite.powershell
178+
# ^ punctuation.definition.variable.powershell
179+
# ^^^ support.variable.drive.powershell
180+
# ^ punctuation.separator.sequence.powershell
181+
${variable}
182+
# ^^^^^^^^^^^ variable.other.readwrite.powershell
183+
# ^ punctuation.definition.variable.powershell
184+
# ^ punctuation.section.interpolation.begin.powershell
185+
# ^ punctuation.section.interpolation.end.powershell
186+
${script:variable}
187+
# ^^^^^^^^^^^^^^^^^^ variable.other.readwrite.powershell
188+
# ^ punctuation.definition.variable.powershell
189+
# ^ punctuation.section.interpolation.begin.powershell
190+
# ^^^^^^ storage.modifier.scope.powershell
191+
# ^ punctuation.separator.sequence.powershell
192+
# ^ punctuation.section.interpolation.end.powershell
194193

195-
# Variable properties should be highlighted
196-
$variable.Name.Method( )
197-
# <- variable.other.readwrite punctuation.definition.variable
198-
#^^^^^^^^ variable.other.readwrite
199-
# ^ punctuation.accessor.dot
200-
# ^^^^ variable.other.member
201-
# ^ punctuation.accessor.dot
202-
# ^^^^^^ meta.function-call variable.function
203-
# @@@@@@ reference
204-
# ^^^ meta.function-call.arguments
205-
# ^^^^^^^^^^^^^^^ - variable.other.readwrite
206-
# ^ - meta.function-call
194+
195+
# Variable properties should be highlighted
196+
$variable.Name
197+
# ^^^^^^^^^ variable.other.readwrite.powershell
198+
# ^ punctuation.definition.variable.powershell
199+
# ^ punctuation.accessor.dot.powershell
200+
# ^^^^ variable.other.member.powershell
201+
202+
# Variable properties should be highlighted
203+
$variable.Name.Method( )
204+
# ^^^^^^^^^ variable.other.readwrite.powershell
205+
# ^ punctuation.definition.variable.powershell
206+
# ^ punctuation.accessor.dot.powershell
207+
# ^^^^ variable.other.member.powershell
208+
# ^ punctuation.accessor.dot.powershell
209+
# ^^^^^^ meta.function-call.powershell variable.function.powershell
210+
# @@@@@@ reference
211+
# ^^^ meta.function-call.arguments.powershell
212+
# ^ punctuation.section.arguments.begin.powershell
213+
# ^ punctuation.section.arguments.end.powershell
207214

208215
# Hashtable
209216
$properties = @{
@@ -1526,7 +1533,8 @@ $file = join-path $env:SystemDrive "$([System.io.path]::GetRandomFileName()).ps1
15261533
# @@@@@@@@@ reference
15271534
# ^^^^^^^^^^^^^^^^ variable.other.readwrite.powershell
15281535
# ^ punctuation.definition.variable.powershell
1529-
# ^^^^ support.variable.drive.powershell
1536+
# ^^^ support.variable.drive.powershell
1537+
# ^ punctuation.separator.sequence.powershell
15301538
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string.interpolated.powershell
15311539
# ^ string.quoted.double.powershell punctuation.definition.string.begin.powershell
15321540
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.interpolation.powershell

Tests/syntax_test_strings.ps1

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,22 +113,23 @@
113113
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string.interpolated.powershell
114114
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ string.quoted.double.powershell
115115
# ^ punctuation.definition.string.begin.powershell
116-
# ^^^^^^^^^^^^^^^^^ meta.interpolation.powershell variable.other.readwrite.powershell - string
116+
# ^^^^^^^^^^^^^^^^^ meta.interpolation.powershell variable.other.readwrite.powershell
117117
# ^ punctuation.definition.variable.powershell
118-
# ^^^^ support.variable.drive.powershell
118+
# ^^^ support.variable.drive.powershell
119+
# ^ punctuation.separator.sequence.powershell
119120
# ^ string.quoted.double.powershell punctuation.definition.string.end.powershell
120121

121122
# Here as well
122123
"This is the name of my computer: ${ENV:ComputerName}"
123124
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string.interpolated.powershell
124125
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ string.quoted.double.powershell
125126
# ^ punctuation.definition.string.begin.powershell
126-
# ^^^^^^^^^^^^^^^^^^^ meta.interpolation.powershell - string
127+
# ^^^^^^^^^^^^^^^^^^^ meta.interpolation.powershell variable.other.readwrite.powershell
127128
# ^ punctuation.definition.variable.powershell
128-
# ^ punctuation.section.braces.begin
129-
# ^^^^ support.variable.drive.powershell
130-
# ^^^^^^^^^^^^ variable.other.readwrite.powershell
131-
# ^ punctuation.section.braces.end
129+
# ^ punctuation.section.interpolation.begin.powershell
130+
# ^^^ support.variable.drive.powershell
131+
# ^ punctuation.separator.sequence.powershell
132+
# ^ punctuation.section.interpolation.end.powershell
132133
# ^ string.quoted.double.powershell punctuation.definition.string.end.powershell
133134

134135
# The @splat references only work in argument mode, should not highlight in strings

0 commit comments

Comments
 (0)