Skip to content

Commit e180d24

Browse files
committed
Consume string with a trailing dot before checking for keywords
This makes negative lookbehind assertions on keywords unnecessary.
1 parent 11aecf6 commit e180d24

File tree

2 files changed

+34
-30
lines changed

2 files changed

+34
-30
lines changed

PowerShell.sublime-syntax

+28-24
Original file line numberDiff line numberDiff line change
@@ -75,73 +75,77 @@ contexts:
7575
- include: main
7676
- match: \b[\w.-]+\.(?i:exe|com|cmd|bat)\b
7777
scope: variable.function.powershell
78+
# Consume a string with a trailing dot
79+
# to prevent members with particular names from being recognized as keywords.
80+
- match: \b[\w-]+(?=\.)
81+
push: members
7882
# Exceptions
79-
- match: (?<![.])\b(?i:throw){{kebab_break}}
83+
- match: \b(?i:throw){{kebab_break}}
8084
scope: keyword.control.exception.raise.powershell
81-
- match: (?<![.])\b(?i:try){{kebab_break}}
85+
- match: \b(?i:try){{kebab_break}}
8286
scope: keyword.control.exception.try.powershell
83-
- match: (?<![.])\b(?i:catch|trap){{kebab_break}}
87+
- match: \b(?i:catch|trap){{kebab_break}}
8488
scope: keyword.control.exception.catch.powershell
85-
- match: (?<![.])\b(?i:finally){{kebab_break}}
89+
- match: \b(?i:finally){{kebab_break}}
8690
scope: keyword.control.exception.finally.powershell
8791
# Conditionals
88-
- match: (?<![.])\b(?i:if){{kebab_break}}
92+
- match: \b(?i:if){{kebab_break}}
8993
scope: keyword.control.conditional.if.powershell
90-
- match: (?<![.])\b(?i:elseif){{kebab_break}}
94+
- match: \b(?i:elseif){{kebab_break}}
9195
scope: keyword.control.conditional.elseif.powershell
92-
- match: (?<![.])\b(?i:else){{kebab_break}}
96+
- match: \b(?i:else){{kebab_break}}
9397
scope: keyword.control.conditional.else.powershell
94-
- match: (?<![.])\b(?i:switch){{kebab_break}}
98+
- match: \b(?i:switch){{kebab_break}}
9599
scope: keyword.control.conditional.switch.powershell
96100
- match: \?
97101
scope: keyword.control.conditional.select.powershell
98102
- match: \b(?i:where(?!-object)){{kebab_break}}
99103
scope: keyword.control.conditional.select.powershell
100104
# Begin/End
101-
- match: (?<![.])\b(?i:begin){{kebab_break}}
105+
- match: \b(?i:begin){{kebab_break}}
102106
scope: keyword.context.block.begin.powershell
103-
- match: (?<![.])\b(?i:process){{kebab_break}}
107+
- match: \b(?i:process){{kebab_break}}
104108
scope: keyword.context.block.process.powershell
105-
- match: (?<![.])\b(?i:end){{kebab_break}}
109+
- match: \b(?i:end){{kebab_break}}
106110
scope: keyword.context.block.end.powershell
107-
- match: (?<![.])\b(?i:clean){{kebab_break}}
111+
- match: \b(?i:clean){{kebab_break}}
108112
scope: keyword.context.block.clean.powershell
109113
# Loops
110114
- match: \b(?i:for|foreach(?!-object)){{kebab_break}}
111115
scope: keyword.control.loop.for.powershell
112-
- match: (?<![.])\b(?i:do){{kebab_break}}
116+
- match: \b(?i:do){{kebab_break}}
113117
scope: keyword.control.loop.do-while.powershell
114-
- match: (?<![.])\b(?i:while){{kebab_break}}
118+
- match: \b(?i:while){{kebab_break}}
115119
scope: keyword.control.loop.while.powershell
116-
- match: (?<![.])\b(?i:until){{kebab_break}}
120+
- match: \b(?i:until){{kebab_break}}
117121
scope: keyword.control.loop.repeat-until.powershell
118122
# Flow
119-
- match: (?<![.])\b(?i:break){{kebab_break}}
123+
- match: \b(?i:break){{kebab_break}}
120124
scope: keyword.control.flow.break.powershell
121-
- match: (?<![.])\b(?i:continue){{kebab_break}}
125+
- match: \b(?i:continue){{kebab_break}}
122126
scope: keyword.control.flow.continue.powershell
123-
- match: (?<![.])\b(?i:exit){{kebab_break}}
127+
- match: \b(?i:exit){{kebab_break}}
124128
scope: keyword.control.flow.exit.powershell
125-
- match: (?<![.])\b(?i:return){{kebab_break}}
129+
- match: \b(?i:return){{kebab_break}}
126130
scope: keyword.control.flow.return.powershell
127131
# Declaration
128-
- match: (?<![.])\b(?i:var){{kebab_break}}
132+
- match: \b(?i:var){{kebab_break}}
129133
# scope: storage.type.variable.powershell
130134
scope: keyword.declaration.variable.powershell
131-
- match: (?<![.])\b(?i:(?:dynamic)?param){{kebab_break}}
135+
- match: \b(?i:(?:dynamic)?param){{kebab_break}}
132136
scope: keyword.declaration.parameter.powershell # This scope is not standard
133137
# Uncategorized keywords
134-
- match: (?<![.])\b(?i:data|default|define|from|in|inlinescript|parallel|sequence){{kebab_break}}
138+
- match: \b(?i:data|default|define|from|in|inlinescript|parallel|sequence){{kebab_break}}
135139
scope: keyword.control.powershell
136140
- match: \B--%\B
137141
scope: keyword.control.powershell
138142
push:
139143
- meta_content_scope: string.unquoted.powershell
140144
- include: pop-before-newline
141-
- match: (?<![.])\b(?i:hidden|static)\b
145+
- match: \b(?i:hidden|static)\b
142146
# This should only be relevant inside a class but will require a rework of how classes are matched. This is a temp fix.
143147
scope: storage.modifier.powershell
144-
- match: (?<![.])\b((?i:class))\s+([\w-]+)\b
148+
- match: \b((?i:class))\s+([\w-]+)\b
145149
captures:
146150
1: storage.type.class.powershell
147151
2: meta.class.powershell entity.name.class.powershell

Tests/syntax_test_PowerShell.ps1

+6-6
Original file line numberDiff line numberDiff line change
@@ -1360,12 +1360,12 @@ New-Object -TypeName System.Diagnostics.Process
13601360
# ^^^^^^^ - keyword.control
13611361
New-Object -TypeName System.Data
13621362
# ^^^^ - keyword.control
1363-
New-Object -TypeName System.if
1364-
# ^^ - keyword.control
1365-
New-Object -TypeName System.Clean
1366-
# ^^^^^ - keyword.control
1367-
New-Object -TypeName System.Throw
1368-
# ^^^^^ - keyword.control
1363+
New-Object -TypeName Sy-stem.if
1364+
# ^^ - keyword.control
1365+
New-Object -TypeName S_ystem.Clean
1366+
# ^^^^^ - keyword.control
1367+
New-Object -TypeName Sy_stem-.Throw
1368+
# ^^^^^ - keyword.control
13691369
echo `"test`"
13701370
# ^^^^^^^^^ - string.quoted
13711371
# ^^ constant.character.escape

0 commit comments

Comments
 (0)