Add remove_mut assist

This commit is contained in:
Aleksey Kladov 2020-02-19 12:44:20 +01:00
parent d07f043ef1
commit 312a779610
4 changed files with 67 additions and 0 deletions

View file

@ -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(

View file

@ -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<Assist> {
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));
})
}

View file

@ -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,
]

View file

@ -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.