diff --git a/crates/tinymist-query/src/analysis/code_action.rs b/crates/tinymist-query/src/analysis/code_action.rs index c35aaa4a..55699afc 100644 --- a/crates/tinymist-query/src/analysis/code_action.rs +++ b/crates/tinymist-query/src/analysis/code_action.rs @@ -91,7 +91,7 @@ impl<'a> CodeActionWorker<'a> { self.autofix_remove_unused_import(root, &diag_range); } else { self.autofix_unused_symbol(&diag_range); - self.autofix_replace_with_placeholder(&diag_range); + self.autofix_replace_with_placeholder(root, &diag_range); self.autofix_remove_declaration(root, &diag_range); } } @@ -310,8 +310,12 @@ impl<'a> CodeActionWorker<'a> { Some(()) } - fn autofix_replace_with_placeholder(&mut self, range: &Range) -> Option<()> { - if range.is_empty() { + fn autofix_replace_with_placeholder( + &mut self, + root: &LinkedNode<'_>, + range: &Range, + ) -> Option<()> { + if range.is_empty() || self.is_spread_binding(root, range) { return None; } @@ -336,6 +340,23 @@ impl<'a> CodeActionWorker<'a> { Some(()) } + fn is_spread_binding(&self, root: &LinkedNode<'_>, range: &Range) -> bool { + if range.is_empty() { + return false; + } + + let cursor = (range.start + range.end) / 2; + let Some(node) = root.leaf_at_compat(cursor) else { + return false; + }; + + if node.kind() == SyntaxKind::Spread { + return true; + } + + node_ancestors(&node).any(|ancestor| ancestor.kind() == SyntaxKind::Spread) + } + /// Remove the declaration corresponding to an unused binding. fn autofix_remove_declaration( &mut self, diff --git a/crates/tinymist-query/src/fixtures/dead_code_code_action/snaps/run_dead_code_code_action_snapshots@destructuring_spread.typ.snap b/crates/tinymist-query/src/fixtures/dead_code_code_action/snaps/run_dead_code_code_action_snapshots@destructuring_spread.typ.snap index 9bb01a48..e663056c 100644 --- a/crates/tinymist-query/src/fixtures/dead_code_code_action/snaps/run_dead_code_code_action_snapshots@destructuring_spread.typ.snap +++ b/crates/tinymist-query/src/fixtures/dead_code_code_action/snaps/run_dead_code_code_action_snapshots@destructuring_spread.typ.snap @@ -1,6 +1,5 @@ --- source: crates/tinymist-query/src/code_action.rs -assertion_line: 180 description: Dead code code actions in /dummy-root/s0.typ expression: "JsonRepr::new_pure(ordered_entries)" input_file: crates/tinymist-query/src/fixtures/dead_code/destructuring_spread.typ @@ -22,21 +21,6 @@ input_file: crates/tinymist-query/src/fixtures/dead_code/destructuring_spread.ty }, "kind": "quickfix", "title": "Prefix `_` to `unused_rest`" - }, - { - "edit": { - "changes": { - "s0.typ": [ - { - "insertTextFormat": 1, - "newText": "_", - "range": "4:11:4:22" - } - ] - } - }, - "kind": "quickfix", - "title": "Replace with `_`" } ], "message": "unused variable: `unused_rest`\nHint: consider removing this variable or prefixing with underscore: `_unused_rest`",