Skip to content

Add support for a "Show Documentation" quick fix menu entry #789

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
Nov 15, 2018
Merged
Changes from 2 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
66 changes: 45 additions & 21 deletions src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Language;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -1192,6 +1193,7 @@ protected async Task HandleCodeActionRequest(
Dictionary<string, MarkerCorrection> markerIndex = null;
List<CodeActionCommand> codeActionCommands = new List<CodeActionCommand>();

// If there are any code fixes, send these commands first so they appear at top of "Code Fix" menu in the client UI.
if (this.codeActionsPerFile.TryGetValue(codeActionParams.TextDocument.Uri, out markerIndex))
{
foreach (var diagnostic in codeActionParams.Context.Diagnostics)
Expand All @@ -1216,22 +1218,34 @@ protected async Task HandleCodeActionRequest(
Arguments = JArray.FromObject(correction.Edits)
});
}
}
}

if (string.Equals(diagnostic.Source, "PSScriptAnalyzer", StringComparison.OrdinalIgnoreCase))
{
codeActionCommands.Add(
new CodeActionCommand
{
Title = $"Show documentation for \"{diagnostic.Code}\"",
Command = "PowerShell.ShowCodeActionDocumentation",
Arguments = JArray.FromObject(new[] { diagnostic.Code })
});
}
// Add "show documentation" commands last so they appear at the bottom of the client UI.
// These commands do not require code fixes. Sometimes we get a batch of diagnostics
// to create commands for. No need to create multiple show doc commands for the same rule.
var ruleNamesProcessed = new HashSet<string>();
foreach (var diagnostic in codeActionParams.Context.Diagnostics)
{
if (string.IsNullOrEmpty(diagnostic.Code)) { continue; }

if (string.Equals(diagnostic.Source, "PSScriptAnalyzer", StringComparison.OrdinalIgnoreCase) &&
!ruleNamesProcessed.Contains(diagnostic.Code))
{
ruleNamesProcessed.Add(diagnostic.Code);

codeActionCommands.Add(
new CodeActionCommand
{
Title = $"Show documentation for \"{diagnostic.Code}\"",
Command = "PowerShell.ShowCodeActionDocumentation",
Arguments = JArray.FromObject(new[] { diagnostic.Code })
});
}
}

await requestContext.SendResult(
codeActionCommands.ToArray());
codeActionCommands.ToArray());
}

protected async Task HandleDocumentFormattingRequest(
Expand Down Expand Up @@ -1540,12 +1554,6 @@ private static async Task DelayThenInvokeDiagnostics(
ILogger Logger,
CancellationToken cancellationToken)
{
// If filesToAnalzye is empty, nothing to do so return early.
if (filesToAnalyze.Length == 0)
{
return;
}

// First of all, wait for the desired delay period before
// analyzing the provided list of files
try
Expand Down Expand Up @@ -1585,6 +1593,7 @@ private static async Task DelayThenInvokeDiagnostics(

await PublishScriptDiagnostics(
scriptFile,
// Concat script analysis errors to any existing parse errors
scriptFile.SyntaxMarkers.Concat(semanticMarkers).ToArray(),
correctionIndex,
eventSender);
Expand Down Expand Up @@ -1652,14 +1661,29 @@ await eventSender(
});
}

// Generate a unique id that is used as a key to look up the associated code action (code fix) when
// we receive and process the textDocument/codeAction message.
private static string GetUniqueIdFromDiagnostic(Diagnostic diagnostic)
{
string source = diagnostic.Source ?? "?";
string code = diagnostic.Code ?? "?";
string severity = diagnostic.Severity != null ? diagnostic.Severity.ToString() : "?";
Position start = diagnostic.Range.Start;
Position end = diagnostic.Range.End;
return $"{source}_{code}_{severity}_{start.Line}:{start.Character}-{end.Line}:{end.Character}";

var sb = new StringBuilder(256);
sb.Append(diagnostic.Source ?? "?");
sb.Append("_");
sb.Append(diagnostic.Code ?? "?");
sb.Append("_");
sb.Append((diagnostic.Severity != null) ? diagnostic.Severity.ToString() : "?");
sb.Append("_");
sb.Append(start.Line);
sb.Append(":");
sb.Append(start.Character);
sb.Append("-");
sb.Append(end.Line);
sb.Append(":");
sb.Append(end.Character);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider chaining these, e.g.

return new StringBuilder(256)
    .Append(diagnostic.Source ?? "?")
    .Append("_")
    .Append(diagnostic.Code ?? "?")
    .Append("_")
    .Append((diagnostic.Severity != null) ? diagnostic.Severity.ToString() : "?")
    .Append("_")
    .Append(start.Line)
    .Append(":")
    .Append(start.Character)
    .Append("-")
    .Append(end.Line)
    .Append(":")
    .Append(end.Character)
    .ToString();

Copy link
Member

@TylerLeonhardt TylerLeonhardt Nov 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small nit... it might be useful to do:

StringBuilder sb = new StringBuilder(256)
    .Append(diagnostic.Source ?? "?")
    .Append("_")
    .Append(diagnostic.Code ?? "?")
    .Append("_")
    .Append((diagnostic.Severity != null) ? diagnostic.Severity.ToString() : "?")
    .Append("_")
    .Append(start.Line)
    .Append(":")
    .Append(start.Character)
    .Append("-")
    .Append(end.Line)
    .Append(":")
    .Append(end.Character)

return sb.ToString();

so that we can throw a breakpoint on the return line and see what the contents of sb are. But I won't block on doing that vs what Patrick suggested

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh, I was just going to suggest this very thing. :-) Will fix.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly my biggest desire is for a function-return-variable in debug view.


return sb.ToString();
}

private static Diagnostic GetDiagnosticFromMarker(ScriptFileMarker scriptFileMarker)
Expand Down