rust-analyzer/crates/ra_assists/src/handlers/remove_mut.rs
Aleksey Kladov 4867968d22 Refactor assists API to be more convenient for adding new assists
It now duplicates completion API in its shape.
2020-05-07 16:27:54 +02:00

33 lines
912 B
Rust

use ra_syntax::{SyntaxKind, TextRange, T};
use crate::{AssistContext, AssistId, Assists};
// 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(acc: &mut Assists, ctx: &AssistContext) -> 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(),
};
let target = mut_token.text_range();
acc.add(AssistId("remove_mut"), "Remove `mut` keyword", target, |edit| {
edit.set_cursor(delete_from);
edit.delete(TextRange::new(delete_from, delete_to));
})
}