summaryrefslogtreecommitdiff
path: root/support/texlab/crates/rename/src/command.rs
blob: 7b17d4271cd7125fa600ac657be66b04b8223cc3 (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
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);
    }

    Some(())
}