Skip to content

Use public InternalHost from origin runspace #874

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

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -109,7 +109,8 @@ function Start-EditorServicesHost {
$EnableConsoleRepl.IsPresent,
$WaitForDebugger.IsPresent,
$AdditionalModules,
$FeatureFlags)
$FeatureFlags,
$Host)

# Build the profile paths using the root paths of the current $profile variable
$profilePaths =
Expand Down
57 changes: 51 additions & 6 deletions src/PowerShellEditorServices.Host/EditorServicesHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Management.Automation.Host;
using System.Reflection;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Threading.Tasks;

namespace Microsoft.PowerShell.EditorServices.Host
{
Expand Down Expand Up @@ -61,6 +64,7 @@ public class EditorServicesHost
{
#region Private Fields

private readonly PSHost internalHost;
private string[] additionalModules;
private string bundledModulesPath;
private DebugAdapter debugAdapter;
Expand Down Expand Up @@ -93,33 +97,66 @@ public class EditorServicesHost
/// <param name="hostDetails">The details of the host which is launching PowerShell Editor Services.</param>
/// <param name="bundledModulesPath">Provides a path to PowerShell modules bundled with the host, if any. Null otherwise.</param>
/// <param name="waitForDebugger">If true, causes the host to wait for the debugger to attach before proceeding.</param>
/// <param name="additionalModules">Modules to be loaded when initializing the new runspace.</param>
/// <param name="featureFlags">Features to enable for this instance.</param>
public EditorServicesHost(
HostDetails hostDetails,
string bundledModulesPath,
bool enableConsoleRepl,
bool waitForDebugger,
string[] additionalModules,
string[] featureFlags)
: this(
hostDetails,
bundledModulesPath,
enableConsoleRepl,
waitForDebugger,
additionalModules,
featureFlags,
GetInternalHostFromDefaultRunspace())
{
}

/// <summary>
/// Initializes a new instance of the EditorServicesHost class and waits for
/// the debugger to attach if waitForDebugger is true.
/// </summary>
/// <param name="hostDetails">The details of the host which is launching PowerShell Editor Services.</param>
/// <param name="bundledModulesPath">Provides a path to PowerShell modules bundled with the host, if any. Null otherwise.</param>
/// <param name="waitForDebugger">If true, causes the host to wait for the debugger to attach before proceeding.</param>
/// <param name="additionalModules">Modules to be loaded when initializing the new runspace.</param>
/// <param name="featureFlags">Features to enable for this instance.</param>
/// <param name="internalHost">The value of the $Host variable in the original runspace.</param>
public EditorServicesHost(
HostDetails hostDetails,
string bundledModulesPath,
bool enableConsoleRepl,
bool waitForDebugger,
string[] additionalModules,
string[] featureFlags,
PSHost internalHost)
{
Validate.IsNotNull(nameof(hostDetails), hostDetails);
Validate.IsNotNull(nameof(internalHost), internalHost);

this.hostDetails = hostDetails;
this.enableConsoleRepl = enableConsoleRepl;
this.bundledModulesPath = bundledModulesPath;
this.additionalModules = additionalModules ?? new string[0];
this.featureFlags = new HashSet<string>(featureFlags ?? new string[0]);
this.serverCompletedTask = new TaskCompletionSource<bool>();
this.internalHost = internalHost;

#if DEBUG
if (waitForDebugger)
{
if (Debugger.IsAttached)
if (System.Diagnostics.Debugger.IsAttached)
{
Debugger.Break();
System.Diagnostics.Debugger.Break();
}
else
{
Debugger.Launch();
System.Diagnostics.Debugger.Launch();
}
}
#endif
Expand Down Expand Up @@ -365,6 +402,14 @@ public void WaitForCompletion()

#region Private Methods

private static PSHost GetInternalHostFromDefaultRunspace()
{
using (var pwsh = System.Management.Automation.PowerShell.Create(RunspaceMode.CurrentRunspace))
{
return pwsh.AddScript("$Host").Invoke<PSHost>().First();
}
}

private EditorSession CreateSession(
HostDetails hostDetails,
ProfilePaths profilePaths,
Expand All @@ -377,7 +422,7 @@ private EditorSession CreateSession(

EditorServicesPSHostUserInterface hostUserInterface =
enableConsoleRepl
? (EditorServicesPSHostUserInterface) new TerminalPSHostUserInterface(powerShellContext, this.logger)
? (EditorServicesPSHostUserInterface) new TerminalPSHostUserInterface(powerShellContext, this.logger, this.internalHost)
: new ProtocolPSHostUserInterface(powerShellContext, messageSender, this.logger);

EditorServicesPSHost psHost =
Expand Down Expand Up @@ -419,7 +464,7 @@ private EditorSession CreateDebugSession(

EditorServicesPSHostUserInterface hostUserInterface =
enableConsoleRepl
? (EditorServicesPSHostUserInterface) new TerminalPSHostUserInterface(powerShellContext, this.logger)
? (EditorServicesPSHostUserInterface) new TerminalPSHostUserInterface(powerShellContext, this.logger, this.internalHost)
: new ProtocolPSHostUserInterface(powerShellContext, messageSender, this.logger);

EditorServicesPSHost psHost =
Expand Down
110 changes: 108 additions & 2 deletions src/PowerShellEditorServices/Console/ConsoleProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,136 @@ static ConsoleProxy()
s_consoleProxy = new UnixConsoleOperations();
}

public static Task<ConsoleKeyInfo> ReadKeyAsync(CancellationToken cancellationToken) =>
s_consoleProxy.ReadKeyAsync(cancellationToken);
/// <summary>
/// Obtains the next character or function key pressed by the user asynchronously.
/// Does not block when other console API's are called.
/// </summary>
/// <param name="intercept">
/// Determines whether to display the pressed key in the console window. <see langword="true" />
/// to not display the pressed key; otherwise, <see langword="false" />.
/// </param>
/// <param name="cancellationToken">The CancellationToken to observe.</param>
/// <returns>
/// An object that describes the <see cref="ConsoleKey" /> constant and Unicode character, if any,
/// that correspond to the pressed console key. The <see cref="ConsoleKeyInfo" /> object also
/// describes, in a bitwise combination of <see cref="ConsoleModifiers" /> values, whether
/// one or more Shift, Alt, or Ctrl modifier keys was pressed simultaneously with the console key.
/// </returns>
public static ConsoleKeyInfo ReadKey(bool intercept, CancellationToken cancellationToken) =>
s_consoleProxy.ReadKey(intercept, cancellationToken);

/// <summary>
/// Obtains the next character or function key pressed by the user asynchronously.
/// Does not block when other console API's are called.
/// </summary>
/// <param name="intercept">
/// Determines whether to display the pressed key in the console window. <see langword="true" />
/// to not display the pressed key; otherwise, <see langword="false" />.
/// </param>
/// <param name="cancellationToken">The CancellationToken to observe.</param>
/// <returns>
/// A task that will complete with a result of the key pressed by the user.
/// </returns>
public static Task<ConsoleKeyInfo> ReadKeyAsync(bool intercept, CancellationToken cancellationToken) =>
s_consoleProxy.ReadKeyAsync(intercept, cancellationToken);

/// <summary>
/// Obtains the horizontal position of the console cursor. Use this method
/// instead of <see cref="System.Console.CursorLeft" /> to avoid triggering
/// pending calls to <see cref="IConsoleOperations.ReadKeyAsync(bool, CancellationToken)" />
/// on Unix platforms.
/// </summary>
/// <returns>The horizontal position of the console cursor.</returns>
public static int GetCursorLeft() =>
s_consoleProxy.GetCursorLeft();

/// <summary>
/// Obtains the horizontal position of the console cursor. Use this method
/// instead of <see cref="System.Console.CursorLeft" /> to avoid triggering
/// pending calls to <see cref="IConsoleOperations.ReadKeyAsync(bool, CancellationToken)" />
/// on Unix platforms.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken" /> to observe.</param>
/// <returns>The horizontal position of the console cursor.</returns>
public static int GetCursorLeft(CancellationToken cancellationToken) =>
s_consoleProxy.GetCursorLeft(cancellationToken);

/// <summary>
/// Obtains the horizontal position of the console cursor. Use this method
/// instead of <see cref="System.Console.CursorLeft" /> to avoid triggering
/// pending calls to <see cref="IConsoleOperations.ReadKeyAsync(bool, CancellationToken)" />
/// on Unix platforms.
/// </summary>
/// <returns>
/// A <see cref="Task{T}" /> representing the asynchronous operation. The
/// <see cref="Task{T}.Result" /> property will return the horizontal position
/// of the console cursor.
/// </returns>
public static Task<int> GetCursorLeftAsync() =>
s_consoleProxy.GetCursorLeftAsync();

/// <summary>
/// Obtains the horizontal position of the console cursor. Use this method
/// instead of <see cref="System.Console.CursorLeft" /> to avoid triggering
/// pending calls to <see cref="IConsoleOperations.ReadKeyAsync(bool, CancellationToken)" />
/// on Unix platforms.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken" /> to observe.</param>
/// <returns>
/// A <see cref="Task{T}" /> representing the asynchronous operation. The
/// <see cref="Task{T}.Result" /> property will return the horizontal position
/// of the console cursor.
/// </returns>
public static Task<int> GetCursorLeftAsync(CancellationToken cancellationToken) =>
s_consoleProxy.GetCursorLeftAsync(cancellationToken);

/// <summary>
/// Obtains the vertical position of the console cursor. Use this method
/// instead of <see cref="System.Console.CursorTop" /> to avoid triggering
/// pending calls to <see cref="IConsoleOperations.ReadKeyAsync(bool, CancellationToken)" />
/// on Unix platforms.
/// </summary>
/// <returns>The vertical position of the console cursor.</returns>
public static int GetCursorTop() =>
s_consoleProxy.GetCursorTop();

/// <summary>
/// Obtains the vertical position of the console cursor. Use this method
/// instead of <see cref="System.Console.CursorTop" /> to avoid triggering
/// pending calls to <see cref="IConsoleOperations.ReadKeyAsync(bool, CancellationToken)" />
/// on Unix platforms.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken" /> to observe.</param>
/// <returns>The vertical position of the console cursor.</returns>
public static int GetCursorTop(CancellationToken cancellationToken) =>
s_consoleProxy.GetCursorTop(cancellationToken);

/// <summary>
/// Obtains the vertical position of the console cursor. Use this method
/// instead of <see cref="System.Console.CursorTop" /> to avoid triggering
/// pending calls to <see cref="IConsoleOperations.ReadKeyAsync(bool, CancellationToken)" />
/// on Unix platforms.
/// </summary>
/// <returns>
/// A <see cref="Task{T}" /> representing the asynchronous operation. The
/// <see cref="Task{T}.Result" /> property will return the vertical position
/// of the console cursor.
/// </returns>
public static Task<int> GetCursorTopAsync() =>
s_consoleProxy.GetCursorTopAsync();

/// <summary>
/// Obtains the vertical position of the console cursor. Use this method
/// instead of <see cref="System.Console.CursorTop" /> to avoid triggering
/// pending calls to <see cref="IConsoleOperations.ReadKeyAsync(bool, CancellationToken)" />
/// on Unix platforms.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken" /> to observe.</param>
/// <returns>
/// A <see cref="Task{T}" /> representing the asynchronous operation. The
/// <see cref="Task{T}.Result" /> property will return the vertical position
/// of the console cursor.
/// </returns>
public static Task<int> GetCursorTopAsync(CancellationToken cancellationToken) =>
s_consoleProxy.GetCursorTopAsync(cancellationToken);

Expand Down
2 changes: 1 addition & 1 deletion src/PowerShellEditorServices/Console/ConsoleReadLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public async Task<SecureString> ReadSecureLineAsync(CancellationToken cancellati

private static async Task<ConsoleKeyInfo> ReadKeyAsync(CancellationToken cancellationToken)
{
return await ConsoleProxy.ReadKeyAsync(cancellationToken);
return await ConsoleProxy.ReadKeyAsync(intercept: true, cancellationToken);
}

private async Task<string> ReadLineAsync(bool isCommandLine, CancellationToken cancellationToken)
Expand Down
Loading