Skip to content

New syntax def #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
162 changes: 162 additions & 0 deletions Support/PowerShell.sublime-syntax
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
%YAML 1.2
---

name: PowerShell
file_extensions: [ps1, psm1, psd1]
scope: source.powershell

contexts:
main:
- match: \b(if|else|for|while)\b
scope: keyword.control.powershell
- include: requires-statement
- include: comment-line
- match: "<#"
scope: start.definition.comment.block.powershell
push: comment-block
- match: \{
scope: start.definition.script-block.powershell
push: script-block
- include: operators-all
- include: variables
- include: strings

comment-block:
- meta_scope: comment.block.powershell
- match: "#>"
scope: end.definition.comment.block.powershell
pop: true
- match: (?i)^\s*(\.)(synopsis|description|example|inputs|outputs|notes|link|component|functionality)
scope: embedded.documentation.title.powershell
captures:
1: keyword.embedded.documentation.title.start.powershell
2: constant.numeric.embedded.documentation.title.name.powershell
- match: (?i)^\s*(\.)(parameter|forwardhelptargetname|forwardhelpcategory|remotehelprunspace|externalhelp)\s+([a-z0-9-_]+)
scope: embedded.documentation.title.powershell
captures:
1: keyword.embedded.documentation.title.start.powershell
2: constant.numeric.embedded.documentation.title.name.powershell
3: keyword.embedded.documentation.title.argument.powershell

script-block:
- include: main
- meta_scope: script-block.powershell
- match: \}
scope: end.definition.script-block.powershell
pop: true

variables:
- match: (?i)(\$)\b(true|false)\b
scope: variable.powershell
captures:
1: keyword.start.variable.name.powershell
2: constant.numeric.variable.name.powershell
- match: (?i)(\$)(?:(script|global):)?([\w_]+)
scope: variable.powershell
captures:
1: keyword.start.variable.name.powershell
2: support.function.variable.scope.powershell
3: variable.name.powershell
- match: \$\{
scope: keyword.start.variable.name.powershell
push: variable-name-long

variable-name-long:
- meta_scope: variable.powershell
- match: "(?i)(script|global):"
captures:
1: support.function.variable.scope.powershell
- match: "[^}]+"
scope: variable.name.powershell
- match: \}
scope: keyword.end.variable.name.powershell
pop: true

strings:
- match: "@'"
push: string-heredoc-single-quoted
- match: '@"'
push: string-heredoc-double-quoted
- match: '"'
push: string-double-quoted
- match: "'"
push: string-single-quoted

string-double-quoted:
- meta_scope: string.quoted.double.powershell
- match: '"'
pop: true
- include: string-interpolation
- include: variables
- include: string-double-quoted-escape-sequences

string-single-quoted:
- meta_scope: string.quoted.single.powershell
- match: "'"
pop: true
- include: string-double-quoted-escape-sequences
- include: variables

string-heredoc-single-quoted:
- meta_scope: string.heredoc.single.quoted.powershell
- include: variables
- match: "''"
scope: constant.character.escape.powershell
- match: "'@"
pop: true

string-heredoc-double-quoted:
- meta_scope: string.heredoc.double.quoted.powershell
- include: variables
- match: "`."
- match: '"@'
pop: true

string-double-quoted-escape-sequences:
- match: "`[0abnfrvt\"'$`]"
scope: constant.character.escape.powershell

string-interpolation:
- match: \$\(
scope: keyword.start.interpolated.subexpression.powershell
push: string-interpolation-subexpression

string-interpolation-subexpression:
- meta_scope: string.interpolated.content.powershell
- include: main
- match: \)
scope: keyword.end.interpolated.subexpression.powershell
pop: true

operators-logical:
- match: (?<!\w)-([ci]?[lg][te]|eq|ne)
scope: keyword.operator.logical.powershell

comment-line:
- match: (?<![\\-])#.*$
scope: comment.line.number-sign.powershell

requires-statement:
- match: (?i)^\s*(#)(requires)\s+-(Version\s+\d(.\d+)?|Assembly\s+(.*)|Module\s+(.*)|PsSnapIn\s+(.*)|ShellId\s+(.*))
scope: comment.requires.statement.powershell
captures:
1: comment.start.requires.statement.powershell
2: keyword.requires.powershell

operators-all:
- include: operators-comparison
- include: operators-comparison-more
- include: operators-unary
- include: operators-logical

operators-comparison:
- match: (?i)(?<!\w)-(?i:is(?:not)?|as)\b
scope: keyword.operator.comparison.powershell

operators-comparison-more:
- match: (?i)(?<!\w)-[ic]?(?:eq|ne|[gl][te]|(?:not)?(?:like|match|contains|in)|replace)(?!\p{L})
scope: keyword.operator.comparison.powershell

operators-unary:
- match: (?i)(?<!\w)-(?:join|split)(?!\p{L})|!
scope: keyword.operator.unary.powershell
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@
</dict>
<dict>
<key>match</key>
<string>(?&lt;!\w)((?i:begin|break|catch|continue|data|define|do|dynamicparam|else|elseif|end|exit|finally|for|foreach(?!-object)|from|if|in|inlinescript|parallel|param|process|return|switch|throw|trap|try|until|using|var|where(?!=-object)|while)|%|\?)(?!\w)</string>
<string>(?&lt;!\w)((?i:begin|break|catch|continue|data|define|do|dynamicparam|else|elseif|end|exit|filter|finally|for|foreach(?!-object)|from|if|in|inlinescript|parallel|param|process|return|switch|throw|trap|try|until|using|var|where(?!=-object)|while)|%|\?)(?!\w)</string>
<key>name</key>
<string>keyword.control.powershell</string>
</dict>
Expand Down Expand Up @@ -522,7 +522,7 @@
<key>function</key>
<dict>
<key>begin</key>
<string>((?i:function|filter|configuration|workflow))\s+((?:\p{L}|\d|_|-|\.)+)</string>
<string>((?i:function|configuration|workflow))\s+((?:\p{L}|\d|_|-|\.)+)</string>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filter belongs here, I believe

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've messed this up. This change wasn't supposed to end up here. I'll try to fix it later.

<key>beginCaptures</key>
<dict>
<key>0</key>
Expand Down
56 changes: 56 additions & 0 deletions Support/syntax_test_PowerShell.sublime-syntax
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SYNTAX TEST "Packages/PowerShell/Support/PowerShell.sublime-syntax"

// <- source.powershell

if ($True) { "huzza!" }
// <- keyword.control.powershell

// Tests for variables =======================================

$foo
// <- keyword.start.variable.name.powershell
// <- variable.powershell variable.name.powershell

${ I'm a variable name }
// <- keyword.start.variable.name.powershell
// <- variable.powershell variable.name.powershell

$script:foo
// <- support.function.variable.scope.powershell

${global: I'm a variable name }
// <- support.function.variable.scope.powershell

// Tests for script blocks =======================================

{ }
// <- start.definition.script-block.powershell
//^ script-block.powershell

{ { } }
// ^ script-block.powershell script-block.powershell

// Tests for block comments =======================================

<#
// <- source.powershell start.definition.comment.block.powershell
// <- source.powershell comment.block.powershell
foo
// <- source.powershell comment.block.powershell
#>
// <- source.powershell end.definition.comment.block.powershell
// <- source.powershell

<#
.HELLO
// <- source.powershell comment.block.powershell

.SYNOPSIS
// <- keyword.embedded.documentation.title.start.powershell

.description
// ^ constant.numeric.embedded.documentation.title.name.powershell

.parameter foo
// ^ keyword.embedded.documentation.title.argument.powershell
#>