diff --git a/src/PowerShellEditorServices/PowerShellEditorServices.csproj b/src/PowerShellEditorServices/PowerShellEditorServices.csproj index 077a37eb7..fb5f89754 100644 --- a/src/PowerShellEditorServices/PowerShellEditorServices.csproj +++ b/src/PowerShellEditorServices/PowerShellEditorServices.csproj @@ -30,7 +30,7 @@ - + @@ -40,6 +40,6 @@ - + diff --git a/src/PowerShellEditorServices/Services/TextDocument/Handlers/CodeActionHandler.cs b/src/PowerShellEditorServices/Services/TextDocument/Handlers/CodeActionHandler.cs index aa302eafa..eb5af4806 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/Handlers/CodeActionHandler.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/Handlers/CodeActionHandler.cs @@ -5,8 +5,7 @@ using System; using System.Collections.Generic; -using System.Diagnostics; -using System.Runtime.InteropServices; +using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; @@ -86,11 +85,23 @@ public async Task Handle(CodeActionParams request, string diagnosticId = AnalysisService.GetUniqueIdFromDiagnostic(diagnostic); if (corrections.TryGetValue(diagnosticId, out MarkerCorrection correction)) { - codeActions.Add(new Command() + codeActions.Add(new CodeAction { Title = correction.Name, - Name = "PowerShell.ApplyCodeActionEdits", - Arguments = JArray.FromObject(correction.Edits) + Kind = CodeActionKind.QuickFix, + Edit = new WorkspaceEdit + { + DocumentChanges = new Container( + new WorkspaceEditDocumentChange( + new TextDocumentEdit + { + TextDocument = new VersionedTextDocumentIdentifier + { + Uri = request.TextDocument.Uri + }, + Edits = new Container(correction.Edits.Select(ScriptRegion.ToTextEdit)) + })) + } }); } } @@ -107,14 +118,21 @@ public async Task Handle(CodeActionParams request, !ruleNamesProcessed.Contains(diagnostic.Code.String)) { ruleNamesProcessed.Add(diagnostic.Code.String); - - codeActions.Add( - new Command + var title = $"Show documentation for: {diagnostic.Code.String}"; + codeActions.Add(new CodeAction + { + Title = title, + // This doesn't fix anything, but I'm adding it here so that it shows up in VS Code's + // Quick fix UI. The VS Code team is working on a way to support documentation CodeAction's better + // but this is good for now until that's ready. + Kind = CodeActionKind.QuickFix, + Command = new Command { - Title = $"Show documentation for \"{diagnostic.Code}\"", + Title = title, Name = "PowerShell.ShowCodeActionDocumentation", - Arguments = JArray.FromObject(new[] { diagnostic.Code }) - }); + Arguments = JArray.FromObject(new[] { diagnostic.Code.String }) + } + }); } } diff --git a/src/PowerShellEditorServices/Services/TextDocument/ScriptRegion.cs b/src/PowerShellEditorServices/Services/TextDocument/ScriptRegion.cs index f18fcd6c4..73c8eb96c 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/ScriptRegion.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/ScriptRegion.cs @@ -5,6 +5,7 @@ using System; using System.Management.Automation.Language; +using OmniSharp.Extensions.LanguageServer.Protocol.Models; namespace Microsoft.PowerShell.EditorServices.Services.TextDocument { @@ -49,6 +50,27 @@ public static ScriptRegion Create(IScriptExtent scriptExtent) scriptExtent.EndOffset); } + internal static TextEdit ToTextEdit(ScriptRegion scriptRegion) + { + return new TextEdit + { + NewText = scriptRegion.Text, + Range = new Range + { + Start = new Position + { + Line = scriptRegion.StartLineNumber - 1, + Character = scriptRegion.StartColumnNumber - 1, + }, + End = new Position + { + Line = scriptRegion.EndLineNumber - 1, + Character = scriptRegion.EndColumnNumber - 1, + } + } + }; + } + #endregion #region Constructors diff --git a/test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs b/test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs index d9e9f735d..4774ed6d0 100644 --- a/test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs +++ b/test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs @@ -665,8 +665,23 @@ await LanguageClient.SendRequest( } }); - Assert.Single(commandOrCodeActions, - command => command.Command.Name == "PowerShell.ApplyCodeActionEdits"); + Assert.Collection(commandOrCodeActions, + command => + { + Assert.Equal( + "Replace gci with Get-ChildItem", + command.CodeAction.Title); + Assert.Equal( + CodeActionKind.QuickFix.Kind, + command.CodeAction.Kind.Kind); + Assert.Single(command.CodeAction.Edit.DocumentChanges); + }, + command => + { + Assert.Equal( + "PowerShell.ShowCodeActionDocumentation", + command.CodeAction.Command.Name); + }); } [Fact] diff --git a/test/PowerShellEditorServices.Test.E2E/PowerShellEditorServices.Test.E2E.csproj b/test/PowerShellEditorServices.Test.E2E/PowerShellEditorServices.Test.E2E.csproj index c62e8c303..7d38a7b12 100644 --- a/test/PowerShellEditorServices.Test.E2E/PowerShellEditorServices.Test.E2E.csproj +++ b/test/PowerShellEditorServices.Test.E2E/PowerShellEditorServices.Test.E2E.csproj @@ -10,7 +10,7 @@ - +