]> git.lizzy.rs Git - rust.git/blob - src/tools/rust-analyzer/crates/ide-assists/src/handlers/remove_mut.rs
Rollup merge of #98391 - joboet:sgx_parker, r=m-ou-se
[rust.git] / src / tools / rust-analyzer / crates / ide-assists / src / handlers / remove_mut.rs
1 use syntax::{SyntaxKind, TextRange, T};
2
3 use crate::{AssistContext, AssistId, AssistKind, Assists};
4
5 // Assist: remove_mut
6 //
7 // Removes the `mut` keyword.
8 //
9 // ```
10 // impl Walrus {
11 //     fn feed(&mut$0 self, amount: u32) {}
12 // }
13 // ```
14 // ->
15 // ```
16 // impl Walrus {
17 //     fn feed(&self, amount: u32) {}
18 // }
19 // ```
20 pub(crate) fn remove_mut(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
21     let mut_token = ctx.find_token_syntax_at_offset(T![mut])?;
22     let delete_from = mut_token.text_range().start();
23     let delete_to = match mut_token.next_token() {
24         Some(it) if it.kind() == SyntaxKind::WHITESPACE => it.text_range().end(),
25         _ => mut_token.text_range().end(),
26     };
27
28     let target = mut_token.text_range();
29     acc.add(
30         AssistId("remove_mut", AssistKind::Refactor),
31         "Remove `mut` keyword",
32         target,
33         |builder| {
34             builder.delete(TextRange::new(delete_from, delete_to));
35         },
36     )
37 }