Skip to content

Commit 49f6e59

Browse files
authored
Merge pull request #335 from PowerShell/rkeithhill/impl-getPSHostPorcesses
Implement getPSHostProcessesRequest.
2 parents 3cd248d + 174b74d commit 49f6e59

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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.Protocol.MessageProtocol;
7+
8+
namespace Microsoft.PowerShell.EditorServices.Protocol.LanguageServer
9+
{
10+
public class GetPSHostProcessesRequest
11+
{
12+
public static readonly
13+
RequestType<object, GetPSHostProcessesResponse[]> Type =
14+
RequestType<object, GetPSHostProcessesResponse[]>.Create("powerShell/getPSHostProcesses");
15+
}
16+
17+
public class GetPSHostProcessesResponse
18+
{
19+
public string ProcessName { get; set; }
20+
21+
public int ProcessId { get; set; }
22+
23+
public string AppDomainName { get; set; }
24+
25+
public string MainWindowTitle { get; set; }
26+
}
27+
}

src/PowerShellEditorServices.Protocol/PowerShellEditorServices.Protocol.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<Compile Include="DebugAdapter\ContinueRequest.cs" />
5757
<Compile Include="DebugAdapter\SetFunctionBreakpointsRequest.cs" />
5858
<Compile Include="DebugAdapter\SetVariableRequest.cs" />
59+
<Compile Include="LanguageServer\GetPSHostProcessesRequest.cs" />
5960
<Compile Include="LanguageServer\GetPSSARulesRequest.cs" />
6061
<Compile Include="LanguageServer\EditorCommands.cs" />
6162
<Compile Include="LanguageServer\CodeAction.cs" />

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ protected override void Initialize()
123123
this.SetRequestHandler(GetPSSARulesRequest.Type, this.HandleGetPSSARulesRequest);
124124
this.SetRequestHandler(SetPSSARulesRequest.Type, this.HandleSetPSSARulesRequest);
125125

126+
this.SetRequestHandler(GetPSHostProcessesRequest.Type, this.HandleGetPSHostProcessesRequest);
127+
126128
// Initialize the extension service
127129
// TODO: This should be made awaited once Initialize is async!
128130
this.editorSession.ExtensionService.Initialize(
@@ -924,6 +926,42 @@ await requestContext.SendResult(
924926
this.editorSession.PowerShellContext.LocalPowerShellVersion));
925927
}
926928

929+
protected async Task HandleGetPSHostProcessesRequest(
930+
object noParams,
931+
RequestContext<GetPSHostProcessesResponse[]> requestContext)
932+
{
933+
var psHostProcesses = new List<GetPSHostProcessesResponse>();
934+
935+
if (this.editorSession.PowerShellContext.LocalPowerShellVersion.Version.Major >= 5)
936+
{
937+
int processId = System.Diagnostics.Process.GetCurrentProcess().Id;
938+
var psCommand = new PSCommand();
939+
psCommand.AddCommand("Get-PSHostProcessInfo");
940+
psCommand.AddCommand("Where-Object")
941+
.AddParameter("Property", "ProcessId")
942+
.AddParameter("NE")
943+
.AddParameter("Value", processId.ToString());
944+
945+
var processes = await editorSession.PowerShellContext.ExecuteCommand<PSObject>(psCommand);
946+
if (processes != null)
947+
{
948+
foreach (dynamic p in processes)
949+
{
950+
psHostProcesses.Add(
951+
new GetPSHostProcessesResponse
952+
{
953+
ProcessName = p.ProcessName,
954+
ProcessId = p.ProcessId,
955+
AppDomainName = p.AppDomainName,
956+
MainWindowTitle = p.MainWindowTitle
957+
});
958+
}
959+
}
960+
}
961+
962+
await requestContext.SendResult(psHostProcesses.ToArray());
963+
}
964+
927965
private bool IsQueryMatch(string query, string symbolName)
928966
{
929967
return symbolName.IndexOf(query, StringComparison.OrdinalIgnoreCase) >= 0;

0 commit comments

Comments
 (0)