From adfa0393049d385115517632b68b4e3ae330a941 Mon Sep 17 00:00:00 2001 From: takeokunn Date: Sat, 14 Feb 2026 18:13:09 +0900 Subject: [PATCH] feat(code-action): add quickfix to remove empty inherit (#755 Split 16) Mark DK_EmptyInherit diagnostic as isPreferred in the code action handler so that editors surface the existing "remove `inherit` keyword" fix as a preferred quickfix. Closes part of #466. --- nixd/lib/Controller/CodeAction.cpp | 1 + .../empty-inherit/empty-inherit.md | 107 ++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 nixd/tools/nixd/test/code-action/empty-inherit/empty-inherit.md diff --git a/nixd/lib/Controller/CodeAction.cpp b/nixd/lib/Controller/CodeAction.cpp index 6e737116f..d1731cdba 100644 --- a/nixd/lib/Controller/CodeAction.cpp +++ b/nixd/lib/Controller/CodeAction.cpp @@ -51,6 +51,7 @@ void Controller::onCodeAction(const lspserver::CodeActionParams &Params, bool IsPreferred = false; switch (D.kind()) { case nixf::Diagnostic::DK_UnusedDefLet: + case nixf::Diagnostic::DK_EmptyInherit: IsPreferred = true; break; default: diff --git a/nixd/tools/nixd/test/code-action/empty-inherit/empty-inherit.md b/nixd/tools/nixd/test/code-action/empty-inherit/empty-inherit.md new file mode 100644 index 000000000..080720a09 --- /dev/null +++ b/nixd/tools/nixd/test/code-action/empty-inherit/empty-inherit.md @@ -0,0 +1,107 @@ +# RUN: nixd --lit-test < %s | FileCheck %s + +Test that `inherit;` (empty inherit) gets a quickfix to remove it. + +<-- initialize(0) + +```json +{ + "jsonrpc":"2.0", + "id":0, + "method":"initialize", + "params":{ + "processId":123, + "rootPath":"", + "capabilities":{ + }, + "trace":"off" + } +} +``` + + +<-- textDocument/didOpen + +```nix file:///empty-inherit.nix +{ inherit; } +``` + +<-- textDocument/codeAction(1) + + +```json +{ + "jsonrpc":"2.0", + "id":1, + "method":"textDocument/codeAction", + "params":{ + "textDocument":{ + "uri":"file:///empty-inherit.nix" + }, + "range":{ + "start":{ + "line": 0, + "character":2 + }, + "end":{ + "line":0, + "character":9 + } + }, + "context":{ + "diagnostics":[], + "triggerKind":2 + } + } +} +``` + +The action removes both `inherit` and `;`, producing `{ }`. + +``` + CHECK: "id": 1, +CHECK-NEXT: "jsonrpc": "2.0", +CHECK-NEXT: "result": [ +CHECK-NEXT: { +CHECK-NEXT: "edit": { +CHECK-NEXT: "changes": { +CHECK-NEXT: "file:///empty-inherit.nix": [ +CHECK-NEXT: { +CHECK-NEXT: "newText": "", +CHECK-NEXT: "range": { +CHECK-NEXT: "end": { +CHECK-NEXT: "character": 9, +CHECK-NEXT: "line": 0 +CHECK-NEXT: }, +CHECK-NEXT: "start": { +CHECK-NEXT: "character": 2, +CHECK-NEXT: "line": 0 +CHECK-NEXT: } +CHECK-NEXT: } +CHECK-NEXT: }, +CHECK-NEXT: { +CHECK-NEXT: "newText": "", +CHECK-NEXT: "range": { +CHECK-NEXT: "end": { +CHECK-NEXT: "character": 10, +CHECK-NEXT: "line": 0 +CHECK-NEXT: }, +CHECK-NEXT: "start": { +CHECK-NEXT: "character": 9, +CHECK-NEXT: "line": 0 +CHECK-NEXT: } +CHECK-NEXT: } +CHECK-NEXT: } +CHECK-NEXT: ] +CHECK-NEXT: } +CHECK-NEXT: }, +CHECK-NEXT: "isPreferred": true, +CHECK-NEXT: "kind": "quickfix", +CHECK-NEXT: "title": "remove `inherit` keyword" +CHECK-NEXT: } +CHECK-NEXT: ] +``` + +```json +{"jsonrpc":"2.0","method":"exit"} +```