From e875dfed2309d8353ffa176b56187bd71ccc56c9 Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Tue, 4 Sep 2018 12:13:29 +1000 Subject: [PATCH 1/3] Fix inner help completion --- src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs index b2257c3c2..e5adb5dc5 100644 --- a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs +++ b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs @@ -13,6 +13,7 @@ using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; using System.Management.Automation; @@ -1126,7 +1127,7 @@ protected async Task HandleCommentHelpRequest( var help = analysisResults?.FirstOrDefault()?.Correction?.Edits[0].Text; result.Content = help != null - ? (lines ?? ScriptFile.GetLines(help)).ToArray() + ? ScriptFile.GetLines(help).ToArray() : null; if (helpLocation != null && From 43c190d8c3b9d7dbe4de491f877dfde11ca8ac79 Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Tue, 4 Sep 2018 12:30:29 +1000 Subject: [PATCH 2/3] [Style] Improve style of comment help request handler --- .../Server/LanguageServer.cs | 91 ++++++++++--------- 1 file changed, 50 insertions(+), 41 deletions(-) diff --git a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs index e5adb5dc5..7de35eed1 100644 --- a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs +++ b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs @@ -17,6 +17,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; @@ -1087,56 +1088,64 @@ protected async Task HandleCommentHelpRequest( CommentHelpRequestParams requestParams, RequestContext 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 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 - ? 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 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); From a9622ba70019c35d489da62756e7b0ae058e63cb Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Tue, 4 Sep 2018 12:35:02 +1000 Subject: [PATCH 3/3] Remove unneeded using --- src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs index 7de35eed1..2ecb0ac09 100644 --- a/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs +++ b/src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs @@ -13,7 +13,6 @@ using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; -using System.Diagnostics; using System.IO; using System.Linq; using System.Management.Automation;