Skip to content

Commit 6241786

Browse files
committed
Embed C# in Add-Type strings
Fixes #201
1 parent d20ddfc commit 6241786

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

PowerShell.sublime-syntax

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ contexts:
137137
- include: pop-before-eol
138138

139139
- include: operators
140+
- include: parameters
140141

142+
parameters:
141143
# Flags/Options/Parameters
142144
- match: \B(--?|[/+])\p{L}(?:[\w-]*\w)?
143145
scope: variable.parameter.option.powershell
@@ -201,6 +203,7 @@ contexts:
201203
###[ COMMANDS ]################################################################
202204

203205
commands:
206+
- include: commands-add-type
204207
- include: commands-verb-noun
205208
- include: commands-reserved
206209
- include: script-invocation
@@ -218,13 +221,39 @@ contexts:
218221
219222
commands-reserved:
220223
# Builtin cmdlets with reserved verbs
221-
- match: \b(?i:(?:foreach|where|sort|tee)-object)\b
224+
- match: \b(?i:(?:ForEach|Where|Sort|Tee)-Object)\b
222225
scope: support.function.powershell
223226

224227
script-invocation:
225228
- match: \b[\w.\\/-]+\.(?i:exe|com|cmd|bat|ps1)\b
226229
scope: variable.function.powershell
227230

231+
commands-add-type:
232+
- match: \b(?i:Add-Type)\b
233+
scope: support.function.powershell
234+
push: inside-add-type
235+
236+
inside-add-type:
237+
- include: pop-eol
238+
239+
- match: '@"'
240+
scope: meta.string.interpolated.powershell string.quoted.double.powershell punctuation.definition.string.begin.powershell
241+
embed: scope:source.cs
242+
embed_scope: meta.string.interpolated.powershell string.quoted.double.powershell source.cs.embedded
243+
escape: ("@)
244+
escape_captures:
245+
1: meta.string.powershell string.quoted.double.powershell punctuation.definition.string.end.powershell
246+
247+
- match: "@'"
248+
scope: meta.string.powershell string.quoted.single.powershell punctuation.definition.string.begin.powershell
249+
embed: scope:source.cs
250+
embed_scope: meta.string.powershell string.quoted.single.powershell source.cs.embedded
251+
escape: ('@)
252+
escape_captures:
253+
1: meta.string.powershell string.quoted.single.powershell punctuation.definition.string.end.powershell
254+
255+
- include: expressions
256+
228257
###[ USINGS ]##################################################################
229258

230259
using-directives:

Tests/syntax_test_Add-Type.ps1

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# SYNTAX TEST "Packages/PowerShell/PowerShell.sublime-syntax"
2+
3+
4+
Add-Type -Language CSharp @"
5+
# ^^^^^^^^ support.function.powershell
6+
# ^^^^^^^^^ variable.parameter.option.powershell
7+
# ^ punctuation.definition.parameter.powershell
8+
# ^^ meta.string.interpolated.powershell string.quoted.double.powershell punctuation.definition.string.begin.powershell
9+
using System;
10+
#^^^^^^^^^^^^^^^^ meta.string.interpolated.powershell string.quoted.double.powershell source.cs.embedded
11+
# ^^^^^ keyword.control.import.cs
12+
# ^^^^^^ meta.path.cs
13+
using System.Runtime.InteropServices;
14+
public class ClassGetForegroundWindow {
15+
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string.interpolated.powershell string.quoted.double.powershell source.cs.embedded
16+
# ^^^^^^ storage.modifier.access.cs
17+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.class.cs
18+
# ^^^^^^^^^^^^^^^^^^^^^^^^ entity.name.class.cs
19+
# ^ meta.class.body.cs meta.block.cs punctuation.section.block.begin.cs
20+
[DllImport("user32.dll")]
21+
public static extern IntPtr GetForegroundWindow();
22+
}
23+
"@
24+
#^ meta.string.powershell string.quoted.double.powershell punctuation.definition.string.end.powershell - source.cs

0 commit comments

Comments
 (0)