Skip to content

Commit 7561b0e

Browse files
committed
Allow default value in function params
And refactor assignment a little
1 parent 9fe8439 commit 7561b0e

File tree

4 files changed

+111
-5
lines changed

4 files changed

+111
-5
lines changed

PowerShell.sublime-syntax

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ contexts:
526526
scope: variable.parameter.powershell
527527
captures:
528528
1: punctuation.definition.variable.begin.powershell
529+
- include: assignment-normal
529530

530531
function-body:
531532
- meta_scope: meta.function.powershell
@@ -583,8 +584,7 @@ contexts:
583584
- include: types-without-members
584585
- include: variables-without-members
585586
- include: literals
586-
- match: '='
587-
scope: keyword.operator.assignment.powershell
587+
- include: assignment-normal
588588

589589
attributes:
590590
- match: (\[)\s*({{attributes}})
@@ -876,8 +876,9 @@ contexts:
876876
# Symbol operators
877877
- match: \!
878878
scope: keyword.operator.logical.powershell
879-
- match: '[+/*%-]?='
880-
scope: keyword.operator.assignment.powershell
879+
- include: assignment-normal
880+
- match: (?:[+/*%-]|\?\?)=
881+
scope: keyword.operator.assignment.augmented.powershell
881882
- match: (?:\+\+|--)(?![ \t]*\d|[[:alpha:]])
882883
scope: keyword.operator.assignment.powershell
883884
- match: '[+-](?=\.?\d)' # This is sort of heuristic
@@ -908,6 +909,17 @@ contexts:
908909
# This is very imprecise. Is there a syntax for 'must come after...'?
909910
scope: keyword.operator.range.powershell
910911

912+
assignment-normal:
913+
- match: =
914+
scope: keyword.operator.assignment.powershell
915+
push: in-assignment
916+
917+
in-assignment:
918+
- include: command-ending-sequences
919+
# INFO: Someday maybe we can nix the workflow stuff
920+
- include: workflow-execution-contexts
921+
- include: expressions
922+
911923
redirection:
912924
# https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_redirection
913925
- match: ([2-6*])(>&)(1)

Tests/syntax_test_Pester.ps1

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# SYNTAX TEST "Packages/PowerShell/PowerShell.sublime-syntax"
2+
3+
BeforeAll {
4+
# your function
5+
function Get-Planet ([string]$Name='*')
6+
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.block.powershell meta.function.powershell
7+
# ^^^^^^^^ keyword.declaration.function.powershell
8+
# ^^^^^^^^^^ entity.name.function.powershell
9+
# ^ punctuation.section.parameters.begin.powershell
10+
# ^ punctuation.section.brackets.begin.powershell
11+
# ^^^^^^ storage.type.powershell
12+
# ^ punctuation.section.brackets.end.powershell
13+
# ^^^^^ variable.parameter.powershell
14+
# ^ punctuation.definition.variable.begin.powershell
15+
# ^ keyword.operator.assignment.powershell
16+
# ^^^ meta.string.powershell string.quoted.single.powershell
17+
# ^ punctuation.definition.string.begin.powershell
18+
# ^ punctuation.definition.string.end.powershell
19+
# ^ punctuation.section.parameters.end.powershell
20+
{
21+
$planets = @(
22+
@{ Name = 'Mercury' }
23+
@{ Name = 'Venus' }
24+
@{ Name = 'Earth' }
25+
@{ Name = 'Mars' }
26+
@{ Name = 'Jupiter' }
27+
@{ Name = 'Saturn' }
28+
@{ Name = 'Uranus' }
29+
@{ Name = 'Neptune' }
30+
) | foreach { [PSCustomObject]$_ }
31+
32+
$planets | where { $_.Name -like $Name }
33+
}
34+
}
35+
36+
# Pester tests
37+
Describe 'Get-Planet' {
38+
It "Given no parameters, it lists all 8 planets" {
39+
$allPlanets = Get-Planet
40+
$allPlanets.Count | Should -Be 8
41+
}
42+
43+
Context "Filtering by Name" {
44+
It "Given valid -Name '<Filter>', it returns '<Expected>'" -TestCases @(
45+
@{ Filter = 'Earth'; Expected = 'Earth' }
46+
@{ Filter = 'ne*' ; Expected = 'Neptune' }
47+
@{ Filter = 'ur*' ; Expected = 'Uranus' }
48+
@{ Filter = 'm*' ; Expected = 'Mercury', 'Mars' }
49+
) {
50+
param ($Filter, $Expected)
51+
52+
$planets = Get-Planet -Name $Filter
53+
$planets.Name | Should -Be $Expected
54+
}
55+
56+
It "Given invalid parameter -Name 'Alpha Centauri', it returns `$null" {
57+
$planets = Get-Planet -Name 'Alpha Centauri'
58+
$planets | Should -Be $null
59+
}
60+
}
61+
}

Tests/syntax_test_PowerShell.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,7 @@ class Vehicle {
14231423
# ^ punctuation.definition.variable.powershell
14241424
# ^ punctuation.accessor.dot.powershell
14251425
# ^^^^^^^ variable.other.member.powershell
1426-
# ^^ keyword.operator.assignment.powershell
1426+
# ^^ keyword.operator.assignment.augmented.powershell
14271427
# ^^^^^^^^^^^^^^ variable.other.readwrite.powershell
14281428
# ^ punctuation.definition.variable.powershell
14291429

Tests/syntax_test_Workflow.ps1

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@
6666

6767
## $Using imports the variable and its current value.
6868
inlinescript {"Inline A1 = $Using:a"}
69+
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function.powershell
70+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.block.powershell
71+
# ^^^^^^^^^^^^ keyword.control.context.powershell
72+
# ^ punctuation.section.block.begin.powershell
73+
# ^^^^^^^^^^^^^^^^^^^^^^ meta.string.interpolated.powershell
74+
# ^^^^^^^^^^^^^ string.quoted.double.powershell
75+
# ^ punctuation.definition.string.begin.powershell
76+
# ^^^^^^^^ meta.interpolation.powershell variable.other.readwrite.powershell
77+
# ^ punctuation.definition.variable.powershell
78+
# ^^^^^ storage.modifier.scope.powershell
79+
# ^ punctuation.accessor.colon.powershell
80+
# ^ string.quoted.double.powershell punctuation.definition.string.end.powershell
81+
# ^ punctuation.section.block.end.powershell
6982
}
7083
Test-Workflow
7184
# @@@@@@@@@@@@@ reference
@@ -86,6 +99,26 @@
8699
## To change the variable in workflow scope, return the
87100
## new value.
88101
$a = inlinescript {$b = $Using:a+1; $b}
102+
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function.powershell
103+
# ^^ variable.other.readwrite.powershell
104+
# ^ punctuation.definition.variable.powershell
105+
# ^ keyword.operator.assignment.powershell
106+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.block.powershell
107+
# ^^^^^^^^^^^^ keyword.control.context.powershell
108+
# ^ punctuation.section.block.begin.powershell
109+
# ^^ variable.other.readwrite.powershell
110+
# ^ punctuation.definition.variable.powershell
111+
# ^ keyword.operator.assignment.powershell
112+
# ^^^^^^^^ variable.other.readwrite.powershell
113+
# ^ punctuation.definition.variable.powershell
114+
# ^^^^^ storage.modifier.scope.powershell
115+
# ^ punctuation.accessor.colon.powershell
116+
# ^ keyword.operator.unary.powershell
117+
# ^ meta.number.integer.decimal.powershell constant.numeric.value.powershell
118+
# ^ punctuation.terminator.statement.powershell
119+
# ^^ variable.other.readwrite.powershell
120+
# ^ punctuation.definition.variable.powershell
121+
# ^ punctuation.section.block.end.powershell
89122
"Workflow New A = $a"
90123
}
91124
Test-Workflow

0 commit comments

Comments
 (0)