Skip to content

Commit a14d007

Browse files
committed
Added Get-Installed and Expand-Alias
1 parent 1fb415e commit a14d007

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

src/PowerShellEditorServices.Host/LanguageServer.cs

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public void Initialize()
5858
this.AddRequestHandler(ShowOnlineHelpRequest.Type, this.HandleShowOnlineHelpRequest);
5959

6060
this.AddRequestHandler(FindModuleRequest.Type, this.HandleFindModuleRequest);
61+
this.AddRequestHandler(GetInstalledModuleRequest.Type, this.HandleGetInstalledModuleRequest);
62+
this.AddRequestHandler(ExpandAliasRequest.Type, this.HandleExpandAliasRequest);
6163

6264
this.AddRequestHandler(DebugAdapterMessages.EvaluateRequest.Type, this.HandleEvaluateRequest);
6365
}
@@ -155,7 +157,7 @@ await editorSession.PowerShellContext.ExecuteCommand<object>(
155157
}
156158

157159
private async Task HandleFindModuleRequest(
158-
string helpParams,
160+
string param,
159161
EditorSession editorSession,
160162
RequestContext<object, object> requestContext)
161163
{
@@ -178,6 +180,71 @@ private async Task HandleFindModuleRequest(
178180
await requestContext.SendResult(new PSModuleResponse { ModuleList = moduleList });
179181
}
180182

183+
private async Task HandleGetInstalledModuleRequest(
184+
string param,
185+
EditorSession editorSession,
186+
RequestContext<object, object> requestContext)
187+
{
188+
var psCommand = new PSCommand();
189+
psCommand.AddScript("Get-InstalledModule | Select Name, Description");
190+
191+
var modules = await editorSession.PowerShellContext.ExecuteCommand<PSObject>(
192+
psCommand);
193+
194+
var moduleList = new List<PSModuleMessage>();
195+
196+
if (modules != null)
197+
{
198+
foreach (dynamic m in modules)
199+
{
200+
moduleList.Add(new PSModuleMessage { Name = m.Name, Description = m.Description });
201+
}
202+
}
203+
204+
await requestContext.SendResult(new PSModuleResponse { ModuleList = moduleList });
205+
}
206+
207+
208+
private async Task HandleExpandAliasRequest(
209+
string content,
210+
EditorSession editorSession,
211+
RequestContext<object, object> requestContext)
212+
{
213+
var psCommand = new PSCommand();
214+
var script = @"
215+
function Expand-Alias {
216+
217+
param($targetScript)
218+
219+
[ref]$errors=$null
220+
221+
$tokens = [System.Management.Automation.PsParser]::Tokenize($targetScript, $errors).Where({$_.type -eq 'command'}) |
222+
Sort Start -Descending
223+
224+
foreach ($token in $tokens) {
225+
$definition=(Get-Command ('`'+$token.Content) -CommandType Alias -ErrorAction SilentlyContinue).Definition
226+
227+
if($definition) {
228+
$lhs=$targetScript.Substring(0, $token.Start)
229+
$rhs=$targetScript.Substring($token.Start + $token.Length)
230+
231+
$targetScript=$lhs + $definition + $rhs
232+
}
233+
}
234+
235+
$targetScript
236+
}
237+
M
238+
Expand-Alias @'" + "\r\n" + content + "\r\n'@";
239+
240+
psCommand.AddScript(script);
241+
242+
var result = await editorSession.PowerShellContext.ExecuteCommand<PSObject>(
243+
psCommand);
244+
245+
await requestContext.SendResult(result.First().ToString());
246+
}
247+
181248
protected Task HandleExitNotification(
182249
object exitParams,
183250
EditorSession editorSession,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 ExpandAliasRequest
11+
{
12+
public static readonly
13+
RequestType<string, object, object> Type =
14+
RequestType<string, object, object>.Create("powerShell/expandAlias");
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 GetInstalledModuleRequest
11+
{
12+
public static readonly
13+
RequestType<string, object, object> Type =
14+
RequestType<string, object, object>.Create("powerShell/getInstalledModule");
15+
}
16+
}

src/PowerShellEditorServices.Protocol/PowerShellEditorServices.Protocol.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@
9292
<Compile Include="LanguageServer\Configuration.cs" />
9393
<Compile Include="LanguageServer\Definition.cs" />
9494
<Compile Include="LanguageServer\DocumentHighlight.cs" />
95+
<Compile Include="LanguageServer\ExpandAliasRequest.cs" />
96+
<Compile Include="LanguageServer\GetInstalledModuleRequest.cs" />
9597
<Compile Include="LanguageServer\Hover.cs" />
9698
<Compile Include="LanguageServer\FindModuleRequest.cs" />
9799
<Compile Include="LanguageServer\PSModuleMessage.cs" />

0 commit comments

Comments
 (0)