Skip to content

Commit 3fcd0c6

Browse files
Move to CodeAction (#1193)
* movve to CodeAction * working * use edit * test update * get test working * codacy * upgrade omnisharp version for test client fix
1 parent 8e8728b commit 3fcd0c6

File tree

5 files changed

+71
-16
lines changed

5 files changed

+71
-16
lines changed

src/PowerShellEditorServices/PowerShellEditorServices.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
3737
<PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" Version="3.1.1" />
3838
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.1" />
39-
<PackageReference Include="OmniSharp.Extensions.LanguageServer" Version="0.14.2" />
39+
<PackageReference Include="OmniSharp.Extensions.LanguageServer" Version="0.15.0" />
4040
<PackageReference Include="PowerShellStandard.Library" Version="5.1.1" />
4141
<PackageReference Include="Serilog" Version="2.9.0" />
4242
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />
@@ -46,6 +46,6 @@
4646
<PackageReference Include="System.Security.Principal" Version="4.3.0" />
4747
<PackageReference Include="System.Security.Principal.Windows" Version="4.7.0" />
4848
<PackageReference Include="UnixConsoleEcho" Version="0.1.0" />
49-
<PackageReference Include="OmniSharp.Extensions.DebugAdapter.Server" Version="0.14.2" />
49+
<PackageReference Include="OmniSharp.Extensions.DebugAdapter.Server" Version="0.15.0" />
5050
</ItemGroup>
5151
</Project>

src/PowerShellEditorServices/Services/TextDocument/Handlers/CodeActionHandler.cs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
using System;
77
using System.Collections.Generic;
8-
using System.Diagnostics;
9-
using System.Runtime.InteropServices;
8+
using System.Linq;
109
using System.Threading;
1110
using System.Threading.Tasks;
1211
using Microsoft.Extensions.Logging;
@@ -86,11 +85,23 @@ public async Task<CommandOrCodeActionContainer> Handle(CodeActionParams request,
8685
string diagnosticId = AnalysisService.GetUniqueIdFromDiagnostic(diagnostic);
8786
if (corrections.TryGetValue(diagnosticId, out MarkerCorrection correction))
8887
{
89-
codeActions.Add(new Command()
88+
codeActions.Add(new CodeAction
9089
{
9190
Title = correction.Name,
92-
Name = "PowerShell.ApplyCodeActionEdits",
93-
Arguments = JArray.FromObject(correction.Edits)
91+
Kind = CodeActionKind.QuickFix,
92+
Edit = new WorkspaceEdit
93+
{
94+
DocumentChanges = new Container<WorkspaceEditDocumentChange>(
95+
new WorkspaceEditDocumentChange(
96+
new TextDocumentEdit
97+
{
98+
TextDocument = new VersionedTextDocumentIdentifier
99+
{
100+
Uri = request.TextDocument.Uri
101+
},
102+
Edits = new Container<TextEdit>(correction.Edits.Select(ScriptRegion.ToTextEdit))
103+
}))
104+
}
94105
});
95106
}
96107
}
@@ -107,14 +118,21 @@ public async Task<CommandOrCodeActionContainer> Handle(CodeActionParams request,
107118
!ruleNamesProcessed.Contains(diagnostic.Code.String))
108119
{
109120
ruleNamesProcessed.Add(diagnostic.Code.String);
110-
111-
codeActions.Add(
112-
new Command
121+
var title = $"Show documentation for: {diagnostic.Code.String}";
122+
codeActions.Add(new CodeAction
123+
{
124+
Title = title,
125+
// This doesn't fix anything, but I'm adding it here so that it shows up in VS Code's
126+
// Quick fix UI. The VS Code team is working on a way to support documentation CodeAction's better
127+
// but this is good for now until that's ready.
128+
Kind = CodeActionKind.QuickFix,
129+
Command = new Command
113130
{
114-
Title = $"Show documentation for \"{diagnostic.Code}\"",
131+
Title = title,
115132
Name = "PowerShell.ShowCodeActionDocumentation",
116-
Arguments = JArray.FromObject(new[] { diagnostic.Code })
117-
});
133+
Arguments = JArray.FromObject(new[] { diagnostic.Code.String })
134+
}
135+
});
118136
}
119137
}
120138

src/PowerShellEditorServices/Services/TextDocument/ScriptRegion.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using System;
77
using System.Management.Automation.Language;
8+
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
89

910
namespace Microsoft.PowerShell.EditorServices.Services.TextDocument
1011
{
@@ -49,6 +50,27 @@ public static ScriptRegion Create(IScriptExtent scriptExtent)
4950
scriptExtent.EndOffset);
5051
}
5152

53+
internal static TextEdit ToTextEdit(ScriptRegion scriptRegion)
54+
{
55+
return new TextEdit
56+
{
57+
NewText = scriptRegion.Text,
58+
Range = new Range
59+
{
60+
Start = new Position
61+
{
62+
Line = scriptRegion.StartLineNumber - 1,
63+
Character = scriptRegion.StartColumnNumber - 1,
64+
},
65+
End = new Position
66+
{
67+
Line = scriptRegion.EndLineNumber - 1,
68+
Character = scriptRegion.EndColumnNumber - 1,
69+
}
70+
}
71+
};
72+
}
73+
5274
#endregion
5375

5476
#region Constructors

test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,8 +665,23 @@ await LanguageClient.SendRequest<CommandOrCodeActionContainer>(
665665
}
666666
});
667667

668-
Assert.Single(commandOrCodeActions,
669-
command => command.Command.Name == "PowerShell.ApplyCodeActionEdits");
668+
Assert.Collection(commandOrCodeActions,
669+
command =>
670+
{
671+
Assert.Equal(
672+
"Replace gci with Get-ChildItem",
673+
command.CodeAction.Title);
674+
Assert.Equal(
675+
CodeActionKind.QuickFix.Kind,
676+
command.CodeAction.Kind.Kind);
677+
Assert.Single(command.CodeAction.Edit.DocumentChanges);
678+
},
679+
command =>
680+
{
681+
Assert.Equal(
682+
"PowerShell.ShowCodeActionDocumentation",
683+
command.CodeAction.Command.Name);
684+
});
670685
}
671686

672687
[Fact]

test/PowerShellEditorServices.Test.E2E/PowerShellEditorServices.Test.E2E.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.1" />
1111
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
1212
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
13-
<PackageReference Include="OmniSharp.Extensions.LanguageClient" Version="0.14.2" />
13+
<PackageReference Include="OmniSharp.Extensions.LanguageClient" Version="0.15.0" />
1414
<PackageReference Include="xunit" Version="2.4.0" />
1515
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
1616
</ItemGroup>

0 commit comments

Comments
 (0)