-
Notifications
You must be signed in to change notification settings - Fork 236
Move PSReadLine logic to cmdlets #1255
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
TylerLeonhardt
merged 2 commits into
PowerShell:master
from
TylerLeonhardt:move-to-psrl-cmdlets
Apr 9, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/PowerShellEditorServices.Hosting/Commands/InvokeReadLineConstructorCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
// | ||
|
||
using System; | ||
using System.Management.Automation; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Microsoft.PowerShell.EditorServices.Commands | ||
{ | ||
/// <summary> | ||
/// The Start-EditorServices command, the conventional entrypoint for PowerShell Editor Services. | ||
/// </summary> | ||
[Cmdlet("__Invoke", "ReadLineConstructor")] | ||
public sealed class InvokeReadLineConstructorCommand : PSCmdlet | ||
{ | ||
protected override void EndProcessing() | ||
{ | ||
Type type = Type.GetType("Microsoft.PowerShell.PSConsoleReadLine, Microsoft.PowerShell.PSReadLine2"); | ||
RuntimeHelpers.RunClassConstructor(type.TypeHandle); | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/PowerShellEditorServices.Hosting/Commands/InvokeReadLineForEditorServicesCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
// | ||
|
||
using System; | ||
using System.Management.Automation; | ||
using System.Management.Automation.Runspaces; | ||
using System.Reflection; | ||
using System.Threading; | ||
|
||
namespace Microsoft.PowerShell.EditorServices.Commands | ||
{ | ||
/// <summary> | ||
/// The Start-EditorServices command, the conventional entrypoint for PowerShell Editor Services. | ||
/// </summary> | ||
[Cmdlet("__Invoke", "ReadLineForEditorServices")] | ||
public sealed class InvokeReadLineForEditorServicesCommand : PSCmdlet | ||
{ | ||
private delegate string ReadLineInvoker( | ||
Runspace runspace, | ||
EngineIntrinsics engineIntrinsics, | ||
CancellationToken cancellationToken); | ||
|
||
private static Lazy<ReadLineInvoker> s_readLine = new Lazy<ReadLineInvoker>(() => | ||
{ | ||
Type type = Type.GetType("Microsoft.PowerShell.PSConsoleReadLine, Microsoft.PowerShell.PSReadLine2"); | ||
MethodInfo method = type?.GetMethod( | ||
"ReadLine", | ||
new[] { typeof(Runspace), typeof(EngineIntrinsics), typeof(CancellationToken) }); | ||
|
||
// TODO: Handle method being null here. This shouldn't ever happen. | ||
|
||
return (ReadLineInvoker)method.CreateDelegate(typeof(ReadLineInvoker)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}); | ||
|
||
/// <summary> | ||
/// The ID to give to the host's profile. | ||
/// </summary> | ||
[Parameter(Mandatory = true)] | ||
[ValidateNotNullOrEmpty] | ||
public CancellationToken CancellationToken { get; set; } | ||
|
||
protected override void EndProcessing() | ||
{ | ||
// This returns a string. | ||
object result = s_readLine.Value( | ||
Runspace.DefaultRunspace, | ||
SessionState.PSVariable.Get("ExecutionContext").Value as EngineIntrinsics, | ||
CancellationToken | ||
); | ||
|
||
WriteObject(result); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can drop the
Cmdlet
decoration and make this internal. When you go to invoke it, use:(and probably staticly store the
CmdletInfo
)Edit: hmm I guess the direct
CommandInfo
route isn't accessible fromPSCommand
directly for some reason. EitherPSCommand.AddCommand
needs an overload forCommandInfo
, or theRunspaces.Command
ctor that takes it needs to be made public.I'd honestly still recommend going this route, and use reflection to access the
Command.ctor(CommandInfo)
overload. Then add an internal extension methodPSCommand.AddCommand(CommandInfo)
so we can use this as necessary.I really dislike adding global state, though I understand if other folks aren't against it enough to think this approach is warranted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to pass on this one for now...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I don't think I explained that well. That part in particular should be pretty easy, this is what I mean:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually can't do this without breaking the contract between the PSES.Host proj and PSES proj.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess I can put the command in PSES...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually then that loads it in a different ALC... ok there's a bit too much going on here. I'm gonna defer this.