-
Notifications
You must be signed in to change notification settings - Fork 222
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
Added Handler for Semantic Tokenization #1328
Changes from 13 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,145 @@ | ||||||
// | ||||||
// 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.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 | ||||||
{ | ||||||
//SemanticTokensHandler is labeled "Obsolete" because that is how Omnisharp marks proposed LSP features. Since we want this proposed feature, we disable this warning. | ||||||
#pragma warning disable 618 | ||||||
internal class PsesSemanticTokens : SemanticTokensHandler | ||||||
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.
Suggested change
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. Tokens* 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. (Fixed in my suggestion) |
||||||
{ | ||||||
private readonly ILogger _logger; | ||||||
private readonly WorkspaceService _workspaceService; | ||||||
static readonly SemanticTokensRegistrationOptions _registrationOptions = new SemanticTokensRegistrationOptions() { | ||||||
rjmholt marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
DocumentSelector = LspUtils.PowerShellDocumentSelector, | ||||||
Legend = new SemanticTokensLegend(), | ||||||
DocumentProvider = new Supports<SemanticTokensDocumentProviderOptions>(isSupported: true, | ||||||
new SemanticTokensDocumentProviderOptions { | ||||||
Edits = true | ||||||
}), | ||||||
RangeProvider = true | ||||||
}; | ||||||
|
||||||
public PsesSemanticTokens(ILogger<PsesSemanticTokens> logger, WorkspaceService workspaceService) : base(_registrationOptions) | ||||||
{ | ||||||
_logger = logger; | ||||||
_workspaceService = workspaceService; | ||||||
} | ||||||
|
||||||
protected override Task Tokenize(SemanticTokensBuilder builder, ITextDocumentIdentifierParams identifier, | ||||||
CancellationToken cancellationToken) | ||||||
{ | ||||||
ScriptFile file = _workspaceService.GetFile(DocumentUri.GetFileSystemPath(identifier)); | ||||||
Token[] tokens = file.ScriptTokens; | ||||||
foreach (var token in tokens){ | ||||||
PushToken(token, builder); | ||||||
} | ||||||
rjmholt marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
return Task.CompletedTask; | ||||||
} | ||||||
|
||||||
private static void PushToken(Token token, SemanticTokensBuilder builder) | ||||||
{ | ||||||
if(token is StringExpandableToken stringExpandableToken) | ||||||
rjmholt marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
{ | ||||||
// Try parsing tokens within the string | ||||||
if (stringExpandableToken.NestedTokens != null) | ||||||
{ | ||||||
foreach (Token t in stringExpandableToken.NestedTokens) | ||||||
{ | ||||||
PushToken(t, builder); | ||||||
} | ||||||
return; | ||||||
} | ||||||
} | ||||||
|
||||||
//Tokens line and col numbers indexed starting from 1, expecting indexing from 0 | ||||||
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.
Suggested change
|
||||||
int line = token.Extent.StartLineNumber - 1; | ||||||
int index = token.Extent.StartColumnNumber - 1; | ||||||
|
||||||
builder.Push(line: line, @char: index, length: token.Text.Length, | ||||||
rjmholt marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
tokenType: MapSemanticToken(token), tokenModifiers: Array.Empty<string>()); | ||||||
} | ||||||
|
||||||
private static SemanticTokenType MapSemanticToken(Token token) | ||||||
{ | ||||||
// First check token flags | ||||||
if ((token.TokenFlags & TokenFlags.Keyword) != 0) | ||||||
{ | ||||||
return SemanticTokenType.Keyword; | ||||||
} | ||||||
|
||||||
if ((token.TokenFlags & TokenFlags.CommandName) != 0) | ||||||
{ | ||||||
return SemanticTokenType.Function; | ||||||
} | ||||||
|
||||||
if (token.Kind != TokenKind.Generic && (token.TokenFlags & | ||||||
(TokenFlags.BinaryOperator | TokenFlags.UnaryOperator | TokenFlags.AssignmentOperator)) != 0) | ||||||
{ | ||||||
return SemanticTokenType.Operator; | ||||||
} | ||||||
|
||||||
if ((token.TokenFlags & TokenFlags.TypeName) != 0) | ||||||
{ | ||||||
return SemanticTokenType.Type; | ||||||
} | ||||||
|
||||||
if ((token.TokenFlags & TokenFlags.MemberName) != 0) | ||||||
{ | ||||||
return SemanticTokenType.Member; | ||||||
} | ||||||
|
||||||
// Only check token kind after checking flags | ||||||
switch (token.Kind) | ||||||
{ | ||||||
case TokenKind.Comment: | ||||||
return SemanticTokenType.Comment; | ||||||
|
||||||
case TokenKind.Parameter: | ||||||
case TokenKind.Generic when token is StringLiteralToken slt && slt.Text.StartsWith("--"): | ||||||
return SemanticTokenType.Parameter; | ||||||
|
||||||
case TokenKind.Variable: | ||||||
case TokenKind.SplattedVariable: | ||||||
return SemanticTokenType.Variable; | ||||||
|
||||||
case TokenKind.StringExpandable: | ||||||
case TokenKind.StringLiteral: | ||||||
case TokenKind.HereStringExpandable: | ||||||
case TokenKind.HereStringLiteral: | ||||||
return SemanticTokenType.String; | ||||||
|
||||||
case TokenKind.Number: | ||||||
return SemanticTokenType.Number; | ||||||
|
||||||
case TokenKind.Generic: | ||||||
return SemanticTokenType.Function; | ||||||
} | ||||||
|
||||||
// Default semantic token | ||||||
return SemanticTokenType.Documentation; | ||||||
} | ||||||
|
||||||
protected override Task<SemanticTokensDocument> GetSemanticTokensDocument( | ||||||
ITextDocumentIdentifierParams @params, | ||||||
CancellationToken cancellationToken) | ||||||
{ | ||||||
return Task.FromResult(new SemanticTokensDocument(GetRegistrationOptions().Legend)); | ||||||
} | ||||||
} | ||||||
#pragma warning restore 618 | ||||||
} |
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.
A question here: we already compile with warnings today, and while that's not a good thing, this particular warning should eventually disappear.
If we keep the pragma we suppress a warning that should eventually either go away, or which we should pay attention to since it will indicate that the proposed API hasn't stabilised. When it is stabilised, we would need to remove the pragma, but we won't know because of the pragma, meaning it may hang around in the code indefinitely (like so many fxcop suppressions).
Instead I think it probably makes sense for us to see the warning and not use a suppression pragma