diff --git a/crates/ra_assists/src/doc_tests/generated.rs b/crates/ra_assists/src/doc_tests/generated.rs index 4ab09b167b..3f56dd5084 100644 --- a/crates/ra_assists/src/doc_tests/generated.rs +++ b/crates/ra_assists/src/doc_tests/generated.rs @@ -548,6 +548,23 @@ fn main() { ) } +#[test] +fn doctest_remove_mut() { + check( + "remove_mut", + r#####" +impl Walrus { + fn feed(&mut<|> self, amount: u32) {} +} +"#####, + r#####" +impl Walrus { + fn feed(&self, amount: u32) {} +} +"#####, + ) +} + #[test] fn doctest_replace_if_let_with_match() { check( diff --git a/crates/ra_assists/src/handlers/remove_mut.rs b/crates/ra_assists/src/handlers/remove_mut.rs new file mode 100644 index 0000000000..6884830eb6 --- /dev/null +++ b/crates/ra_assists/src/handlers/remove_mut.rs @@ -0,0 +1,32 @@ +use ra_syntax::{SyntaxKind, TextRange, T}; + +use crate::{Assist, AssistCtx, AssistId}; + +// Assist: remove_mut +// +// Removes the `mut` keyword. +// +// ``` +// impl Walrus { +// fn feed(&mut<|> self, amount: u32) {} +// } +// ``` +// -> +// ``` +// impl Walrus { +// fn feed(&self, amount: u32) {} +// } +// ``` +pub(crate) fn remove_mut(ctx: AssistCtx) -> Option { + let mut_token = ctx.find_token_at_offset(T![mut])?; + let delete_from = mut_token.text_range().start(); + let delete_to = match mut_token.next_token() { + Some(it) if it.kind() == SyntaxKind::WHITESPACE => it.text_range().end(), + _ => mut_token.text_range().end(), + }; + + ctx.add_assist(AssistId("remove_mut"), "Remove `mut` keyword", |edit| { + edit.set_cursor(delete_from); + edit.delete(TextRange::from_to(delete_from, delete_to)); + }) +} diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index f4a7497dbe..d7998b0d1a 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -108,6 +108,7 @@ mod handlers { mod introduce_variable; mod inline_local_variable; mod raw_string; + mod remove_mut; mod replace_if_let_with_match; mod split_import; mod remove_dbg; @@ -147,6 +148,7 @@ mod handlers { raw_string::make_raw_string, raw_string::make_usual_string, raw_string::remove_hash, + remove_mut::remove_mut, early_return::convert_to_guarded_return, auto_import::auto_import, ] diff --git a/docs/user/assists.md b/docs/user/assists.md index f737a2fa49..9861332af8 100644 --- a/docs/user/assists.md +++ b/docs/user/assists.md @@ -527,6 +527,22 @@ fn main() { } ``` +## `remove_mut` + +Removes the `mut` keyword. + +```rust +// BEFORE +impl Walrus { + fn feed(&mut┃ self, amount: u32) {} +} + +// AFTER +impl Walrus { + fn feed(&self, amount: u32) {} +} +``` + ## `replace_if_let_with_match` Replaces `if let` with an else branch with a `match` expression.