|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft. All rights reserved. |
| 3 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 4 | +// |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Management.Automation.Language; |
| 9 | +using System.Threading; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using Microsoft.Extensions.Logging; |
| 12 | +using Microsoft.PowerShell.EditorServices.Services; |
| 13 | +using Microsoft.PowerShell.EditorServices.Services.TextDocument; |
| 14 | +using Microsoft.PowerShell.EditorServices.Utility; |
| 15 | +using OmniSharp.Extensions.LanguageServer.Protocol; |
| 16 | +using OmniSharp.Extensions.LanguageServer.Protocol.Document.Proposals; |
| 17 | +using OmniSharp.Extensions.LanguageServer.Protocol.Models; |
| 18 | +using OmniSharp.Extensions.LanguageServer.Protocol.Models.Proposals; |
| 19 | + |
| 20 | +namespace Microsoft.PowerShell.EditorServices.Handlers |
| 21 | +{ |
| 22 | + internal class PsesSemanticTokensHandler : SemanticTokensHandler |
| 23 | + { |
| 24 | + private static readonly SemanticTokensRegistrationOptions s_registrationOptions = new SemanticTokensRegistrationOptions |
| 25 | + { |
| 26 | + DocumentSelector = LspUtils.PowerShellDocumentSelector, |
| 27 | + Legend = new SemanticTokensLegend(), |
| 28 | + DocumentProvider = new Supports<SemanticTokensDocumentProviderOptions>( |
| 29 | + isSupported: true, |
| 30 | + new SemanticTokensDocumentProviderOptions |
| 31 | + { |
| 32 | + Edits = true |
| 33 | + }), |
| 34 | + RangeProvider = true |
| 35 | + }; |
| 36 | + |
| 37 | + private readonly ILogger _logger; |
| 38 | + private readonly WorkspaceService _workspaceService; |
| 39 | + |
| 40 | + public PsesSemanticTokensHandler(ILogger<PsesSemanticTokensHandler> logger, WorkspaceService workspaceService) |
| 41 | + : base(s_registrationOptions) |
| 42 | + { |
| 43 | + _logger = logger; |
| 44 | + _workspaceService = workspaceService; |
| 45 | + } |
| 46 | + |
| 47 | + protected override Task Tokenize(SemanticTokensBuilder builder, ITextDocumentIdentifierParams identifier, |
| 48 | + CancellationToken cancellationToken) |
| 49 | + { |
| 50 | + ScriptFile file = _workspaceService.GetFile(identifier.TextDocument.Uri); |
| 51 | + foreach (Token token in file.ScriptTokens) |
| 52 | + { |
| 53 | + PushToken(token, builder); |
| 54 | + } |
| 55 | + return Task.CompletedTask; |
| 56 | + } |
| 57 | + |
| 58 | + private static void PushToken(Token token, SemanticTokensBuilder builder) |
| 59 | + { |
| 60 | + foreach (SemanticToken sToken in ConvertToSemanticTokens(token)) |
| 61 | + { |
| 62 | + builder.Push( |
| 63 | + sToken.Line, |
| 64 | + sToken.Column, |
| 65 | + length: sToken.Text.Length, |
| 66 | + sToken.Type, |
| 67 | + tokenModifiers: sToken.TokenModifiers); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + internal static IEnumerable<SemanticToken> ConvertToSemanticTokens(Token token) |
| 72 | + { |
| 73 | + if (token is StringExpandableToken stringExpandableToken) |
| 74 | + { |
| 75 | + // Try parsing tokens within the string |
| 76 | + if (stringExpandableToken.NestedTokens != null) |
| 77 | + { |
| 78 | + foreach (Token t in stringExpandableToken.NestedTokens) |
| 79 | + { |
| 80 | + foreach (SemanticToken subToken in ConvertToSemanticTokens(t)) |
| 81 | + yield return subToken; |
| 82 | + } |
| 83 | + yield break; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + SemanticTokenType mappedType = MapSemanticTokenType(token); |
| 88 | + if (mappedType == null) |
| 89 | + { |
| 90 | + yield break; |
| 91 | + } |
| 92 | + |
| 93 | + //Note that both column and line numbers are 0-based |
| 94 | + yield return new SemanticToken( |
| 95 | + token.Text, |
| 96 | + mappedType, |
| 97 | + line: token.Extent.StartLineNumber - 1, |
| 98 | + column: token.Extent.StartColumnNumber - 1, |
| 99 | + tokenModifiers: Array.Empty<string>()); |
| 100 | + } |
| 101 | + |
| 102 | + private static SemanticTokenType MapSemanticTokenType(Token token) |
| 103 | + { |
| 104 | + // First check token flags |
| 105 | + if ((token.TokenFlags & TokenFlags.Keyword) != 0) |
| 106 | + { |
| 107 | + return SemanticTokenType.Keyword; |
| 108 | + } |
| 109 | + |
| 110 | + if ((token.TokenFlags & TokenFlags.CommandName) != 0) |
| 111 | + { |
| 112 | + return SemanticTokenType.Function; |
| 113 | + } |
| 114 | + |
| 115 | + if (token.Kind != TokenKind.Generic && (token.TokenFlags & |
| 116 | + (TokenFlags.BinaryOperator | TokenFlags.UnaryOperator | TokenFlags.AssignmentOperator)) != 0) |
| 117 | + { |
| 118 | + return SemanticTokenType.Operator; |
| 119 | + } |
| 120 | + |
| 121 | + if ((token.TokenFlags & TokenFlags.TypeName) != 0) |
| 122 | + { |
| 123 | + return SemanticTokenType.Type; |
| 124 | + } |
| 125 | + |
| 126 | + if ((token.TokenFlags & TokenFlags.MemberName) != 0) |
| 127 | + { |
| 128 | + return SemanticTokenType.Member; |
| 129 | + } |
| 130 | + |
| 131 | + // Only check token kind after checking flags |
| 132 | + switch (token.Kind) |
| 133 | + { |
| 134 | + case TokenKind.Comment: |
| 135 | + return SemanticTokenType.Comment; |
| 136 | + |
| 137 | + case TokenKind.Parameter: |
| 138 | + case TokenKind.Generic when token is StringLiteralToken slt && slt.Text.StartsWith("--"): |
| 139 | + return SemanticTokenType.Parameter; |
| 140 | + |
| 141 | + case TokenKind.Variable: |
| 142 | + case TokenKind.SplattedVariable: |
| 143 | + return SemanticTokenType.Variable; |
| 144 | + |
| 145 | + case TokenKind.StringExpandable: |
| 146 | + case TokenKind.StringLiteral: |
| 147 | + case TokenKind.HereStringExpandable: |
| 148 | + case TokenKind.HereStringLiteral: |
| 149 | + return SemanticTokenType.String; |
| 150 | + |
| 151 | + case TokenKind.Number: |
| 152 | + return SemanticTokenType.Number; |
| 153 | + |
| 154 | + case TokenKind.Generic: |
| 155 | + return SemanticTokenType.Function; |
| 156 | + } |
| 157 | + |
| 158 | + return null; |
| 159 | + } |
| 160 | + |
| 161 | + protected override Task<SemanticTokensDocument> GetSemanticTokensDocument( |
| 162 | + ITextDocumentIdentifierParams @params, |
| 163 | + CancellationToken cancellationToken) |
| 164 | + { |
| 165 | + return Task.FromResult(new SemanticTokensDocument(GetRegistrationOptions().Legend)); |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments