diff --git a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs index b2257c3c2..2ecb0ac09 100644 --- a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs +++ b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs @@ -16,6 +16,7 @@ using System.IO; using System.Linq; using System.Management.Automation; +using System.Management.Automation.Language; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; @@ -1086,56 +1087,64 @@ protected async Task HandleCommentHelpRequest( CommentHelpRequestParams requestParams, RequestContext requestContext) { - var scriptFile = this.editorSession.Workspace.GetFile(requestParams.DocumentUri); - var triggerLine0b = requestParams.TriggerPosition.Line; - var triggerLine1b = triggerLine0b + 1; + ScriptFile scriptFile = this.editorSession.Workspace.GetFile(requestParams.DocumentUri); + int triggerLine = requestParams.TriggerPosition.Line + 1; string helpLocation; - var functionDefinitionAst = editorSession.LanguageService.GetFunctionDefinitionForHelpComment( + FunctionDefinitionAst functionDefinitionAst = editorSession.LanguageService.GetFunctionDefinitionForHelpComment( scriptFile, - triggerLine1b, + triggerLine, out helpLocation); + var result = new CommentHelpRequestResult(); - IList lines = null; - if (functionDefinitionAst != null) - { - var funcExtent = functionDefinitionAst.Extent; - var funcText = funcExtent.Text; - if (helpLocation.Equals("begin")) - { - // check if the previous character is `<` because it invalidates - // the param block the follows it. - lines = ScriptFile.GetLines(funcText); - var relativeTriggerLine0b = triggerLine1b - funcExtent.StartLineNumber; - if (relativeTriggerLine0b > 0 && lines[relativeTriggerLine0b].IndexOf("<") > -1) - { - lines[relativeTriggerLine0b] = string.Empty; - } - funcText = string.Join("\n", lines); - } + if (functionDefinitionAst == null) + { + await requestContext.SendResult(result); + return; + } - var analysisResults = await this.editorSession.AnalysisService.GetSemanticMarkersAsync( - funcText, - AnalysisService.GetCommentHelpRuleSettings( - true, - false, - requestParams.BlockComment, - true, - helpLocation)); - - var help = analysisResults?.FirstOrDefault()?.Correction?.Edits[0].Text; - result.Content = help != null - ? (lines ?? ScriptFile.GetLines(help)).ToArray() - : null; - - if (helpLocation != null && - !helpLocation.Equals("before", StringComparison.OrdinalIgnoreCase)) + IScriptExtent funcExtent = functionDefinitionAst.Extent; + string funcText = funcExtent.Text; + if (helpLocation.Equals("begin")) + { + // check if the previous character is `<` because it invalidates + // the param block the follows it. + IList lines = ScriptFile.GetLines(funcText); + int relativeTriggerLine0b = triggerLine - funcExtent.StartLineNumber; + if (relativeTriggerLine0b > 0 && lines[relativeTriggerLine0b].IndexOf("<") > -1) { - // we need to trim the leading `{` and newline when helpLocation=="begin" - // we also need to trim the leading newline when helpLocation=="end" - result.Content = result.Content?.Skip(1).ToArray(); + lines[relativeTriggerLine0b] = string.Empty; } + + funcText = string.Join("\n", lines); + } + + ScriptFileMarker[] analysisResults = await this.editorSession.AnalysisService.GetSemanticMarkersAsync( + funcText, + AnalysisService.GetCommentHelpRuleSettings( + enable: true, + exportedOnly: false, + blockComment: requestParams.BlockComment, + vscodeSnippetCorrection: true, + placement: helpLocation)); + + string helpText = analysisResults?.FirstOrDefault()?.Correction?.Edits[0].Text; + + if (helpText == null) + { + await requestContext.SendResult(result); + return; + } + + result.Content = ScriptFile.GetLines(helpText).ToArray(); + + if (helpLocation != null && + !helpLocation.Equals("before", StringComparison.OrdinalIgnoreCase)) + { + // we need to trim the leading `{` and newline when helpLocation=="begin" + // we also need to trim the leading newline when helpLocation=="end" + result.Content = result.Content.Skip(1).ToArray(); } await requestContext.SendResult(result);