Skip to content

Bug fixes and improvements for 0.2.0 #51

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 7 commits into from
Nov 21, 2015
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 0.1.0.{build}
version: 0.2.0.{build}
os: Unstable
configuration: Release
clone_depth: 10
Expand All @@ -10,7 +10,7 @@ branches:
assembly_info:
patch: true
file: '**\AssemblyInfo.*'
assembly_version: '0.1.0' # This version number should always have 0 for the last section
assembly_version: '0.2.0' # This version number should always have 0 for the last section
assembly_file_version: '{version}'
assembly_informational_version: '{version}'

Expand Down
8 changes: 4 additions & 4 deletions src/PowerShellEditorServices.Host/DebugAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected async Task HandleLaunchRequest(
// Execute the given PowerShell script and send the response.
// Note that we aren't waiting for execution to complete here
// because the debugger could stop while the script executes.
editorSession.powerShellContext
editorSession.PowerShellContext
.ExecuteScriptAtPath(launchParams.Program)
.ContinueWith(
async (t) =>
Expand Down Expand Up @@ -141,15 +141,15 @@ protected Task HandleDisconnectRequest(
if (e.NewSessionState == PowerShellContextState.Ready)
{
await requestContext.SendResult(null);
editorSession.powerShellContext.SessionStateChanged -= handler;
editorSession.PowerShellContext.SessionStateChanged -= handler;

// TODO: Find a way to exit more gracefully!
Environment.Exit(0);
}
};

editorSession.powerShellContext.SessionStateChanged += handler;
editorSession.powerShellContext.AbortExecution();
editorSession.PowerShellContext.SessionStateChanged += handler;
editorSession.PowerShellContext.AbortExecution();

return Task.FromResult(true);
}
Expand Down
107 changes: 78 additions & 29 deletions src/PowerShellEditorServices.Host/LanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ internal class LanguageServer : IMessageProcessor
private static CancellationTokenSource existingRequestCancellation;

private MessageDispatcher<EditorSession> messageDispatcher;
private LanguageServerSettings currentSettings = new LanguageServerSettings();

public LanguageServer()
{
Expand All @@ -42,6 +43,7 @@ public void Initialize()
this.AddEventHandler(DidOpenTextDocumentNotification.Type, this.HandleDidOpenTextDocumentNotification);
this.AddEventHandler(DidCloseTextDocumentNotification.Type, this.HandleDidCloseTextDocumentNotification);
this.AddEventHandler(DidChangeTextDocumentNotification.Type, this.HandleDidChangeTextDocumentNotification);
this.AddEventHandler(DidChangeConfigurationNotification<SettingsWrapper>.Type, this.HandleDidChangeConfigurationNotification);

this.AddRequestHandler(DefinitionRequest.Type, this.HandleDefinitionRequest);
this.AddRequestHandler(ReferencesRequest.Type, this.HandleReferencesRequest);
Expand Down Expand Up @@ -94,6 +96,9 @@ protected async Task HandleInitializeRequest(
EditorSession editorSession,
RequestContext<InitializeResult, InitializeError> requestContext)
{
// Grab the workspace path from the parameters
editorSession.Workspace.WorkspacePath = initializeParams.RootPath;

await requestContext.SendResult(
new InitializeResult
{
Expand Down Expand Up @@ -130,20 +135,19 @@ protected Task HandleShutdownRequest(
}

protected async Task HandleShowOnlineHelpRequest(
object helpParams,
string helpParams,
EditorSession editorSession,
RequestContext<object, object> requestContext)
{
var psCommand = new PSCommand();

if (helpParams == null) { helpParams = "get-help"; }

var script = string.Format("get-help {0} -Online", helpParams);

psCommand.AddScript(script);
var psCommand = new PSCommand();
psCommand.AddCommand("Get-Help");
psCommand.AddArgument(helpParams);
psCommand.AddParameter("Online");

var result = await editorSession.powerShellContext.ExecuteCommand<object>(
psCommand);
await editorSession.PowerShellContext.ExecuteCommand<object>(
psCommand);

await requestContext.SendResult(null);
}
Expand Down Expand Up @@ -226,6 +230,36 @@ protected Task HandleDidChangeTextDocumentNotification(
return Task.FromResult(true);
}

protected async Task HandleDidChangeConfigurationNotification(
DidChangeConfigurationParams<SettingsWrapper> configChangeParams,
EditorSession editorSession,
EventContext eventContext)
{
bool oldScriptAnalysisEnabled =
this.currentSettings.ScriptAnalysis.Enable.HasValue;

this.currentSettings.Update(
configChangeParams.Settings.Powershell);

if (oldScriptAnalysisEnabled != this.currentSettings.ScriptAnalysis.Enable)
{
// If the user just turned off script analysis, send a diagnostics
// event to clear the analysis markers that they already have
if (!this.currentSettings.ScriptAnalysis.Enable.Value)
{
ScriptFileMarker[] emptyAnalysisDiagnostics = new ScriptFileMarker[0];

foreach (var scriptFile in editorSession.Workspace.GetOpenedFiles())
{
await PublishScriptDiagnostics(
scriptFile,
emptyAnalysisDiagnostics,
eventContext);
}
}
}
}

protected async Task HandleDefinitionRequest(
TextDocumentPosition textDocumentPosition,
EditorSession editorSession,
Expand Down Expand Up @@ -387,21 +421,18 @@ protected async Task HandleCompletionResolveRequest(
if (completionItem.Kind == CompletionItemKind.Function)
{
RunspaceHandle runspaceHandle =
await editorSession.powerShellContext.GetRunspaceHandle();
await editorSession.PowerShellContext.GetRunspaceHandle();

// Get the documentation for the function
CommandInfo commandInfo =
CommandHelpers.GetCommandInfo(
completionItem.Label,
runspaceHandle.Runspace);

if (commandInfo != null)
{
completionItem.Documentation =
CommandHelpers.GetCommandSynopsis(
commandInfo,
runspaceHandle.Runspace);
}
completionItem.Documentation =
CommandHelpers.GetCommandSynopsis(
commandInfo,
runspaceHandle.Runspace);

runspaceHandle.Dispose();
}
Expand Down Expand Up @@ -744,6 +775,12 @@ private Task RunScriptDiagnostics(
EditorSession editorSession,
EventContext eventContext)
{
if (!this.currentSettings.ScriptAnalysis.Enable.Value)
{
// If the user has disabled script analysis, skip it entirely
return TaskConstants.Completed;
}

// If there's an existing task, attempt to cancel it
try
{
Expand Down Expand Up @@ -827,24 +864,36 @@ private static async Task DelayThenInvokeDiagnostics(

var allMarkers = scriptFile.SyntaxMarkers.Concat(semanticMarkers);

// Always send syntax and semantic errors. We want to
// make sure no out-of-date markers are being displayed.
await eventContext.SendEvent(
PublishDiagnosticsNotification.Type,
new PublishDiagnosticsNotification
{
Uri = scriptFile.ClientFilePath,
Diagnostics =
allMarkers
.Select(GetDiagnosticFromMarker)
.ToArray()
});

await PublishScriptDiagnostics(
scriptFile,
semanticMarkers,
eventContext);
}

Logger.Write(LogLevel.Verbose, "Analysis complete.");
}

private async static Task PublishScriptDiagnostics(
ScriptFile scriptFile,
ScriptFileMarker[] semanticMarkers,
EventContext eventContext)
{
var allMarkers = scriptFile.SyntaxMarkers.Concat(semanticMarkers);

// Always send syntax and semantic errors. We want to
// make sure no out-of-date markers are being displayed.
await eventContext.SendEvent(
PublishDiagnosticsNotification.Type,
new PublishDiagnosticsNotification
{
Uri = scriptFile.ClientFilePath,
Diagnostics =
allMarkers
.Select(GetDiagnosticFromMarker)
.ToArray()
});
}

private static Diagnostic GetDiagnosticFromMarker(ScriptFileMarker scriptFileMarker)
{
return new Diagnostic
Expand Down
52 changes: 52 additions & 0 deletions src/PowerShellEditorServices.Host/LanguageServerSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

namespace Microsoft.PowerShell.EditorServices.Host
{
internal class LanguageServerSettings
{
public ScriptAnalysisSettings ScriptAnalysis { get; set; }

public LanguageServerSettings()
{
this.ScriptAnalysis = new ScriptAnalysisSettings();
}

public void Update(LanguageServerSettings settings)
{
if (settings != null)
{
this.ScriptAnalysis.Update(settings.ScriptAnalysis);
}
}
}

internal class ScriptAnalysisSettings
{
public bool? Enable { get; set; }

public ScriptAnalysisSettings()
{
this.Enable = true;
}

public void Update(ScriptAnalysisSettings settings)
{
if (settings != null)
{
this.Enable = settings.Enable;
}
}
}

internal class SettingsWrapper
{
// NOTE: This property is capitalized as 'Powershell' because the
// mode name sent from the client is written as 'powershell' and
// JSON.net is using camelCasing.

public LanguageServerSettings Powershell { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/PowerShellEditorServices.Host/MessageLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ async Task ListenForMessages()
// Set up the PowerShell session
this.editorSession = new EditorSession();
this.editorSession.StartSession(this.consoleHost);
this.editorSession.powerShellContext.OutputWritten += powerShellContext_OutputWritten;
this.editorSession.PowerShellContext.OutputWritten += powerShellContext_OutputWritten;

if (this.runDebugAdapter)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<Compile Include="DebugAdapter.cs" />
<Compile Include="IMessageProcessor.cs" />
<Compile Include="LanguageServer.cs" />
<Compile Include="LanguageServerSettings.cs" />
<Compile Include="MessageLoop.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// 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 DidChangeConfigurationNotification<TConfig>
{
public static readonly
EventType<DidChangeConfigurationParams<TConfig>> Type =
EventType<DidChangeConfigurationParams<TConfig>>.Create("workspace/didChangeConfiguration");
}

public class DidChangeConfigurationParams<TConfig>
{
public TConfig Settings { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace Microsoft.PowerShell.EditorServices.Protocol.LanguageServer
{
public class ShowOnlineHelpRequest
{
public static readonly RequestType<object, object, object> Type = RequestType<object, object, object>.Create("showonlinehelp");
public static readonly
RequestType<string, object, object> Type =
RequestType<string, object, object>.Create("powerShell/showOnlineHelp");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<Compile Include="DebugAdapter\V8MessageSerializer.cs" />
<Compile Include="DebugAdapter\Variable.cs" />
<Compile Include="DebugAdapter\VariablesRequest.cs" />
<Compile Include="LanguageServer\Configuration.cs" />
<Compile Include="LanguageServer\Definition.cs" />
<Compile Include="LanguageServer\DocumentHighlight.cs" />
<Compile Include="LanguageServer\Hover.cs" />
Expand Down
38 changes: 22 additions & 16 deletions src/PowerShellEditorServices/Language/CommandHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,31 @@ public static string GetCommandSynopsis(

PSObject helpObject = null;

using (PowerShell powerShell = PowerShell.Create())
{
powerShell.Runspace = runspace;
powerShell.AddCommand("Get-Help");
powerShell.AddArgument(commandInfo);
helpObject = powerShell.Invoke<PSObject>().FirstOrDefault();
}

if (helpObject != null)
if (commandInfo != null &&
(commandInfo.CommandType == CommandTypes.Cmdlet ||
commandInfo.CommandType == CommandTypes.Function ||
commandInfo.CommandType == CommandTypes.Filter))
{
// Extract the synopsis string from the object
synopsisString =
(string)helpObject.Properties["synopsis"].Value ??
string.Empty;
using (PowerShell powerShell = PowerShell.Create())
{
powerShell.Runspace = runspace;
powerShell.AddCommand("Get-Help");
powerShell.AddArgument(commandInfo);
helpObject = powerShell.Invoke<PSObject>().FirstOrDefault();
}

// Ignore the placeholder value for this field
if (string.Equals(synopsisString, "SHORT DESCRIPTION", System.StringComparison.InvariantCultureIgnoreCase))
if (helpObject != null)
{
synopsisString = string.Empty;
// Extract the synopsis string from the object
synopsisString =
(string)helpObject.Properties["synopsis"].Value ??
string.Empty;

// Ignore the placeholder value for this field
if (string.Equals(synopsisString, "SHORT DESCRIPTION", System.StringComparison.InvariantCultureIgnoreCase))
{
synopsisString = string.Empty;
}
}
}

Expand Down
Loading