From 9c0b58ecd866fdc08174bc10f8459a7b35e906a5 Mon Sep 17 00:00:00 2001 From: "Patrick M. Meinecke" Date: Tue, 3 Oct 2017 19:49:05 -0400 Subject: [PATCH 1/2] Fix cluttered debug history This change fixes an issue where background commands would be added to PowerShell's history. This was due to a limitation in the PowerShell debugger where the only way commands are excluded is if the command is part of a hard coded set of commands. As a workaround, a command from that set (prompt) is added as the first statement. Resolves PowerShell/vscode-powershell#873 --- .../Session/PowerShell3Operations.cs | 4 ++- .../Session/PowerShell4Operations.cs | 27 +++++++++++++++++++ .../Session/PowerShellContext.cs | 4 ++- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/PowerShellEditorServices/Session/PowerShell3Operations.cs b/src/PowerShellEditorServices/Session/PowerShell3Operations.cs index 2199e1839..d2f99e287 100644 --- a/src/PowerShellEditorServices/Session/PowerShell3Operations.cs +++ b/src/PowerShellEditorServices/Session/PowerShell3Operations.cs @@ -33,8 +33,10 @@ public IEnumerable ExecuteCommandInDebugger( { IEnumerable executionResult = null; - using (var nestedPipeline = currentRunspace.CreateNestedPipeline()) + string historyString = psCommand.Commands[0].CommandText; + using (var nestedPipeline = currentRunspace.CreateNestedPipeline(historyString, sendOutputToHost)) { + nestedPipeline.Commands.Clear(); foreach (var command in psCommand.Commands) { nestedPipeline.Commands.Add(command); diff --git a/src/PowerShellEditorServices/Session/PowerShell4Operations.cs b/src/PowerShellEditorServices/Session/PowerShell4Operations.cs index ea4070225..61ca86aa8 100644 --- a/src/PowerShellEditorServices/Session/PowerShell4Operations.cs +++ b/src/PowerShellEditorServices/Session/PowerShell4Operations.cs @@ -13,6 +13,15 @@ namespace Microsoft.PowerShell.EditorServices.Session { internal class PowerShell4Operations : IVersionSpecificOperations { + private static SortedSet s_noHistoryCommandNames = new SortedSet(StringComparer.OrdinalIgnoreCase) + { + "prompt", + "Set-PSDebuggerAction", + "Get-PSDebuggerStopArgs", + "Set-PSDebugMode", + "TabExpansion2" + }; + public void ConfigureDebugger(Runspace runspace) { #if !PowerShellv3 @@ -54,6 +63,24 @@ public IEnumerable ExecuteCommandInDebugger( }; } + // There's no way to tell the debugger not to add the command to history. It does however, + // check if the first command is in a static list of commands that shouldn't be added + // to history. We use that here to get around that limitation. + if (!sendOutputToHost && !s_noHistoryCommandNames.Contains(psCommand.Commands[0].CommandText)) + { + var newCommand = new PSCommand() + .AddCommand("prompt") + .AddCommand("Microsoft.PowerShell.Core\\Out-Null") + .AddStatement(); + + foreach (Command command in psCommand.Commands) + { + newCommand.AddCommand(command); + } + + psCommand = newCommand; + } + DebuggerCommandResults commandResults = currentRunspace.Debugger.ProcessCommand( psCommand, diff --git a/src/PowerShellEditorServices/Session/PowerShellContext.cs b/src/PowerShellEditorServices/Session/PowerShellContext.cs index 0c6e1ee98..a157dcb4e 100644 --- a/src/PowerShellEditorServices/Session/PowerShellContext.cs +++ b/src/PowerShellEditorServices/Session/PowerShellContext.cs @@ -1490,10 +1490,12 @@ private SessionDetails GetSessionDetailsInRunspace(Runspace runspace) { powerShell.Runspace = runspace; powerShell.Commands = command; + var invocationSettings = new PSInvocationSettings(); + invocationSettings.AddToHistory = false; return powerShell - .Invoke() + .Invoke(null, invocationSettings) .FirstOrDefault(); } }); From 6955ec05957bae606e3c055614a2de36109d45fb Mon Sep 17 00:00:00 2001 From: "Patrick M. Meinecke" Date: Tue, 3 Oct 2017 20:06:14 -0400 Subject: [PATCH 2/2] Fix command history navigation while debugging This change fixes an issue where the debugger was unable to obtain command history. Resolves PowerShell/vscode-powershell#550 --- src/PowerShellEditorServices/Console/ConsoleReadLine.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/PowerShellEditorServices/Console/ConsoleReadLine.cs b/src/PowerShellEditorServices/Console/ConsoleReadLine.cs index d2e77c332..bbe91ea65 100644 --- a/src/PowerShellEditorServices/Console/ConsoleReadLine.cs +++ b/src/PowerShellEditorServices/Console/ConsoleReadLine.cs @@ -303,10 +303,11 @@ private async Task ReadLine(bool isCommandLine, CancellationToken cancel command.AddCommand("Get-History"); currentHistory = - await this.powerShellContext.ExecuteCommand( - command, - false, - false) as Collection; + new Collection( + (await this.powerShellContext.ExecuteCommand( + command, + false, + false)).ToList()); if (currentHistory != null) {