Skip to content

Commit f361099

Browse files
committed
Merge pull request #72 from vors/syntax
Fix bugs for milestone 2.0+
2 parents 284dc74 + bd89ebc commit f361099

File tree

7 files changed

+169
-70
lines changed

7 files changed

+169
-70
lines changed

Support/PowershellSyntax.tmLanguage

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,29 @@
244244
</dict>
245245
<dict>
246246
<key>match</key>
247-
<string>(?&lt;!\w)((?i:begin|break|catch|class|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|workflow)|%|\?)(?!\w)</string>
247+
<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>
248248
<key>name</key>
249249
<string>keyword.control.powershell</string>
250250
</dict>
251+
<dict>
252+
<key>captures</key>
253+
<dict>
254+
<key>1</key>
255+
<dict>
256+
<key>name</key>
257+
<string>storage.type.powershell</string>
258+
</dict>
259+
<key>2</key>
260+
<dict>
261+
<key>name</key>
262+
<string>entity.name.function</string>
263+
</dict>
264+
</dict>
265+
<key>comment</key>
266+
<string>capture should be entity.name.type, but it doesn't provide a good color in the default schema.</string>
267+
<key>match</key>
268+
<string>(?&lt;!\w)((?i:class)|%|\?)(?:\s)+((?:\p{L}|\d|_|-|)+)\b</string>
269+
</dict>
251270
<dict>
252271
<key>match</key>
253272
<string>(?&lt;!\w)-(?i:is(?:not)?|as)\b</string>
@@ -331,7 +350,7 @@
331350
</dict>
332351
</dict>
333352
<key>end</key>
334-
<string>(?&lt;=\))\]</string>
353+
<string>\]</string>
335354
<key>endCaptures</key>
336355
<dict>
337356
<key>0</key>
@@ -501,7 +520,7 @@
501520
<key>function</key>
502521
<dict>
503522
<key>begin</key>
504-
<string>(?i:function|configuration)\s+((?:\p{L}|\d|_|-)+)</string>
523+
<string>((?i:function|configuration|workflow))\s+((?:\p{L}|\d|_|-|\.)+)</string>
505524
<key>beginCaptures</key>
506525
<dict>
507526
<key>0</key>
@@ -692,17 +711,19 @@
692711
<key>0</key>
693712
<dict>
694713
<key>name</key>
695-
<string>entity.name.type</string>
714+
<string>entity.other.attribute-name</string>
696715
</dict>
697716
</dict>
717+
<key>comment</key>
718+
<string>name should be entity.name.type but default schema doesn't have a good color for it</string>
698719
<key>end</key>
699720
<string>\]</string>
700721
<key>endCaptures</key>
701722
<dict>
702723
<key>0</key>
703724
<dict>
704725
<key>name</key>
705-
<string>entity.name.type</string>
726+
<string>entity.other.attribute-name</string>
706727
</dict>
707728
</dict>
708729
<key>patterns</key>
@@ -711,7 +732,7 @@
711732
<key>match</key>
712733
<string>(\p{L}|\.|``\d+)+?</string>
713734
<key>name</key>
714-
<string>entity.name.type</string>
735+
<string>entity.other.attribute-name</string>
715736
</dict>
716737
<dict>
717738
<key>include</key>
@@ -777,7 +798,7 @@
777798
<key>2</key>
778799
<dict>
779800
<key>name</key>
780-
<string>support.variable.automatic.powershell</string>
801+
<string>support.constant.automatic.powershell</string>
781802
</dict>
782803
<key>3</key>
783804
<dict>
@@ -786,7 +807,7 @@
786807
</dict>
787808
</dict>
788809
<key>comment</key>
789-
<string>Automatic variables are not constants, but they are read-only...</string>
810+
<string>Automatic variables are not constants, but they are read-only. In monokai (default) color schema support.variable doesn't have color, so we use constant.</string>
790811
<key>match</key>
791812
<string>(\$)(?i:(\$|\^|\?|_|Args|ConsoleFileName|Event|EventArgs|EventSubscriber|ForEach|Input|LastExitCode|Matches|MyInvocation|NestedPromptLevel|Profile|PSBoundParameters|PsCmdlet|PsCulture|PSDebugContext|PSItem|PSCommandPath|PSScriptRoot|PsUICulture|Pwd|Sender|SourceArgs|SourceEventArgs|StackTrace|Switch|This))((?:\.(?:\p{L}|\d|_)+)*\b)?\b</string>
792813
</dict>

appveyor.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
install:
22
- ps: write-host -ForegroundColor Yellow "installing sublime text 3"
33
- ps: start-filedownload "http://c758482.r82.cf2.rackcdn.com/Sublime%20Text%20Build%203059%20x64.zip"
4-
- ps: write-host -ForegroundColor Yellow "installing Sublime%20Text%20Build%203059%20x64.zip"
54
- ps: 7z.exe x "Sublime%20Text%20Build%203059%20x64.zip" -o"C:\st" > $null
5+
- ps: write-host -ForegroundColor Yellow "copying PowerShell package"
66
- ps: mkdir "C:\st\Data\Packages" -force > $null
77
- ps: mkdir "C:\st\Data\Packages\PowerShell" -force > $null
88
- ps: cp -Recurse * "C:\st\Data\Packages\PowerShell" -Force
9+
- ps: write-host -ForegroundColor Yellow "installing UnitTesting package"
910
- ps: git clone -q --branch=master https://github.com/randy3k/UnitTesting.git "C:\st\Data\Packages\UnitTesting"
1011
- ps: write-host -ForegroundColor Yellow "installing pester"
1112
- ps: cinst pester

tests/pester/Syntax.Tests.ps1

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,28 @@ Describe "Syntax highlighting" {
1717
}
1818

1919
# splitted in two lines, because of a bug in Sort-Object
20-
$stScopes = cat -Raw $scopesFile | ConvertFrom-Json; $stScopes = $stScopes | sort -Property @('startOffset', 'endOffset')
20+
$stScopes = Get-SublimeScopesFromFile $scopesFile
2121
# tokens are already sorted
2222
$psScopes = Get-TokensFromFile $testFile | Convert-TokenToScope
2323

2424
It "split tokens across the scopes 0 times" {
2525
$stIndex = 0
2626
$errorCounter = 0
2727

28-
$psScopes | %{
29-
while ($stScopes[$stIndex].endOffset -le $_.startOffset) {
28+
foreach ($psScope in $psScopes) {
29+
while ($stScopes[$stIndex].endOffset -le $psScope.startOffset) {
3030
$stIndex++
3131
if ($stIndex -ge $stScopes.Length) {
3232
break
3333
}
3434
}
3535

36+
if ($stIndex -ge $stScopes.Length) {
37+
# we are done with sublime scopes
38+
break
39+
}
40+
3641
$stScope = $stScopes[$stIndex]
37-
$psScope = $_
3842

3943
#Write-Host "PowerShell scope $psScope"
4044
#Write-Host "SublimeText scope $stScope"
@@ -49,6 +53,12 @@ Describe "Syntax highlighting" {
4953
$ignore = $true
5054
}
5155

56+
if (@('7>') -contains $psScope.Text) {
57+
# this is fine. It's more like a powershell parsing problem.
58+
$ignore = $true
59+
}
60+
61+
5262
# TODO: These are bugs
5363
if (@('Number', 'Redirection') -contains $psScope.Kind) {
5464
$ignore = $true
@@ -61,8 +71,7 @@ Describe "Syntax highlighting" {
6171
}
6272
}
6373
}
64-
# TODO: These are bugs, make it 0
65-
$errorCounter | Should be @(0..2)
74+
$errorCounter | Should be 0
6675
}
6776

6877
It "produces same tokens for lower case" {

tests/pester/SyntaxHelper.psm1

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ function Get-SublimeScopesFromFile
1818
[string] $filePath
1919
)
2020

21-
# splitted in two lines, because of a bug in Sort-Object
2221
$scopes = cat -Raw $filePath | ConvertFrom-Json
2322
$scopes = $scopes | sort -Property @('startOffset', 'endOffset')
2423
return $scopes
@@ -134,8 +133,21 @@ function Test-ScopesEqual
134133
if ($leftScope.startOffset -ne $rightScope.startOffset) {return $false}
135134
if ($leftScope.endOffset -ne $rightScope.endOffset) {return $false}
136135
if ($leftScope.Text -ne $rightScope.Text) {return $false}
137-
#TODO: this is week, need to verify that Kind is the same
138-
#if ($leftScope.Kind.Split('.')[0] -ne $rightScope.Kind.Split('.')[0]) {return $false}
136+
if ($leftScope.Kind -ne $rightScope.Kind)
137+
{
138+
# TODO: what's that??
139+
$bugsExclude = @('entity.name.function*', 'keyword.operator*', 'storage.type*', 'entity.other.attribute-name*', '*constant*')
140+
foreach ($bug in $bugsExclude)
141+
{
142+
if (($leftScope.Kind -like $bug) -and ($rightScope.Kind -like $bug))
143+
{
144+
Write-Warning "[Fix me in SyntaxHelper.psm1] Ignore mistmatch in left scope $leftScope and right scope $rightScope"
145+
return $true
146+
}
147+
}
148+
149+
return $false
150+
}
139151
return $true
140152
}
141153

tests/py/__init__.py

Lines changed: 57 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -55,57 +55,63 @@ def getTestFilePath(self):
5555

5656
def getTokens(self):
5757
selectors = [
58-
"comment.block",
59-
"comment.documentation.embedded",
60-
"comment.line.number-sign",
61-
"constant.character.escape",
62-
"constant.language",
63-
"constant.numeric.hexadecimal",
64-
"constant.numeric.scientific",
65-
"constant.string.documentation",
66-
"entity.name",
67-
"entity.name.function.invocation",
68-
"entity.other",
69-
"entity.other.attribute-name",
70-
"entity.other.attribute.parameter",
71-
"interpolated.complex.source",
72-
"interpolated.simple.source",
73-
"keyword.control",
74-
"keyword.operator.assignment",
75-
"keyword.operator.bitwise",
76-
"keyword.operator.comparison",
77-
"keyword.operator.documentation",
78-
"keyword.operator.logical",
79-
"keyword.operator.math",
80-
"keyword.operator.other",
81-
"keyword.operator.range",
82-
"keyword.operator.redirection",
83-
"keyword.operator.string-format",
84-
"keyword.operator.unary",
85-
"keyword.other",
86-
"keyword.other.statement-separator",
87-
"meta",
88-
"meta.group.array-expression",
89-
"meta.group.complex.subexpression",
90-
"meta.scriptblock",
91-
"punctuation.end.definition.comment.block",
92-
"punctuation.start.definition.comment.block",
93-
#"source", # this scope represents the whole file
94-
"storage",
95-
"storage.modifier.scope",
96-
"string.quoted.double",
97-
"string.quoted.double.heredoc",
98-
"string.quoted.single",
99-
"string.quoted.single.heredoc",
100-
"support.constant",
101-
"support.constant.variable",
102-
"support.function",
103-
"support.variable.automatic",
104-
"support.variable.drive",
105-
"variable.language",
106-
"variable.other",
107-
"variable.other.normal",
108-
"variable.other.readwrite",
58+
"comment.block.powershell",
59+
"comment.documentation.embedded.powershell",
60+
"comment.line.number-sign.powershell",
61+
"constant.character.escape.powershell",
62+
"constant.language.powershell",
63+
"constant.numeric.hexadecimal.powershell",
64+
"constant.numeric.scientific.powershell",
65+
"constant.string.documentation.powershell",
66+
"entity.name.function",
67+
"entity.name.function ",
68+
"entity.name.function.invocation.powershell",
69+
"entity.name.tag",
70+
"entity.other.attribute-name",
71+
"entity.other.attribute-name ",
72+
"entity.other.attribute-name.powershell",
73+
"entity.other.attribute.parameter.powershell",
74+
"interpolated.complex.source.powershell",
75+
"interpolated.simple.source.powershell",
76+
"keyword.control.powershell",
77+
"keyword.operator.assignment.powershell",
78+
"keyword.operator.bitwise.powershell",
79+
"keyword.operator.comparison.powershell",
80+
"keyword.operator.documentation.powershell",
81+
"keyword.operator.logical.powershell",
82+
"keyword.operator.math.powershell",
83+
"keyword.operator.other.powershell",
84+
"keyword.operator.range.powershell",
85+
"keyword.operator.redirection.powershell",
86+
"keyword.operator.string-format.powershell",
87+
"keyword.operator.unary.powershell",
88+
"keyword.other.powershell",
89+
"keyword.other.statement-separator.powershell",
90+
"meta.function",
91+
"meta.group.array-expression.powershell",
92+
"meta.group.complex.subexpression.powershell",
93+
"meta.scriptblock.powershell",
94+
"PowerShell",
95+
"punctuation.end.definition.comment.block.powershell",
96+
"punctuation.start.definition.comment.block.powershell",
97+
"source.powershell",
98+
"storage.modifier.scope.powershell",
99+
"storage.type",
100+
"storage.type.powershell",
101+
"string.quoted.double.heredoc.powershell",
102+
"string.quoted.double.powershell",
103+
"string.quoted.single.heredoc.powershell",
104+
"string.quoted.single.powershell",
105+
"support.constant.automatic.powershell",
106+
"support.constant.powershell",
107+
"support.constant.variable.powershell",
108+
"support.function.powershell",
109+
"support.variable.automatic.powershell",
110+
"support.variable.drive.powershell",
111+
"variable.language.powershell",
112+
"variable.other.normal.powershell",
113+
"variable.other.powershell",
114+
"variable.other.readwrite.powershell",
109115
]
110116
tokens = []
111117
for selector in selectors:

tests/py/syntax_def/test_token_gen.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,19 @@ def testGetTokens(self):
1818
self.writeTokensToFile(self.getTokens(), "test-file.ps1.tokens")
1919

2020
def testGetLowerTokens(self):
21+
def escaped_lower(i, line):
22+
should = not (i > 0 and line[i-1] == '`')
23+
if (should):
24+
return line[i].lower()
25+
else:
26+
return line[i]
27+
2128
with open(self.test_path) as f:
2229
content = f.readlines()
2330
for line in content:
24-
self.append(line.lower())
31+
# escaped chars (like `n) are case-sensitive, don't upper them
32+
line_low = ''.join([escaped_lower(i, line) for i in range(len(line))])
33+
self.append(line_low)
2534
self.writeTokensToFile(self.getTokens(), "test-file.ps1.lower.tokens")
2635

2736
def testGetUpperTokens(self):

tests/samples/test-file.ps1

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,29 @@ function Get-MemberSignature {
4848
# The whole script name is the command, not just the part before the extension
4949
Some-Script.ps1 -argOne one -argTwo "two"
5050
&"Some Script.bat" -param $PSVersionTable -arg 43
51+
52+
$args
53+
$input
54+
$PSScriptRoot
5155
}
5256
}
5357

5458
function echo([string]$text) {
5559
write-host $text
5660
}
5761

62+
# declarations should be consistent
63+
function foo.bar() {}
64+
Function foo() {}
65+
66+
class A {}
67+
Class Foo-Bar {}
68+
69+
workflow w1 {}
70+
Workflow work {}
71+
72+
configuration c {}
73+
Configuration c {}
5874

5975
# Highlight types
6076
[int[]][char[]]"hello world"
@@ -427,3 +443,28 @@ Get-things.ps1 -value @args
427443

428444
# array subexpression
429445
@(This $a is it. | "$(this-is | @($('yeah'| "" )) )")
446+
447+
# TODO: [minor] D:\dev\Find-String should not be treated as cmdlet name
448+
Import-Module D:\dev\Find-String
449+
450+
451+
function Get-EscapedPath
452+
{
453+
param(
454+
[Parameter(
455+
Position=0,
456+
Mandatory=$true
457+
ValueFromPipeline=$true,
458+
ValueFromPipelineByPropertyName=$true)
459+
]
460+
[string]$path
461+
)
462+
463+
process {
464+
if ($path.Contains(' '))
465+
{
466+
return '"' + $path + '"'
467+
}
468+
return $path
469+
}
470+
}

0 commit comments

Comments
 (0)