-
Notifications
You must be signed in to change notification settings - Fork 241
Added Handler for Semantic Tokenization #1328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 18 commits
2790655
04458e4
2e46016
b3077cc
f68e4e3
6f28dc5
d960ac7
2853580
a55108c
75b6386
01bde39
ea0fab2
ccf1028
170fb6b
4764d2e
5572a16
f8411cc
4b7db57
a20241b
bebc507
5550780
3fb9abe
73bda8d
4703804
c013b1d
5ab13c4
62566a8
bafff92
5a631dd
7df4e67
b2e43b1
4a6955f
f4562c0
6264c0b
ae5a498
9ae83aa
b4d558c
874db6e
bec8bd6
9848c71
855790c
514012c
5294cf4
9aed66b
b4024e7
4908752
2e4f098
8f5db93
42714b5
1657fdf
ebae357
f109d71
c9b858d
d8f0a36
3fba85f
8592540
d4a86ac
e86c5b8
6c21195
09d632c
7c4b3b7
99c1c0b
725e8eb
1aedd88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,175 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Copyright (c) Microsoft. All rights reserved. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using System; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using System.Collections.Generic; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using System.Management.Automation.Language; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using System.Threading; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using System.Threading.Tasks; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using Microsoft.Extensions.Logging; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using Microsoft.PowerShell.EditorServices.Services; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using Microsoft.PowerShell.EditorServices.Services.TextDocument; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using Microsoft.PowerShell.EditorServices.Utility; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using OmniSharp.Extensions.LanguageServer.Protocol; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using OmniSharp.Extensions.LanguageServer.Protocol.Document.Proposals; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using OmniSharp.Extensions.LanguageServer.Protocol.Models; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using OmniSharp.Extensions.LanguageServer.Protocol.Models.Proposals; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| namespace Microsoft.PowerShell.EditorServices.Handlers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| internal class PsesSemanticTokens : SemanticTokensHandler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private readonly ILogger _logger; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private readonly WorkspaceService _workspaceService; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private static readonly SemanticTokensRegistrationOptions s_registrationOptions = new SemanticTokensRegistrationOptions() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| DocumentSelector = LspUtils.PowerShellDocumentSelector, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Legend = new SemanticTokensLegend(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| DocumentProvider = new Supports<SemanticTokensDocumentProviderOptions>(isSupported: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| new SemanticTokensDocumentProviderOptions { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Edits = true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| RangeProvider = true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private readonly ILogger _logger; | |
| private readonly WorkspaceService _workspaceService; | |
| private static readonly SemanticTokensRegistrationOptions s_registrationOptions = new SemanticTokensRegistrationOptions() { | |
| DocumentSelector = LspUtils.PowerShellDocumentSelector, | |
| Legend = new SemanticTokensLegend(), | |
| DocumentProvider = new Supports<SemanticTokensDocumentProviderOptions>(isSupported: true, | |
| new SemanticTokensDocumentProviderOptions { | |
| Edits = true | |
| }), | |
| RangeProvider = true | |
| }; | |
| private static readonly SemanticTokensRegistrationOptions s_registrationOptions = new SemanticTokensRegistrationOptions | |
| { | |
| DocumentSelector = LspUtils.PowerShellDocumentSelector, | |
| Legend = new SemanticTokensLegend(), | |
| DocumentProvider = new Supports<SemanticTokensDocumentProviderOptions>( | |
| isSupported: true, | |
| new SemanticTokensDocumentProviderOptions | |
| { | |
| Edits = true | |
| }), | |
| RangeProvider = true | |
| }; | |
| private readonly ILogger _logger; | |
| private readonly WorkspaceService _workspaceService; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this but with { on new lines and remove redundant () on SemanticTokensRegistrationOptions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed above
rjmholt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
rjmholt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
rjmholt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
rjmholt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
rjmholt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
rjmholt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
rjmholt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
rjmholt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| //Tokens line and col numbers indexed starting from 1, expecting indexing from 0 | |
| // Tokens line and col numbers indexed starting from 1, expecting indexing from 0 |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| SemanticToken sToken = new SemanticToken(token.Text, mappedType, | |
| line, index, Array.Empty<string>()); | |
| SemanticToken sToken = new SemanticToken( | |
| token.Text, | |
| mappedType, | |
| line, | |
| index, | |
| Array.Empty<string>()); |
rjmholt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
rjmholt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
| // | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Management.Automation.Language; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.PowerShell.EditorServices.Services.TextDocument; | ||
| using Microsoft.PowerShell.EditorServices.Handlers; | ||
| using OmniSharp.Extensions.LanguageServer.Protocol; | ||
| using OmniSharp.Extensions.LanguageServer.Protocol.Document.Proposals; | ||
| using OmniSharp.Extensions.LanguageServer.Protocol.Models.Proposals; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.PowerShell.EditorServices.Test.Language | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tests here look good. One extra thing I would do is include tests for the less conspicuous tokens like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Those tokens aren't mapped to SemanticTokenTypes and are handled by EditorSyntax
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I didn't realise that. That seems a bit strange, and maybe like we're doing more work than we should... We should maybe discuss at some point
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I map the tokens that can be recognized as one of these https://github.com/OmniSharp/csharp-language-server-protocol/blob/f93f973ec96d895c0c6056f5f5942c4646071ab9/src/Protocol/Models/Proposals/SemanticTokenTypes.cs |
||
| { | ||
| public class SemanticTokenTest | ||
| { | ||
| [Fact] | ||
| public async Task MapsFunctionElements() | ||
| { | ||
| string text = @" | ||
| function Get-Sum { | ||
| param( [int]$a, [int]$b ) | ||
| return $a + $b | ||
| } | ||
| "; | ||
| ScriptFile scriptFile = new ScriptFile( | ||
| // Use any absolute path. Even if it doesn't exist. | ||
| DocumentUri.FromFileSystemPath(Path.Combine(Path.GetTempPath(), "TestFile.ps1")), | ||
| text, | ||
| Version.Parse("5.0")); | ||
|
|
||
| foreach(Token t in scriptFile.ScriptTokens) | ||
| { | ||
| List<SemanticToken> mappedTokens = PsesSemanticTokens.ConvertToSemanticTokens(t); | ||
| switch(t.Text) | ||
| { | ||
| case "function": | ||
| case "param": | ||
| case "return": | ||
| Assert.Single(mappedTokens); | ||
| Assert.Equal(SemanticTokenType.Keyword, mappedTokens[0].Type); | ||
| break; | ||
| case "Get-Sum": | ||
| Assert.Single(mappedTokens); | ||
| Assert.Equal(SemanticTokenType.Function, mappedTokens[0].Type); | ||
| break; | ||
| case "$a": | ||
| case "$b": | ||
| Assert.Single(mappedTokens); | ||
| Assert.Equal(SemanticTokenType.Variable, mappedTokens[0].Type); | ||
rjmholt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| break; | ||
| case "[int]": | ||
| Assert.Single(mappedTokens); | ||
| Assert.Equal(SemanticTokenType.Type, mappedTokens[0].Type); | ||
| break; | ||
| case "+": | ||
| Assert.Single(mappedTokens); | ||
| Assert.Equal(SemanticTokenType.Operator, mappedTokens[0].Type); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TokenizesStringExpansion() | ||
| { | ||
| string text = "Write-Host \"$(Test-Property Get-Whatever) $(Get-Whatever)\""; | ||
| ScriptFile scriptFile = new ScriptFile( | ||
| // Use any absolute path. Even if it doesn't exist. | ||
| DocumentUri.FromFileSystemPath(Path.Combine(Path.GetTempPath(), "TestFile.ps1")), | ||
| text, | ||
| Version.Parse("5.0")); | ||
|
|
||
| Token commandToken = scriptFile.ScriptTokens[0]; | ||
| List<SemanticToken> mappedTokens = PsesSemanticTokens.ConvertToSemanticTokens(commandToken); | ||
| Assert.Single(mappedTokens); | ||
| Assert.Equal(SemanticTokenType.Function, mappedTokens[0].Type); | ||
|
|
||
| Token stringExpandableToken = scriptFile.ScriptTokens[1]; | ||
| mappedTokens = PsesSemanticTokens.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) | ||
| ); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tokens*
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Fixed in my suggestion)