Skip to content

Commit 5db708b

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

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

PowerShell.sublime-syntax

Lines changed: 27 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,36 @@ 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+
- match: '@"'
239+
scope: meta.string.interpolated.powershell string.quoted.double.powershell punctuation.definition.string.begin.powershell
240+
embed: scope:source.cs
241+
embed_scope: meta.string.interpolated.powershell string.quoted.double.powershell source.cs.embedded
242+
escape: ("@)
243+
escape_captures:
244+
1: meta.string.powershell string.quoted.single.powershell punctuation.definition.string.end.powershell
245+
- match: "@'"
246+
scope: meta.string.powershell string.quoted.single.powershell punctuation.definition.string.begin.powershell
247+
embed: scope:source.cs
248+
embed_scope: meta.string.powershell string.quoted.single.powershell source.cs.embedded
249+
escape: ('@)
250+
escape_captures:
251+
1: meta.string.powershell string.quoted.single.powershell punctuation.definition.string.end.powershell
252+
- include: expressions
253+
228254
###[ USINGS ]##################################################################
229255

230256
using-directives:

Tests/syntax_test_PowerShell.ps1

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,3 +1744,26 @@ function get-number {}
17441744
#^^^^^^^^^ keyword.other.region.end.powershell
17451745
#<- comment.line
17461746
# ^ meta.fold.end
1747+
1748+
Add-Type -Language CSharp @"
1749+
# ^^^^^^^^ support.function.powershell
1750+
# ^^^^^^^^^ variable.parameter.option.powershell
1751+
# ^ punctuation.definition.parameter.powershell
1752+
# ^^ meta.string.interpolated.powershell string.quoted.double.powershell punctuation.definition.string.begin.powershell
1753+
using System;
1754+
#^^^^^^^^^^^^^^^^ meta.string.interpolated.powershell string.quoted.double.powershell source.cs.embedded
1755+
# ^^^^^ keyword.control.import.cs
1756+
# ^^^^^^ meta.path.cs
1757+
# ^ punctuation.terminator.statement.cs
1758+
using System.Runtime.InteropServices;
1759+
public class ClassGetForegroundWindow {
1760+
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string.interpolated.powershell string.quoted.double.powershell source.cs.embedded
1761+
# ^^^^^^ storage.modifier.access.cs
1762+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.class.cs
1763+
# ^^^^^ keyword.declaration.class.cs
1764+
# ^^^^^^^^^^^^^^^^^^^^^^^^ entity.name.class.cs
1765+
# ^ meta.class.body.cs meta.block.cs punctuation.section.block.begin.cs
1766+
[DllImport("user32.dll")]
1767+
public static extern IntPtr GetForegroundWindow();
1768+
}
1769+
"@

0 commit comments

Comments
 (0)