forked from SublimeText/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-file.ps1
More file actions
470 lines (366 loc) · 10.2 KB
/
Copy pathtest-file.ps1
File metadata and controls
470 lines (366 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
throw "NEVER RUN THIS FILE!"
### New PowerShell 4 stuff:
# the undiscoverable foreach
@("any","array","has").foreach({ $_ })
@('any','array','has').foreach{ $_ }
# the undiscoverable where
@("any","array","has").where({ $_.Length -gt 3 })
@("any","array","has").where{ $_.Length -gt 3 }
# dynamic method invocation
$var = get-item $Env:SystemRoot
$WhatToGet = "Files"
$var.("Get" + $WhatToGet)()
$var.($Full + "Name")
$Full = "Full"
$var.($Full + "Name")
Configuration Crazyness {
Node Whatever {
}
}
### PowerShell 3 and older:
# st: trimTrailingWhiteSpaceOnSave false
<#
Let's see what embedded docs in comments look like...
TODO: inside here, .<tab> should present list of doc keywords
.synopsis
this is some text
.PARAMETER eco
#>
# An example function: The name should (all) be highlighted right.
function Get-MemberSignature {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline=$true, Mandatory=$true)]
[System.Reflection.MethodBase]$MethodBase,
[Switch]$Simple
)
process {
someApp.exe -argOne "one" -argTwo two
# The whole script name is the command, not just the part before the extension
Some-Script.ps1 -argOne one -argTwo "two"
&"Some Script.bat" -param $PSVersionTable -arg 43
$args
$input
$PSScriptRoot
}
}
function echo([string]$text) {
write-host $text
}
# declarations should be consistent
function foo.bar() {}
Function foo() {}
class A {}
Class Foo-Bar {}
workflow w1 {}
Workflow work {}
configuration c {}
Configuration c {}
# Highlight types
[int[]][char[]]"hello world"
[string]$someVariable = [char[]](104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100)
[Collections.Generic.List``2[char]]$x = [char[]](104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100)
[Collections.Generic.List[char]]$x = [char[]](104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100)
Get-MemberSignature
Get-Command
get-thing | Out-WithYou > $null # destroy
# Same with line comment:
# .synopsis
# TODO: .<tab> should present list of doc keywords here too
# TODO: extract user's session vars and style those too.
# TODO: sytyle all ps automatic variables
$a = $false, $true, $null, $False, $FaLsE
# STRINGS ===============================================================
""
''
""""
"''"
'""'
''''
"'"
'"'
# In PowerShell all quoted strings can wrap lines
"This is
a string"
# single ones too
'This is a
string.'
"This is a string."
'This is a string.'
# Escaped characters should be highlighted
"Escaped chars: `", `n, `$, `b, `t, `""
# Including '' in a 'single quoted string'
'But here they''re not escape chars: `", `n, `$, `b, `"'
# And doubled up quotes
"""This is a string."""
"`"This is a string in quotes`"
`"This is still the same string.`""
# Subexpressions cause powershell to reparse, so double quotes create strings inside them
"String with embedded complex subexpression: $(get-item "$mypath/*.*" | out-string)."
# With scriptblock...
"String with embedded complex subexpression and scriptblock $(get-item | each-object { out-string } )."
# More deeply nested...
"String with embedded complex subexpression and complex scriptblock: $(get-item; "String ($variable)" | foreach-object { (get-item "This `"thing`"" | get-member ) } )."
"Now with variables: $result"
# In PowerShell 3 this is necessary to indicate the variable is just "drive"
"Sometimes we use ${drive}:item when refering to objects as variables"
# In PowerShell 2 this is necessary to indicate the variable is "drive:item"
"Sometimes we use ${drive:item} when refering to objects as variables"
# In PowerShell 3 the variable here is "drive:item" not just "drive" (as it would have been in 2)
"Sometimes we use $drive:item when refering to objects as variables"
"But this variable dot-notation $variable.foo shouldn't highlight in a string"
"Although obviously in a $($subexpression.Length) it should be!"
# TODO: can we highlight this as an error?
if(!($PSVersionTable.PSVersion -ge '3.0')) {
# In PowerShell 3, this will cause an ERROR
"In PowerShell 2 we could $drive: to add a colon"
}
function get-numbers { 1..3 }
# NOTE: PowerShell can't actually parse this line correctly:
"This is deeply nested: $( get-number | %{ invoke-command { $(("you, do this)) anyway")) } } )"
"When you call a method: $( get-number | %{ invoke-command $( [string]::format("Like (this{0})","what?") ) $var } )"
# " <-- this stops the string above, until we fix the parenthesis on methods.
# TODO: Highlight member access
# TODO: Highlight array access
$(& $($variable.ToArray()[0]))
"This string is
valid"
"This string is `
valid but the ` is consumed."
"This string is ``
valid but the `` is not consumed."
"This string is `
valid. There's a space at the end."
'This is a valid `
string. And the ` isn''t consumed'
"Some variables $true here and $false there and $_"
"Some escaped variables `$true here and `$false there `$_"
''
# Note there are no escaped sequences here but for ''.
'This is some `n ''$%&/ string too '''
'This should be a string (there''s no code in it): $(get-onions | very-boring | yeah-right)'
@"
"This is a here string
here." Quotes are "allowed" in it.
And $variables should be ${variables here too!}
"@
@"
This is a here string piped to something
"@ | Write-Host
@'
This is a ''single-quote'' here string piped to something
'@ | Write-Host
# Variables:
$this_is_a_vaAriable = 100
${this ` is a variable name} = 100,500
# about_scopes lists global, local, script, private (and numbered) scopes.
$global:variable
$private:heyyou
$script:blah_blah
${script: stupid variable name}
"We need $global:variables in; strings ${ tooh-torooh }"
@"
"We need $global:variables in; strings ${ tooh-torooh }"
"@
# we don't know what could be a drive letter, so we treat everything that way
$Env:PATH
$Variable:VerbosePreference
# TODO: we should at least highlight built-in aliases
cd C:\Windows
# Yes, this would actually read in win.ini:
${C:win.ini}
${Env:Path}
# array expressions
# note: we only deal with commands that have legit verbs, so "this-is" is bad, and "an-expression" is unhighlighted
@( this-is | an-expression | "that evaluates $(to-an | array)");
# array operator
$a = 1,2,3,4
$a = "This, shouldn't be, styled."
$a = $("Guess what, happens ""here, hey""" | "Hm... $("this, is" strange.) you can't really pipe to a string, but nevermind for now.")
# numeric constants
100, 200, 300, 0.5, 100.500, .5
1E+73
0xaf20fffKB, 0xaf20fffmb, 0x0aL, 0x0aLmb, 0x0adLPB
100d
.5d
10.5d
1E+3d
.5gb
100.500kb
100dmb
.5dgb
10.5dkb
1E+3dgb
10*10
# terabyte and petabyte
42tb, 42pb, 42Ltb, 42Dpb
# with signs
-5e3, +5e3, +0x34adMB, -0x34adMB, +5Lmb
# L for long
100l, 100L, 100lmb, 100LMB, 100lgb, 100lkb, 42Dpb, 42ltb
1.21e+3l
1.21E-3d
2.12e3lmb
3.1415e3lgb
3.1415e3lkb
# TODO: you can also use this syntax for exponential notation
1e73
1E73
1.12e5
1.12E5
1.12e+5
1.12E+5
# file ext shouldn't be styled as numeric
# TODO: the whole file name really ought to be styled as a command *sigh*
this-isnot.ps1
# FIXME:
a_mistake.here.ps1
"anothermistake.ps1"
# FIXME:
notepad.exe
# TODO: file ext shouldn't be styled as numeric, even if it's short:
get-content test.1
# Piping and redirection:
notepad.exe > log.txt
notepad.exe 1> log.txt
notepad.exe 2>&1
notepad.exe 3>&1
notepad.exe 4>&1
notepad.exe 5>&1
notepad.exe 6>&1
notepad.exe 2>&1> log.txt
if($false) {
# Note: 7 isn't a valid stream
notepad.exe 7>&1
}
# TODO: these script blocks should highlight?
# TODO
switch (get-childitem) -regex {
"abc" { }
# TODO: This should fold!
{$_.PSIsContainer()} {
get-childitem
}
default {
"$(this is it)"
}
}
do {
}
foreach($item in get-childitem) {
}
for($i = 0; $i -lt 10; $i++) {
}
get-item $()
if (10 -cgt 100) { }
$a -is $b
$b -contains $c
$x -notcontains $c
$a -match $b
$a -notmatch $b
$x -like $c
100 -and 0
# Negative: these are cmdlets with unapproved verbs
# we should not highlight keywords in them
Foo-Is
Foo-Not
Foo-Join
Foo-Bxor
Foo-f
Foo-eq
Foo-match
# Logical operators
$a -ceq 4 -and $a -ine $d -or
$c -is [Type]
$c -isnot [Type]
$c -as [Type]
$a -ceq 4 -and $a -ine $d -or
# Bitwise operators
$k = $y -bor $k
$x = $y -band $x
# TODO Missing:
$z = -bnot $x
$k = $y -xor $b
$k = $y -bxor $b
$a -eq 4 -and $a -ne $d -or $k -gt 42
$a -ceq 4 -and $a -ine $d -or
#TODO: There are case-sensitive and case-insensitive operators:
$a -icontains $c
$a -ccontains $c
$a -iNotContains $c
$a -cNotContains $c
$a -cmatch $c
$x -iMatch $c
$x -iNotMatch $c
$a -iLike $b
$b -cLike $c
"hey" -cgt "Hey"
"Hey" -igt "hey"
"hey" -cge "Hey"
"Hey" -ige "hey"
"HEY" -clt "hey"
"HEY" -ilt "hey"
"HEY" -cle "hey"
"HEY" -ile "hey"
# this isn't a valid operator:
$x -foobar $y
# format
"{0:c}" -f 12
# TODO: join operator
1,2,3,4 -join "|"
$abc += 200
"this text" > here.txt; epic-fail
# TODO: Built in aliases, at least
"This $(
get-item -lt (
gi $("this is") -filter "txt.txt"
); 10 -gt 11 | out-string | set-content $(gi sublime:output.txt) )"""
# FIXME:
# heredocs also admit subexps. but single pairs of quote are not invalid!
@"
$(1 -lt 0 | get-item | out-thing "j")
"This is a normal string." (with quotes around it)
The belt was 42" long!
'This is a single quoted string.'
# this is not a comment.
This -is a variable $a but these -not operators.
"@
@'
-gt "This that"
'@
What the heck? # TODO: That shouldn't look the same as a string
0..10 | foreach-object {
"something $_"
}
"This should $a not be a {scriptblock}"
{ this-is $(it-at | )
}
# Is there a way we can make "anything" that's in a command position look like a command?
gci * | gm | fl * -force | out-string
useful.ps1 | out-file foo.txt
# TODO: parameter names
# TODO: splatting variables
Get-things.ps1 -value @args
# array subexpression
@(This $a is it. | "$(this-is | @($('yeah'| "" )) )")
# TODO: [minor] D:\dev\Find-String should not be treated as cmdlet name
Import-Module D:\dev\Find-String
function Get-EscapedPath
{
param(
[Parameter(
Position=0,
Mandatory=$true
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)
]
[string]$path
)
process {
if ($path.Contains(' '))
{
return '"' + $path + '"'
}
return $path
}
}