Skip to content

Commit

Permalink
Don't match keywords if they are preceded by a dot and add the 'seque…
Browse files Browse the repository at this point in the history
…nce' keyword (SublimeText#186)

* Change keyword matching regex

Keywords (with the exception of 'where' and 'foreach') should not be considered as such if they are preceded by a '.'.
The tests relative to keywords used as members or methods now pass.
'process' is now considered a block keyword.

* Add 'sequence' keyword

Also make the relative syntax test pass.

* Change test assertion in Function syntax test

Now the test reflects the change of the 'process' keyword from uncategorised to block.

* Change Parameter matching regex to match 'PositionalBinding'

The relevant test in the function syntax test now passes.

* Consume string with a trailing dot before checking for keywords

This makes negative lookbehind assertions on keywords unnecessary.

* Add scope to matched unquoted strings

Co-authored-by: deathaxe <deathaxe82@googlemail.com>

* Match word boundary to avoid highlighting invalid arguments

Co-authored-by: deathaxe <deathaxe82@googlemail.com>

---------

Co-authored-by: deathaxe <deathaxe82@googlemail.com>
  • Loading branch information
matteocoder and deathaxe authored Jan 10, 2025
1 parent da9cdd7 commit bea280e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
15 changes: 11 additions & 4 deletions PowerShell.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ contexts:
- include: main
- match: \b[\w.-]+\.(?i:exe|com|cmd|bat)\b
scope: variable.function.powershell
# Consume a string with a trailing dot
# to prevent members with particular names from being recognized as keywords.
- match: \b[\w-]+(?=\.)
scope: variable.other.object.powershell
push: members
# Exceptions
- match: \b(?i:throw){{kebab_break}}
scope: keyword.control.exception.raise.powershell
Expand All @@ -100,6 +105,8 @@ contexts:
# Begin/End
- match: \b(?i:begin){{kebab_break}}
scope: keyword.context.block.begin.powershell
- match: \b(?i:process){{kebab_break}}
scope: keyword.context.block.process.powershell
- match: \b(?i:end){{kebab_break}}
scope: keyword.context.block.end.powershell
- match: \b(?i:clean){{kebab_break}}
Expand Down Expand Up @@ -129,7 +136,7 @@ contexts:
- match: \b(?i:(?:dynamic)?param){{kebab_break}}
scope: keyword.declaration.parameter.powershell # This scope is not standard
# Uncategorized keywords
- match: \b(?i:data|default|define|from|in|inlinescript|parallel|process){{kebab_break}}
- match: \b(?i:data|default|define|from|in|inlinescript|parallel|sequence){{kebab_break}}
scope: keyword.control.powershell
- match: \B--%\B
scope: keyword.control.powershell
Expand Down Expand Up @@ -292,10 +299,10 @@ contexts:
(?xi:
\b(
Mandatory | ValueFromPipeline(?:ByPropertyName)?
| ValueFromRemainingArguments | Position
| ValueFromRemainingArguments | Position(?:alBinding)?
| (?:Default)?ParameterSetName | SupportsShouldProcess | SupportsPaging
| PositionalBinding | HelpUri | ConfirmImpact | HelpMessage
)
| HelpUri | ConfirmImpact | HelpMessage
)\b
\s*(=)?
)
captures:
Expand Down
2 changes: 1 addition & 1 deletion Tests/syntax_test_Function.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ function Verb-Noun {
{
}
Process {
# <- keyword.control
# <- keyword.context.block.process
if ($pscmdlet.ShouldProcess("Target", "Operation")) {
# <- keyword.control
# ^ punctuation.section.group.begin
Expand Down
6 changes: 6 additions & 0 deletions Tests/syntax_test_PowerShell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,12 @@ New-Object -TypeName System.Diagnostics.Process
# ^^^^^^^ - keyword.control
New-Object -TypeName System.Data
# ^^^^ - keyword.control
New-Object -TypeName Sy-stem.if
# ^^ - keyword.control
New-Object -TypeName S_ystem.Clean
# ^^^^^ - keyword.control
New-Object -TypeName Sy_stem-.Throw
# ^^^^^ - keyword.control
echo `"test`"
# ^^^^^^^^^ - string.quoted
# ^^ constant.character.escape
Expand Down

0 comments on commit bea280e

Please sign in to comment.