Skip to content

Commit 111ebd3

Browse files
omniomiTylerLeonhardt
authored andcommitted
Using, Requires, Hidden, and Static (#82)
* add hidden control word * Using directive. * Add static keyword. * add tests for using. * tweak scoping. * Requires typical cases * language specific scoping
1 parent 28a32a4 commit 111ebd3

File tree

5 files changed

+224
-45
lines changed

5 files changed

+224
-45
lines changed

PowerShellSyntax.tmLanguage

Lines changed: 186 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@
5656
<key>include</key>
5757
<string>#commentEmbeddedDocs</string>
5858
</dict>
59+
<dict>
60+
<key>include</key>
61+
<string>#RequiresDirective</string>
62+
</dict>
5963
</array>
6064
</dict>
6165
<dict>
@@ -84,6 +88,10 @@
8488
<key>include</key>
8589
<string>#attribute</string>
8690
</dict>
91+
<dict>
92+
<key>include</key>
93+
<string>#UsingDirective</string>
94+
</dict>
8795
<dict>
8896
<key>include</key>
8997
<string>#type</string>
@@ -246,10 +254,18 @@
246254
</dict>
247255
<dict>
248256
<key>match</key>
249-
<string>(?&lt;!\w)((?i:begin|break|catch|continue|data|define|do|dynamicparam|else|elseif|end|exit|finally|for|foreach(?!-object)|from|if|in|inlinescript|parallel|param|process|return|switch|throw|trap|try|until|using|var|where(?!-object)|while)|%|\?)(?!\w)</string>
257+
<string>(?&lt;!\w)((?i:begin|break|catch|continue|data|define|do|dynamicparam|else|elseif|end|exit|finally|for|foreach(?!-object)|from|if|in|inlinescript|parallel|param|process|return|switch|throw|trap|try|until|var|where(?!-object)|while)|%|\?)(?!\w)</string>
250258
<key>name</key>
251259
<string>keyword.control.powershell</string>
252260
</dict>
261+
<dict>
262+
<key>comment</key>
263+
<string>This should only be relevant inside a class but will require a rework of how classes are matched. This is a temp fix.</string>
264+
<key>match</key>
265+
<string>(?&lt;!\w)((?i:hidden|static))(?!\w)</string>
266+
<key>name</key>
267+
<string>storage.modifier.powershell</string>
268+
</dict>
253269
<dict>
254270
<key>captures</key>
255271
<dict>
@@ -499,30 +515,6 @@
499515
<key>name</key>
500516
<string>comment.documentation.embedded.powershell</string>
501517
</dict>
502-
<dict>
503-
<key>captures</key>
504-
<dict>
505-
<key>1</key>
506-
<dict>
507-
<key>name</key>
508-
<string>constant.string.documentation.powershell</string>
509-
</dict>
510-
<key>2</key>
511-
<dict>
512-
<key>name</key>
513-
<string>keyword.operator.documentation.powershell</string>
514-
</dict>
515-
<key>3</key>
516-
<dict>
517-
<key>name</key>
518-
<string>string.quoted.double.heredoc.powershell</string>
519-
</dict>
520-
</dict>
521-
<key>match</key>
522-
<string>(?i:requires\s+-(Version\s+\d(.\d+)?|Assembly\s+(.*)|Module\s+(.*)|PsSnapIn\s+(.*)|ShellId\s+(.*)))</string>
523-
<key>name</key>
524-
<string>comment.documentation.embedded.powershell</string>
525-
</dict>
526518
</array>
527519
</dict>
528520
<key>doubleQuotedStringEscapes</key>
@@ -1061,6 +1053,175 @@
10611053
</dict>
10621054
</array>
10631055
</dict>
1056+
<key>UsingDirective</key>
1057+
<dict>
1058+
<key>match</key>
1059+
<string>(?&lt;!\w)(?i:(using))\s+(?i:(namespace|module))\s+(?i:((?:\w+(?:\.)?)+))</string>
1060+
<key>captures</key>
1061+
<dict>
1062+
<key>1</key>
1063+
<dict>
1064+
<key>name</key>
1065+
<string>keyword.control.using.powershell</string>
1066+
</dict>
1067+
<key>2</key>
1068+
<dict>
1069+
<key>name</key>
1070+
<string>keyword.other.powershell</string>
1071+
</dict>
1072+
<key>3</key>
1073+
<dict>
1074+
<key>name</key>
1075+
<string>variable.parameter.powershell</string>
1076+
</dict>
1077+
</dict>
1078+
</dict>
1079+
<key>RequiresDirective</key>
1080+
<dict>
1081+
<key>patterns</key>
1082+
<array>
1083+
<dict>
1084+
<key>match</key>
1085+
<string>(?i:(requires))\s(?i:(-Modules))\s(((?:\w+)\,?\s?){1,})$</string>
1086+
<key>captures</key>
1087+
<dict>
1088+
<key>0</key>
1089+
<dict>
1090+
<key>name</key>
1091+
<string>meta.requires.powershell</string>
1092+
</dict>
1093+
<key>1</key>
1094+
<dict>
1095+
<key>name</key>
1096+
<string>keyword.control.requires.powershell</string>
1097+
</dict>
1098+
<key>2</key>
1099+
<dict>
1100+
<key>name</key>
1101+
<string>keyword.other.powershell</string>
1102+
</dict>
1103+
<key>3</key>
1104+
<dict>
1105+
<key>name</key>
1106+
<string>variable.parameter.powershell</string>
1107+
</dict>
1108+
</dict>
1109+
</dict>
1110+
<dict>
1111+
<key>match</key>
1112+
<string>(?i:(requires))\s(?i:(-ShellId))\s(\w+)$</string>
1113+
<key>captures</key>
1114+
<dict>
1115+
<key>0</key>
1116+
<dict>
1117+
<key>name</key>
1118+
<string>meta.requires.powershell</string>
1119+
</dict>
1120+
<key>1</key>
1121+
<dict>
1122+
<key>name</key>
1123+
<string>keyword.control.requires.powershell</string>
1124+
</dict>
1125+
<key>2</key>
1126+
<dict>
1127+
<key>name</key>
1128+
<string>keyword.other.powershell</string>
1129+
</dict>
1130+
<key>3</key>
1131+
<dict>
1132+
<key>name</key>
1133+
<string>variable.parameter.powershell</string>
1134+
</dict>
1135+
</dict>
1136+
</dict>
1137+
<dict>
1138+
<key>match</key>
1139+
<string>(?i:(requires))\s(?i:(-RunAsAdministrator))$</string>
1140+
<key>captures</key>
1141+
<dict>
1142+
<key>0</key>
1143+
<dict>
1144+
<key>name</key>
1145+
<string>meta.requires.powershell</string>
1146+
</dict>
1147+
<key>1</key>
1148+
<dict>
1149+
<key>name</key>
1150+
<string>keyword.control.requires.powershell</string>
1151+
</dict>
1152+
<key>2</key>
1153+
<dict>
1154+
<key>name</key>
1155+
<string>keyword.other.powershell</string>
1156+
</dict>
1157+
</dict>
1158+
</dict>
1159+
<dict>
1160+
<key>match</key>
1161+
<string>(?i:(requires))\s(?i:(-Version))\s((?:\d\.?){1,})$</string>
1162+
<key>captures</key>
1163+
<dict>
1164+
<key>0</key>
1165+
<dict>
1166+
<key>name</key>
1167+
<string>meta.requires.powershell</string>
1168+
</dict>
1169+
<key>1</key>
1170+
<dict>
1171+
<key>name</key>
1172+
<string>keyword.control.requires.powershell</string>
1173+
</dict>
1174+
<key>2</key>
1175+
<dict>
1176+
<key>name</key>
1177+
<string>keyword.other.powershell</string>
1178+
</dict>
1179+
<key>3</key>
1180+
<dict>
1181+
<key>name</key>
1182+
<string>variable.parameter.powershell</string>
1183+
</dict>
1184+
</dict>
1185+
</dict>
1186+
<dict>
1187+
<key>match</key>
1188+
<string>(?i:(requires))(?:\s(-PSSnapin)\s(\w+))(?:\s(?i:(-Version))\s((?:\d\.?){1,}))?$</string>
1189+
<key>captures</key>
1190+
<dict>
1191+
<key>0</key>
1192+
<dict>
1193+
<key>name</key>
1194+
<string>meta.requires.powershell</string>
1195+
</dict>
1196+
<key>1</key>
1197+
<dict>
1198+
<key>name</key>
1199+
<string>keyword.control.requires.powershell</string>
1200+
</dict>
1201+
<key>2</key>
1202+
<dict>
1203+
<key>name</key>
1204+
<string>keyword.other.powershell</string>
1205+
</dict>
1206+
<key>3</key>
1207+
<dict>
1208+
<key>name</key>
1209+
<string>variable.parameter.powershell</string>
1210+
</dict>
1211+
<key>4</key>
1212+
<dict>
1213+
<key>name</key>
1214+
<string>keyword.other.powershell</string>
1215+
</dict>
1216+
<key>5</key>
1217+
<dict>
1218+
<key>name</key>
1219+
<string>variable.parameter.powershell</string>
1220+
</dict>
1221+
</dict>
1222+
</dict>
1223+
</array>
1224+
</dict>
10641225
<key>variableNoProperty</key>
10651226
<dict>
10661227
<key>patterns</key>

examples/TheBigTestFile.ps1

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
using namespace System.Management.Automation
2+
#Requires -PSSnapin DiskSnapin -Version 1.2
3+
#Requires -PSSnapin DiskSnapin
4+
#Requires -Version 3
5+
#Requires -Version 3.0
6+
#Requires -RunAsAdministrator
7+
#Requires -Modules PSWorkflow
8+
#Requires -Modules PSWorkflow, ActiveDirectory
9+
#Requires -ShellId MyLocalShell
10+
#Requires -Modules @{
11+
ModuleName="PSScheduledJob"
12+
ModuleVersion="1.0.0.0"
13+
}
14+
115
throw "Do not run this file!"
216

317
<#
@@ -194,7 +208,7 @@ switch -Regex ("fourteen") {}
194208
switch -Wildcard ($a) {}
195209
switch -regex -file .\somefile.txt {}
196210
switch (3) {}
197-
switch (4, 2) {}
211+
switch (4, 2) {}
198212

199213
switch -Regex -File $filePath {
200214
'.' {}
@@ -213,7 +227,7 @@ switch ('this') {
213227

214228
# Illegal backtick
215229
Invoke-Command -arg1 $val1 `
216-
-arg2 $val2 `
230+
-arg2 $val2 `
217231

218232
# Functions and filters
219233
functioN MyFunction{}
@@ -234,14 +248,14 @@ filter myfilter($param) {}
234248
Filter my-Filter ($param){}
235249

236250
function foo
237-
#comment
251+
#comment
238252
{
239-
253+
240254
}
241255

242256
# This one will not highlight the function name,
243257
# because of the comments after 'function'. TODO?
244-
function
258+
function
245259
<# another comment #>
246260
test
247261
(
@@ -261,15 +275,15 @@ function Test-Drive([string]$roman) {
261275

262276
function Get-EscapedPath
263277
{
264-
param(
278+
param(
265279
[Parameter(
266-
Position=0,
280+
Position=0,
267281
Mandatory=$true
268282
ValueFromPipeline=$true,
269283
ValueFromPipelineByPropertyName=$true)
270284
]
271285
[string]$path
272-
)
286+
)
273287

274288
process {
275289
if ($path.Contains(' '))
@@ -326,7 +340,7 @@ class Car: Vehicle {
326340

327341
SetLength([int]$Length) {
328342
$this.Length = $Length
329-
}
343+
}
330344
}
331345

332346
# Illegal class name
@@ -373,7 +387,7 @@ $a -match $b
373387
$a -notmatch $b
374388
$x -like $c
375389
100 -and 0
376-
$a -ceq 4 -and $a -ine $d -or
390+
$a -ceq 4 -and $a -ine $d -or
377391
$c -is [Type]
378392
$c -isnot [Type]
379393
$c -as [Type]
@@ -441,7 +455,7 @@ Foo-match
441455
# Should also be able to use with line comments
442456
# .DESCRIPTION
443457
# .EXAMPLE sdkl
444-
#
458+
#
445459
# .EXTERNALHELP some
446460
# .REMOTEHELPRUNSPACE some
447461
# .ExternalHelp some

examples/advancedFunction.ps1

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using module Microsoft.PowerShell.Management
2+
13
<#
24
.Synopsis
35
Short description
@@ -22,8 +24,8 @@
2224
#>
2325
function Verb-Noun
2426
{
25-
[CmdletBinding(DefaultParameterSetName='Parameter Set 1',
26-
SupportsShouldProcess=$true,
27+
[CmdletBinding(DefaultParameterSetName='Parameter Set 1',
28+
SupportsShouldProcess=$true,
2729
PositionalBinding=$false,
2830
HelpUri = 'http://www.microsoft.com/',
2931
ConfirmImpact='Medium')]
@@ -32,17 +34,17 @@ function Verb-Noun
3234
Param
3335
(
3436
# Param1 help description
35-
[Parameter(Mandatory=$true,
37+
[Parameter(Mandatory=$true,
3638
ValueFromPipeline=$true,
37-
ValueFromPipelineByPropertyName=$true,
38-
ValueFromRemainingArguments=$false,
39+
ValueFromPipelineByPropertyName=$true,
40+
ValueFromRemainingArguments=$false,
3941
Position=0,
4042
ParameterSetName='Parameter Set 1')]
4143
[ValidateNotNull()]
4244
[ValidateNotNullOrEmpty()]
4345
[ValidateCount(0,5)]
4446
[ValidateSet("sun", "moon", "earth")]
45-
[Alias("p1")]
47+
[Alias("p1")]
4648
$Param1,
4749

4850
# Param2 help description

0 commit comments

Comments
 (0)