Skip to content

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

Merged
merged 5 commits into from
Dec 9, 2015
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions src/PowerShellEditorServices.Host/LanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Copy link
Contributor

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?

Expand-Alias @'" + "\r\n" + content + "\r\n'@";
Copy link
Contributor

Choose a reason for hiding this comment

The 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'@");

Copy link
Contributor

Choose a reason for hiding this comment

The 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 __Expand-Alias (two leading underscores)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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,
Expand Down Expand Up @@ -235,7 +276,7 @@ protected async Task HandleDidChangeConfigurationNotification(
EditorSession editorSession,
EventContext eventContext)
{
bool oldScriptAnalysisEnabled =
bool oldScriptAnalysisEnabled =
this.currentSettings.ScriptAnalysis.Enable.HasValue;

this.currentSettings.Update(
Expand Down Expand Up @@ -887,8 +928,8 @@ await eventContext.SendEvent(
new PublishDiagnosticsNotification
{
Uri = scriptFile.ClientFilePath,
Diagnostics =
allMarkers
Diagnostics =
allMarkers
.Select(GetDiagnosticFromMarker)
.ToArray()
});
Expand Down
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
Copy link
Contributor

Choose a reason for hiding this comment

The 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; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@
<Compile Include="LanguageServer\Configuration.cs" />
<Compile Include="LanguageServer\Definition.cs" />
<Compile Include="LanguageServer\DocumentHighlight.cs" />
<Compile Include="LanguageServer\ExpandAliasRequest.cs" />
<Compile Include="LanguageServer\Hover.cs" />
<Compile Include="LanguageServer\PSModuleMessage.cs" />
<Compile Include="LanguageServer\ShowOnlineHelpRequest.cs" />
<Compile Include="LanguageServer\SignatureHelp.cs" />
<Compile Include="LanguageServer\WorkspaceSymbols.cs" />
Expand Down