-
Notifications
You must be signed in to change notification settings - Fork 235
Add "Expand Alias" support #59
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,7 @@ public void Initialize() | |
this.AddRequestHandler(WorkspaceSymbolRequest.Type, this.HandleWorkspaceSymbolRequest); | ||
|
||
this.AddRequestHandler(ShowOnlineHelpRequest.Type, this.HandleShowOnlineHelpRequest); | ||
this.AddRequestHandler(ExpandAliasRequest.Type, this.HandleExpandAliasRequest); | ||
|
||
this.AddRequestHandler(DebugAdapterMessages.EvaluateRequest.Type, this.HandleEvaluateRequest); | ||
} | ||
|
@@ -152,6 +153,46 @@ await editorSession.PowerShellContext.ExecuteCommand<object>( | |
await requestContext.SendResult(null); | ||
} | ||
|
||
private async Task HandleExpandAliasRequest( | ||
string content, | ||
EditorSession editorSession, | ||
RequestContext<object, object> requestContext) | ||
{ | ||
var psCommand = new PSCommand(); | ||
var script = @" | ||
function Expand-Alias { | ||
|
||
param($targetScript) | ||
|
||
[ref]$errors=$null | ||
|
||
$tokens = [System.Management.Automation.PsParser]::Tokenize($targetScript, $errors).Where({$_.type -eq 'command'}) | | ||
Sort Start -Descending | ||
|
||
foreach ($token in $tokens) { | ||
$definition=(Get-Command ('`'+$token.Content) -CommandType Alias -ErrorAction SilentlyContinue).Definition | ||
|
||
if($definition) { | ||
$lhs=$targetScript.Substring(0, $token.Start) | ||
$rhs=$targetScript.Substring($token.Start + $token.Length) | ||
|
||
$targetScript=$lhs + $definition + $rhs | ||
} | ||
} | ||
|
||
$targetScript | ||
} | ||
M | ||
Expand-Alias @'" + "\r\n" + content + "\r\n'@"; | ||
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. This is a security risk since the content string is being inserted and executed directly as a script. I think it might be safer if you take the "Expand-Alias" call out of the script string and then add another command to the psCommand to call it. Something like this: psCommand
.AddScript(script)
.AddCommand("Expand-Alias")
.AddArgument(@'" + "\r\n" + content + "\r\n'@"); 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. I wonder if it also makes sense to obfuscate the "Expand-Alias" command somehow so that it doesn't overwrite a similarly-named command that the user has imported/created in their session. Maybe call it 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. Looks like it, I removed it on my box. Interestingly it works with and without it. It can be deleted, let me know if you want me to do it. |
||
|
||
psCommand.AddScript(script); | ||
|
||
var result = await editorSession.PowerShellContext.ExecuteCommand<PSObject>( | ||
psCommand); | ||
|
||
await requestContext.SendResult(result.First().ToString()); | ||
} | ||
|
||
protected Task HandleExitNotification( | ||
object exitParams, | ||
EditorSession editorSession, | ||
|
@@ -235,7 +276,7 @@ protected async Task HandleDidChangeConfigurationNotification( | |
EditorSession editorSession, | ||
EventContext eventContext) | ||
{ | ||
bool oldScriptAnalysisEnabled = | ||
bool oldScriptAnalysisEnabled = | ||
this.currentSettings.ScriptAnalysis.Enable.HasValue; | ||
|
||
this.currentSettings.Update( | ||
|
@@ -887,8 +928,8 @@ await eventContext.SendEvent( | |
new PublishDiagnosticsNotification | ||
{ | ||
Uri = scriptFile.ClientFilePath, | ||
Diagnostics = | ||
allMarkers | ||
Diagnostics = | ||
allMarkers | ||
.Select(GetDiagnosticFromMarker) | ||
.ToArray() | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
// | ||
|
||
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol; | ||
|
||
namespace Microsoft.PowerShell.EditorServices.Protocol.LanguageServer | ||
{ | ||
public class ExpandAliasRequest | ||
{ | ||
public static readonly | ||
RequestType<string, object, object> Type = | ||
RequestType<string, object, object>.Create("powerShell/expandAlias"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
// | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.PowerShell.EditorServices.Protocol.LanguageServer | ||
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. Looks like this needs to be removed until the Gallery PR |
||
{ | ||
public class PSModuleMessage | ||
{ | ||
public string Name { get; set; } | ||
public string Description { get; set; } | ||
} | ||
|
||
public class PSModuleResponse | ||
{ | ||
public List<PSModuleMessage> ModuleList { get; set; } | ||
} | ||
} |
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.
Is the 'M' here a typo?