Skip to content

Commit 65d5bc5

Browse files
committed
Implement getPSHostProcessesRequest.
1 parent 3cd248d commit 65d5bc5

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-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: 35 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,39 @@ 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+
int processId = System.Diagnostics.Process.GetCurrentProcess().Id;
936+
var psCommand = new PSCommand();
937+
psCommand.AddCommand("Get-PSHostProcessInfo");
938+
psCommand.AddCommand("Where-Object")
939+
.AddParameter("Property", "ProcessId")
940+
.AddParameter("NE")
941+
.AddParameter("Value", processId.ToString());
942+
943+
var processes = await editorSession.PowerShellContext.ExecuteCommand<PSObject>(psCommand);
944+
if (processes != null)
945+
{
946+
foreach (dynamic p in processes)
947+
{
948+
psHostProcesses.Add(
949+
new GetPSHostProcessesResponse
950+
{
951+
ProcessName = p.ProcessName,
952+
ProcessId = p.ProcessId,
953+
AppDomainName = p.AppDomainName,
954+
MainWindowTitle = p.MainWindowTitle
955+
});
956+
}
957+
}
958+
959+
await requestContext.SendResult(psHostProcesses.ToArray());
960+
}
961+
927962
private bool IsQueryMatch(string query, string symbolName)
928963
{
929964
return symbolName.IndexOf(query, StringComparison.OrdinalIgnoreCase) >= 0;

0 commit comments

Comments
 (0)