fix(csharp): apply Roslyn's suppress and severity quick fixes (#7667)

Choosing Roslyn's "Suppress or configure issues" on a C# diagnostic did nothing: Roslyn returns its entries in the client command roslyn.client.nestedCodeAction, which the IDE did not handle. The entries are now listed by group (suppress, severity of the ID, of its category, of all analyzers) and applied like other code actions, so severities are written to .editorconfig. A newly created .editorconfig was ignored until a project reload, so the projects below it are now reported to Roslyn as changed. The real Roslyn test is ignored by default.

Closes #7646

Co-authored-by: wildcatDownstairs <[email protected]>
Цей коміт міститься в:
LU XIANGRUIтаwildcatDownstairs authored and GitHub committed 2026-12-03 16:45:44 +08:00
1 джерело 62bac6ec8c
коміт 27164e8919
10 files changed
+535 -3

No files matched your search

+2
Переглянути файл
@@ -1,4 +1,5 @@
import { MOVE_TO_FILE_COMMAND, moveToFile } from "./typescriptCommands";
import { chooseRoslynAction, ROSLYN_NESTED_COMMAND } from "./roslynActions";
import { serverCodeLenses } from "./serverCodeLens";
/**
* LSP-backed editor features: completion, diagnostics, hover / quick docs,
@@ -141,6 +142,7 @@ export function activate(): () => void {
registerOverlay({ id: "editor-lsp.memberChooser", component: MemberChooserDialog }),
registerClientCommand(JAVA_REFACTORING_COMMAND, runJavaRefactoringCommand),
registerClientCommand(MOVE_TO_FILE_COMMAND, moveToFile),
registerClientCommand(ROSLYN_NESTED_COMMAND, chooseRoslynAction),
registerEditorBanner(outsideProjectBanner),
registerStatusItem({ id: "editor-lsp.usagesStatus", align: "left", priority: 10, component: UsagesStatus }),
// IDEA's Documentation tool window: right side, listed once F1 moved documentation there.
+2 -2
Переглянути файл
@@ -237,7 +237,7 @@ export async function applyQuickFix(view: EditorView) {
* Commands a server leaves to the client (jdtls' `java.action.applyRefactoringCommand`, which
* opens a dialog or asks for a destination). A handler returns false for arguments it doesn't take.
*/
type ClientCommand = (view: EditorView, args: readonly unknown[]) => Promise<boolean>;
type ClientCommand = (view: EditorView, args: readonly unknown[], action: CodeAction) => Promise<boolean>;
const clientCommands = new Map<string, ClientCommand>();
export function registerClientCommand(name: string, run: ClientCommand): () => void {
@@ -264,7 +264,7 @@ export async function applyCodeAction(view: EditorView, action: EditorQuickFix)
return;
}
const client = !action.edit && action.command ? clientCommands.get(action.command.command) : undefined;
if (client && (await client(view, action.command!.arguments.map(parseArgument)))) return;
if (client && (await client(view, action.command!.arguments.map(parseArgument), action))) return;
const before = view.state.doc;
// The server's edit and its reformatting undo as one step (with the refactoring's own Undo).
const grouped = beginHistoryGroup(view);
+93
Переглянути файл
@@ -0,0 +1,93 @@
import { EditorState } from "@codemirror/state";
import { describe, expect, it, vi } from "vitest";
import type { CodeAction } from "@/bindings";
const edit = { files: [{ kind: "create" as const, path: "/p/.editorconfig", overwrite: false }] };
const lspResolveCodeAction = vi.fn(async (_doc: number, action: CodeAction) => ({
status: "ok" as const,
data: { ...action, edit },
}));
const lspExecuteCodeAction = vi.fn(async (_request: number, _doc: number, _action: CodeAction) => ({
status: "ok" as const,
data: [] as string[],
}));
vi.mock("@/platform/ipc", async (orig) => {
const actual = await orig<typeof import("@/platform/ipc")>();
return {
...actual,
isTauri: () => true,
commands: { ...actual.commands, lspResolveCodeAction, lspExecuteCodeAction },
};
});
type Item = { label: string; section: { label: string }; run: () => Promise<void> };
const popups: { title: string; items: Item[] }[] = [];
vi.mock("./popupStore", async (orig) => ({
...(await orig<typeof import("./popupStore")>()),
caretAnchor: () => ({ x: 0, y: 0 }),
openPopup: (p: (typeof popups)[number]) => popups.push(p),
}));
vi.mock("@/editor/api", async (orig) => ({
...(await orig<typeof import("@/editor/api")>()),
whenSynced: async () => {},
}));
vi.mock("./lsp", async (orig) => ({
...(await orig<typeof import("./lsp")>()),
docOf: () => ({ doc: 7, path: "/p/Program.cs", language: "csharp" }),
}));
const { chooseRoslynAction } = await import("./roslynActions");
/** The shape `roslyn-language-server` 5.13 answers. */
const entry = (group: string, title: string) => ({
title,
kind: "quickfix",
data: { CodeActionPath: ["Suppress or configure issues", group, title] },
});
const parent = {
title: "Suppress or configure issues",
kind: "quickfix",
diagnostics: [],
isPreferred: false,
disabledReason: null,
edit: null,
command: null,
data: null,
server: 3,
} as CodeAction;
const view = { state: EditorState.create({ doc: "class C;" }), themeClasses: "" } as never;
describe("Roslyn's nested code actions", () => {
it("lists the entries by group without Fix All, then resolves and applies the chosen one", async () => {
const args = [
{
NestedCodeActions: [
entry("Suppress CS8618", "in Source"),
{
...entry("Suppress CS8618", "Fix All: in Source"),
command: { title: "", command: "roslyn.client.fixAllCodeAction" },
},
entry("Configure CS8618 severity", "None"),
entry("Configure CS8618 severity", "Error"),
],
},
];
expect(await chooseRoslynAction(view, args, parent)).toBe(true);
const { title, items } = popups[0]!;
expect(title).toBe("Suppress or configure issues");
expect(items.map((i) => `${i.section.label} / ${i.label}`)).toEqual([
"Suppress CS8618 / in Source",
"Configure CS8618 severity / None",
"Configure CS8618 severity / Error",
]);
void items[2]!.run();
await vi.waitFor(() => expect(lspExecuteCodeAction).toHaveBeenCalled());
expect(lspResolveCodeAction.mock.calls[0]![1]).toMatchObject({ title: "Error", server: 3, edit: null });
expect(lspExecuteCodeAction.mock.calls[0]![2]).toMatchObject({ data: args[0]!.NestedCodeActions[3]!.data, edit });
});
it("leaves arguments it does not know to the server", async () => {
expect(await chooseRoslynAction(view, ["x"], parent)).toBe(false);
expect(await chooseRoslynAction(view, [], parent)).toBe(false);
});
});
+58
Переглянути файл
@@ -0,0 +1,58 @@
/**
* Roslyn leaves its nested code actions ("Suppress or configure issues": suppressions and
* `.editorconfig` severities) to the client. Their entries are listed under their group's title
* and each is resolved and applied like any code action.
*/
import type { EditorView } from "@codemirror/view";
import type { CodeAction } from "@/platform/ipc";
import { applyCodeAction } from "./intentions";
import { caretAnchor, openPopup } from "./popupStore";
export const ROSLYN_NESTED_COMMAND = "roslyn.client.nestedCodeAction";
/** Fix All needs a scope the IDE has no dialog for. */
const ROSLYN_FIX_ALL_COMMAND = "roslyn.client.fixAllCodeAction";
interface NestedAction {
title: string;
kind?: string;
data?: { CodeActionPath?: string[] };
command?: { title: string; command: string; arguments?: unknown[] };
}
export async function chooseRoslynAction(
view: EditorView,
args: readonly unknown[],
parent: CodeAction,
): Promise<boolean> {
const nested = (args[0] as { NestedCodeActions?: NestedAction[] } | undefined)?.NestedCodeActions;
const entries = nested?.filter((a) => a.command?.command !== ROSLYN_FIX_ALL_COMMAND);
if (!entries?.length) return false;
openPopup({
kind: "list",
anchor: caretAnchor(view, view.state.selection.main.head),
title: parent.title,
themeClasses: view.themeClasses,
items: entries.map((entry, i) => {
// The path is the parent's title, the group's, then the entry's own.
const group = entry.data?.CodeActionPath?.slice(1, -1).join(" › ") ?? "";
return {
id: `roslyn:${i}`,
label: entry.title,
section: { id: group, label: group },
run: () =>
applyCodeAction(view, {
...parent,
title: entry.title,
kind: entry.kind ?? parent.kind,
data: (entry.data ?? null) as CodeAction["data"],
edit: null,
command: entry.command
? { ...entry.command, arguments: (entry.command.arguments ?? []) as string[] }
: null,
}),
};
}),
});
return true;
}