Skip to content

Commit 9038901

Browse files
committed
Add context-sensitive classes
1 parent ada344c commit 9038901

File tree

3 files changed

+223
-122
lines changed

3 files changed

+223
-122
lines changed

PowerShell.sublime-syntax

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,6 @@ contexts:
187187
- match: \}
188188
scope: punctuation.section.braces.end.powershell
189189
pop: 1
190-
191-
# TODO: Fix classes, so this doesn't nest here.
192-
- include: class-methods
193-
- match: \b(?i:hidden|static)\b
194-
scope: storage.modifier.powershell
195-
- include: attributes
196-
197-
# Back to normal stuff
198190
- include: statements
199191

200192
###[ COMMANDS ]################################################################
@@ -294,7 +286,7 @@ contexts:
294286
push: [function-body, function-inline-params, function-name]
295287

296288
function-name:
297-
- match: (?:(global|local|script|private):)?((?:\p{L}|\d|_|-|\.)+)
289+
- match: (?:(global|local|script|private):)?({{identifier_function}}+)
298290
captures:
299291
1: storage.modifier.scope.powershell
300292
2: entity.name.function.powershell
@@ -313,7 +305,7 @@ contexts:
313305
pop: 1
314306
- include: comma-separators
315307
- include: types
316-
- match: (\$)\p{L}[\p{L}\p{N}]*
308+
- match: (\$){{identifier_simple}}
317309
scope: variable.parameter.powershell
318310
captures:
319311
1: punctuation.definition.variable.begin.powershell
@@ -330,8 +322,6 @@ contexts:
330322
scope: punctuation.section.block.end.powershell
331323
pop: 1
332324

333-
- include: attributes
334-
335325
- match: \b(?i:(?:Dynamic)?Param){{kebab_break}}
336326
scope: keyword.declaration.parameter.powershell # This scope is not standard
337327
push: expect-param-context
@@ -340,6 +330,7 @@ contexts:
340330
scope: keyword.context.block.powershell
341331
push: expect-function-context
342332

333+
- include: attributes
343334
- include: statements
344335

345336
expect-function-context:
@@ -515,24 +506,46 @@ contexts:
515506
###[ CLASSES ]#################################################################
516507

517508
classes:
518-
- match: \b((?i:class))\s+([\w-]+)\b
519-
captures:
520-
1: storage.type.class.powershell
521-
2: meta.class.powershell entity.name.class.powershell
509+
- match: \b(?i)class(?=\s)
510+
scope: keyword.declaration.class.powershell
511+
push: [class-body, class-parents, class-name]
522512

523-
class-methods:
524-
- match: ^(?:\s*)(?i)(hidden|static)?\s*(\[)((?!\d+|\.)[\p{L}\p{N}.]+)(\])\s*((?:\p{L}|\d|_|-|\.)+)\s*(?=\()
525-
scope: meta.function.powershell
526-
captures:
527-
1: storage.modifier.powershell
528-
2: punctuation.section.brackets.begin.powershell
529-
3: storage.type.powershell
530-
4: punctuation.section.brackets.end.powershell
531-
5: entity.name.function.powershell
532-
push:
533-
- match: (?=\()
534-
pop: 1
535-
- include: comment-line
513+
class-name:
514+
- match: '{{identifier_simple}}'
515+
scope: entity.name.class.powershell
516+
pop: 1
517+
- include: else-pop
518+
519+
class-parents:
520+
- match: ':'
521+
scope: punctuation.separator.type.powershell
522+
push: inside-class-parents
523+
- include: else-pop
524+
525+
inside-class-parents:
526+
- include: comma-separators
527+
- match: '{{identifier_simple}}'
528+
scope: entity.other.inherited-class.powershell
529+
- include: else-pop
530+
531+
class-body:
532+
- meta_scope: meta.class.powershell
533+
- match: \{
534+
scope: punctuation.section.block.begin.powershell
535+
push: inside-class-body
536+
- include: else-pop
537+
538+
inside-class-body:
539+
- match: \}
540+
scope: punctuation.section.block.end.powershell
541+
pop: 1
542+
- match: \b(?i:hidden|static)\b
543+
scope: storage.modifier.powershell
544+
- include: attributes
545+
- include: types-without-members
546+
- include: variables-without-members
547+
- match: (?={{identifier_simple}})
548+
push: [function-body, function-inline-params, function-name]
536549

537550
###[ VARIABLES ]###############################################################
538551

@@ -603,6 +616,16 @@ contexts:
603616
scope: punctuation.accessor.dot.powershell
604617
- match: ::(?=\w)
605618
scope: punctuation.accessor.double-colon.powershell
619+
- match: (new)(\()
620+
captures:
621+
1: meta.function-call.powershell support.function.constructor.powershell
622+
2: meta.function-call.arguments.powershell punctuation.section.arguments.begin.powershell
623+
push:
624+
- meta_content_scope: meta.function-call.arguments.powershell
625+
- match: \)
626+
scope: meta.function-call.arguments.powershell punctuation.section.arguments.end.powershell
627+
pop: 1
628+
- include: expressions
606629
- match: (\w+)(\()
607630
captures:
608631
1: meta.function-call.powershell variable.function.powershell
@@ -963,8 +986,9 @@ variables:
963986

964987
# Variables
965988
# https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_variables
966-
identifier_simple: '[\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Lo}\p{Nd}_?]+'
989+
identifier_simple: '[\p{L}\p{Nd}_?]+'
967990
identifier_special: '[^}]*[^}`]'
991+
identifier_function: '[\p{L}\p{Nd}_.-]+'
968992
# https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables
969993
var_constants: |-
970994
\b(?xi:

Tests/syntax_test_Class.ps1

Lines changed: 107 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -6,93 +6,127 @@ using namespace system.management.automation
66
# ^ punctuation.accessor.dot.powershell
77
# ^ punctuation.accessor.dot.powershell
88

9+
class Foo : Bar, Baz { }
10+
#^^^^^^^^^^^^^^^^^^^^^^^^ meta.class.powershell
11+
#^^^^ keyword.declaration.class.powershell
12+
# ^^^ entity.name.class.powershell
13+
# @@@ definition
14+
# ^ punctuation.separator.type.powershell
15+
# ^^^ entity.other.inherited-class.powershell
16+
# @@@ reference
17+
# ^ punctuation.separator.sequence.powershell
18+
# ^^^ entity.other.inherited-class.powershell
19+
# @@@ reference
20+
# ^ punctuation.section.block.begin.powershell
21+
# ^ punctuation.section.block.end.powershell
22+
923
# Define a class
1024
class TypeName
11-
# <- storage.type
12-
# ^^^^^^^^ entity.name.class
25+
#^^^^^^^^^^^^^ meta.class.powershell
26+
#^^^^ keyword.declaration.class.powershell
27+
# ^^^^^^^^ entity.name.class.powershell
1328
# @@@@@@@@ definition
1429
{
1530
# Property with validate set
1631
# <- punctuation.definition.comment
1732
# ^^^^^^^^^^^^^^^^^^^^^^^^^^ comment.line
1833
[ValidateSet("val1", "Val2")]
19-
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.attribute
20-
# <- punctuation.section.brackets.begin
21-
# ^^^^^^^^^^ support.function.attribute
22-
# ^ punctuation.section.group.begin
23-
# ^^^^^^ string.quoted.double
24-
# ^ punctuation.separator
25-
# ^^^^^^ string.quoted.double
26-
# ^ punctuation.section.group.end
27-
# ^ punctuation.section.brackets.end
34+
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.class.powershell
35+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.attribute.powershell
36+
# ^ punctuation.section.brackets.begin.powershell
37+
# ^^^^^^^^^^^ support.function.attribute.powershell
38+
# ^ punctuation.section.group.begin.powershell
39+
# ^^^^^^ meta.string.interpolated.powershell string.quoted.double.powershell
40+
# ^ punctuation.definition.string.begin.powershell
41+
# ^ punctuation.definition.string.end.powershell
42+
# ^ punctuation.separator.sequence.powershell
43+
# ^^^^^^ meta.string.interpolated.powershell string.quoted.double.powershell
44+
# ^ punctuation.definition.string.begin.powershell
45+
# ^ punctuation.definition.string.end.powershell
46+
# ^ punctuation.section.group.end.powershell
47+
# ^ punctuation.section.brackets.end.powershell
2848
[string] $P1
29-
# <- punctuation.section.brackets.begin
30-
# ^^^^^ storage.type
31-
# ^ punctuation.section.brackets.end
32-
# ^ punctuation.definition.variable
33-
# ^^ variable.other.readwrite
49+
#^^^^^^^^^^^^^^^ meta.class.powershell
50+
# ^ punctuation.section.brackets.begin.powershell
51+
# ^^^^^^ storage.type.powershell
52+
# ^ punctuation.section.brackets.end.powershell
53+
# ^^^ variable.other.readwrite.powershell
54+
# ^ punctuation.definition.variable.powershell
3455

3556
# Static property
3657
# <- punctuation.definition.comment
3758
# ^^^^^^^^^^^^^^^ comment.line
3859
static [hashtable] $P2
39-
#^^^^^ storage.modifier
40-
# ^ punctuation.section.brackets.begin
41-
# ^^^^^^^^^ storage.type
42-
# ^ punctuation.section.brackets.end
43-
# ^ punctuation.definition.variable
44-
# ^^ variable.other.readwrite
60+
#^^^^^^^^^^^^^^^^^^^^^^^^^ meta.class.powershell
61+
# ^^^^^^ storage.modifier.powershell
62+
# ^ punctuation.section.brackets.begin.powershell
63+
# ^^^^^^^^^ storage.type.powershell
64+
# ^ punctuation.section.brackets.end.powershell
65+
# ^^^ variable.other.readwrite.powershell
66+
# ^ punctuation.definition.variable.powershell
4567

4668
# Hidden property does not show as result of Get-Member
4769
# <- punctuation.definition.comment
4870
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ comment.line
4971
hidden [int] $P3
50-
#^^^^^ storage.modifier
51-
# ^ punctuation.section.brackets.begin
52-
# ^ storage.type
53-
# ^ punctuation.section.brackets.end
54-
# ^ punctuation.definition.variable
55-
# ^^ variable.other.readwrite
72+
#^^^^^^^^^^^^^^^^^^^ meta.class.powershell
73+
# ^^^^^^ storage.modifier.powershell
74+
# ^ punctuation.section.brackets.begin.powershell
75+
# ^^^ storage.type.powershell
76+
# ^ punctuation.section.brackets.end.powershell
77+
# ^^^ variable.other.readwrite.powershell
78+
# ^ punctuation.definition.variable.powershell
5679

5780
# Constructor
5881
# <- punctuation.definition.comment
5982
# ^^^^^^^^^^^ comment.line
6083
TypeName ([string] $s) {
61-
# ^ punctuation.section.group.begin
62-
# ^ punctuation.section.brackets.begin
63-
# ^^^^^^ storage.type
64-
# ^ punctuation.section.brackets.end
65-
# ^ punctuation.definition.variable
66-
# ^ variable.other.readwrite
67-
# ^ punctuation.section.group.end
84+
#^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.class.powershell
85+
# ^^^^^^^^^^^^^^^^^^^^^^^^ meta.function.powershell
86+
# ^^^^^^^^ entity.name.function.powershell
87+
# @@@@@@@@ definition
88+
# ^ punctuation.section.parameters.begin.powershell
89+
# ^ punctuation.section.brackets.begin.powershell
90+
# ^^^^^^ storage.type.powershell
91+
# ^ punctuation.section.brackets.end.powershell
92+
# ^^ variable.parameter.powershell
93+
# ^ punctuation.definition.variable.begin.powershell
94+
# ^ punctuation.section.parameters.end.powershell
95+
# ^ punctuation.section.block.begin.powershell
6896
$this.P1 = $s
69-
# <- punctuation.definition.variable
70-
# ^ variable.language
71-
# ^^ variable.other.member
72-
# ^ keyword.operator.assignment
73-
# ^ punctuation.definition.variable
74-
# ^ variable.other.readwrite
97+
#^^^^^^^^^^^^^^^^^^^^ meta.class.powershell meta.function.powershell
98+
# ^^^^^ variable.language.powershell
99+
# ^ punctuation.definition.variable.powershell
100+
# ^ punctuation.accessor.dot.powershell
101+
# ^^ variable.other.member.powershell
102+
# ^ keyword.operator.assignment.powershell
103+
# ^^ variable.other.readwrite.powershell
104+
# ^ punctuation.definition.variable.powershell
75105
}
76106

77107
# Static method
78108
# <- punctuation.definition.comment
79109
# ^^^^^^^^^^^^^ comment.line
80110
static [void] MemberMethod1([hashtable] $h) {
81-
# <- storage.modifier
82-
# ^ punctuation.section.brackets.begin
83-
# ^^^^ storage.type
84-
# ^ punctuation.section.brackets.end
85-
# ^^^^^^^^^^^^^ entity.name.function
86-
# @@@@@@@@@@@@@ definition
87-
# ^ punctuation.section.group.begin
88-
# ^ punctuation.section.brackets.begin
89-
# ^^^^^^^^^ storage.type
90-
# ^ punctuation.section.brackets.end
91-
# ^ punctuation.definition.variable
92-
# ^ variable.other.readwrite
93-
# ^ punctuation.section.group.end
111+
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.class.powershell
112+
#^^^ meta.function.powershell
113+
# ^^^^^^ storage.modifier.powershell
114+
# ^ punctuation.section.brackets.begin.powershell
115+
# ^^^^ storage.type.powershell
116+
# ^ punctuation.section.brackets.end.powershell
117+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function.powershell
118+
# ^^^^^^^^^^^^^ entity.name.function.powershell
119+
# @@@@@@@@@@@@@ definition
120+
# ^ punctuation.section.parameters.begin.powershell
121+
# ^ punctuation.section.brackets.begin.powershell
122+
# ^^^^^^^^^ storage.type.powershell
123+
# ^ punctuation.section.brackets.end.powershell
124+
# ^^ variable.parameter.powershell
125+
# ^ punctuation.definition.variable.begin.powershell
126+
# ^ punctuation.section.parameters.end.powershell
127+
# ^ punctuation.section.block.begin.powershell
94128
[TypeName]::P2 = $h
95-
#^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.block.powershell meta.block.powershell
129+
#^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.class.powershell meta.function.powershell
96130
# ^ punctuation.section.brackets.begin.powershell
97131
# ^^^^^^^^ support.type.powershell
98132
# ^ punctuation.section.brackets.end.powershell
@@ -107,27 +141,29 @@ class TypeName
107141
# <- punctuation.definition.comment
108142
# ^^^^^^^^^^^^^^^ comment.line
109143
[int] MemberMethod2([int] $i) {
110-
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.block.powershell
111-
#^^^^^^^^^^^^^^^^^^^^^^ meta.function.powershell
144+
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.class.powershell
145+
#^^^ meta.function.powershell
112146
# ^ punctuation.section.brackets.begin.powershell
113147
# ^^^ storage.type.powershell
114148
# ^ punctuation.section.brackets.end.powershell
149+
# ^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function.powershell
115150
# ^^^^^^^^^^^^^ entity.name.function.powershell
116151
# @@@@@@@@@@@@@ definition
117-
# ^^^^^^^^^^ meta.group.powershell
118-
# ^ punctuation.section.group.begin.powershell
152+
# ^ punctuation.section.parameters.begin.powershell
119153
# ^ punctuation.section.brackets.begin.powershell
120154
# ^^^ storage.type.powershell
121155
# ^ punctuation.section.brackets.end.powershell
122-
# ^^ variable.other.readwrite.powershell
123-
# ^ punctuation.definition.variable.powershell
124-
# ^ punctuation.section.group.end.powershell
125-
# ^ meta.block.powershell punctuation.section.braces.begin.powershell
156+
# ^^ variable.parameter.powershell
157+
# ^ punctuation.definition.variable.begin.powershell
158+
# ^ punctuation.section.parameters.end.powershell
159+
# ^ punctuation.section.block.begin.powershell
126160
return $this.P3
127-
# <- keyword.control
128-
# ^ punctuation.definition.variable
129-
# ^^^^ variable.language
130-
# ^^ variable.other.member
161+
#^^^^^^^^^^^^^^^^^^^^^^ meta.class.powershell meta.function.powershell
162+
# ^^^^^^ keyword.control.flow.return.powershell
163+
# ^^^^^ variable.language.powershell
164+
# ^ punctuation.definition.variable.powershell
165+
# ^ punctuation.accessor.dot.powershell
166+
# ^^ variable.other.member.powershell
131167
}
132168
}
133169

@@ -166,12 +202,15 @@ class Book {
166202
[string[]] $Tags
167203
# Default constructor
168204
Book() { $this.Init(@{}) }
205+
# @@@@ definition
169206
# @@@@ reference
170207
# Convenience constructor from hashtable
171208
Book([hashtable]$Properties) { $this.Init($Properties) }
209+
# @@@@ definition
172210
# @@@@ reference
173211
# Common constructor for title and author
174212
Book([string]$Title, [string]$Author) {
213+
# @@@@ definition
175214
$this.Init(@{Title = $Title; Author = $Author })
176215
# @@@@ reference
177216
}
@@ -336,6 +375,7 @@ class BookList {
336375
}
337376
# Find every book matching the filtering scriptblock.
338377
static [Book[]] FindAll([scriptblock]$Predicate) {
378+
# @@@@@@@ definition
339379
[BookList]::Initialize()
340380
# @@@@@@@@@@ reference
341381
return [BookList]::Books.FindAll($Predicate)

0 commit comments

Comments
 (0)