Skip to content

Fix help completion at the beginning of function body #477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1063,32 +1063,43 @@ protected async Task HandleCommentHelpRequest(
RequestContext<CommentHelpRequestResult> requestContext)
{
var scriptFile = this.editorSession.Workspace.GetFile(requestParams.DocumentUri);
var expectedFunctionLine = requestParams.TriggerPosition.Line + 2;
var triggerLine0b = requestParams.TriggerPosition.Line;
var triggerLine1b = triggerLine0b + 1;

string helpLocation;
var functionDefinitionAst = this.editorSession.LanguageService.GetFunctionDefinitionForHelpComment(
var functionDefinitionAst = editorSession.LanguageService.GetFunctionDefinitionForHelpComment(
scriptFile,
requestParams.TriggerPosition.Line + 1,
triggerLine1b,
out helpLocation);
var result = new CommentHelpRequestResult();
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.
var lines = ScriptFile.GetLines(funcText).ToArray();
var relativeTriggerLine0b = triggerLine1b - funcExtent.StartLineNumber;
if (relativeTriggerLine0b > 0 && lines[relativeTriggerLine0b].IndexOf("<") > -1)
{
lines[relativeTriggerLine0b] = string.Empty;
}

funcText = string.Join("\n", lines);
}

var analysisResults = await this.editorSession.AnalysisService.GetSemanticMarkersAsync(
functionDefinitionAst.Extent.Text,
funcText,
AnalysisService.GetCommentHelpRuleSettings(
true,
false,
requestParams.BlockComment,
true,
helpLocation));
result.Content = analysisResults?
.FirstOrDefault()?
.Correction?
.Edits[0]
.Text
.Split('\n')
.Select(x => x.Trim('\r'))
.ToArray();
var help = analysisResults?.FirstOrDefault()?.Correction?.Edits[0].Text;
result.Content = help == null ? null : ScriptFile.GetLines(help).ToArray();
if (helpLocation != null &&
!helpLocation.Equals("before", StringComparison.OrdinalIgnoreCase))
{
Expand Down
21 changes: 16 additions & 5 deletions src/PowerShellEditorServices/Workspace/ScriptFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,21 @@ public ScriptFile (

#region Public Methods

/// <summary>
/// Get the lines in a string.
/// </summary>
/// <param name="text">Input string to be split up into lines.</param>
/// <returns>The lines in the string.</returns>
public static IEnumerable<string> GetLines(string text)
{
if (text == null)
{
throw new ArgumentNullException(nameof(text));
}

return text.Split('\n').Select(line => line.TrimEnd('\r'));
}

/// <summary>
/// Gets a line from the file's contents.
/// </summary>
Expand Down Expand Up @@ -479,11 +494,7 @@ private void SetFileContents(string fileContents)
{
// Split the file contents into lines and trim
// any carriage returns from the strings.
this.FileLines =
fileContents
.Split('\n')
.Select(line => line.TrimEnd('\r'))
.ToList();
this.FileLines = GetLines(fileContents).ToList();

// Parse the contents to get syntax tree and errors
this.ParseFileContents();
Expand Down