Skip to content

Commit 23ec46b

Browse files
committed
Add context-sensitive functions
1 parent 6241786 commit 23ec46b

File tree

3 files changed

+255
-156
lines changed

3 files changed

+255
-156
lines changed

PowerShell.sublime-syntax

Lines changed: 82 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,6 @@ contexts:
192192
- include: class-methods
193193
- match: \b(?i:hidden|static)\b
194194
scope: storage.modifier.powershell
195-
196-
# TODO: Fix functions, so these don't nest here.
197-
- include: function-sections
198195
- include: attributes
199196

200197
# Back to normal stuff
@@ -291,29 +288,86 @@ contexts:
291288
###[ FUNCTIONS ]###############################################################
292289

293290
functions:
294-
- match: ^(?:\s*)(?i)(function|filter|configuration|workflow)\s+(?:(global|local|script|private):)?((?:\p{L}|\d|_|-|\.)+)
295-
scope: meta.function.powershell
291+
- match: ^\s*(?i)(function|filter|configuration|workflow)(?=\s)
296292
captures:
297-
1: storage.type.powershell
298-
2: storage.modifier.scope.powershell
299-
3: entity.name.function.powershell
300-
push:
301-
- match: (?=\{|\()
302-
pop: 1
303-
- include: comment-line
293+
1: keyword.declaration.function.powershell
294+
push: [function-body, function-inline-params, function-name]
295+
296+
function-name:
297+
- match: (?:(global|local|script|private):)?((?:\p{L}|\d|_|-|\.)+)
298+
captures:
299+
1: storage.modifier.scope.powershell
300+
2: entity.name.function.powershell
301+
- match: (?=[{(])
302+
pop: 1
303+
304+
function-inline-params:
305+
- match: \(
306+
scope: punctuation.section.parameters.begin.powershell
307+
push: inside-function-inline-params
308+
- include: else-pop
309+
310+
inside-function-inline-params:
311+
- match: \)
312+
scope: punctuation.section.parameters.end.powershell
313+
pop: 1
314+
- include: comma-separators
315+
- include: types
316+
- match: (\$)\p{L}[\p{L}\p{N}]*
317+
scope: variable.parameter.powershell
318+
captures:
319+
1: punctuation.definition.variable.begin.powershell
320+
321+
function-body:
322+
- meta_scope: meta.function.powershell
323+
- match: \{
324+
scope: punctuation.section.block.begin.powershell
325+
push: inside-function-body
326+
- include: else-pop
327+
328+
inside-function-body:
329+
- match: \}
330+
scope: punctuation.section.block.end.powershell
331+
pop: 1
332+
333+
- include: attributes
304334

305-
function-sections:
306-
- match: \b(?i:(?:dynamic)?param){{kebab_break}}
335+
- match: \b(?i:(?:Dynamic)?Param){{kebab_break}}
307336
scope: keyword.declaration.parameter.powershell # This scope is not standard
308-
# Begin/Process/End/Clean
309-
- match: \b(?i:begin){{kebab_break}}
310-
scope: keyword.context.block.begin.powershell
311-
- match: \b(?i:process){{kebab_break}}
312-
scope: keyword.context.block.process.powershell
313-
- match: \b(?i:end){{kebab_break}}
314-
scope: keyword.context.block.end.powershell
315-
- match: \b(?i:clean){{kebab_break}}
316-
scope: keyword.context.block.clean.powershell
337+
push: expect-param-context
338+
339+
- match: \b(?i:Begin|Process|End|Clean){{kebab_break}}
340+
scope: keyword.context.block.powershell
341+
push: expect-function-context
342+
343+
- include: statements
344+
345+
expect-function-context:
346+
- match: \{
347+
scope: punctuation.section.block.begin.powershell
348+
push: inside-function-context
349+
- include: else-pop
350+
351+
inside-function-context:
352+
- match: \}
353+
scope: punctuation.section.block.end.powershell
354+
pop: 1
355+
- include: statements
356+
357+
expect-param-context:
358+
- match: \(
359+
scope: punctuation.section.block.begin.powershell
360+
push: inside-param-context
361+
- include: else-pop
362+
363+
inside-param-context:
364+
- match: \)
365+
scope: punctuation.section.block.end.powershell
366+
pop: 1
367+
- include: comma-separators
368+
- include: attributes
369+
- include: types
370+
- include: variables-without-property
317371

318372
attributes:
319373
- match: (\[)\s*({{attributes}})
@@ -444,8 +498,7 @@ contexts:
444498
scope: punctuation.terminator.statement.powershell
445499
- match: \`(?=\n|$)
446500
scope: punctuation.separator.continuation.line.powershell
447-
- match: ','
448-
scope: punctuation.separator.sequence.powershell
501+
- include: comma-separators
449502
- match: '&'
450503
scope: keyword.operator.background.powershell
451504
- match: \.\.(?=\-?\d|\(|\$)
@@ -870,6 +923,10 @@ contexts:
870923

871924
###[ COMPONENTS ]##############################################################
872925

926+
comma-separators:
927+
- match: ','
928+
scope: punctuation.separator.sequence.powershell
929+
873930
escape-sequences:
874931
- match: '`[0abenfrvt"''$`]'
875932
scope: constant.character.escape.powershell

Tests/syntax_test_Function.ps1

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,11 @@ function Test-Path {}
7474
#>
7575
# <- punctuation.definition.comment.block.end
7676
function Verb-Noun {
77-
#^^^^^^^^^^^^^^^^^ meta.function
78-
#^^^^^^^ storage.type
79-
# ^^^^^^^^^ entity.name.function
77+
#^^^^^^^^^^^^^^^^^^^ meta.function.powershell
78+
#^^^^^^^ keyword.declaration.function.powershell
79+
# ^^^^^^^^^ entity.name.function.powershell
8080
# @@@@@@@@@ definition
81+
# ^ punctuation.section.block.begin.powershell
8182
[CmdletBinding(DefaultParameterSetName = 'Parameter Set 1',
8283
# <- meta.attribute punctuation.section.brackets.begin
8384
# ^ meta.attribute support.function.attribute
@@ -112,7 +113,7 @@ function Verb-Noun {
112113
# ^ meta.attribute punctuation.section.group.end
113114
# ^ meta.attribute punctuation.section.brackets.end
114115
[OutputType([String])]
115-
#^^^^^^^^^^^^^^^^^^^^^^^^^ meta.block.powershell
116+
#^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function.powershell
116117
# ^^^^^^^^^^^^^^^^^^^^^^ meta.attribute.powershell
117118
# ^ punctuation.section.brackets.begin.powershell
118119
# ^^^^^^^^^^ support.function.attribute.powershell
@@ -123,9 +124,11 @@ function Verb-Noun {
123124
# ^ punctuation.section.group.end.powershell
124125
# ^ punctuation.section.brackets.end.powershell
125126
Param
126-
#^^^^^ keyword.declaration.parameter
127+
#^^^^^^^^ meta.function.powershell
128+
# ^^^^^ keyword.declaration.parameter.powershell
127129
(
128-
# <- punctuation.section.group.begin
130+
#^^^^ meta.function.powershell
131+
# ^ punctuation.section.block.begin.powershell
129132
# Param1 help description
130133
# <- comment.line punctuation.definition.comment
131134
# ^^^^^^^^^^^^^^^^^^^^^^^ comment.line
@@ -285,13 +288,15 @@ function Verb-Noun {
285288
# ^ punctuation.section.group.end
286289
# ^ punctuation.section.brackets.end
287290
[int32]
288-
# <- punctuation.section.brackets.begin
289-
#^^^^^ storage.type
290-
# ^ punctuation.section.brackets.end
291+
#^^^^^^^^^^^^^^ meta.function.powershell
292+
# ^ punctuation.section.brackets.begin.powershell
293+
# ^^^^^ storage.type.powershell
294+
# ^ punctuation.section.brackets.end.powershell
291295
$Param2,
292-
# <- punctuation.definition.variable
293-
# ^ variable.other.readwrite
294-
# ^ punctuation.separator
296+
#^^^^^^^^^^^^^^^ meta.function.powershell
297+
# ^^^^^^^ variable.other.readwrite.powershell
298+
# ^ punctuation.definition.variable.powershell
299+
# ^ punctuation.separator.sequence.powershell
295300

296301
# Param3 help description
297302
# <- comment.line punctuation.definition.comment
@@ -323,7 +328,7 @@ function Verb-Noun {
323328
# ^ meta.attribute punctuation.section.group.end
324329
# ^ meta.attribute punctuation.section.brackets.end
325330
[String]
326-
#^^^^^^^^^^^^^^^ meta.block.powershell meta.group.powershell
331+
#^^^^^^^^^^^^^^^ meta.function.powershell
327332
# ^ punctuation.section.brackets.begin.powershell
328333
# ^^^^^^ support.type.powershell
329334
# ^ punctuation.section.brackets.end.powershell
@@ -357,7 +362,7 @@ function Verb-Noun {
357362
# ^ meta.attribute punctuation.section.group.end
358363
# ^ meta.attribute punctuation.section.brackets.end
359364
[string]
360-
#^^^^^^^^^^^^^^^ meta.block.powershell meta.group.powershell
365+
#^^^^^^^^^^^^^^^ meta.function.powershell
361366
# ^ punctuation.section.brackets.begin.powershell
362367
# ^^^^^^ storage.type.powershell
363368
# ^ punctuation.section.brackets.end.powershell
@@ -388,45 +393,61 @@ function Verb-Noun {
388393
# ^ comment.line punctuation.definition.comment
389394
# ^ comment.line
390395
[string]
391-
#^^^^^^^^^^^^^^^ meta.block.powershell meta.group.powershell
396+
#^^^^^^^^^^^^^^^ meta.function.powershell
392397
# ^ punctuation.section.brackets.begin.powershell
393398
# ^^^^^^ storage.type.powershell
394399
# ^ punctuation.section.brackets.end.powershell
395400
$Param5
396401
# <- punctuation.definition.variable
397402
# ^ variable.other.readwrite
398403
)
399-
# <- punctuation.section.group.end
404+
#^^^^^^^^ meta.function.powershell
405+
# ^ punctuation.section.block.end.powershell
400406

401407
Begin
402-
#^^^^^ keyword.context.block.begin
408+
#^^^^^^^^ meta.function.powershell
409+
# ^^^^^ keyword.context.block.powershell
403410
{
411+
#^^^^ meta.function.powershell
412+
# ^ punctuation.section.block.begin.powershell
404413
}
405414
Process {
406-
# <- keyword.context.block.process
415+
#^^^^^^^^^^^^ meta.function.powershell
416+
# ^^^^^^^ keyword.context.block.powershell
417+
# ^ punctuation.section.block.begin.powershell
407418
if ($pscmdlet.ShouldProcess("Target", "Operation")) {
408-
# <- keyword.control
409-
# ^ punctuation.section.group.begin
410-
# ^ punctuation.definition.variable
411-
# ^^^^^^^^ variable.language
412-
# ^^^^^^^^^^^^^ variable.function
413-
# @@@@@@@@@@@@@ reference
414-
# ^ punctuation.section.arguments.begin
415-
# ^^^^^^^^ string.quoted.double
416-
# ^ punctuation.separator
417-
# ^^^^^^^^^^^ string.quoted.double
418-
# ^ punctuation.section.arguments.end
419-
# ^ punctuation.section.group.end
419+
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function.powershell
420+
# ^^ keyword.control.conditional.if.powershell
421+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.group.powershell
422+
# ^ punctuation.section.group.begin.powershell
423+
# ^^^^^^^^^ variable.language.powershell
424+
# ^ punctuation.definition.variable.powershell
425+
# ^ punctuation.accessor.dot.powershell
426+
# ^^^^^^^^^^^^^ meta.function-call.powershell variable.function.powershell
427+
# @@@@@@@@@@@@@ reference
428+
# ^^^^^^^^^^^^^^^^^^^^^^^ meta.function-call.arguments.powershell
429+
# ^ punctuation.section.arguments.begin.powershell
430+
# ^^^^^^^^ meta.string.interpolated.powershell string.quoted.double.powershell
431+
# ^ punctuation.definition.string.begin.powershell
432+
# ^ punctuation.definition.string.end.powershell
433+
# ^ punctuation.separator.sequence.powershell
434+
# ^^^^^^^^^^^ meta.string.interpolated.powershell string.quoted.double.powershell
435+
# ^ punctuation.definition.string.begin.powershell
436+
# ^ punctuation.definition.string.end.powershell
437+
# ^ punctuation.section.arguments.end.powershell
438+
# ^ punctuation.section.group.end.powershell
439+
# ^ meta.block.powershell punctuation.section.braces.begin.powershell
420440
}
421441
}
422442
End {
423-
#^^^ keyword.context.block.end
443+
#^^^^^^^^ meta.function.powershell
444+
# ^^^ keyword.context.block.powershell
445+
# ^ punctuation.section.block.begin.powershell
424446

425447
}
426448
clean { }
427-
#^^^^^^^^^^^^ meta.block.powershell
428-
# ^^^^^ keyword.context.block.clean.powershell
429-
# ^^^ meta.block.powershell
430-
# ^ punctuation.section.braces.begin.powershell
431-
# ^ punctuation.section.braces.end.powershell
449+
#^^^^^^^^^^^^ meta.function.powershell
450+
# ^^^^^ keyword.context.block.powershell
451+
# ^ punctuation.section.block.begin.powershell
452+
# ^ punctuation.section.block.end.powershell
432453
}

0 commit comments

Comments
 (0)