|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.Extensions.Logging; |
| 10 | +using Microsoft.PowerShell.EditorServices.Services; |
| 11 | +using Microsoft.PowerShell.EditorServices.Services.Symbols; |
| 12 | +using Microsoft.PowerShell.EditorServices.Services.TextDocument; |
| 13 | +using Microsoft.PowerShell.EditorServices.Utility; |
| 14 | +using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities; |
| 15 | +using OmniSharp.Extensions.LanguageServer.Protocol.Document; |
| 16 | +using OmniSharp.Extensions.LanguageServer.Protocol.Models; |
| 17 | + |
| 18 | +namespace Microsoft.PowerShell.EditorServices.Handlers; |
| 19 | + |
| 20 | +/// <summary> |
| 21 | +/// Resolves PowerShell types and parameters as inlay hints for the LSP client. See: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_inlayHints |
| 22 | +/// </summary> |
| 23 | +internal class PsesInlayHandler( |
| 24 | + ILoggerFactory loggerFactory, |
| 25 | + SymbolsService symbolsService, |
| 26 | + WorkspaceService workspaceService |
| 27 | +) : InlayHintsHandlerBase |
| 28 | +{ |
| 29 | + private readonly ILogger logger = loggerFactory.CreateLogger<PsesInlayHandler>(); |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Expresses the capabilities of our Inlay Hints handler to the LSP |
| 33 | + /// </summary> |
| 34 | + protected override InlayHintRegistrationOptions CreateRegistrationOptions(InlayHintClientCapabilities capability, ClientCapabilities clientCapabilities) => new() |
| 35 | + { |
| 36 | + DocumentSelector = LspUtils.PowerShellDocumentSelector, |
| 37 | + WorkDoneProgress = false, //TODO: Report progress for large documents |
| 38 | + ResolveProvider = false //TODO: Add a resolve Provider for detailed inlay information |
| 39 | + }; |
| 40 | + |
| 41 | + public override async Task<InlayHint> Handle(InlayHint request, CancellationToken cancellationToken) => throw new NotImplementedException(); |
| 42 | + |
| 43 | + public override async Task<InlayHintContainer> Handle(InlayHintParams request, CancellationToken cancellationToken) |
| 44 | + { |
| 45 | + if (cancellationToken.IsCancellationRequested) |
| 46 | + { |
| 47 | + logger.LogDebug("InlayHint request canceled for file: {Uri}", request.TextDocument.Uri); |
| 48 | + return null; |
| 49 | + } |
| 50 | + |
| 51 | + // TODO: Limit search to request.range |
| 52 | + ScriptFile scriptFile = workspaceService.GetFile(request.TextDocument.Uri); |
| 53 | + |
| 54 | + IEnumerable<SymbolReference> symbolReferences = |
| 55 | + symbolsService.FindSymbolsInFile(scriptFile); |
| 56 | + |
| 57 | + if (symbolReferences is null) |
| 58 | + { |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + IEnumerable<InlayHint> inlayHints = |
| 63 | + from s in symbolReferences |
| 64 | + where s.Type == SymbolType.Variable | s.Type == SymbolType.Parameter |
| 65 | + select new InlayHint |
| 66 | + { |
| 67 | + Kind = InlayHintKind.Type, |
| 68 | + Position = new Position( |
| 69 | + s.ScriptRegion.StartLineNumber - 1, |
| 70 | + s.ScriptRegion.StartColumnNumber - 1), |
| 71 | + Label = s.Type.ToString() |
| 72 | + }; |
| 73 | + |
| 74 | + return new InlayHintContainer(inlayHints); |
| 75 | + } |
| 76 | +} |
0 commit comments