summaryrefslogtreecommitdiff
path: root/support/texlab/src/features/rename/entry.rs
blob: 6eac79030b24780dfac568c13cf76c3036d56416 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use std::sync::Arc;

use lsp_types::RenameParams;
use rowan::{ast::AstNode, TextRange};
use rustc_hash::FxHashMap;

use crate::{
    features::cursor::{CursorContext, HasPosition},
    syntax::{
        bibtex::{self, HasName},
        latex,
    },
    DocumentData,
};

use super::{Indel, RenameResult};

pub(super) fn prepare_entry_rename<P: HasPosition>(
    context: &CursorContext<P>,
) -> Option<TextRange> {
    let (_, range) = context
        .find_citation_key_word()
        .or_else(|| context.find_entry_key())?;

    Some(range)
}

pub(super) fn rename_entry(context: &CursorContext<RenameParams>) -> Option<RenameResult> {
    prepare_entry_rename(context)?;
    let (key_text, _) = context
        .find_citation_key_word()
        .or_else(|| context.find_entry_key())?;

    let mut changes = FxHashMap::default();
    for document in context.request.workspace.documents_by_uri.values() {
        let uri = Arc::clone(&document.uri);
        match &document.data {
            DocumentData::Latex(data) => {
                let root = latex::SyntaxNode::new_root(data.green.clone());
                let edits: Vec<_> = root
                    .descendants()
                    .filter_map(latex::Citation::cast)
                    .filter_map(|citation| citation.key_list())
                    .flat_map(|keys| keys.keys())
                    .filter(|key| key.to_string() == key_text)
                    .map(|key| Indel {
                        delete: latex::small_range(&key),
                        insert: context.request.params.new_name.clone(),
                    })
                    .collect();
                changes.insert(uri, edits);
            }
            DocumentData::Bibtex(data) => {
                let root = bibtex::SyntaxNode::new_root(data.green.clone());
                let edits: Vec<_> = root
                    .descendants()
                    .filter_map(bibtex::Entry::cast)
                    .filter_map(|entry| entry.name_token())
                    .filter(|key| key.text() == key_text)
                    .map(|key| Indel {
                        delete: key.text_range(),
                        insert: context.request.params.new_name.clone(),
                    })
                    .collect();
                changes.insert(uri, edits);
            }
            DocumentData::BuildLog(_) => {}
        }
    }

    Some(RenameResult { changes })
}