From 51e972a60fd3df1758aad01061b7728dfbcd150a Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Mon, 18 Nov 2019 21:20:53 -0800 Subject: [PATCH 1/3] Clear the terminal via the LSP --- .../PowerShellEditorServices.Commands.psd1 | 5 ++- .../Commands/Public/Clear-Host.ps1 | 12 +++++ .../EditorOperationsService.cs | 5 +++ .../Extensions/EditorTerminal.cs | 45 +++++++++++++++++++ .../Extensions/EditorWindow.cs | 10 +++++ .../Extensions/IEditorOperations.cs | 5 +++ 6 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 module/PowerShellEditorServices/Commands/Public/Clear-Host.ps1 create mode 100644 src/PowerShellEditorServices/Services/PowerShellContext/Extensions/EditorTerminal.cs diff --git a/module/PowerShellEditorServices/Commands/PowerShellEditorServices.Commands.psd1 b/module/PowerShellEditorServices/Commands/PowerShellEditorServices.Commands.psd1 index da611b92f..cc32ef9fa 100644 --- a/module/PowerShellEditorServices/Commands/PowerShellEditorServices.Commands.psd1 +++ b/module/PowerShellEditorServices/Commands/PowerShellEditorServices.Commands.psd1 @@ -78,7 +78,8 @@ FunctionsToExport = @('Register-EditorCommand', 'Join-ScriptExtent', 'Test-ScriptExtent', 'Open-EditorFile', - 'New-EditorFile') + 'New-EditorFile', + 'Clear-Host') # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. CmdletsToExport = @() @@ -87,7 +88,7 @@ CmdletsToExport = @() VariablesToExport = @() # Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. -AliasesToExport = @('psedit') +AliasesToExport = @('psedit', 'cls') # DSC resources to export from this module # DscResourcesToExport = @() diff --git a/module/PowerShellEditorServices/Commands/Public/Clear-Host.ps1 b/module/PowerShellEditorServices/Commands/Public/Clear-Host.ps1 new file mode 100644 index 000000000..05be70ca4 --- /dev/null +++ b/module/PowerShellEditorServices/Commands/Public/Clear-Host.ps1 @@ -0,0 +1,12 @@ +# +# Copyright (c) Microsoft. All rights reserved. +# Licensed under the MIT license. See LICENSE file in the project root for full license information. +# + +function Clear-Host { + [Alias('cls')] + param() + + [System.Console]::Clear() + $psEditor.Window.Terminal.Clear() +} diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/EditorOperationsService.cs b/src/PowerShellEditorServices/Services/PowerShellContext/EditorOperationsService.cs index 1d6ddd247..eeac33c5c 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/EditorOperationsService.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/EditorOperationsService.cs @@ -179,5 +179,10 @@ public async Task SetStatusBarMessageAsync(string message, int? timeout) Timeout = timeout }); } + + public void ClearTerminal() + { + _languageServer.SendNotification("editor/clearTerminal"); + } } } diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/Extensions/EditorTerminal.cs b/src/PowerShellEditorServices/Services/PowerShellContext/Extensions/EditorTerminal.cs new file mode 100644 index 000000000..37ece1df7 --- /dev/null +++ b/src/PowerShellEditorServices/Services/PowerShellContext/Extensions/EditorTerminal.cs @@ -0,0 +1,45 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + +namespace Microsoft.PowerShell.EditorServices.Services.PowerShellContext +{ + /// + /// Provides a PowerShell-facing API which allows scripts to + /// interact with the editor's terminal. + /// + public class EditorTerminal + { + #region Private Fields + + private IEditorOperations editorOperations; + + #endregion + + #region Constructors + + /// + /// Creates a new instance of the EditorTerminal class. + /// + /// An IEditorOperations implementation which handles operations in the host editor. + internal EditorTerminal(IEditorOperations editorOperations) + { + this.editorOperations = editorOperations; + } + + #endregion + + #region Public Methods + + /// + /// Triggers to the editor to clear the terminal. + /// + public void Clear() + { + this.editorOperations.ClearTerminal(); + } + + #endregion + } +} diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/Extensions/EditorWindow.cs b/src/PowerShellEditorServices/Services/PowerShellContext/Extensions/EditorWindow.cs index f21a574ea..5a3d3f60d 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/Extensions/EditorWindow.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/Extensions/EditorWindow.cs @@ -17,6 +17,15 @@ public class EditorWindow #endregion + #region Public Properties + + /// + /// Gets the terminal interface for the editor API. + /// + public EditorTerminal Terminal { get; private set; } + + #endregion + #region Constructors /// @@ -26,6 +35,7 @@ public class EditorWindow internal EditorWindow(IEditorOperations editorOperations) { this.editorOperations = editorOperations; + this.Terminal = new EditorTerminal(editorOperations); } #endregion diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/Extensions/IEditorOperations.cs b/src/PowerShellEditorServices/Services/PowerShellContext/Extensions/IEditorOperations.cs index 15a167203..ccc41e949 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/Extensions/IEditorOperations.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/Extensions/IEditorOperations.cs @@ -124,5 +124,10 @@ public interface IEditorOperations /// If non-null, a timeout in milliseconds for how long the message should remain visible. /// A Task that can be tracked for completion. Task SetStatusBarMessageAsync(string message, int? timeout); + + /// + /// Triggers to the editor to clear the terminal. + /// + void ClearTerminal(); } } From 319fe6289b0f0ed4f63661b033f89f4cf30840c7 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Mon, 18 Nov 2019 21:55:28 -0800 Subject: [PATCH 2/3] use actual Clear-Host impl --- .../PowerShellEditorServices/Commands/Public/Clear-Host.ps1 | 4 +++- .../Services/PowerShellContext/Extensions/EditorTerminal.cs | 2 +- .../Services/PowerShellContext/Extensions/EditorWindow.cs | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/module/PowerShellEditorServices/Commands/Public/Clear-Host.ps1 b/module/PowerShellEditorServices/Commands/Public/Clear-Host.ps1 index 05be70ca4..dbbcb387f 100644 --- a/module/PowerShellEditorServices/Commands/Public/Clear-Host.ps1 +++ b/module/PowerShellEditorServices/Commands/Public/Clear-Host.ps1 @@ -3,10 +3,12 @@ # Licensed under the MIT license. See LICENSE file in the project root for full license information. # +Microsoft.PowerShell.Management\Get-Item function:Clear-Host | Microsoft.PowerShell.Management\Set-Item function:__clearhost + function Clear-Host { [Alias('cls')] param() - [System.Console]::Clear() + __clearhost $psEditor.Window.Terminal.Clear() } diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/Extensions/EditorTerminal.cs b/src/PowerShellEditorServices/Services/PowerShellContext/Extensions/EditorTerminal.cs index 37ece1df7..a6fb52ed2 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/Extensions/EditorTerminal.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/Extensions/EditorTerminal.cs @@ -13,7 +13,7 @@ public class EditorTerminal { #region Private Fields - private IEditorOperations editorOperations; + private readonly IEditorOperations editorOperations; #endregion diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/Extensions/EditorWindow.cs b/src/PowerShellEditorServices/Services/PowerShellContext/Extensions/EditorWindow.cs index 5a3d3f60d..cf580b961 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/Extensions/EditorWindow.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/Extensions/EditorWindow.cs @@ -13,7 +13,7 @@ public class EditorWindow { #region Private Fields - private IEditorOperations editorOperations; + private readonly IEditorOperations editorOperations; #endregion From 24a0aaa96046c3fc906a8fdef180e29a3e2a1044 Mon Sep 17 00:00:00 2001 From: Tyler Leonhardt Date: Tue, 26 Nov 2019 10:35:18 -0800 Subject: [PATCH 3/3] add clear for only Windows --- .../Commands/PowerShellEditorServices.Commands.psd1 | 2 +- .../PowerShellEditorServices/Commands/Public/Clear-Host.ps1 | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/module/PowerShellEditorServices/Commands/PowerShellEditorServices.Commands.psd1 b/module/PowerShellEditorServices/Commands/PowerShellEditorServices.Commands.psd1 index cc32ef9fa..f46db0773 100644 --- a/module/PowerShellEditorServices/Commands/PowerShellEditorServices.Commands.psd1 +++ b/module/PowerShellEditorServices/Commands/PowerShellEditorServices.Commands.psd1 @@ -88,7 +88,7 @@ CmdletsToExport = @() VariablesToExport = @() # Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. -AliasesToExport = @('psedit', 'cls') +AliasesToExport = @('psedit', 'cls', 'clear') # DSC resources to export from this module # DscResourcesToExport = @() diff --git a/module/PowerShellEditorServices/Commands/Public/Clear-Host.ps1 b/module/PowerShellEditorServices/Commands/Public/Clear-Host.ps1 index dbbcb387f..4aa646942 100644 --- a/module/PowerShellEditorServices/Commands/Public/Clear-Host.ps1 +++ b/module/PowerShellEditorServices/Commands/Public/Clear-Host.ps1 @@ -12,3 +12,7 @@ function Clear-Host { __clearhost $psEditor.Window.Terminal.Clear() } + +if (!$IsMacOS -or $IsLinux) { + Set-Alias -Name clear -Value Clear-Host +}