From 3249cd97db020acf375d68dd3b577fe2e7108292 Mon Sep 17 00:00:00 2001 From: MartinGC94 Date: Mon, 22 Jul 2024 23:04:45 +0200 Subject: [PATCH 1/3] Add new Decorator and Label semantic tokens for attributes and loop labels. Also removes the incorrect Generic -> Function mapping. --- .../TextDocument/Handlers/PsesSemanticTokensHandler.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/PowerShellEditorServices/Services/TextDocument/Handlers/PsesSemanticTokensHandler.cs b/src/PowerShellEditorServices/Services/TextDocument/Handlers/PsesSemanticTokensHandler.cs index e5e69e60f..494843bb5 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/Handlers/PsesSemanticTokensHandler.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/Handlers/PsesSemanticTokensHandler.cs @@ -108,6 +108,11 @@ private static SemanticTokenType MapSemanticTokenType(Token token) return SemanticTokenType.Operator; } + if ((token.TokenFlags & TokenFlags.AttributeName) != 0) + { + return SemanticTokenType.Decorator; + } + if ((token.TokenFlags & TokenFlags.TypeName) != 0) { return SemanticTokenType.Type; @@ -142,8 +147,8 @@ private static SemanticTokenType MapSemanticTokenType(Token token) case TokenKind.Number: return SemanticTokenType.Number; - case TokenKind.Generic: - return SemanticTokenType.Function; + case TokenKind.Label: + return SemanticTokenType.Label; } return null; From dcf642b1c7ccd40a71dc1617195fb6bdc50c6c9b Mon Sep 17 00:00:00 2001 From: MartinGC94 Date: Sat, 7 Sep 2024 15:15:07 +0200 Subject: [PATCH 2/3] Update test --- .../Language/SemanticTokenTest.cs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/test/PowerShellEditorServices.Test/Language/SemanticTokenTest.cs b/test/PowerShellEditorServices.Test/Language/SemanticTokenTest.cs index c9f3c01d4..c34336342 100644 --- a/test/PowerShellEditorServices.Test/Language/SemanticTokenTest.cs +++ b/test/PowerShellEditorServices.Test/Language/SemanticTokenTest.cs @@ -20,7 +20,8 @@ public void TokenizesFunctionElements() { const string text = @" function Get-Sum { - param( [int]$a, [int]$b ) + param( [parameter()] [int]$a, [int]$b ) + :loopLabel while (0) {break loopLabel} return $a + $b } "; @@ -38,10 +39,21 @@ function Get-Sum { case "function": case "param": case "return": + case "while": + case "break": Assert.Single(mappedTokens, sToken => SemanticTokenType.Keyword == sToken.Type); break; - case "Get-Sum": - Assert.Single(mappedTokens, sToken => SemanticTokenType.Function == sToken.Type); + case "parameter": + Assert.Single(mappedTokens, sToken => SemanticTokenType.Decorator == sToken.Type); + break; + case "0": + Assert.Single(mappedTokens, sToken => SemanticTokenType.Number == sToken.Type); + break; + case ":loopLabel": + Assert.Single(mappedTokens, sToken => SemanticTokenType.Label == sToken.Type); + break; + case "loopLabel": + Assert.Single(mappedTokens, sToken => SemanticTokenType.Property == sToken.Type); break; case "$a": case "$b": @@ -74,7 +86,6 @@ public void TokenizesStringExpansion() Token stringExpandableToken = scriptFile.ScriptTokens[1]; mappedTokens = new List(PsesSemanticTokensHandler.ConvertToSemanticTokens(stringExpandableToken)); Assert.Collection(mappedTokens, - sToken => Assert.Equal(SemanticTokenType.Function, sToken.Type), sToken => Assert.Equal(SemanticTokenType.Function, sToken.Type), sToken => Assert.Equal(SemanticTokenType.Function, sToken.Type) ); From 2785754902f309bbbdccc257be32c5b921dcadc9 Mon Sep 17 00:00:00 2001 From: MartinGC94 Date: Sun, 29 Sep 2024 21:44:11 +0200 Subject: [PATCH 3/3] Fix test --- .../Language/SemanticTokenTest.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/PowerShellEditorServices.Test/Language/SemanticTokenTest.cs b/test/PowerShellEditorServices.Test/Language/SemanticTokenTest.cs index c34336342..196fa9ca6 100644 --- a/test/PowerShellEditorServices.Test/Language/SemanticTokenTest.cs +++ b/test/PowerShellEditorServices.Test/Language/SemanticTokenTest.cs @@ -114,7 +114,11 @@ function Get-A*A { Assert.Single(mappedTokens, sToken => SemanticTokenType.Keyword == sToken.Type); break; case "Get-A*A": - Assert.Single(mappedTokens, sToken => SemanticTokenType.Function == sToken.Type); + if (t.TokenFlags.HasFlag(TokenFlags.CommandName)) + { + Assert.Single(mappedTokens, sToken => SemanticTokenType.Function == sToken.Type); + } + break; } }