summaryrefslogtreecommitdiff
path: root/support/texlab/crates/rename/src/command.rs
blob: 037e09e4ca8d2d7e87615524e193213750952597 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use base_db::{semantics::Span, DocumentData};
use rowan::{TextRange, TextSize};
use syntax::latex;

use crate::{RenameBuilder, RenameParams};

pub(super) fn prepare_rename(params: &RenameParams) -> Option<Span> {
    let data = params.feature.document.data.as_tex()?;
    let token = data
        .root_node()
        .token_at_offset(params.offset)
        .find(|token| token.kind() == latex::COMMAND_NAME)?;

    let range = token.text_range();
    let text = token.text()[1..].into();
    Some(Span::new(
        text,
        TextRange::new(range.start() + TextSize::of('\\'), range.end()),
    ))
}

pub(super) fn rename(builder: &mut RenameBuilder) -> Option<()> {
    let name = prepare_rename(&builder.params)?;

    for document in &builder.params.feature.project.documents {
        let DocumentData::Tex(data) = &document.data else {
            continue;
        };

        let mut edits = Vec::new();
        for command in &data.semantics.commands {
            if command.text == name.text {
                let range = TextRange::new(command.range.start(), command.range.end());
                edits.push(range);
            }
        }

        builder
            .result
            .changes
            .insert(*document, edits.iter().map(|&x| x.into()).collect());
    }

    Some(())
}