|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Threading; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Microsoft.Extensions.Logging; |
| 5 | +using Microsoft.PowerShell.EditorServices; |
| 6 | +using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities; |
| 7 | +using OmniSharp.Extensions.LanguageServer.Protocol.Models; |
| 8 | +using OmniSharp.Extensions.LanguageServer.Protocol.Server; |
| 9 | + |
| 10 | +namespace PowerShellEditorServices.Engine.Services.Handlers |
| 11 | +{ |
| 12 | + public class HoverHandler : IHoverHandler |
| 13 | + { |
| 14 | + private readonly DocumentSelector _documentSelector = new DocumentSelector( |
| 15 | + new DocumentFilter() |
| 16 | + { |
| 17 | + Pattern = "**/*.ps*1" |
| 18 | + } |
| 19 | + ); |
| 20 | + |
| 21 | + private readonly ILogger _logger; |
| 22 | + private readonly SymbolsService _symbolsService; |
| 23 | + private readonly WorkspaceService _workspaceService; |
| 24 | + private readonly PowerShellContextService _powerShellContextService; |
| 25 | + |
| 26 | + private HoverCapability _capability; |
| 27 | + |
| 28 | + public HoverHandler( |
| 29 | + ILoggerFactory factory, |
| 30 | + SymbolsService symbolsService, |
| 31 | + WorkspaceService workspaceService, |
| 32 | + PowerShellContextService powerShellContextService) |
| 33 | + { |
| 34 | + _logger = factory.CreateLogger<HoverHandler>(); |
| 35 | + _symbolsService = symbolsService; |
| 36 | + _workspaceService = workspaceService; |
| 37 | + _powerShellContextService = powerShellContextService; |
| 38 | + } |
| 39 | + |
| 40 | + public TextDocumentRegistrationOptions GetRegistrationOptions() |
| 41 | + { |
| 42 | + return new TextDocumentRegistrationOptions() |
| 43 | + { |
| 44 | + DocumentSelector = _documentSelector, |
| 45 | + }; |
| 46 | + } |
| 47 | + |
| 48 | + public async Task<Hover> Handle(HoverParams request, CancellationToken cancellationToken) |
| 49 | + { |
| 50 | + ScriptFile scriptFile = |
| 51 | + _workspaceService.GetFile( |
| 52 | + request.TextDocument.Uri.ToString()); |
| 53 | + |
| 54 | + SymbolDetails symbolDetails = |
| 55 | + await _symbolsService.FindSymbolDetailsAtLocationAsync( |
| 56 | + scriptFile, |
| 57 | + (int) request.Position.Line + 1, |
| 58 | + (int) request.Position.Character + 1, |
| 59 | + _powerShellContextService); |
| 60 | + |
| 61 | + List<MarkedString> symbolInfo = new List<MarkedString>(); |
| 62 | + Range symbolRange = null; |
| 63 | + |
| 64 | + if (symbolDetails != null) |
| 65 | + { |
| 66 | + symbolInfo.Add(new MarkedString("PowerShell", symbolDetails.DisplayString)); |
| 67 | + |
| 68 | + if (!string.IsNullOrEmpty(symbolDetails.Documentation)) |
| 69 | + { |
| 70 | + symbolInfo.Add(new MarkedString("markdown", symbolDetails.Documentation)); |
| 71 | + } |
| 72 | + |
| 73 | + symbolRange = GetRangeFromScriptRegion(symbolDetails.SymbolReference.ScriptRegion); |
| 74 | + } |
| 75 | + |
| 76 | + return new Hover |
| 77 | + { |
| 78 | + Contents = new MarkedStringsOrMarkupContent(symbolInfo), |
| 79 | + Range = symbolRange |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + public void SetCapability(HoverCapability capability) |
| 84 | + { |
| 85 | + _capability = capability; |
| 86 | + } |
| 87 | + |
| 88 | + private static Range GetRangeFromScriptRegion(ScriptRegion scriptRegion) |
| 89 | + { |
| 90 | + return new Range |
| 91 | + { |
| 92 | + Start = new Position |
| 93 | + { |
| 94 | + Line = scriptRegion.StartLineNumber - 1, |
| 95 | + Character = scriptRegion.StartColumnNumber - 1 |
| 96 | + }, |
| 97 | + End = new Position |
| 98 | + { |
| 99 | + Line = scriptRegion.EndLineNumber - 1, |
| 100 | + Character = scriptRegion.EndColumnNumber - 1 |
| 101 | + } |
| 102 | + }; |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments