Skip to content

Commit e813fbd

Browse files
Clear the terminal via the LSP (#1108)
* Clear the terminal via the LSP * use actual Clear-Host impl * add clear for only Windows
1 parent 3591ee1 commit e813fbd

File tree

6 files changed

+87
-3
lines changed

6 files changed

+87
-3
lines changed

module/PowerShellEditorServices/Commands/PowerShellEditorServices.Commands.psd1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ FunctionsToExport = @('Register-EditorCommand',
7878
'Join-ScriptExtent',
7979
'Test-ScriptExtent',
8080
'Open-EditorFile',
81-
'New-EditorFile')
81+
'New-EditorFile',
82+
'Clear-Host')
8283

8384
# 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.
8485
CmdletsToExport = @()
@@ -87,7 +88,7 @@ CmdletsToExport = @()
8788
VariablesToExport = @()
8889

8990
# 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.
90-
AliasesToExport = @('psedit')
91+
AliasesToExport = @('psedit', 'cls', 'clear')
9192

9293
# DSC resources to export from this module
9394
# DscResourcesToExport = @()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#
2+
# Copyright (c) Microsoft. All rights reserved.
3+
# Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
#
5+
6+
Microsoft.PowerShell.Management\Get-Item function:Clear-Host | Microsoft.PowerShell.Management\Set-Item function:__clearhost
7+
8+
function Clear-Host {
9+
[Alias('cls')]
10+
param()
11+
12+
__clearhost
13+
$psEditor.Window.Terminal.Clear()
14+
}
15+
16+
if (!$IsMacOS -or $IsLinux) {
17+
Set-Alias -Name clear -Value Clear-Host
18+
}

src/PowerShellEditorServices/Services/PowerShellContext/EditorOperationsService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,5 +179,10 @@ public async Task SetStatusBarMessageAsync(string message, int? timeout)
179179
Timeout = timeout
180180
});
181181
}
182+
183+
public void ClearTerminal()
184+
{
185+
_languageServer.SendNotification("editor/clearTerminal");
186+
}
182187
}
183188
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
namespace Microsoft.PowerShell.EditorServices.Services.PowerShellContext
7+
{
8+
/// <summary>
9+
/// Provides a PowerShell-facing API which allows scripts to
10+
/// interact with the editor's terminal.
11+
/// </summary>
12+
public class EditorTerminal
13+
{
14+
#region Private Fields
15+
16+
private readonly IEditorOperations editorOperations;
17+
18+
#endregion
19+
20+
#region Constructors
21+
22+
/// <summary>
23+
/// Creates a new instance of the EditorTerminal class.
24+
/// </summary>
25+
/// <param name="editorOperations">An IEditorOperations implementation which handles operations in the host editor.</param>
26+
internal EditorTerminal(IEditorOperations editorOperations)
27+
{
28+
this.editorOperations = editorOperations;
29+
}
30+
31+
#endregion
32+
33+
#region Public Methods
34+
35+
/// <summary>
36+
/// Triggers to the editor to clear the terminal.
37+
/// </summary>
38+
public void Clear()
39+
{
40+
this.editorOperations.ClearTerminal();
41+
}
42+
43+
#endregion
44+
}
45+
}

src/PowerShellEditorServices/Services/PowerShellContext/Extensions/EditorWindow.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@ public class EditorWindow
1313
{
1414
#region Private Fields
1515

16-
private IEditorOperations editorOperations;
16+
private readonly IEditorOperations editorOperations;
17+
18+
#endregion
19+
20+
#region Public Properties
21+
22+
/// <summary>
23+
/// Gets the terminal interface for the editor API.
24+
/// </summary>
25+
public EditorTerminal Terminal { get; private set; }
1726

1827
#endregion
1928

@@ -26,6 +35,7 @@ public class EditorWindow
2635
internal EditorWindow(IEditorOperations editorOperations)
2736
{
2837
this.editorOperations = editorOperations;
38+
this.Terminal = new EditorTerminal(editorOperations);
2939
}
3040

3141
#endregion

src/PowerShellEditorServices/Services/PowerShellContext/Extensions/IEditorOperations.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,10 @@ public interface IEditorOperations
124124
/// <param name="timeout">If non-null, a timeout in milliseconds for how long the message should remain visible.</param>
125125
/// <returns>A Task that can be tracked for completion.</returns>
126126
Task SetStatusBarMessageAsync(string message, int? timeout);
127+
128+
/// <summary>
129+
/// Triggers to the editor to clear the terminal.
130+
/// </summary>
131+
void ClearTerminal();
127132
}
128133
}

0 commit comments

Comments
 (0)