From 312a7796105ae33f8f73e77291c0c4d46f6202d5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 19 Feb 2020 12:44:20 +0100 Subject: Add `remove_mut` assist --- crates/ra_assists/src/doc_tests/generated.rs | 17 +++++++++++++++ crates/ra_assists/src/handlers/remove_mut.rs | 32 ++++++++++++++++++++++++++++ crates/ra_assists/src/lib.rs | 2 ++ 3 files changed, 51 insertions(+) create mode 100644 crates/ra_assists/src/handlers/remove_mut.rs (limited to 'crates/ra_assists/src') diff --git a/crates/ra_assists/src/doc_tests/generated.rs b/crates/ra_assists/src/doc_tests/generated.rs index 4ab09b167..3f56dd508 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 000000000..6884830eb --- /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 f4a7497db..d7998b0d1 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, ] -- cgit v1.2.3