Skip to content

Commit a1423b1

Browse files
committed
Add a dedicated using context
and don't return to it when you leave. Also, make a context for shebang. Fixes #198
1 parent 22907c4 commit a1423b1

File tree

4 files changed

+75
-20
lines changed

4 files changed

+75
-20
lines changed

PowerShell.sublime-syntax

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ file_extensions:
1212

1313
first_line_match: |-
1414
(?x:
15-
^\#!.*\b(?:pwsh|powershell)\b
15+
^\#!.*{{shebang_language}}
1616
| ^\# \s* -\*- [^*]* mode: \s* powershell [^*]* -\*-
1717
)
1818
@@ -21,7 +21,28 @@ contexts:
2121
- include: comments
2222

2323
main:
24-
- include: statements
24+
- meta_include_prototype: false
25+
- match: ''
26+
push: [statements, imports, shebang]
27+
28+
shebang:
29+
- meta_include_prototype: false
30+
- match: ^\s*(\#!)
31+
captures:
32+
1: punctuation.definition.comment.powershell
33+
set: shebang-body
34+
- include: else-pop
35+
36+
shebang-body:
37+
- meta_include_prototype: false
38+
- meta_scope: comment.line.shebang.powershell
39+
- match: '{{shebang_language}}'
40+
scope: constant.language.shebang.powershell
41+
- include: pop-eol
42+
43+
imports:
44+
- include: using-directives
45+
- include: else-pop
2546

2647
statements:
2748
- include: redirection
@@ -88,7 +109,6 @@ contexts:
88109
- include: commands
89110
- include: constants
90111
- include: variables
91-
- include: using-directives
92112
- include: types
93113
- include: hashtables
94114
- include: strings
@@ -127,13 +147,6 @@ contexts:
127147
captures:
128148
1: punctuation.definition.parameter.powershell
129149

130-
using-directives:
131-
- match: \b(?i:(using))\s+(?i:(namespace|module))\s+(?i:((?:\w+(?:\.)?)+))
132-
captures:
133-
1: keyword.control.using.powershell
134-
2: keyword.other.powershell
135-
3: variable.parameter.powershell
136-
137150
requires-directives:
138151
- match: (#)((?i:requires))\s
139152
captures:
@@ -202,6 +215,40 @@ contexts:
202215
# Back to normal stuff
203216
- include: statements
204217

218+
###[ USINGS ]##################################################################
219+
220+
using-directives:
221+
- match: \b(?i)using(?=\s)
222+
scope: keyword.control.import.powershell
223+
push: inside-using
224+
225+
inside-using:
226+
- match: \bnamespace(?=\s)
227+
scope: keyword.control.import.powershell
228+
set: inside-using-namespace
229+
- match: \bmodule(?=\s)
230+
scope: keyword.control.import.powershell
231+
set: inside-using-module
232+
- include: else-pop
233+
234+
inside-using-namespace:
235+
- include: pop-eol
236+
- match: \p{L}[\p{L}\p{N}]+(\.)
237+
scope: meta.path.powershell
238+
captures:
239+
1: punctuation.accessor.dot.powershell
240+
- match: \p{L}[\p{L}\p{N}]+
241+
scope: meta.path.powershell
242+
243+
inside-using-module:
244+
- include: pop-eol
245+
- match: \p{L}[\p{L}\p{N}]+(\.)
246+
scope: meta.path.powershell
247+
captures:
248+
1: punctuation.accessor.dot.powershell
249+
- match: \p{L}[\p{L}\p{N}]+
250+
scope: meta.path.powershell support.type.powershell
251+
205252
###[ FUNCTIONS ]###############################################################
206253

207254
functions:
@@ -278,7 +325,7 @@ contexts:
278325
1: punctuation.accessor.dot.powershell
279326
- match: '{{builtin_types}}'
280327
scope: storage.type.powershell
281-
- match: (?!\d+|\.)[\p{L}\p{N}.]+
328+
- match: \p{L}[\p{L}\p{N}]+
282329
scope: support.type.powershell
283330
- include: types-without-members
284331
- match: \,
@@ -819,6 +866,7 @@ variables:
819866
| int (?:32|64)? | long | byte | float | double | decimal
820867
| array | hashtable
821868
)\b
869+
shebang_language: \b(?:pwsh|powershell)\b
822870

823871
# Attributes
824872
attributes: |-

Tests/syntax_test_Class.ps1

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# SYNTAX TEST "Packages/PowerShell/PowerShell.sublime-syntax"
22
using namespace system.management.automation
3-
# <- keyword.control.using
4-
# ^^^^^^^^^ keyword.other
5-
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ variable.parameter
3+
#^^^^ keyword.control.import.powershell
4+
# ^^^^^^^^^ keyword.control.import.powershell
5+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.path.powershell
6+
# ^ punctuation.accessor.dot.powershell
7+
# ^ punctuation.accessor.dot.powershell
68

79
# Define a class
810
class TypeName

Tests/syntax_test_Function.ps1

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# SYNTAX TEST "Packages/PowerShell/PowerShell.sublime-syntax"
22

33
using module Microsoft.Management
4-
# <- keyword.control.using
5-
# ^ keyword.other
6-
# ^ variable.parameter
4+
#^^^^ keyword.control.import.powershell
5+
# ^^^^^^ keyword.control.import.powershell
6+
# ^^^^^^^^^^^^^^^^^^^^ meta.path.powershell
7+
# ^ punctuation.accessor.dot.powershell
8+
# ^^^^^^^^^^ support.type.powershell
79

810
function Test-Path {}
911
# @@@@@@@@@ definition

Tests/syntax_test_PowerShell.ps1

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# SYNTAX TEST "Packages/PowerShell/PowerShell.sublime-syntax"
22
using namespace System.Management.Automation
3-
# <- keyword.control.using
4-
# ^ keyword.other
5-
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ variable.parameter
3+
#^^^^ keyword.control.import.powershell
4+
# ^^^^^^^^^ keyword.control.import.powershell
5+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.path.powershell
6+
# ^ punctuation.accessor.dot.powershell
7+
# ^ punctuation.accessor.dot.powershell
8+
69
#Requires -PSSnapin DiskSnapin -Version 1.2
710
# <- punctuation.definition.keyword
811
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.requires

0 commit comments

Comments
 (0)