|
| 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.Linq; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.Extensions.Logging; |
| 10 | +using Microsoft.PowerShell.EditorServices; |
| 11 | +using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities; |
| 12 | +using OmniSharp.Extensions.LanguageServer.Protocol.Models; |
| 13 | +using OmniSharp.Extensions.LanguageServer.Protocol.Server; |
| 14 | + |
| 15 | +namespace PowerShellEditorServices.Engine.Services.Handlers |
| 16 | +{ |
| 17 | + public class SignatureHelpHandler : ISignatureHelpHandler |
| 18 | + { |
| 19 | + private static readonly SignatureInformation[] s_emptySignatureResult = new SignatureInformation[0]; |
| 20 | + |
| 21 | + private readonly DocumentSelector _documentSelector = new DocumentSelector( |
| 22 | + new DocumentFilter() |
| 23 | + { |
| 24 | + Pattern = "**/*.ps*1" |
| 25 | + } |
| 26 | + ); |
| 27 | + |
| 28 | + private readonly ILogger _logger; |
| 29 | + private readonly SymbolsService _symbolsService; |
| 30 | + private readonly WorkspaceService _workspaceService; |
| 31 | + private readonly PowerShellContextService _powerShellContextService; |
| 32 | + |
| 33 | + private SignatureHelpCapability _capability; |
| 34 | + |
| 35 | + public SignatureHelpHandler( |
| 36 | + ILoggerFactory factory, |
| 37 | + SymbolsService symbolsService, |
| 38 | + WorkspaceService workspaceService, |
| 39 | + PowerShellContextService powerShellContextService) |
| 40 | + { |
| 41 | + _logger = factory.CreateLogger<HoverHandler>(); |
| 42 | + _symbolsService = symbolsService; |
| 43 | + _workspaceService = workspaceService; |
| 44 | + _powerShellContextService = powerShellContextService; |
| 45 | + } |
| 46 | + |
| 47 | + public SignatureHelpRegistrationOptions GetRegistrationOptions() |
| 48 | + { |
| 49 | + return new SignatureHelpRegistrationOptions |
| 50 | + { |
| 51 | + DocumentSelector = _documentSelector, |
| 52 | + // A sane default of " ". We may be able to include others like "-". |
| 53 | + TriggerCharacters = new Container<string>(" ") |
| 54 | + }; |
| 55 | + } |
| 56 | + |
| 57 | + public async Task<SignatureHelp> Handle(SignatureHelpParams request, CancellationToken cancellationToken) |
| 58 | + { |
| 59 | + ScriptFile scriptFile = |
| 60 | + _workspaceService.GetFile( |
| 61 | + request.TextDocument.Uri.ToString()); |
| 62 | + |
| 63 | + ParameterSetSignatures parameterSets = |
| 64 | + await _symbolsService.FindParameterSetsInFileAsync( |
| 65 | + scriptFile, |
| 66 | + (int) request.Position.Line + 1, |
| 67 | + (int) request.Position.Character + 1, |
| 68 | + _powerShellContextService); |
| 69 | + |
| 70 | + SignatureInformation[] signatures = s_emptySignatureResult; |
| 71 | + |
| 72 | + if (parameterSets != null) |
| 73 | + { |
| 74 | + signatures = new SignatureInformation[parameterSets.Signatures.Length]; |
| 75 | + for (int i = 0; i < signatures.Length; i++) |
| 76 | + { |
| 77 | + var parameters = new ParameterInformation[parameterSets.Signatures[i].Parameters.Count()]; |
| 78 | + int j = 0; |
| 79 | + foreach (ParameterInfo param in parameterSets.Signatures[i].Parameters) |
| 80 | + { |
| 81 | + parameters[j] = CreateParameterInfo(param); |
| 82 | + j++; |
| 83 | + } |
| 84 | + |
| 85 | + signatures[i] = new SignatureInformation |
| 86 | + { |
| 87 | + Label = parameterSets.CommandName + " " + parameterSets.Signatures[i].SignatureText, |
| 88 | + Documentation = null, |
| 89 | + Parameters = parameters, |
| 90 | + }; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return new SignatureHelp |
| 95 | + { |
| 96 | + Signatures = signatures, |
| 97 | + ActiveParameter = null, |
| 98 | + ActiveSignature = 0 |
| 99 | + }; |
| 100 | + } |
| 101 | + |
| 102 | + public void SetCapability(SignatureHelpCapability capability) |
| 103 | + { |
| 104 | + _capability = capability; |
| 105 | + } |
| 106 | + |
| 107 | + private static ParameterInformation CreateParameterInfo(ParameterInfo parameterInfo) |
| 108 | + { |
| 109 | + return new ParameterInformation |
| 110 | + { |
| 111 | + Label = parameterInfo.Name, |
| 112 | + Documentation = string.Empty |
| 113 | + }; |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments