Skip to content

Commit ffe7c61

Browse files
committed
Add support for Enter-PSSession and Enter-PSHostProcess
This change adds support for remote sessions (PSv4+) and attaching to processes on the same machine (PSv5+).
1 parent b2d444c commit ffe7c61

15 files changed

+419
-60
lines changed

src/PowerShellEditorServices.Protocol/LanguageServer/PowerShellVersionRequest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ namespace Microsoft.PowerShell.EditorServices.Protocol.LanguageServer
1111
public class PowerShellVersionRequest
1212
{
1313
public static readonly
14-
RequestType<object, PowerShellVersionResponse> Type =
15-
RequestType<object, PowerShellVersionResponse>.Create("powerShell/getVersion");
14+
RequestType<object, PowerShellVersion> Type =
15+
RequestType<object, PowerShellVersion>.Create("powerShell/getVersion");
1616
}
1717

18-
public class PowerShellVersionResponse
18+
public class PowerShellVersion
1919
{
2020
public string Version { get; set; }
2121

@@ -25,11 +25,11 @@ public class PowerShellVersionResponse
2525

2626
public string Architecture { get; set; }
2727

28-
public PowerShellVersionResponse()
28+
public PowerShellVersion()
2929
{
3030
}
3131

32-
public PowerShellVersionResponse(PowerShellVersionDetails versionDetails)
32+
public PowerShellVersion(PowerShellVersionDetails versionDetails)
3333
{
3434
this.Version = versionDetails.VersionString;
3535
this.DisplayVersion = $"{versionDetails.Version.Major}.{versionDetails.Version.Minor}";
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
using Microsoft.PowerShell.EditorServices.Session;
7+
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
8+
9+
namespace Microsoft.PowerShell.EditorServices.Protocol.LanguageServer
10+
{
11+
public class RunspaceChangedEvent
12+
{
13+
public static readonly
14+
EventType<RunspaceDetails> Type =
15+
EventType<RunspaceDetails>.Create("powerShell/runspaceChanged");
16+
}
17+
18+
public class RunspaceDetails
19+
{
20+
public PowerShellVersion PowerShellVersion { get; set; }
21+
22+
public RunspaceType RunspaceType { get; set; }
23+
24+
public string ConnectionString { get; set; }
25+
26+
public RunspaceDetails()
27+
{
28+
}
29+
30+
public RunspaceDetails(RunspaceChangedEventArgs eventArgs)
31+
{
32+
this.PowerShellVersion = new PowerShellVersion(eventArgs.RunspaceVersion);
33+
this.RunspaceType = eventArgs.RunspaceType;
34+
this.ConnectionString = eventArgs.ConnectionString;
35+
}
36+
}
37+
}

src/PowerShellEditorServices.Protocol/PowerShellEditorServices.Protocol.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<Compile Include="LanguageServer\FindModuleRequest.cs" />
6262
<Compile Include="LanguageServer\InstallModuleRequest.cs" />
6363
<Compile Include="LanguageServer\PowerShellVersionRequest.cs" />
64+
<Compile Include="LanguageServer\RunspaceChanged.cs" />
6465
<Compile Include="LanguageServer\SetPSSARulesRequest.cs" />
6566
<Compile Include="LanguageServer\ProjectTemplate.cs" />
6667
<Compile Include="MessageProtocol\Channel\NamedPipeClientChannel.cs" />

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public LanguageServer(HostDetails hostDetails, ProfilePaths profilePaths, Channe
5252
this.editorSession = new EditorSession();
5353
this.editorSession.StartSession(hostDetails, profilePaths);
5454
this.editorSession.ConsoleService.OutputWritten += this.powerShellContext_OutputWritten;
55+
this.editorSession.PowerShellContext.RunspaceChanged += PowerShellContext_RunspaceChanged;
5556

5657
// Attach to ExtensionService events
5758
this.editorSession.ExtensionService.CommandAdded += ExtensionService_ExtensionAdded;
@@ -910,10 +911,10 @@ protected async Task HandleWorkspaceSymbolRequest(
910911

911912
protected async Task HandlePowerShellVersionRequest(
912913
object noParams,
913-
RequestContext<PowerShellVersionResponse> requestContext)
914+
RequestContext<PowerShellVersion> requestContext)
914915
{
915916
await requestContext.SendResult(
916-
new PowerShellVersionResponse(
917+
new PowerShellVersion(
917918
this.editorSession.PowerShellContext.PowerShellVersionDetails));
918919
}
919920

@@ -988,6 +989,13 @@ protected Task HandleEvaluateRequest(
988989

989990
#region Event Handlers
990991

992+
private async void PowerShellContext_RunspaceChanged(object sender, RunspaceChangedEventArgs e)
993+
{
994+
await this.SendEvent(
995+
RunspaceChangedEvent.Type,
996+
new RunspaceDetails(e));
997+
}
998+
991999
private async void powerShellContext_OutputWritten(object sender, OutputWrittenEventArgs e)
9921000
{
9931001
// Queue the output for writing

src/PowerShellEditorServices/Language/CommandHelpers.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public static async Task<CommandInfo> GetCommandInfo(
2828
command.AddCommand(@"Microsoft.PowerShell.Core\Get-Command");
2929
command.AddArgument(commandName);
3030

31-
var results = await powerShellContext.ExecuteCommand<CommandInfo>(command, false, false);
32-
return results.FirstOrDefault();
31+
var results = await powerShellContext.ExecuteCommand<object>(command, false, false);
32+
return results.OfType<CommandInfo>().FirstOrDefault();
3333
}
3434

3535
/// <summary>

src/PowerShellEditorServices/Language/LanguageService.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ await CommandHelpers.GetCommandInfo(
346346
this.powerShellContext);
347347

348348
foundDefinition =
349-
await FindDeclarationForBuiltinCommand(
349+
FindDeclarationForBuiltinCommand(
350350
cmdInfo,
351351
foundSymbol,
352352
workspace);
@@ -532,22 +532,17 @@ private ScriptFile[] GetBuiltinCommandScriptFiles(
532532
return new List<ScriptFile>().ToArray();
533533
}
534534

535-
private async Task<SymbolReference> FindDeclarationForBuiltinCommand(
536-
CommandInfo cmdInfo,
535+
private SymbolReference FindDeclarationForBuiltinCommand(
536+
CommandInfo commandInfo,
537537
SymbolReference foundSymbol,
538538
Workspace workspace)
539539
{
540540
SymbolReference foundDefinition = null;
541-
if (cmdInfo != null)
541+
if (commandInfo != null)
542542
{
543543
int index = 0;
544544
ScriptFile[] nestedModuleFiles;
545545

546-
CommandInfo commandInfo =
547-
await CommandHelpers.GetCommandInfo(
548-
foundSymbol.SymbolName,
549-
this.powerShellContext);
550-
551546
nestedModuleFiles =
552547
GetBuiltinCommandScriptFiles(
553548
commandInfo.Module,

src/PowerShellEditorServices/Nano.PowerShellEditorServices.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
<Compile Include="Session\SessionPSHost.cs" />
114114
<Compile Include="Session\SessionPSHostRawUserInterface.cs" />
115115
<Compile Include="Session\SessionPSHostUserInterface.cs" />
116+
<Compile Include="Session\RunspaceChangedEventArgs.cs" />
116117
<Compile Include="Session\SessionStateChangedEventArgs.cs" />
117118
<Compile Include="Utility\AsyncContextThread.cs" />
118119
<Compile Include="Utility\AsyncDebouncer.cs" />
@@ -149,6 +150,7 @@
149150
<Compile Include="..\PowerShellEditorServices.Protocol\LanguageServer\FindModuleRequest.cs" />
150151
<Compile Include="..\PowerShellEditorServices.Protocol\LanguageServer\InstallModuleRequest.cs" />
151152
<Compile Include="..\PowerShellEditorServices.Protocol\LanguageServer\PowerShellVersionRequest.cs" />
153+
<Compile Include="..\PowerShellEditorServices.Protocol\LanguageServer\RunspaceChanged.cs" />
152154
<Compile Include="..\PowerShellEditorServices.Protocol\MessageProtocol\Channel\NamedPipeClientChannel.cs" />
153155
<Compile Include="..\PowerShellEditorServices.Protocol\MessageProtocol\Channel\NamedPipeServerChannel.cs" />
154156
<Compile Include="..\PowerShellEditorServices.Protocol\MessageProtocol\Channel\TcpSocketClientChannel.cs" />

src/PowerShellEditorServices/PowerShellEditorServices.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
<Compile Include="Session\PowerShellVersionDetails.cs" />
122122
<Compile Include="Session\ProfilePaths.cs" />
123123
<Compile Include="Session\ProgressDetails.cs" />
124+
<Compile Include="Session\RunspaceChangedEventArgs.cs" />
124125
<Compile Include="Session\RunspaceHandle.cs" />
125126
<Compile Include="Session\SessionPSHost.cs" />
126127
<Compile Include="Session\SessionPSHostRawUserInterface.cs" />

src/PowerShellEditorServices/Session/PowerShell4Operations.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ internal class PowerShell4Operations : IVersionSpecificOperations
1616
public void ConfigureDebugger(Runspace runspace)
1717
{
1818
#if !PowerShellv3
19-
runspace.Debugger.SetDebugMode(DebugModes.LocalScript | DebugModes.RemoteScript);
19+
if (runspace.Debugger != null)
20+
{
21+
runspace.Debugger.SetDebugMode(DebugModes.LocalScript | DebugModes.RemoteScript);
22+
}
2023
#endif
2124
}
2225

src/PowerShellEditorServices/Session/PowerShell5Operations.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ internal class PowerShell5Operations : PowerShell4Operations
1212
public override void PauseDebugger(Runspace runspace)
1313
{
1414
#if !PowerShellv3 && !PowerShellv4
15-
runspace.Debugger.SetDebuggerStepMode(true);
15+
if (runspace.Debugger != null)
16+
{
17+
runspace.Debugger.SetDebuggerStepMode(true);
18+
}
1619
#endif
1720
}
1821
}

0 commit comments

Comments
 (0)