diff --git a/.github/workflows/syntax.yml b/.github/workflows/syntax.yml index 7531ebe3..6e0d1cf0 100644 --- a/.github/workflows/syntax.yml +++ b/.github/workflows/syntax.yml @@ -32,14 +32,12 @@ jobs: strategy: matrix: include: - - build: latest - packages: master - - build: stable + - build: 4203 packages: binary - build: 4107 packages: binary steps: - - uses: actions/checkout@v5 + - uses: actions/checkout@v7 - uses: SublimeText/syntax-test-action@v2 with: build: ${{ matrix.build }} diff --git a/PowerShell.sublime-syntax b/PowerShell.sublime-syntax index c210897b..e19ee668 100644 --- a/PowerShell.sublime-syntax +++ b/PowerShell.sublime-syntax @@ -25,7 +25,7 @@ contexts: main: - meta_include_prototype: false - match: '' - push: [statements, imports, shebang] + push: [statements, script-param-block, imports, shebang] shebang: - meta_include_prototype: false @@ -46,17 +46,17 @@ contexts: - include: using-directives - include: else-pop + script-param-block: + - include: attributes + - include: param-block + - include: else-pop + statements: - include: classes - include: functions - include: workflows - include: desired-state-configurations - - include: exceptions - - include: loops - - include: flow - - include: conditionals - - match: \B\.(?= ) scope: keyword.operator.source.powershell push: @@ -68,6 +68,7 @@ contexts: - include: commands-quoted - include: else-pop + - include: flow - include: expressions expressions: @@ -81,6 +82,11 @@ contexts: - include: escape-sequences + # Statements that may return values + - include: exceptions + - include: loops + - include: conditionals + # Normal code - include: types - include: strings @@ -339,8 +345,8 @@ contexts: inside-unquoted-string: - meta_include_prototype: false - - meta_scope: string.unquoted.powershell - - match: $|(?=[\s#|>;,()}]) + - meta_scope: meta.string.powershell string.unquoted.powershell + - match: $|(?=[\s#|>;,()}"']) pop: 1 - match: \{ push: inside-unquoted-string-brace @@ -1082,7 +1088,7 @@ contexts: - match: (?={{csharp_indicator}}) set: scope:source.cs.embedded.powershell with_prototype: - - match: (?=^{{single_quote}}@) + - match: (?={{no_indent}}{{single_quote}}@) pop: 1 - match: '{{single_quote}}{2}' scope: constant.character.escape.powershell @@ -1091,8 +1097,9 @@ contexts: inside-single-quoted-heredoc-string: - meta_include_prototype: false - meta_scope: meta.string.powershell string.quoted.single.heredoc.powershell - - match: ^{{single_quote}}@ - scope: punctuation.definition.string.end.powershell + - match: '{{no_indent}}({{single_quote}}@)' + captures: + 1: punctuation.definition.string.end.powershell pop: 1 - match: '{{single_quote}}{2}' scope: constant.character.escape.powershell @@ -1108,8 +1115,9 @@ contexts: inside-double-quoted-heredoc-string: - meta_include_prototype: false - meta_scope: meta.string.interpolated.powershell string.quoted.double.heredoc.powershell - - match: ^{{double_quote}}@ - scope: punctuation.definition.string.end.powershell + - match: '{{no_indent}}({{double_quote}}@)' + captures: + 1: punctuation.definition.string.end.powershell pop: 1 - include: escape-sequences - include: string-interpolations @@ -1120,7 +1128,7 @@ contexts: - match: (?={{csharp_indicator}}) set: scope:source.cs.embedded.powershell with_prototype: - - match: (?=^{{double_quote}}@) + - match: (?={{no_indent}}{{double_quote}}@) pop: 1 - include: escape-sequences - include: else-pop @@ -1427,6 +1435,9 @@ variables: )\b shebang_language: \b(?:pwsh|powershell)\b + # Indentation and whitespace patterns + no_indent: ^ + # Attributes attributes: |- \b(?xi: diff --git a/tests/syntax_test_PowerShell.ps1 b/tests/syntax_test_PowerShell.ps1 index 469bfc6c..12273506 100644 --- a/tests/syntax_test_PowerShell.ps1 +++ b/tests/syntax_test_PowerShell.ps1 @@ -281,6 +281,23 @@ using namespace System.Management.Automation # ^ punctuation.section.arguments.begin.powershell # ^ punctuation.section.arguments.end.powershell + # Statement value assignment + $x = if ($a) { 1 } else { 2 } +# ^^ keyword.control.conditional +# ^^^^ keyword.control.conditional + + $x = switch ($v) { 1 { 'one' } default { 'other' } } +# ^^^^^^ keyword.control.conditional.switch + + $x = foreach ($i in 1..3) { $i * 2 } +# ^^^^^^^ keyword.control.loop + + $x = try { Get-Thing } catch { $null } +# ^^^ keyword.control.exception +# @@@@@@@@@ reference +# ^^^^^ keyword.control.exception + + # Hashtable $properties = @{ # <- punctuation.definition.variable diff --git a/tests/syntax_test_script_params.ps1 b/tests/syntax_test_script_params.ps1 new file mode 100644 index 00000000..9f445508 --- /dev/null +++ b/tests/syntax_test_script_params.ps1 @@ -0,0 +1,29 @@ +# SYNTAX TEST "Packages/PowerShell/PowerShell.sublime-syntax" + + [CmdletBinding(PositionalBinding=$false)] +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.attribute.powershell +# ^ punctuation.section.brackets.begin.powershell +# ^^^^^^^^^^^^^ support.function.attribute.powershell +# ^ punctuation.section.group.begin.powershell +# ^^^^^^^^^^^^^^^^^ variable.parameter.attribute.powershell +# ^ keyword.operator.assignment.powershell +# ^^^^^^ constant.language.boolean.false.powershell +# ^ punctuation.definition.variable.powershell +# ^ punctuation.section.group.end.powershell +# ^ punctuation.section.brackets.end.powershell + param( +# ^^^^^ keyword.declaration.parameter.powershell +# ^^ meta.block.parameters.powershell +# ^ punctuation.section.block.begin.powershell + [Parameter(Mandatory=$true)] + [string]$base, + + [Parameter(Mandatory=$true)] + [string]$head + ) + + Set-StrictMode -version 2.0 + $ErrorActionPreference="Stop" + + [CmdletBinding(PositionalBinding=$false)] +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - meta.attribute - meta.annotation diff --git a/tests/syntax_test_strings.ps1 b/tests/syntax_test_strings.ps1 index 4bfcdbea..d00fd9b3 100644 --- a/tests/syntax_test_strings.ps1 +++ b/tests/syntax_test_strings.ps1 @@ -796,3 +796,11 @@ # ^ punctuation.definition.keyword.powershell # ^^^^^ meta.number.float.decimal.powershell constant.numeric.value.powershell # ^ punctuation.separator.decimal.powershell + +###[ Real Samples ]############################################################ + + git -c http.extraheader="AUTHORIZATION: Basic $base64Pat" pull origin main-vs-deps +# @@@ reference +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string +# ^^^^^^^^^^^^^^^^^ string.unquoted +# ^^^^^^^^^^^^^^^^^^^^^^ string.quoted.double