|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Management.Automation.Language; |
| 5 | +using System.Threading; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Microsoft.Extensions.Logging; |
| 8 | +using Microsoft.PowerShell.EditorServices; |
| 9 | +using Microsoft.PowerShell.EditorServices.Protocol.LanguageServer; |
| 10 | + |
| 11 | +namespace PowerShellEditorServices.Engine.Services.Handlers |
| 12 | +{ |
| 13 | + public class GetCommentHelpHandler : IGetCommentHelpHandler |
| 14 | + { |
| 15 | + private readonly ILogger _logger; |
| 16 | + private readonly WorkspaceService _workspaceService; |
| 17 | + private readonly AnalysisService _analysisService; |
| 18 | + private readonly SymbolsService _symbolsService; |
| 19 | + |
| 20 | + public GetCommentHelpHandler( |
| 21 | + ILoggerFactory factory, |
| 22 | + WorkspaceService workspaceService, |
| 23 | + AnalysisService analysisService, |
| 24 | + SymbolsService symbolsService) |
| 25 | + { |
| 26 | + _logger = factory.CreateLogger<GetCommentHelpHandler>(); |
| 27 | + _workspaceService = workspaceService; |
| 28 | + _analysisService = analysisService; |
| 29 | + _symbolsService = symbolsService; |
| 30 | + } |
| 31 | + |
| 32 | + public async Task<CommentHelpRequestResult> Handle(CommentHelpRequestParams request, CancellationToken cancellationToken) |
| 33 | + { |
| 34 | + var result = new CommentHelpRequestResult(); |
| 35 | + |
| 36 | + if (!_workspaceService.TryGetFile(request.DocumentUri, out ScriptFile scriptFile)) |
| 37 | + { |
| 38 | + return result; |
| 39 | + } |
| 40 | + |
| 41 | + int triggerLine = (int) request.TriggerPosition.Line + 1; |
| 42 | + |
| 43 | + FunctionDefinitionAst functionDefinitionAst = _symbolsService.GetFunctionDefinitionForHelpComment( |
| 44 | + scriptFile, |
| 45 | + triggerLine, |
| 46 | + out string helpLocation); |
| 47 | + |
| 48 | + if (functionDefinitionAst == null) |
| 49 | + { |
| 50 | + return result; |
| 51 | + } |
| 52 | + |
| 53 | + IScriptExtent funcExtent = functionDefinitionAst.Extent; |
| 54 | + string funcText = funcExtent.Text; |
| 55 | + if (helpLocation.Equals("begin")) |
| 56 | + { |
| 57 | + // check if the previous character is `<` because it invalidates |
| 58 | + // the param block the follows it. |
| 59 | + IList<string> lines = ScriptFile.GetLinesInternal(funcText); |
| 60 | + int relativeTriggerLine0b = triggerLine - funcExtent.StartLineNumber; |
| 61 | + if (relativeTriggerLine0b > 0 && lines[relativeTriggerLine0b].IndexOf("<", StringComparison.OrdinalIgnoreCase) > -1) |
| 62 | + { |
| 63 | + lines[relativeTriggerLine0b] = string.Empty; |
| 64 | + } |
| 65 | + |
| 66 | + funcText = string.Join("\n", lines); |
| 67 | + } |
| 68 | + |
| 69 | + List<ScriptFileMarker> analysisResults = await _analysisService.GetSemanticMarkersAsync( |
| 70 | + funcText, |
| 71 | + AnalysisService.GetCommentHelpRuleSettings( |
| 72 | + enable: true, |
| 73 | + exportedOnly: false, |
| 74 | + blockComment: request.BlockComment, |
| 75 | + vscodeSnippetCorrection: true, |
| 76 | + placement: helpLocation)); |
| 77 | + |
| 78 | + string helpText = analysisResults?.FirstOrDefault()?.Correction?.Edits[0].Text; |
| 79 | + |
| 80 | + if (helpText == null) |
| 81 | + { |
| 82 | + return result; |
| 83 | + } |
| 84 | + |
| 85 | + result.Content = ScriptFile.GetLinesInternal(helpText).ToArray(); |
| 86 | + |
| 87 | + if (helpLocation != null && |
| 88 | + !helpLocation.Equals("before", StringComparison.OrdinalIgnoreCase)) |
| 89 | + { |
| 90 | + // we need to trim the leading `{` and newline when helpLocation=="begin" |
| 91 | + // we also need to trim the leading newline when helpLocation=="end" |
| 92 | + result.Content = result.Content.Skip(1).ToArray(); |
| 93 | + } |
| 94 | + |
| 95 | + return result; |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments