Skip to content

Commit 04efac5

Browse files
authored
Fix inner help completion (#740)
* Fix inner help completion * [Style] Improve style of comment help request handler
1 parent 9650998 commit 04efac5

File tree

1 file changed

+50
-41
lines changed

1 file changed

+50
-41
lines changed

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

+50-41
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.IO;
1717
using System.Linq;
1818
using System.Management.Automation;
19+
using System.Management.Automation.Language;
1920
using System.Text.RegularExpressions;
2021
using System.Threading;
2122
using System.Threading.Tasks;
@@ -1069,56 +1070,64 @@ protected async Task HandleCommentHelpRequest(
10691070
CommentHelpRequestParams requestParams,
10701071
RequestContext<CommentHelpRequestResult> requestContext)
10711072
{
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;
10751075

10761076
string helpLocation;
1077-
var functionDefinitionAst = editorSession.LanguageService.GetFunctionDefinitionForHelpComment(
1077+
FunctionDefinitionAst functionDefinitionAst = editorSession.LanguageService.GetFunctionDefinitionForHelpComment(
10781078
scriptFile,
1079-
triggerLine1b,
1079+
triggerLine,
10801080
out helpLocation);
1081+
10811082
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-
}
10971083

1098-
funcText = string.Join("\n", lines);
1099-
}
1084+
if (functionDefinitionAst == null)
1085+
{
1086+
await requestContext.SendResult(result);
1087+
return;
1088+
}
11001089

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)
11171099
{
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;
11211101
}
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();
11221131
}
11231132

11241133
await requestContext.SendResult(result);

0 commit comments

Comments
 (0)