Skip to content

Commit b8f8ac2

Browse files
committed
A final fix for variables
1 parent 477de13 commit b8f8ac2

File tree

3 files changed

+284
-5
lines changed

3 files changed

+284
-5
lines changed

Support/PowershellSyntax.YAML-tmLanguage

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ patterns:
3535
begin: (?<!(?<!`)")"
3636
end: '"(?!")'
3737
patterns:
38-
- include: '#variable'
38+
- include: '#variableNoProperty'
3939
- include: '#doubleQuotedStringEscapes'
4040
- include: '#interpolation'
4141
- match: '`\s*$'
@@ -52,7 +52,7 @@ patterns:
5252
begin: '@"$'
5353
end: ^"@
5454
patterns:
55-
- include: '#variable'
55+
- include: '#variableNoProperty'
5656
- include: '#doubleQuotedStringEscapes'
5757
- include: '#interpolation'
5858
- name: meta.group.array-expression.powershell
@@ -209,6 +209,66 @@ repository:
209209
'3': {name: variable.other.readwrite.powershell}
210210
'4': {name: keyword.other.powershell}
211211
'5': {name: entity.name.function.invocation.powershell}
212+
variableNoProperty:
213+
patterns:
214+
- comment: 'These are special constants.'
215+
match: (\$)(?i:(False|Null|True))\b
216+
captures:
217+
'1': {name: keyword.other.powershell}
218+
'2': {name: constant.language.powershell}
219+
220+
- comment: 'These are the other built-in constants.'
221+
match: (\$)(?i:(Error|ExecutionContext|Host|Home|PID|PsHome|PsVersionTable|ShellID))\b
222+
captures:
223+
'1': {name: keyword.other.powershell}
224+
'2': {name: support.constant.variable.powershell}
225+
'3': {name: entity.name.function.invocation.powershell}
226+
227+
- comment: 'Automatic variables are not constants, but they are read-only...'
228+
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))\b
229+
captures:
230+
'1': {name: keyword.other.powershell}
231+
'2': {name: support.variable.automatic.powershell}
232+
'3': {name: entity.name.function.invocation.powershell}
233+
234+
- comment: Style preference variables as language variables so that they stand out.
235+
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))\b
236+
captures:
237+
'1': {name: keyword.other.powershell}
238+
'2': {name: variable.language.powershell}
239+
'3': {name: entity.name.function.invocation.powershell}
240+
241+
# then, there are four variations on variables, in general:
242+
# with and without scope, and with and without curly braces
243+
- match: (?i:(\$)(global|local|private|script|using|workflow):((?:\p{L}|\d|_)+))
244+
captures:
245+
'1': {name: keyword.other.powershell}
246+
'2': {name: storage.modifier.scope.powershell}
247+
'3': {name: variable.other.normal.powershell}
248+
'4': {name: entity.name.function.invocation.powershell}
249+
250+
- match: (?i:(\$\{)(global|local|private|script|using|workflow):([^}]*[^}`])(\}))
251+
captures:
252+
'1': {name: keyword.other.powershell}
253+
'2': {name: storage.modifier.scope.powershell}
254+
'3': {name: variable.other.readwrite.powershell}
255+
'4': {name: keyword.other.powershell}
256+
'5': {name: entity.name.function.invocation.powershell}
257+
258+
- match: (?i:(\$)((?:\p{L}|\d|_)+:)?((?:\p{L}|\d|_)+))
259+
captures:
260+
'1': {name: keyword.other.powershell}
261+
'2': {name: support.variable.drive.powershell}
262+
'3': {name: variable.other.readwrite.powershell}
263+
'4': {name: entity.name.function.invocation.powershell}
264+
265+
- match: (?i:(\$\{)((?:\p{L}|\d|_)+:)?([^}]*[^}`])(\}))
266+
captures:
267+
'1': {name: keyword.other.powershell}
268+
'2': {name: support.variable.drive.powershell}
269+
'3': {name: variable.other.readwrite.powershell}
270+
'4': {name: keyword.other.powershell}
271+
'5': {name: entity.name.function.invocation.powershell}
212272

213273
commands:
214274
comment: 'Verb-Noun pattern:'

Support/PowershellSyntax.tmLanguage

Lines changed: 217 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
<array>
9292
<dict>
9393
<key>include</key>
94-
<string>#variable</string>
94+
<string>#variableNoProperty</string>
9595
</dict>
9696
<dict>
9797
<key>include</key>
@@ -143,7 +143,7 @@
143143
<array>
144144
<dict>
145145
<key>include</key>
146-
<string>#variable</string>
146+
<string>#variableNoProperty</string>
147147
</dict>
148148
<dict>
149149
<key>include</key>
@@ -912,6 +912,221 @@
912912
</dict>
913913
</array>
914914
</dict>
915+
<key>variableNoProperty</key>
916+
<dict>
917+
<key>patterns</key>
918+
<array>
919+
<dict>
920+
<key>captures</key>
921+
<dict>
922+
<key>1</key>
923+
<dict>
924+
<key>name</key>
925+
<string>keyword.other.powershell</string>
926+
</dict>
927+
<key>2</key>
928+
<dict>
929+
<key>name</key>
930+
<string>constant.language.powershell</string>
931+
</dict>
932+
</dict>
933+
<key>comment</key>
934+
<string>These are special constants.</string>
935+
<key>match</key>
936+
<string>(\$)(?i:(False|Null|True))\b</string>
937+
</dict>
938+
<dict>
939+
<key>captures</key>
940+
<dict>
941+
<key>1</key>
942+
<dict>
943+
<key>name</key>
944+
<string>keyword.other.powershell</string>
945+
</dict>
946+
<key>2</key>
947+
<dict>
948+
<key>name</key>
949+
<string>support.constant.variable.powershell</string>
950+
</dict>
951+
<key>3</key>
952+
<dict>
953+
<key>name</key>
954+
<string>entity.name.function.invocation.powershell</string>
955+
</dict>
956+
</dict>
957+
<key>comment</key>
958+
<string>These are the other built-in constants.</string>
959+
<key>match</key>
960+
<string>(\$)(?i:(Error|ExecutionContext|Host|Home|PID|PsHome|PsVersionTable|ShellID))\b</string>
961+
</dict>
962+
<dict>
963+
<key>captures</key>
964+
<dict>
965+
<key>1</key>
966+
<dict>
967+
<key>name</key>
968+
<string>keyword.other.powershell</string>
969+
</dict>
970+
<key>2</key>
971+
<dict>
972+
<key>name</key>
973+
<string>support.variable.automatic.powershell</string>
974+
</dict>
975+
<key>3</key>
976+
<dict>
977+
<key>name</key>
978+
<string>entity.name.function.invocation.powershell</string>
979+
</dict>
980+
</dict>
981+
<key>comment</key>
982+
<string>Automatic variables are not constants, but they are read-only...</string>
983+
<key>match</key>
984+
<string>(\$)(?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))\b</string>
985+
</dict>
986+
<dict>
987+
<key>captures</key>
988+
<dict>
989+
<key>1</key>
990+
<dict>
991+
<key>name</key>
992+
<string>keyword.other.powershell</string>
993+
</dict>
994+
<key>2</key>
995+
<dict>
996+
<key>name</key>
997+
<string>variable.language.powershell</string>
998+
</dict>
999+
<key>3</key>
1000+
<dict>
1001+
<key>name</key>
1002+
<string>entity.name.function.invocation.powershell</string>
1003+
</dict>
1004+
</dict>
1005+
<key>comment</key>
1006+
<string>Style preference variables as language variables so that they stand out.</string>
1007+
<key>match</key>
1008+
<string>(\$)(?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))\b</string>
1009+
</dict>
1010+
<dict>
1011+
<key>captures</key>
1012+
<dict>
1013+
<key>1</key>
1014+
<dict>
1015+
<key>name</key>
1016+
<string>keyword.other.powershell</string>
1017+
</dict>
1018+
<key>2</key>
1019+
<dict>
1020+
<key>name</key>
1021+
<string>storage.modifier.scope.powershell</string>
1022+
</dict>
1023+
<key>3</key>
1024+
<dict>
1025+
<key>name</key>
1026+
<string>variable.other.normal.powershell</string>
1027+
</dict>
1028+
<key>4</key>
1029+
<dict>
1030+
<key>name</key>
1031+
<string>entity.name.function.invocation.powershell</string>
1032+
</dict>
1033+
</dict>
1034+
<key>match</key>
1035+
<string>(?i:(\$)(global|local|private|script|using|workflow):((?:\p{L}|\d|_)+))</string>
1036+
</dict>
1037+
<dict>
1038+
<key>captures</key>
1039+
<dict>
1040+
<key>1</key>
1041+
<dict>
1042+
<key>name</key>
1043+
<string>keyword.other.powershell</string>
1044+
</dict>
1045+
<key>2</key>
1046+
<dict>
1047+
<key>name</key>
1048+
<string>storage.modifier.scope.powershell</string>
1049+
</dict>
1050+
<key>3</key>
1051+
<dict>
1052+
<key>name</key>
1053+
<string>variable.other.readwrite.powershell</string>
1054+
</dict>
1055+
<key>4</key>
1056+
<dict>
1057+
<key>name</key>
1058+
<string>keyword.other.powershell</string>
1059+
</dict>
1060+
<key>5</key>
1061+
<dict>
1062+
<key>name</key>
1063+
<string>entity.name.function.invocation.powershell</string>
1064+
</dict>
1065+
</dict>
1066+
<key>match</key>
1067+
<string>(?i:(\$\{)(global|local|private|script|using|workflow):([^}]*[^}`])(\}))</string>
1068+
</dict>
1069+
<dict>
1070+
<key>captures</key>
1071+
<dict>
1072+
<key>1</key>
1073+
<dict>
1074+
<key>name</key>
1075+
<string>keyword.other.powershell</string>
1076+
</dict>
1077+
<key>2</key>
1078+
<dict>
1079+
<key>name</key>
1080+
<string>support.variable.drive.powershell</string>
1081+
</dict>
1082+
<key>3</key>
1083+
<dict>
1084+
<key>name</key>
1085+
<string>variable.other.readwrite.powershell</string>
1086+
</dict>
1087+
<key>4</key>
1088+
<dict>
1089+
<key>name</key>
1090+
<string>entity.name.function.invocation.powershell</string>
1091+
</dict>
1092+
</dict>
1093+
<key>match</key>
1094+
<string>(?i:(\$)((?:\p{L}|\d|_)+:)?((?:\p{L}|\d|_)+))</string>
1095+
</dict>
1096+
<dict>
1097+
<key>captures</key>
1098+
<dict>
1099+
<key>1</key>
1100+
<dict>
1101+
<key>name</key>
1102+
<string>keyword.other.powershell</string>
1103+
</dict>
1104+
<key>2</key>
1105+
<dict>
1106+
<key>name</key>
1107+
<string>support.variable.drive.powershell</string>
1108+
</dict>
1109+
<key>3</key>
1110+
<dict>
1111+
<key>name</key>
1112+
<string>variable.other.readwrite.powershell</string>
1113+
</dict>
1114+
<key>4</key>
1115+
<dict>
1116+
<key>name</key>
1117+
<string>keyword.other.powershell</string>
1118+
</dict>
1119+
<key>5</key>
1120+
<dict>
1121+
<key>name</key>
1122+
<string>entity.name.function.invocation.powershell</string>
1123+
</dict>
1124+
</dict>
1125+
<key>match</key>
1126+
<string>(?i:(\$\{)((?:\p{L}|\d|_)+:)?([^}]*[^}`])(\}))</string>
1127+
</dict>
1128+
</array>
1129+
</dict>
9151130
</dict>
9161131
<key>scopeName</key>
9171132
<string>source.powershell</string>

bin/test-file.ps1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,13 @@ string.'
9999
"Sometimes we use ${drive:item} when refering to objects as variables"
100100

101101

102-
# TODO: In PowerShell 3 the variable here is "drive:item" not just "drive" (as it would have been in 3)
102+
# In PowerShell 3 the variable here is "drive:item" not just "drive" (as it would have been in 2)
103103
"Sometimes we use $drive:item when refering to objects as variables"
104104

105+
"But this variable dot-notation $variable.foo shouldn't highlight in a string"
106+
107+
"Although obviously in a $($subexpression.Length) it should be!"
108+
105109
# TODO: can we highlight this as an error?
106110
if(!($PSVersionTable.PSVersion -ge '3.0') {
107111
# In PowerShell 3, this will cause an ERROR

0 commit comments

Comments
 (0)