2020-08-12 11:26:51 -05:00
|
|
|
use syntax::{SyntaxKind, TextRange, T};
|
2020-02-19 05:44:20 -06:00
|
|
|
|
2020-06-28 17:36:05 -05:00
|
|
|
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
2020-02-19 05:44:20 -06:00
|
|
|
|
|
|
|
// Assist: remove_mut
|
|
|
|
//
|
|
|
|
// Removes the `mut` keyword.
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// impl Walrus {
|
2021-01-06 14:15:48 -06:00
|
|
|
// fn feed(&mut$0 self, amount: u32) {}
|
2020-02-19 05:44:20 -06:00
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// impl Walrus {
|
|
|
|
// fn feed(&self, amount: u32) {}
|
|
|
|
// }
|
|
|
|
// ```
|
2022-07-20 08:02:08 -05:00
|
|
|
pub(crate) fn remove_mut(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
2020-11-06 15:21:56 -06:00
|
|
|
let mut_token = ctx.find_token_syntax_at_offset(T![mut])?;
|
2020-02-19 05:44:20 -06:00
|
|
|
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(),
|
|
|
|
};
|
|
|
|
|
2020-05-06 05:51:28 -05:00
|
|
|
let target = mut_token.text_range();
|
2020-06-28 17:36:05 -05:00
|
|
|
acc.add(
|
2020-07-02 16:48:35 -05:00
|
|
|
AssistId("remove_mut", AssistKind::Refactor),
|
2020-06-28 17:36:05 -05:00
|
|
|
"Remove `mut` keyword",
|
|
|
|
target,
|
|
|
|
|builder| {
|
|
|
|
builder.delete(TextRange::new(delete_from, delete_to));
|
|
|
|
},
|
|
|
|
)
|
2020-02-19 05:44:20 -06:00
|
|
|
}
|