Skip to content

Commit a60a91e

Browse files
author
Kapil Borle
committed
Add method to detect comment help location in a function
1 parent 6cdd65b commit a60a91e

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/PowerShellEditorServices/Language/LanguageService.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,57 @@ public FunctionDefinitionAst GetFunctionDefinitionAtLine(
539539
return functionDefinitionAst as FunctionDefinitionAst;
540540
}
541541

542+
// todo add xml doc
543+
public FunctionDefinitionAst GetFunctionDefinitionForHelpComment(
544+
ScriptFile scriptFile,
545+
int lineNumber,
546+
out string helpLocation)
547+
{
548+
var foundAst = scriptFile.ScriptAst.FindAll(
549+
ast =>
550+
{
551+
// find all the script definitions that contain the line `lineNumber`
552+
var fdAst = ast as FunctionDefinitionAst;
553+
if (fdAst == null)
554+
{
555+
return false;
556+
}
557+
558+
return fdAst.Body.Extent.StartLineNumber < lineNumber &&
559+
fdAst.Body.Extent.EndLineNumber > lineNumber;
560+
},
561+
true).Aggregate((x, y) =>
562+
{
563+
// of all the function definitions found, return the innermost function definition that contains
564+
// `lineNumber`
565+
if (x.Extent.StartOffset >= y.Extent.StartOffset && x.Extent.EndOffset <= x.Extent.EndOffset)
566+
{
567+
return x;
568+
}
569+
570+
return y;
571+
});
572+
573+
// TODO use tokens to check for non empty character instead of just checking for line offset
574+
// check if the line number is the first line in the function body
575+
// check if the line number is the last line in the function body
576+
if (foundAst == null)
577+
{
578+
helpLocation = "before";
579+
return GetFunctionDefinitionAtLine(scriptFile, lineNumber + 1);
580+
}
581+
582+
// check if the next line contains a function definition
583+
var funcDefnAst = foundAst as FunctionDefinitionAst;
584+
if (funcDefnAst.Body.Extent.StartLineNumber == lineNumber - 1)
585+
{
586+
helpLocation = "begin";
587+
}
588+
589+
helpLocation = "end";
590+
return funcDefnAst;
591+
}
592+
542593
#endregion
543594

544595
#region Private Fields

0 commit comments

Comments
 (0)