Skip to content

Commit a4c4fdf

Browse files
bergmeisterandyleejordan
authored andcommitted
Return a code action for each diagnostic record
Currently, PSSA's `SuggestCorrections` property of the `DiagnosticRecord` object already is an array, but no built-in rules return more than one suggested correction, which led to `PowerShellEditorServices` not correctly translating this array before returning it as an code action as it only ever displayed one. This fixes it by making it generic again so it returns a code action for each suggest correction. It's basically just performing a `foreach` loop and calling `ToTextEdit` just once. This is a pre-requisite for an implementation of this, where a built-in rule will return multiple suggest corrections: PowerShell/PSScriptAnalyzer#1767 I've manually tested this with a modified version of PSScriptAnalyzer where I return two suggested corrections. The extension's UI now shows me the two different suggested code actions and also applies them correctly.
1 parent 640eac8 commit a4c4fdf

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

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

+18-15
Original file line numberDiff line numberDiff line change
@@ -81,24 +81,27 @@ public override async Task<CommandOrCodeActionContainer> Handle(CodeActionParams
8181
string diagnosticId = AnalysisService.GetUniqueIdFromDiagnostic(diagnostic);
8282
if (corrections.TryGetValue(diagnosticId, out MarkerCorrection correction))
8383
{
84-
codeActions.Add(new CodeAction
84+
foreach (ScriptRegion edit in correction.Edits)
8585
{
86-
Title = correction.Name,
87-
Kind = CodeActionKind.QuickFix,
88-
Edit = new WorkspaceEdit
86+
codeActions.Add(new CodeAction
8987
{
90-
DocumentChanges = new Container<WorkspaceEditDocumentChange>(
91-
new WorkspaceEditDocumentChange(
92-
new TextDocumentEdit
93-
{
94-
TextDocument = new OptionalVersionedTextDocumentIdentifier
88+
Title = correction.Name,
89+
Kind = CodeActionKind.QuickFix,
90+
Edit = new WorkspaceEdit
91+
{
92+
DocumentChanges = new Container<WorkspaceEditDocumentChange>(
93+
new WorkspaceEditDocumentChange(
94+
new TextDocumentEdit
9595
{
96-
Uri = request.TextDocument.Uri
97-
},
98-
Edits = new TextEditContainer(correction.Edits.Select(ScriptRegion.ToTextEdit))
99-
}))
100-
}
101-
});
96+
TextDocument = new OptionalVersionedTextDocumentIdentifier
97+
{
98+
Uri = request.TextDocument.Uri
99+
},
100+
Edits = new TextEditContainer(ScriptRegion.ToTextEdit(edit))
101+
}))
102+
}
103+
});
104+
}
102105
}
103106
}
104107

0 commit comments

Comments
 (0)