forked from SublimeText/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsyntax_test_Class.ps1
More file actions
478 lines (440 loc) · 18.1 KB
/
syntax_test_Class.ps1
File metadata and controls
478 lines (440 loc) · 18.1 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
471
472
473
474
475
476
477
478
# SYNTAX TEST "Packages/PowerShell/PowerShell.sublime-syntax"
using namespace system.management.automation
#^^^^ keyword.control.import.powershell
# ^^^^^^^^^ keyword.control.import.powershell
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.path.powershell
# ^ punctuation.accessor.dot.powershell
# ^ punctuation.accessor.dot.powershell
class Foo : Bar, Baz { }
#^^^^^^^^^^^^^^^^^^^^^^^^ meta.class.powershell
#^^^^ keyword.declaration.class.powershell
# ^^^ entity.name.class.powershell
# @@@ definition
# ^ punctuation.separator.type.powershell
# ^^^ entity.other.inherited-class.powershell
# @@@ reference
# ^ punctuation.separator.sequence.powershell
# ^^^ entity.other.inherited-class.powershell
# @@@ reference
# ^ punctuation.section.block.begin.powershell
# ^ punctuation.section.block.end.powershell
# Define a class
class TypeName
#^^^^^^^^^^^^^ meta.class.powershell
#^^^^ keyword.declaration.class.powershell
# ^^^^^^^^ entity.name.class.powershell
# @@@@@@@@ definition
{
# Property with validate set
# <- punctuation.definition.comment
# ^^^^^^^^^^^^^^^^^^^^^^^^^^ comment.line
[ValidateSet("val1", "Val2")]
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.class.powershell
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.attribute.powershell
# ^ punctuation.section.brackets.begin.powershell
# ^^^^^^^^^^^ support.function.attribute.powershell
# ^ punctuation.section.group.begin.powershell
# ^^^^^^ meta.string.interpolated.powershell string.quoted.double.powershell
# ^ punctuation.definition.string.begin.powershell
# ^ punctuation.definition.string.end.powershell
# ^ punctuation.separator.sequence.powershell
# ^^^^^^ meta.string.interpolated.powershell string.quoted.double.powershell
# ^ punctuation.definition.string.begin.powershell
# ^ punctuation.definition.string.end.powershell
# ^ punctuation.section.group.end.powershell
# ^ punctuation.section.brackets.end.powershell
[string] $P1
#^^^^^^^^^^^^^^^ meta.class.powershell
# ^ punctuation.section.brackets.begin.powershell
# ^^^^^^ storage.type.powershell
# ^ punctuation.section.brackets.end.powershell
# ^^^ variable.other.readwrite.powershell
# ^ punctuation.definition.variable.powershell
# Static property
# <- punctuation.definition.comment
# ^^^^^^^^^^^^^^^ comment.line
static [hashtable] $P2
#^^^^^^^^^^^^^^^^^^^^^^^^^ meta.class.powershell
# ^^^^^^ storage.modifier.powershell
# ^ punctuation.section.brackets.begin.powershell
# ^^^^^^^^^ storage.type.powershell
# ^ punctuation.section.brackets.end.powershell
# ^^^ variable.other.readwrite.powershell
# ^ punctuation.definition.variable.powershell
# Hidden property does not show as result of Get-Member
# <- punctuation.definition.comment
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ comment.line
hidden [int] $P3
#^^^^^^^^^^^^^^^^^^^ meta.class.powershell
# ^^^^^^ storage.modifier.powershell
# ^ punctuation.section.brackets.begin.powershell
# ^^^ storage.type.powershell
# ^ punctuation.section.brackets.end.powershell
# ^^^ variable.other.readwrite.powershell
# ^ punctuation.definition.variable.powershell
# Constructor
# <- punctuation.definition.comment
# ^^^^^^^^^^^ comment.line
TypeName ([string] $s) {
#^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.class.powershell
# ^^^^^^^^^^^^^^^^^^^^^^^^ meta.function.powershell
# ^^^^^^^^ entity.name.function.powershell
# @@@@@@@@ definition
# ^ punctuation.section.parameters.begin.powershell
# ^ punctuation.section.brackets.begin.powershell
# ^^^^^^ storage.type.powershell
# ^ punctuation.section.brackets.end.powershell
# ^^ variable.parameter.powershell
# ^ punctuation.definition.variable.begin.powershell
# ^ punctuation.section.parameters.end.powershell
# ^ punctuation.section.block.begin.powershell
$this.P1 = $s
#^^^^^^^^^^^^^^^^^^^^ meta.class.powershell meta.function.powershell
# ^^^^^ variable.language.powershell
# ^ punctuation.definition.variable.powershell
# ^ punctuation.accessor.dot.powershell
# ^^ variable.other.member.powershell
# ^ keyword.operator.assignment.powershell
# ^^ variable.other.readwrite.powershell
# ^ punctuation.definition.variable.powershell
}
# ^ - meta.function
# Static method
# <- punctuation.definition.comment
# ^^^^^^^^^^^^^ comment.line
static [void] MemberMethod1([hashtable] $h) {
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.class.powershell
# ^^^^^^ storage.modifier.powershell
# ^ punctuation.section.brackets.begin.powershell
# ^^^^ storage.type.powershell
# ^ punctuation.section.brackets.end.powershell
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function.powershell
# ^^^^^^^^^^^^^ entity.name.function.powershell
# @@@@@@@@@@@@@ definition
# ^ punctuation.section.parameters.begin.powershell
# ^ punctuation.section.brackets.begin.powershell
# ^^^^^^^^^ storage.type.powershell
# ^ punctuation.section.brackets.end.powershell
# ^^ variable.parameter.powershell
# ^ punctuation.definition.variable.begin.powershell
# ^ punctuation.section.parameters.end.powershell
# ^ punctuation.section.block.begin.powershell
[TypeName]::P2 = $h
#^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.class.powershell meta.function.powershell
# ^ punctuation.section.brackets.begin.powershell
# ^^^^^^^^ support.type.powershell
# ^ punctuation.section.brackets.end.powershell
# ^^ punctuation.accessor.double-colon.powershell
# ^^ variable.other.member.powershell
# ^ keyword.operator.assignment.powershell
# ^^ variable.other.readwrite.powershell
# ^ punctuation.definition.variable.powershell
}
# Instance method
# <- punctuation.definition.comment
# ^^^^^^^^^^^^^^^ comment.line
[int] MemberMethod2([int] $i) {
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.class.powershell
# ^ punctuation.section.brackets.begin.powershell
# ^^^ storage.type.powershell
# ^ punctuation.section.brackets.end.powershell
# ^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function.powershell
# ^^^^^^^^^^^^^ entity.name.function.powershell
# @@@@@@@@@@@@@ definition
# ^ punctuation.section.parameters.begin.powershell
# ^ punctuation.section.brackets.begin.powershell
# ^^^ storage.type.powershell
# ^ punctuation.section.brackets.end.powershell
# ^^ variable.parameter.powershell
# ^ punctuation.definition.variable.begin.powershell
# ^ punctuation.section.parameters.end.powershell
# ^ punctuation.section.block.begin.powershell
return $this.P3
#^^^^^^^^^^^^^^^^^^^^^^ meta.class.powershell meta.function.powershell
# ^^^^^^ keyword.control.flow.return.powershell
# ^^^^^ variable.language.powershell
# ^ punctuation.definition.variable.powershell
# ^ punctuation.accessor.dot.powershell
# ^^ variable.other.member.powershell
}
}
[TypeName]::MemberMethod1()
# @@@@@@@@@@@@@ reference
$object.MemberMethod2()
# @@@@@@@@@@@@@ reference
# PowerShell documentation examples
# https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_classes
# Example 1 - Minimal definition
class Device {
# @@@@@@ definition
[string]$Brand
}
$dev = [Device]::new()
# @@@ reference
$dev.Brand = "Fabrikam, Inc."
$dev
# Example 2 - Class with instance members
class Book {
# @@@@ definition
# Class properties
[string] $Title
[string] $Author
[string] $Synopsis
[string] $Publisher
[datetime] $PublishDate
[int] $PageCount
[string[]] $Tags
# Default constructor
Book() { $this.Init(@{}) }
# @@@@ definition
# @@@@ reference
# Convenience constructor from hashtable
Book([hashtable]$Properties) { $this.Init($Properties) }
# @@@@ definition
# @@@@ reference
# Common constructor for title and author
Book([string]$Title, [string]$Author) {
# @@@@ definition
$this.Init(@{Title = $Title; Author = $Author })
# @@@@ reference
}
# Shared initializer method
[void] Init([hashtable]$Properties) {
# @@@@ definition
foreach ($Property in $Properties.Keys) {
$this.$Property = $Properties.$Property
}
}
# Method to calculate reading time as 2 minutes per page
[timespan] GetReadingTime() {
# @@@@@@@@@@@@@@ definition
if ($this.PageCount -le 0) {
throw 'Unable to determine reading time from page count.'
}
$Minutes = $this.PageCount * 2
return [timespan]::new(0, $Minutes, 0)
# @@@ reference
}
# Method to calculate how long ago a book was published
[timespan] GetPublishedAge() {
# @@@@@@@@@@@@@@@ definition
if (
$null -eq $this.PublishDate -or
$this.PublishDate -eq [datetime]::MinValue
) { throw 'PublishDate not defined' }
return (Get-Date) - $this.PublishDate
# @@@@@@@@ reference
}
# Method to return a string representation of the book
[string] ToString() {
# @@@@@@@@ definition
return "$($this.Title) by $($this.Author) ($($this.PublishDate.Year))"
}
}
[BookList]::Add([Book]::new(@{
# @@@ reference
# @@@ reference
Title = 'The Fellowship of the Ring'
Author = 'J.R.R. Tolkien'
Publisher = 'George Allen & Unwin'
PublishDate = '1954-07-29'
PageCount = 423
Tags = @('Fantasy', 'Adventure')
}))
[BookList]::Find({
# @@@@ reference
param ($b)
$b.PublishDate -gt '1950-01-01'
}).Title
[BookList]::FindAll({
# @@@@@@@ reference
param($b)
$b.Author -match 'Tolkien'
}).Title
[BookList]::Remove($Book)
# @@@@@@ reference
[BookList]::Books.Title
[BookList]::RemoveBy('Author', 'J.R.R. Tolkien')
# @@@@@@@@ reference
"Titles: $([BookList]::Books.Title)"
[BookList]::Add($Book)
# @@@ reference
[BookList]::Add($Book)
# @@@ reference
# Example 3 - Class with static members
class BookList {
# @@@@@@@@ definition
# Static property to hold the list of books
static [System.Collections.Generic.List[Book]] $Books
# Static method to initialize the list of books. Called in the other
# static methods to avoid needing to explicit initialize the value.
static [void] Initialize() { [BookList]::Initialize($false) }
# @@@@@@@@@@ definition
# @@@@@@@@@@ reference
static [bool] Initialize([bool]$Force) {
# @@@@@@@@@@ definition
if ([BookList]::Books.Count -gt 0 -and -not $Force) {
return $false
}
[BookList]::Books = [System.Collections.Generic.List[Book]]::new()
# @@@ reference
return $true
}
# Ensure a book is valid for the list.
static [void] Validate([book]$Book) {
# @@@@@@@@ definition
$Prefix = @(
'Book validation failed: Book must be defined with the Title,'
'Author, and PublishDate properties, but'
) -join ' '
if ($null -eq $Book) { throw "$Prefix was null" }
if ([string]::IsNullOrEmpty($Book.Title)) {
# @@@@@@@@@@@@@ reference
throw "$Prefix Title wasn't defined"
}
if ([string]::IsNullOrEmpty($Book.Author)) {
# @@@@@@@@@@@@@ reference
throw "$Prefix Author wasn't defined"
}
if ([datetime]::MinValue -eq $Book.PublishDate) {
throw "$Prefix PublishDate wasn't defined"
}
}
# Static methods to manage the list of books.
# Add a book if it's not already in the list.
static [void] Add([Book]$Book) {
# @@@ definition
[BookList]::Initialize()
# @@@@@@@@@@ reference
[BookList]::Validate($Book)
# @@@@@@@@ reference
if ([BookList]::Books.Contains($Book)) {
# @@@@@@@@ reference
throw "Book '$Book' already in list"
}
$FindPredicate = {
param([Book]$b)
$b.Title -eq $Book.Title -and
$b.Author -eq $Book.Author -and
$b.PublishDate -eq $Book.PublishDate
}.GetNewClosure()
#^^^^^^^^^^^^^^^^^^^^^^^^ meta.class.powershell meta.function.powershell
#^^^^^^^^ meta.block.powershell
# ^ punctuation.section.braces.end.powershell
# ^ punctuation.accessor.dot.powershell
# ^^^^^^^^^^^^^ meta.function-call.powershell variable.function.powershell
# @@@@@@@@@@@@@ reference
# ^^ meta.function-call.arguments.powershell
# ^ punctuation.section.arguments.begin.powershell
# ^ punctuation.section.arguments.end.powershell
if ([BookList]::Books.Find($FindPredicate)) {
# @@@@ reference
throw "Book '$Book' already in list"
}
[BookList]::Books.Add($Book)
# @@@ reference
}
# Clear the list of books.
static [void] Clear() {
# @@@@@ definition
[BookList]::Initialize()
# @@@@@@@@@@ reference
[BookList]::Books.Clear()
# @@@@@ reference
}
# Find a specific book using a filtering scriptblock.
static [Book] Find([scriptblock]$Predicate) {
# @@@@ definition
[BookList]::Initialize()
# @@@@@@@@@@ reference
return [BookList]::Books.Find($Predicate)
# @@@@ reference
}
# Find every book matching the filtering scriptblock.
static [Book[]] FindAll([scriptblock]$Predicate) {
# @@@@@@@ definition
[BookList]::Initialize()
# @@@@@@@@@@ reference
return [BookList]::Books.FindAll($Predicate)
# @@@@@@@ reference
}
# Remove a specific book.
static [void] Remove([Book]$Book) {
# @@@@@@ definition
[BookList]::Initialize()
# @@@@@@@@@@ reference
[BookList]::Books.Remove($Book)
# @@@@@@ reference
}
# Remove a book by property value.
static [void] RemoveBy([string]$Property, [string]$Value) {
# @@@@@@@@ definition
[BookList]::Initialize()
# @@@@@@@@@@ reference
$Index = [BookList]::Books.FindIndex({
# @@@@@@@@@ reference
param($b)
$b.$Property -eq $Value
}.GetNewClosure())
#^^^^^^^^^^^^^^^^^^^^^^^^^ meta.class.powershell meta.function.powershell meta.function-call.arguments.powershell
#^^^^^^^^ meta.block.powershell
# ^ punctuation.section.braces.end.powershell
# ^ punctuation.accessor.dot.powershell
# ^^^^^^^^^^^^^ meta.function-call.powershell variable.function.powershell
# @@@@@@@@@@@@@ reference
# ^^ meta.function-call.arguments.powershell
# ^ punctuation.section.arguments.begin.powershell
# ^ punctuation.section.arguments.end.powershell
# ^ punctuation.section.arguments.end.powershell
if ($Index -ge 0) {
[BookList]::Books.RemoveAt($Index)
# @@@@@@@@ reference
}
}
}
# Example 4 - Class definition with and without Runspace affinity
# Class definition with Runspace affinity (default behavior)
class UnsafeClass {
# @@@@@@@@@@@ definition
static [Object] ShowRunspaceId($Val) {
# @@@@@@@@@@@@@@ definition
return [pscustomobject]@{
ThreadId = [Threading.Thread]::CurrentThread.ManagedThreadId
RunspaceId = [runspace]::DefaultRunspace.Id
}
}
}
$unsafe = [UnsafeClass]::new()
# @@@ reference
while ($true) {
1..10 | ForEach-Object -Parallel {
# @@@@@@@@@@@@@@ reference
Start-Sleep -ms 100
# @@@@@@@@@@@ reference
($Using:unsafe)::ShowRunspaceId($_)
# @@@@@@@@@@@@@@ reference
}
}
# Class definition with NoRunspaceAffinity attribute
[NoRunspaceAffinity()]
class SafeClass {
# @@@@@@@@@ definition
static [Object] ShowRunspaceId($Val) {
# @@@@@@@@@@@@@@ definition
return [pscustomobject]@{
ThreadId = [Threading.Thread]::CurrentThread.ManagedThreadId
RunspaceId = [runspace]::DefaultRunspace.Id
}
}
}
$safe = [SafeClass]::new()
# @@@ reference
while ($true) {
1..10 | ForEach-Object -Parallel {
# @@@@@@@@@@@@@@ reference
Start-Sleep -ms 100
# @@@@@@@@@@@ reference
($Using:safe)::ShowRunspaceId($_)
# @@@@@@@@@@@@@@ reference
}
}