Skip to content
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

Fix inner help completion #740

Merged
merged 3 commits into from
Sep 17, 2018
Merged
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
91 changes: 50 additions & 41 deletions src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Language;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -1086,56 +1087,64 @@ protected async Task HandleCommentHelpRequest(
CommentHelpRequestParams requestParams,
RequestContext<CommentHelpRequestResult> requestContext)
{
var scriptFile = this.editorSession.Workspace.GetFile(requestParams.DocumentUri);
var triggerLine0b = requestParams.TriggerPosition.Line;
var triggerLine1b = triggerLine0b + 1;
ScriptFile scriptFile = this.editorSession.Workspace.GetFile(requestParams.DocumentUri);
int triggerLine = requestParams.TriggerPosition.Line + 1;

string helpLocation;
var functionDefinitionAst = editorSession.LanguageService.GetFunctionDefinitionForHelpComment(
FunctionDefinitionAst functionDefinitionAst = editorSession.LanguageService.GetFunctionDefinitionForHelpComment(
scriptFile,
triggerLine1b,
triggerLine,
out helpLocation);

var result = new CommentHelpRequestResult();
IList<string> lines = null;
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.
lines = ScriptFile.GetLines(funcText);
var relativeTriggerLine0b = triggerLine1b - funcExtent.StartLineNumber;
if (relativeTriggerLine0b > 0 && lines[relativeTriggerLine0b].IndexOf("<") > -1)
{
lines[relativeTriggerLine0b] = string.Empty;
}

funcText = string.Join("\n", lines);
}
if (functionDefinitionAst == null)
{
await requestContext.SendResult(result);
return;
}

var analysisResults = await this.editorSession.AnalysisService.GetSemanticMarkersAsync(
funcText,
AnalysisService.GetCommentHelpRuleSettings(
true,
false,
requestParams.BlockComment,
true,
helpLocation));

var help = analysisResults?.FirstOrDefault()?.Correction?.Edits[0].Text;
result.Content = help != null
? (lines ?? ScriptFile.GetLines(help)).ToArray()
: null;

if (helpLocation != null &&
!helpLocation.Equals("before", StringComparison.OrdinalIgnoreCase))
IScriptExtent funcExtent = functionDefinitionAst.Extent;
string funcText = funcExtent.Text;
if (helpLocation.Equals("begin"))
{
// check if the previous character is `<` because it invalidates
// the param block the follows it.
IList<string> lines = ScriptFile.GetLines(funcText);
int relativeTriggerLine0b = triggerLine - funcExtent.StartLineNumber;
if (relativeTriggerLine0b > 0 && lines[relativeTriggerLine0b].IndexOf("<") > -1)
{
// we need to trim the leading `{` and newline when helpLocation=="begin"
// we also need to trim the leading newline when helpLocation=="end"
result.Content = result.Content?.Skip(1).ToArray();
lines[relativeTriggerLine0b] = string.Empty;
}

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

ScriptFileMarker[] analysisResults = await this.editorSession.AnalysisService.GetSemanticMarkersAsync(
funcText,
AnalysisService.GetCommentHelpRuleSettings(
enable: true,
exportedOnly: false,
blockComment: requestParams.BlockComment,
vscodeSnippetCorrection: true,
placement: helpLocation));

string helpText = analysisResults?.FirstOrDefault()?.Correction?.Edits[0].Text;

if (helpText == null)
{
await requestContext.SendResult(result);
return;
}

result.Content = ScriptFile.GetLines(helpText).ToArray();

if (helpLocation != null &&
!helpLocation.Equals("before", StringComparison.OrdinalIgnoreCase))
{
// we need to trim the leading `{` and newline when helpLocation=="begin"
// we also need to trim the leading newline when helpLocation=="end"
result.Content = result.Content.Skip(1).ToArray();
}

await requestContext.SendResult(result);
Expand Down