Skip to content

Commit

Permalink
WIP Fix unsued variable code code action
Browse files Browse the repository at this point in the history
Fix #218
  • Loading branch information
Draggu committed Feb 1, 2025
1 parent 08c222e commit eaa57a8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 74 deletions.
32 changes: 24 additions & 8 deletions src/ide/code_actions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use cairo_lang_syntax::node::SyntaxNode;
use cairo_lang_syntax::node::{SyntaxNode, Token, TypedSyntaxNode};
use cairo_lang_utils::ordered_hash_map::{Entry, OrderedHashMap};
use cairo_language_server_macros::node_path_match;
use itertools::Itertools;
use lsp_types::{
CodeAction, CodeActionOrCommand, CodeActionParams, CodeActionResponse, Diagnostic,
Expand All @@ -9,6 +10,7 @@ use tracing::{debug, warn};

use crate::lang::db::{AnalysisDatabase, LsSyntaxGroup};
use crate::lang::lsp::{LsProtoGroup, ToCairo};
use crate::lang::syntax::SyntaxNodeExt;

mod add_missing_trait;
mod create_module_file;
Expand Down Expand Up @@ -74,13 +76,27 @@ fn get_code_actions_for_diagnostics(
.flat_map(|(code, diagnostics)| match code.as_str() {
"E0001" => diagnostics
.into_iter()
.map(|diagnostic| {
rename_unused_variable::rename_unused_variable(
db,
node,
diagnostic.clone(),
params.text_document.uri.clone(),
)
.flat_map(|diagnostic| {
let action = |var_name: &str| {
let action = rename_unused_variable::rename_unused_variable(
var_name,
diagnostic.clone(),
params.text_document.uri.clone(),
);

Some([action])
};

node_path_match! {
node;

// let
node ..=> PathSegmentSimple => path: ExprPath => StatementLet
-> action(&path.as_syntax_node().get_text(db));
// let mut
node ..=> ident: PatternIdentifier => StatementLet
-> action(&ident.name(db).token(db).text(db));
}
})
.collect_vec(),
"E0002" => {
Expand Down
13 changes: 2 additions & 11 deletions src/ide/code_actions/rename_unused_variable.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
use std::collections::HashMap;

use cairo_lang_syntax::node::SyntaxNode;
use cairo_lang_utils::Upcast;
use lsp_types::{CodeAction, CodeActionKind, Diagnostic, TextEdit, Url, WorkspaceEdit};

use crate::lang::db::AnalysisDatabase;

/// Create a code action that prefixes an unused variable with an `_`.
pub fn rename_unused_variable(
db: &AnalysisDatabase,
node: &SyntaxNode,
diagnostic: Diagnostic,
uri: Url,
) -> CodeAction {
pub fn rename_unused_variable(var_name: &str, diagnostic: Diagnostic, uri: Url) -> CodeAction {
CodeAction {
title: format!("Rename to `_{}`", node.get_text(db.upcast())),
title: format!("Rename to `_{var_name}`"),
kind: Some(CodeActionKind::QUICKFIX),
edit: Some(WorkspaceEdit {
changes: Some(HashMap::from_iter([(
Expand Down
68 changes: 13 additions & 55 deletions tests/e2e/code_actions/remove_unused_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ fn on_let_keyword() {
fn a() {
le<caret>t b = 1234;
}
", @r#"
Title: Rename to `_let`
Add new text: "_"
At: Range { start: Position { line: 1, character: 8 }, end: Position { line: 1, character: 9 } }
"#);
", @"No code actions.");
}

#[test]
Expand All @@ -21,7 +17,7 @@ fn before_name() {
let <caret>b = 1234;
}
", @r#"
Title: Rename to `_b`
Title: Rename to `_b `
Add new text: "_"
At: Range { start: Position { line: 1, character: 8 }, end: Position { line: 1, character: 9 } }
"#);
Expand All @@ -34,7 +30,7 @@ fn after_name() {
let b<caret> = 1234;
}
", @r#"
Title: Rename to `_ `
Title: Rename to `_b `
Add new text: "_"
At: Range { start: Position { line: 1, character: 8 }, end: Position { line: 1, character: 9 } }
"#);
Expand All @@ -46,11 +42,7 @@ fn before_value() {
fn a() {
let b = <caret>1234;
}
", @r#"
Title: Rename to `_1234`
Add new text: "_"
At: Range { start: Position { line: 1, character: 8 }, end: Position { line: 1, character: 9 } }
"#);
", @"No code actions.");
}

#[test]
Expand All @@ -59,11 +51,7 @@ fn on_value() {
fn a() {
let b = 12<caret>34;
}
", @r#"
Title: Rename to `_1234`
Add new text: "_"
At: Range { start: Position { line: 1, character: 8 }, end: Position { line: 1, character: 9 } }
"#);
", @"No code actions.");
}

#[test]
Expand All @@ -72,11 +60,7 @@ fn after_value() {
fn a() {
let b = 1234<caret>;
}
", @r#"
Title: Rename to `_;`
Add new text: "_"
At: Range { start: Position { line: 1, character: 8 }, end: Position { line: 1, character: 9 } }
"#);
", @"No code actions.");
}

#[test]
Expand All @@ -85,12 +69,7 @@ fn after_let_statement() {
fn a() {
let b = 1234;<caret>
}
", @r#"
Title: Rename to `_
`
Add new text: "_"
At: Range { start: Position { line: 1, character: 8 }, end: Position { line: 1, character: 9 } }
"#);
", @"No code actions.");
}

#[test]
Expand All @@ -99,11 +78,7 @@ fn on_let_keyword_when_mut() {
fn a() {
le<caret>t mut b = 1234;
}
", @r#"
Title: Rename to `_let`
Add new text: "_"
At: Range { start: Position { line: 1, character: 12 }, end: Position { line: 1, character: 13 } }
"#);
", @"No code actions.");
}

#[test]
Expand All @@ -126,7 +101,7 @@ fn after_name_when_mut() {
let mut b<caret> = 1234;
}
", @r#"
Title: Rename to `_ `
Title: Rename to `_b`
Add new text: "_"
At: Range { start: Position { line: 1, character: 12 }, end: Position { line: 1, character: 13 } }
"#);
Expand All @@ -138,11 +113,7 @@ fn before_value_when_mut() {
fn a() {
let mut b = <caret>1234;
}
", @r#"
Title: Rename to `_1234`
Add new text: "_"
At: Range { start: Position { line: 1, character: 12 }, end: Position { line: 1, character: 13 } }
"#);
", @"No code actions.");
}

#[test]
Expand All @@ -151,11 +122,7 @@ fn on_value_when_mut() {
fn a() {
let mut b = 12<caret>34;
}
", @r#"
Title: Rename to `_1234`
Add new text: "_"
At: Range { start: Position { line: 1, character: 12 }, end: Position { line: 1, character: 13 } }
"#);
", @"No code actions.");
}

#[test]
Expand All @@ -164,11 +131,7 @@ fn after_value_when_mut() {
fn a() {
let mut b = 1234<caret>;
}
", @r#"
Title: Rename to `_;`
Add new text: "_"
At: Range { start: Position { line: 1, character: 12 }, end: Position { line: 1, character: 13 } }
"#);
", @"No code actions.");
}

#[test]
Expand All @@ -177,10 +140,5 @@ fn after_let_statement_when_mut() {
fn a() {
let mut b = 1234;<caret>
}
", @r#"
Title: Rename to `_
`
Add new text: "_"
At: Range { start: Position { line: 1, character: 12 }, end: Position { line: 1, character: 13 } }
"#);
", @"No code actions.");
}

0 comments on commit eaa57a8

Please sign in to comment.