From 5a4065689f755a70d48360edba72e59a25d28eea Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Thu, 31 Oct 2019 08:35:09 -0400 Subject: [PATCH 1/3] Remove extra newline in GetComment feature --- .../Handlers/GetCommentHelpHandler.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/Handlers/GetCommentHelpHandler.cs b/src/PowerShellEditorServices/Services/PowerShellContext/Handlers/GetCommentHelpHandler.cs index 1f79582a1..8418ae26e 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/Handlers/GetCommentHelpHandler.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/Handlers/GetCommentHelpHandler.cs @@ -5,7 +5,6 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Management.Automation.Language; using System.Threading; using System.Threading.Tasks; @@ -80,23 +79,26 @@ public async Task Handle(CommentHelpRequestParams requ vscodeSnippetCorrection: true, placement: helpLocation)); - string helpText = analysisResults?.FirstOrDefault()?.Correction?.Edits[0].Text; + string helpText = analysisResults?[0]?.Correction?.Edits[0].Text; if (helpText == null) { return result; } - result.Content = ScriptFile.GetLinesInternal(helpText).ToArray(); + List helpLines = ScriptFile.GetLinesInternal(helpText); 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(); + helpLines = helpLines.GetRange(1, helpLines.Count - 1); } + // Trim trailing newline from help text. + result.Content = helpLines.GetRange(0, helpLines.Count - 1).ToArray(); + return result; } } From 1b778dd1c5e997479aed1fcf9bb5bac94535ccfd Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Thu, 31 Oct 2019 08:39:56 -0400 Subject: [PATCH 2/3] future proof if PSSA removes it --- .../PowerShellContext/Handlers/GetCommentHelpHandler.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/Handlers/GetCommentHelpHandler.cs b/src/PowerShellEditorServices/Services/PowerShellContext/Handlers/GetCommentHelpHandler.cs index 8418ae26e..1213d373f 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/Handlers/GetCommentHelpHandler.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/Handlers/GetCommentHelpHandler.cs @@ -97,7 +97,10 @@ public async Task Handle(CommentHelpRequestParams requ } // Trim trailing newline from help text. - result.Content = helpLines.GetRange(0, helpLines.Count - 1).ToArray(); + if (string.IsNullOrEmpty(helpLines[helpLines.Count - 1])) + { + result.Content = helpLines.GetRange(0, helpLines.Count - 1).ToArray(); + } return result; } From db2aae644a290e1067b63f5907aa2fbf35d710d3 Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Thu, 31 Oct 2019 11:30:48 -0700 Subject: [PATCH 3/3] Add suggested changes --- .../Handlers/GetCommentHelpHandler.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/Handlers/GetCommentHelpHandler.cs b/src/PowerShellEditorServices/Services/PowerShellContext/Handlers/GetCommentHelpHandler.cs index 1213d373f..13f4deac0 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/Handlers/GetCommentHelpHandler.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/Handlers/GetCommentHelpHandler.cs @@ -79,7 +79,12 @@ public async Task Handle(CommentHelpRequestParams requ vscodeSnippetCorrection: true, placement: helpLocation)); - string helpText = analysisResults?[0]?.Correction?.Edits[0].Text; + if (analysisResults == null || analysisResults.Count == 0) + { + return result; + } + + string helpText = analysisResults[0]?.Correction?.Edits[0].Text; if (helpText == null) { @@ -92,16 +97,19 @@ public async Task Handle(CommentHelpRequestParams requ !helpLocation.Equals("before", StringComparison.OrdinalIgnoreCase)) { // we need to trim the leading `{` and newline when helpLocation=="begin" + helpLines.RemoveAt(helpLines.Count - 1); + // we also need to trim the leading newline when helpLocation=="end" - helpLines = helpLines.GetRange(1, helpLines.Count - 1); + helpLines.RemoveAt(0); } // Trim trailing newline from help text. if (string.IsNullOrEmpty(helpLines[helpLines.Count - 1])) { - result.Content = helpLines.GetRange(0, helpLines.Count - 1).ToArray(); + helpLines.RemoveAt(helpLines.Count - 1); } + result.Content = helpLines.ToArray(); return result; } }