Skip to content

Commit 77eab6d

Browse files
author
Kapil Borle
committed
Auto-complete help in function body
1 parent a60a91e commit 77eab6d

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

+16-14
Original file line numberDiff line numberDiff line change
@@ -1082,31 +1082,33 @@ protected async Task HandleCommentHelpRequest(
10821082
{
10831083
var scriptFile = EditorSession.Workspace.GetFile(requestParams.DocumentUri);
10841084
var expectedFunctionLine = requestParams.TriggerPosition.Line + 2;
1085-
var functionDefinitionAst = EditorSession.LanguageService.GetFunctionDefinitionAtLine(
1085+
string helpLocation;
1086+
var functionDefinitionAst = EditorSession.LanguageService.GetFunctionDefinitionForHelpComment(
10861087
scriptFile,
1087-
expectedFunctionLine);
1088-
var result = new CommentHelpRequestResult();
1088+
requestParams.TriggerPosition.Line + 1,
1089+
out helpLocation);
10891090

1091+
var result = new CommentHelpRequestResult();
10901092
if (functionDefinitionAst != null)
10911093
{
1092-
// todo create a semantic marker api that take only string
1094+
// todo create a semantic marker api that take only string
10931095
var analysisResults = await EditorSession.AnalysisService.GetSemanticMarkersAsync(
10941096
scriptFile,
10951097
AnalysisService.GetCommentHelpRuleSettings(
10961098
true,
10971099
false,
10981100
requestParams.BlockComment,
10991101
true,
1100-
"before"));
1101-
1102-
var analysisResult = analysisResults?.FirstOrDefault(x =>
1103-
{
1104-
return x.Correction != null
1105-
&& x.Correction.Edits[0].StartLineNumber == expectedFunctionLine;
1106-
});
1107-
1108-
// find the analysis result whose correction starts on
1109-
result.Content = analysisResult?.Correction.Edits[0].Text.Split('\n').Select(x => x.Trim('\r')).ToArray();
1102+
helpLocation));
1103+
1104+
result.Content = analysisResults?
1105+
.FirstOrDefault()?
1106+
.Correction?
1107+
.Edits[0]
1108+
.Text
1109+
.Split('\n')
1110+
.Select(x => x.Trim('\r'))
1111+
.ToArray();
11101112
}
11111113

11121114
await requestContext.SendResult(result);

src/PowerShellEditorServices/Language/LanguageService.cs

+20-11
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ public FunctionDefinitionAst GetFunctionDefinitionForHelpComment(
545545
int lineNumber,
546546
out string helpLocation)
547547
{
548-
var foundAst = scriptFile.ScriptAst.FindAll(
548+
var foundAsts = scriptFile.ScriptAst.FindAll(
549549
ast =>
550550
{
551551
// find all the script definitions that contain the line `lineNumber`
@@ -558,7 +558,16 @@ public FunctionDefinitionAst GetFunctionDefinitionForHelpComment(
558558
return fdAst.Body.Extent.StartLineNumber < lineNumber &&
559559
fdAst.Body.Extent.EndLineNumber > lineNumber;
560560
},
561-
true).Aggregate((x, y) =>
561+
true);
562+
563+
// check if the next line contains a function definition
564+
if (foundAsts == null || !foundAsts.Any())
565+
{
566+
helpLocation = "before";
567+
return GetFunctionDefinitionAtLine(scriptFile, lineNumber + 1);
568+
}
569+
570+
var funcDefnAst = foundAsts.Cast<FunctionDefinitionAst>().Aggregate((x, y) =>
562571
{
563572
// of all the function definitions found, return the innermost function definition that contains
564573
// `lineNumber`
@@ -570,24 +579,24 @@ public FunctionDefinitionAst GetFunctionDefinitionForHelpComment(
570579
return y;
571580
});
572581

582+
// TODO fix help completion in nested functions
573583
// TODO use tokens to check for non empty character instead of just checking for line offset
574584
// check if the line number is the first line in the function body
575585
// check if the line number is the last line in the function body
576-
if (foundAst == null)
586+
if (funcDefnAst.Body.Extent.StartLineNumber == lineNumber - 1)
577587
{
578-
helpLocation = "before";
579-
return GetFunctionDefinitionAtLine(scriptFile, lineNumber + 1);
588+
helpLocation = "begin";
589+
return funcDefnAst;
580590
}
581591

582-
// check if the next line contains a function definition
583-
var funcDefnAst = foundAst as FunctionDefinitionAst;
584-
if (funcDefnAst.Body.Extent.StartLineNumber == lineNumber - 1)
592+
if (funcDefnAst.Body.Extent.EndLineNumber == lineNumber + 1)
585593
{
586-
helpLocation = "begin";
594+
helpLocation = "end";
595+
return funcDefnAst;
587596
}
588597

589-
helpLocation = "end";
590-
return funcDefnAst;
598+
helpLocation = null;
599+
return null;
591600
}
592601

593602
#endregion

0 commit comments

Comments
 (0)