|
16 | 16 | using System.IO; |
17 | 17 | using System.Linq; |
18 | 18 | using System.Management.Automation; |
| 19 | +using System.Management.Automation.Language; |
19 | 20 | using System.Text.RegularExpressions; |
20 | 21 | using System.Threading; |
21 | 22 | using System.Threading.Tasks; |
@@ -1069,56 +1070,64 @@ protected async Task HandleCommentHelpRequest( |
1069 | 1070 | CommentHelpRequestParams requestParams, |
1070 | 1071 | RequestContext<CommentHelpRequestResult> requestContext) |
1071 | 1072 | { |
1072 | | - var scriptFile = this.editorSession.Workspace.GetFile(requestParams.DocumentUri); |
1073 | | - var triggerLine0b = requestParams.TriggerPosition.Line; |
1074 | | - var triggerLine1b = triggerLine0b + 1; |
| 1073 | + ScriptFile scriptFile = this.editorSession.Workspace.GetFile(requestParams.DocumentUri); |
| 1074 | + int triggerLine = requestParams.TriggerPosition.Line + 1; |
1075 | 1075 |
|
1076 | 1076 | string helpLocation; |
1077 | | - var functionDefinitionAst = editorSession.LanguageService.GetFunctionDefinitionForHelpComment( |
| 1077 | + FunctionDefinitionAst functionDefinitionAst = editorSession.LanguageService.GetFunctionDefinitionForHelpComment( |
1078 | 1078 | scriptFile, |
1079 | | - triggerLine1b, |
| 1079 | + triggerLine, |
1080 | 1080 | out helpLocation); |
| 1081 | + |
1081 | 1082 | var result = new CommentHelpRequestResult(); |
1082 | | - IList<string> lines = null; |
1083 | | - if (functionDefinitionAst != null) |
1084 | | - { |
1085 | | - var funcExtent = functionDefinitionAst.Extent; |
1086 | | - var funcText = funcExtent.Text; |
1087 | | - if (helpLocation.Equals("begin")) |
1088 | | - { |
1089 | | - // check if the previous character is `<` because it invalidates |
1090 | | - // the param block the follows it. |
1091 | | - lines = ScriptFile.GetLines(funcText); |
1092 | | - var relativeTriggerLine0b = triggerLine1b - funcExtent.StartLineNumber; |
1093 | | - if (relativeTriggerLine0b > 0 && lines[relativeTriggerLine0b].IndexOf("<") > -1) |
1094 | | - { |
1095 | | - lines[relativeTriggerLine0b] = string.Empty; |
1096 | | - } |
1097 | 1083 |
|
1098 | | - funcText = string.Join("\n", lines); |
1099 | | - } |
| 1084 | + if (functionDefinitionAst == null) |
| 1085 | + { |
| 1086 | + await requestContext.SendResult(result); |
| 1087 | + return; |
| 1088 | + } |
1100 | 1089 |
|
1101 | | - var analysisResults = await this.editorSession.AnalysisService.GetSemanticMarkersAsync( |
1102 | | - funcText, |
1103 | | - AnalysisService.GetCommentHelpRuleSettings( |
1104 | | - true, |
1105 | | - false, |
1106 | | - requestParams.BlockComment, |
1107 | | - true, |
1108 | | - helpLocation)); |
1109 | | - |
1110 | | - var help = analysisResults?.FirstOrDefault()?.Correction?.Edits[0].Text; |
1111 | | - result.Content = help != null |
1112 | | - ? (lines ?? ScriptFile.GetLines(help)).ToArray() |
1113 | | - : null; |
1114 | | - |
1115 | | - if (helpLocation != null && |
1116 | | - !helpLocation.Equals("before", StringComparison.OrdinalIgnoreCase)) |
| 1090 | + IScriptExtent funcExtent = functionDefinitionAst.Extent; |
| 1091 | + string funcText = funcExtent.Text; |
| 1092 | + if (helpLocation.Equals("begin")) |
| 1093 | + { |
| 1094 | + // check if the previous character is `<` because it invalidates |
| 1095 | + // the param block the follows it. |
| 1096 | + IList<string> lines = ScriptFile.GetLines(funcText); |
| 1097 | + int relativeTriggerLine0b = triggerLine - funcExtent.StartLineNumber; |
| 1098 | + if (relativeTriggerLine0b > 0 && lines[relativeTriggerLine0b].IndexOf("<") > -1) |
1117 | 1099 | { |
1118 | | - // we need to trim the leading `{` and newline when helpLocation=="begin" |
1119 | | - // we also need to trim the leading newline when helpLocation=="end" |
1120 | | - result.Content = result.Content?.Skip(1).ToArray(); |
| 1100 | + lines[relativeTriggerLine0b] = string.Empty; |
1121 | 1101 | } |
| 1102 | + |
| 1103 | + funcText = string.Join("\n", lines); |
| 1104 | + } |
| 1105 | + |
| 1106 | + ScriptFileMarker[] analysisResults = await this.editorSession.AnalysisService.GetSemanticMarkersAsync( |
| 1107 | + funcText, |
| 1108 | + AnalysisService.GetCommentHelpRuleSettings( |
| 1109 | + enable: true, |
| 1110 | + exportedOnly: false, |
| 1111 | + blockComment: requestParams.BlockComment, |
| 1112 | + vscodeSnippetCorrection: true, |
| 1113 | + placement: helpLocation)); |
| 1114 | + |
| 1115 | + string helpText = analysisResults?.FirstOrDefault()?.Correction?.Edits[0].Text; |
| 1116 | + |
| 1117 | + if (helpText == null) |
| 1118 | + { |
| 1119 | + await requestContext.SendResult(result); |
| 1120 | + return; |
| 1121 | + } |
| 1122 | + |
| 1123 | + result.Content = ScriptFile.GetLines(helpText).ToArray(); |
| 1124 | + |
| 1125 | + if (helpLocation != null && |
| 1126 | + !helpLocation.Equals("before", StringComparison.OrdinalIgnoreCase)) |
| 1127 | + { |
| 1128 | + // we need to trim the leading `{` and newline when helpLocation=="begin" |
| 1129 | + // we also need to trim the leading newline when helpLocation=="end" |
| 1130 | + result.Content = result.Content.Skip(1).ToArray(); |
1122 | 1131 | } |
1123 | 1132 |
|
1124 | 1133 | await requestContext.SendResult(result); |
|
0 commit comments