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

Clear the terminal via the LSP #1108

Merged
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -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 = @()
Expand All @@ -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', 'clear')

# DSC resources to export from this module
# DscResourcesToExport = @()
Expand Down
18 changes: 18 additions & 0 deletions module/PowerShellEditorServices/Commands/Public/Clear-Host.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Copyright (c) Microsoft. All rights reserved.
# 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')]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also clear?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clear is not an alias it's an application on non-Windows. Is it an alias on Windows?

param()

__clearhost
$psEditor.Window.Terminal.Clear()
}

if (!$IsMacOS -or $IsLinux) {
Set-Alias -Name clear -Value Clear-Host
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,10 @@ public async Task SetStatusBarMessageAsync(string message, int? timeout)
Timeout = timeout
});
}

public void ClearTerminal()
{
_languageServer.SendNotification("editor/clearTerminal");
}
}
}
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Provides a PowerShell-facing API which allows scripts to
/// interact with the editor's terminal.
/// </summary>
public class EditorTerminal
{
#region Private Fields

private readonly IEditorOperations editorOperations;

#endregion

#region Constructors

/// <summary>
/// Creates a new instance of the EditorTerminal class.
/// </summary>
/// <param name="editorOperations">An IEditorOperations implementation which handles operations in the host editor.</param>
internal EditorTerminal(IEditorOperations editorOperations)
{
this.editorOperations = editorOperations;
}

#endregion

#region Public Methods

/// <summary>
/// Triggers to the editor to clear the terminal.
/// </summary>
public void Clear()
{
this.editorOperations.ClearTerminal();
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ public class EditorWindow
{
#region Private Fields

private IEditorOperations editorOperations;
private readonly IEditorOperations editorOperations;

#endregion

#region Public Properties

/// <summary>
/// Gets the terminal interface for the editor API.
/// </summary>
public EditorTerminal Terminal { get; private set; }

#endregion

Expand All @@ -26,6 +35,7 @@ public class EditorWindow
internal EditorWindow(IEditorOperations editorOperations)
{
this.editorOperations = editorOperations;
this.Terminal = new EditorTerminal(editorOperations);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,10 @@ public interface IEditorOperations
/// <param name="timeout">If non-null, a timeout in milliseconds for how long the message should remain visible.</param>
/// <returns>A Task that can be tracked for completion.</returns>
Task SetStatusBarMessageAsync(string message, int? timeout);

/// <summary>
/// Triggers to the editor to clear the terminal.
/// </summary>
void ClearTerminal();
}
}