Skip to content

Commit 9bfd10b

Browse files
author
Michael Lyons
committed
Adapted tests from PowerShell/EditorSyntax
1 parent f3cb2bb commit 9bfd10b

File tree

6 files changed

+1866
-39
lines changed

6 files changed

+1866
-39
lines changed

Support/PowershellSyntax.sublime-syntax renamed to PowerShell.sublime-syntax

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ file_extensions:
99
- psm1
1010
- psd1
1111

12+
variables:
13+
bytes_unit: (?i:[kmgtp]b)
14+
1215
contexts:
1316

1417
main:
1518
- include: comments
16-
- match: '[2-6]>&1|>>|>|<<|<|>|>\||[1-6]>|[1-6]>>'
17-
scope: keyword.operator.redirection.powershell
19+
- include: redirection
1820
- include: commands
1921
- include: variable
2022
- include: interpolated-string-content
@@ -60,19 +62,23 @@ contexts:
6062
- match: (?<!\w|-)((?i:class)|%|\?)(?:\s)+((?:\p{L}|\d|_|-|)+)\b
6163
# capture should be entity.name.type, but it doesn't provide a good color in the default schema.
6264
captures:
63-
1: storage.type.powershell
64-
2: entity.name.function
65+
1: storage.type.class.powershell
66+
2: meta.class.powershell entity.name.class.powershell
67+
- match: (?<!\w)-(?i:replace)\b
68+
scope: keyword.operator.powershell
6569
- match: (?<!\w)-(?i:as)\b
6670
scope: keyword.operator.comparison.powershell
67-
- match: (?<!\w)-(?i:[ic]?(?:eq|ne|[gl][te]|(?:not)?(?:like|match|contains|in)|replace))(?!\p{L})
71+
- match: (?<!\w)-(?i:[ic]?(?:eq|ne|[gl][te]))(?!\p{L})
6872
scope: keyword.operator.comparison.powershell
73+
- match: (?<!\w)-(?i:[ic]?(?:not)?(?:like|match|contains|in))(?!\p{L})
74+
scope: keyword.operator.logical.powershell
6975
- match: (?<!\w)-(?i:join|split)(?!\p{L})|!
7076
scope: keyword.operator.unary.powershell
7177
- match: (?<!\w)-(?i:is(?:not)?)\b
7278
scope: keyword.operator.logical.powershell
7379
- match: (?<!\w)-(?i:and|or|not|xor)(?!\p{L})|!
7480
scope: keyword.operator.logical.powershell
75-
- match: (?<!\w)-(?i:band|bor|bnot|bxor)(?!\p{L})
81+
- match: (?<!\w)-(?i:band|bor|bnot|bxor|sh[lr])(?!\p{L})
7682
scope: keyword.operator.bitwise.powershell
7783
- match: (?<!\w)-(?i:f)(?!\p{L})
7884
scope: keyword.operator.string-format.powershell
@@ -100,6 +106,19 @@ contexts:
100106
- include: comment-block
101107
- include: comment-line
102108

109+
redirection:
110+
- match: ([2-6])(>&)(1)|([1-6])(>)|([1-6])(>>)
111+
captures:
112+
1: constant.numeric.decimal.file-descriptor.powershell
113+
2: keyword.operator.redirection.powershell
114+
3: constant.numeric.decimal.file-descriptor.powershell
115+
4: constant.numeric.decimal.file-descriptor.powershell
116+
5: keyword.operator.redirection.powershell
117+
6: constant.numeric.decimal.file-descriptor.powershell
118+
7: keyword.operator.redirection.powershell
119+
- match: '>>?|<<?|>\|'
120+
scope: keyword.operator.redirection.powershell
121+
103122
requires-directive:
104123
- match: (?<=#)(?i:requires)\s
105124
scope: keyword.control.requires.powershell
@@ -131,12 +150,10 @@ contexts:
131150
scope: punctuation.section.bracket.end.powershell
132151
pop: true
133152
- match: \(
134-
captures:
135-
0: punctuation.section.group.begin.powershell
153+
scope: punctuation.section.group.begin.powershell
136154
push:
137155
- match: \)
138-
captures:
139-
0: punctuation.section.group.end.powershell
156+
scope: punctuation.section.group.end.powershell
140157
pop: true
141158
- include: variable
142159
- include: variable-no-property
@@ -171,13 +188,11 @@ contexts:
171188

172189
comment-block:
173190
- match: <#
174-
captures:
175-
0: punctuation.definition.comment.block.begin.powershell
191+
scope: punctuation.definition.comment.block.begin.powershell
176192
push:
177193
- meta_scope: comment.block.powershell
178194
- match: '#>'
179-
captures:
180-
0: punctuation.definition.comment.block.end.powershell
195+
scope: punctuation.definition.comment.block.end.powershell
181196
pop: true
182197
- include: comment-embedded-docs
183198

@@ -211,17 +226,21 @@ contexts:
211226
- include: double-quoted-string
212227
- include: single-quoted-string
213228
- match: \@"(?=$)
229+
scope: punctuation.definition.string.begin.powershell
214230
push:
215231
- meta_scope: string.quoted.double.heredoc.powershell
216232
- match: ^"@
233+
scope: punctuation.definition.string.end.powershell
217234
pop: true
218235
- include: variable-no-property
219236
- include: double-quoted-string-escapes
220237
- include: interpolation
221238
- match: \@'(?=$)
239+
scope: punctuation.definition.string.begin.powershell
222240
push:
223241
- meta_scope: string.quoted.single.heredoc.powershell
224242
- match: ^'@
243+
scope: punctuation.definition.string.end.powershell
225244
pop: true
226245
- match: "''"
227246
scope: constant.character.escape.powershell
@@ -306,7 +325,7 @@ contexts:
306325
interpolation:
307326
- match: (\$)(\()
308327
captures:
309-
1: keyword.other.variable.definition.powershell
328+
1: punctuation.definition.variable.powershell
310329
2: punctuation.section.group.begin.powershell
311330
push:
312331
- meta_content_scope: interpolated.complex.source.powershell
@@ -318,31 +337,31 @@ contexts:
318337
- include: interpolated-string-content
319338

320339
numeric-constant:
321-
- match: ((?:[-+]|\b)0(?i:x)[0-9a-fA-F_]+(?i:u|l|ul|lu)?)((?i:[kmgtp]b)?)\b
340+
- match: ((?:[-+]|\b)0(?i:x)[0-9a-fA-F_]+(?i:u|l|ul|lu)?)({{bytes_unit}}?)\b
322341
captures:
323-
1: constant.numeric.hex.powershell
342+
1: constant.numeric.integer.hexadecimal.powershell
324343
2: keyword.other.powershell
325-
- match: ((?:[-+]|\b)(?:[0-9_]+)?\.[0-9_]+(?:(?i:e)[0-9]+)?(?i:[fdm])?)((?i:[kmgtp]b)?)\b
344+
- match: ((?:[-+]|\b)(?:[0-9_]+)?\.[0-9_]+(?:(?i:e)[0-9]+)?(?i:[fdm])?)({{bytes_unit}}?)\b
326345
captures:
327346
1: constant.numeric.integer.powershell
328347
2: keyword.other.powershell
329-
- match: ((?:[-+]|\b)0(?:b|B)[01_]+(?i:u|l|ul|lu)?)((?i:[kmgtp]b)?)\b
348+
- match: ((?:[-+]|\b)0(?:b|B)[01_]+(?i:u|l|ul|lu)?)({{bytes_unit}}?)\b
330349
captures:
331-
1: constant.numeric.octal.powershell
350+
1: constant.numeric.integer.binary.powershell
332351
2: keyword.other.powershell
333-
- match: ((?:[-+]|\b)[0-9_]+(?:e|E)(?:[0-9_])?+(?i:[fdm])?)((?i:[kmgtp]b)?)\b
352+
- match: ((?:[-+]|\b)[0-9_]+(?i:e)(?:[0-9_])?+(?i:[fdm])?)({{bytes_unit}}?)\b
334353
captures:
335354
1: constant.numeric.integer.powershell
336355
2: keyword.other.powershell
337-
- match: ((?:[-+]|\b)[0-9_]+\.(?:e|E)(?:[0-9_])?+(?i:[fdm])?)((?i:[kmgtp]b)?)\b
356+
- match: ((?:[-+]|\b)[0-9_]+\.(?i:e)(?:[0-9_])?+(?i:[fdm])?)({{bytes_unit}}?)\b
338357
captures:
339358
1: constant.numeric.integer.powershell
340359
2: keyword.other.powershell
341-
- match: ((?:[-+]|\b)[0-9_]+[\.]?(?i:[fdm]))((?i:[kmgtp]b)?)\b'
360+
- match: ((?:[-+]|\b)[0-9_]+[\.]?(?i:[fdm]))({{bytes_unit}}?)\b
342361
captures:
343362
1: constant.numeric.integer.powershell
344363
2: keyword.other.powershell
345-
- match: ((?:[-+]|\b)[0-9_]+[\.]?(?i:u|l|ul|lu)?)((?i:[kmgtp]b)?)\b
364+
- match: ((?:[-+]|\b)[0-9_]+[\.]?(?i:u|l|ul|lu)?)({{bytes_unit}}?)\b
346365
captures:
347366
1: constant.numeric.integer.powershell
348367
2: keyword.other.powershell
@@ -377,52 +396,52 @@ contexts:
377396
captures:
378397
1: support.constant.variable.powershell
379398
2: punctuation.definition.variable.powershell
380-
3: entity.name.function.invocation.powershell
399+
3: variable.other.member.powershell
381400
- match: ((\$)(?i:\$|\^|\?|_|Args|ConsoleFileName|Event|EventArgs|EventSubscriber|ForEach|Input|LastExitCode|Matches|MyInvocation|NestedPromptLevel|Profile|PSBoundParameters|PsCmdlet|PsCulture|PSDebugContext|PSItem|PSCommandPath|PSScriptRoot|PsUICulture|Pwd|Sender|SourceArgs|SourceEventArgs|StackTrace|Switch|This))((?:\.(?:\p{L}|\d|_)+)*\b)?\b
382401
captures:
383402
1: variable.language.powershell
384403
2: punctuation.definition.variable.powershell
385-
3: entity.name.function.invocation.powershell
386-
- match: (\$)(?i:(ConfirmPreference|DebugPreference|ErrorActionPreference|ErrorView|FormatEnumerationLimit|MaximumAliasCount|MaximumDriveCount|MaximumErrorCount|MaximumFunctionCount|MaximumHistoryCount|MaximumVariableCount|OFS|OutputEncoding|ProgressPreference|PsCulture|PSDebugContext|PSDefaultParameterValues|PSEmailServer|PSItem|PSModuleAutoloadingPreference|PSSenderInfo|PSSessionApplicationName|PSSessionConfigurationName|PSSessionOption|VerbosePreference|WarningPreference|WhatIfPreference))((?:\.(?:\p{L}|\d|_)+)*\b)?\b
404+
3: variable.other.member.powershell
405+
- match: (\$(?i:(ConfirmPreference|DebugPreference|ErrorActionPreference|ErrorView|FormatEnumerationLimit|MaximumAliasCount|MaximumDriveCount|MaximumErrorCount|MaximumFunctionCount|MaximumHistoryCount|MaximumVariableCount|OFS|OutputEncoding|ProgressPreference|PsCulture|PSDebugContext|PSDefaultParameterValues|PSEmailServer|PSItem|PSModuleAutoloadingPreference|PSSenderInfo|PSSessionApplicationName|PSSessionConfigurationName|PSSessionOption|VerbosePreference|WarningPreference|WhatIfPreference)))((?:\.(?:\p{L}|\d|_)+)*\b)?\b
387406
comment: Style preference variables as language variables so that they stand out.
388407
captures:
389408
1: punctuation.definition.variable.powershell
390409
2: variable.language.powershell
391-
3: entity.name.function.invocation.powershell
410+
3: variable.other.member.powershell
392411
- match: (?i:(\$|@)(global|local|private|script|using|workflow):((?:\p{L}|\d|_)+))((?:\.(?:\p{L}|\d|_)+)*\b)?
393412
captures:
394413
1: punctuation.definition.variable.powershell
395414
2: storage.modifier.scope.powershell
396415
3: variable.other.readwrite.powershell
397-
4: entity.name.function.invocation.powershell
416+
4: variable.other.member.powershell
398417
- match: (?i:(\$)(\{)(global|local|private|script|using|workflow):([^}]*[^}`])(\}))((?:\.(?:\p{L}|\d|_)+)*\b)?
399418
captures:
400419
1: punctuation.definition.variable.powershell
401420
2: punctuation.section.braces.begin.powershell
402421
3: storage.modifier.scope.powershell
403422
4: variable.other.readwrite.powershell
404423
5: punctuation.section.braces.end.powershell
405-
6: entity.name.function.invocation.powershell
424+
6: variable.other.member.powershell
406425
- match: (?i:(\$|@)((?:\p{L}|\d|_)+:)?((?:\p{L}|\d|_)+))((?:\.(?:\p{L}|\d|_)+)*\b)?
407426
captures:
408427
1: punctuation.definition.variable.powershell
409428
2: support.variable.drive.powershell
410429
3: variable.other.readwrite.powershell
411-
4: entity.name.function.invocation.powershell
430+
4: variable.other.member.powershell
412431
- match: (?i:(\$)(\{)((?:\p{L}|\d|_)+:)?([^}]*[^}`])(\}))((?:\.(?:\p{L}|\d|_)+)*\b)?
413432
captures:
414433
1: punctuation.definition.variable.powershell
415434
2: punctuation.section.braces.begin.powershell
416435
3: support.variable.drive.powershell
417436
4: variable.other.readwrite.powershell
418437
5: punctuation.section.braces.end.powershell
419-
6: entity.name.function.invocation.powershell
438+
6: variable.other.member.powershell
420439

421440
variable-no-property:
422441
- match: (\$)(?i:False|Null|True)\b
423442
scope: constant.language.powershell
424443
captures:
425-
1: keyword.other.variable.definition.powershell
444+
1: punctuation.definition.variable.powershell
426445
- match: (\$)(?i:Error|ExecutionContext|Host|Home|PID|PsHome|PsVersionTable|ShellID)\b
427446
scope: support.constant.variable.powershell
428447
captures:
@@ -448,11 +467,11 @@ contexts:
448467
3: variable.other.readwrite.powershell
449468
4: keyword.other.powershell
450469
5: entity.name.function.invocation.powershell
451-
- match: (?i:(\$)((?:\p{L}|\d|_)+:)?((?:\p{L}|\d|_)+))
470+
- match: (?i:(\$)((?:\p{L}|\d|_)+:)?(?:\p{L}|\d|_)+)
471+
scope: variable.other.readwrite.powershell
452472
captures:
453473
1: punctuation.definition.variable.powershell
454474
2: support.variable.drive.powershell
455-
3: variable.other.readwrite.powershell
456475
- match: (?i:(\$)(\{)((?:\p{L}|\d|_)+:)?([^}]*[^}`])(\}))
457476
captures:
458477
1: punctuation.definition.variable.powershell
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{
2-
"word_wrap": false,
3-
"translate_tabs_to_spaces": true
1+
{
2+
"word_wrap": false,
3+
"translate_tabs_to_spaces": true
44
}

Symbol List Functions.tmPreferences renamed to Support/Symbol List Classes.tmPreferences

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<key>name</key>
66
<string>Symbol List: Functions</string>
77
<key>scope</key>
8-
<string>source.powershell meta.function</string>
8+
<string>source.powershell entity.name.class</string>
99
<key>settings</key>
1010
<dict>
1111
<key>showInSymbolList</key>

Tests/syntax_test_Class.ps1

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# SYNTAX TEST "Packages/PowerShell/PowerShell.sublime-syntax"
2+
using namespace system.management.automation
3+
# <- keyword.control.using
4+
# ^^^^^^^^^ keyword.other
5+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ variable.parameter
6+
7+
# Define a class
8+
class TypeName
9+
# <- storage.type
10+
# ^ entity.name.class
11+
{
12+
# Property with validate set
13+
# <- punctuation.definition.comment
14+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^ comment.line
15+
[ValidateSet("val1", "Val2")]
16+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.attribute
17+
# <- punctuation.section.bracket.begin
18+
# ^^^^^^^^^^ support.function.attribute
19+
# ^ punctuation.section.group.begin
20+
# ^^^^^^ string.quoted.double
21+
# ^ punctuation.separator
22+
# ^^^^^^ string.quoted.double
23+
# ^ punctuation.section.group.end
24+
# ^ punctuation.section.bracket.end
25+
[string] $P1
26+
# <- punctuation.section.bracket.begin
27+
# ^^^^^ storage.type
28+
# ^ punctuation.section.bracket.end
29+
# ^ punctuation.definition.variable
30+
# ^^ variable.other.readwrite
31+
32+
# Static property
33+
# <- punctuation.definition.comment
34+
# ^^^^^^^^^^^^^^^ comment.line
35+
static [hashtable] $P2
36+
#^^^^^ storage.modifier
37+
# ^ punctuation.section.bracket.begin
38+
# ^^^^^^^^^ storage.type
39+
# ^ punctuation.section.bracket.end
40+
# ^ punctuation.definition.variable
41+
# ^^ variable.other.readwrite
42+
43+
# Hidden property does not show as result of Get-Member
44+
# <- punctuation.definition.comment
45+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ comment.line
46+
hidden [int] $P3
47+
#^^^^^ storage.modifier
48+
# ^ punctuation.section.bracket.begin
49+
# ^ storage.type
50+
# ^ punctuation.section.bracket.end
51+
# ^ punctuation.definition.variable
52+
# ^^ variable.other.readwrite
53+
54+
# Constructor
55+
# <- punctuation.definition.comment
56+
# ^^^^^^^^^^^ comment.line
57+
TypeName ([string] $s) {
58+
# ^ punctuation.section.group.begin
59+
# ^ punctuation.section.bracket.begin
60+
# ^^^^^^ storage.type
61+
# ^ punctuation.section.bracket.end
62+
# ^ punctuation.definition.variable
63+
# ^ variable.other.readwrite
64+
# ^ punctuation.section.group.end
65+
$this.P1 = $s
66+
# <- punctuation.definition.variable
67+
# ^ variable.language
68+
# ^^ variable.other.member
69+
# ^ keyword.operator.assignment
70+
# ^ punctuation.definition.variable
71+
# ^ variable.other.readwrite
72+
}
73+
74+
# Static method
75+
# <- punctuation.definition.comment
76+
# ^^^^^^^^^^^^^ comment.line
77+
static [void] MemberMethod1([hashtable] $h) {
78+
# <- storage.modifier
79+
# ^ punctuation.section.bracket.begin
80+
# ^^^^ storage.type
81+
# ^ punctuation.section.bracket.end
82+
# ^ punctuation.section.group.begin
83+
# ^ punctuation.section.bracket.begin
84+
# ^^^^^^^^^ storage.type
85+
# ^ punctuation.section.bracket.end
86+
# ^ punctuation.definition.variable
87+
# ^ variable.other.readwrite
88+
# ^ punctuation.section.group.end
89+
[TypeName]::P2 = $h
90+
# <- punctuation.section.bracket.begin
91+
# ^ storage.type
92+
# ^ punctuation.section.bracket.end
93+
# ^ keyword.operator.assignment
94+
# ^ punctuation.definition.variable
95+
# ^ variable.other.readwrite
96+
}
97+
98+
# Instance method
99+
# <- punctuation.definition.comment
100+
# ^^^^^^^^^^^^^^^ comment.line
101+
[int] MemberMethod2([int] $i) {
102+
# <- punctuation.section.bracket.begin
103+
# ^ storage.type
104+
# ^ punctuation.section.bracket.end
105+
# ^ punctuation.section.group.begin
106+
# ^ punctuation.section.bracket.begin
107+
# ^^^ storage.type
108+
# ^ punctuation.section.bracket.end
109+
# ^ punctuation.definition.variable
110+
# ^ variable.other.readwrite
111+
# ^ punctuation.section.group.end
112+
$this.P3 = $i
113+
# <- punctuation.definition.variable
114+
# ^^^ variable.language
115+
# ^^ variable.other.member
116+
# ^ keyword.operator.assignment
117+
# ^ punctuation.definition.variable
118+
# ^ variable.other.readwrite
119+
return $this.P3
120+
# <- keyword.control
121+
# ^ punctuation.definition.variable
122+
# ^^^^ variable.language
123+
# ^^ variable.other.member
124+
}
125+
}

0 commit comments

Comments
 (0)