summaryrefslogtreecommitdiff
path: root/support/texlab/src/rename/bibtex_entry.rs
blob: 46484c7f23130f0abdf8dbdcbda27f151fd7d0fc (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use crate::range::RangeExt;
use crate::syntax::*;
use crate::workspace::*;
use futures_boxed::boxed;
use lsp_types::*;
use std::collections::HashMap;

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct BibtexEntryPrepareRenameProvider;

impl FeatureProvider for BibtexEntryPrepareRenameProvider {
    type Params = TextDocumentPositionParams;
    type Output = Option<Range>;

    #[boxed]
    async fn execute<'a>(
        &'a self,
        request: &'a FeatureRequest<TextDocumentPositionParams>,
    ) -> Option<Range> {
        find_key(&request.document().tree, request.params.position).map(Span::range)
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct BibtexEntryRenameProvider;

impl FeatureProvider for BibtexEntryRenameProvider {
    type Params = RenameParams;
    type Output = Option<WorkspaceEdit>;

    #[boxed]
    async fn execute<'a>(
        &'a self,
        request: &'a FeatureRequest<RenameParams>,
    ) -> Option<WorkspaceEdit> {
        let key_name = find_key(
            &request.document().tree,
            request.params.text_document_position.position,
        )?;
        let mut changes = HashMap::new();
        for document in request.related_documents() {
            let mut edits = Vec::new();
            match &document.tree {
                SyntaxTree::Latex(tree) => {
                    tree.citations
                        .iter()
                        .flat_map(LatexCitation::keys)
                        .filter(|citation| citation.text() == key_name.text)
                        .map(|citation| {
                            TextEdit::new(citation.range(), request.params.new_name.clone())
                        })
                        .for_each(|edit| edits.push(edit));
                }
                SyntaxTree::Bibtex(tree) => {
                    for entry in tree.entries() {
                        if let Some(key) = &entry.key {
                            if key.text() == key_name.text {
                                edits.push(TextEdit::new(
                                    key.range(),
                                    request.params.new_name.clone(),
                                ));
                            }
                        }
                    }
                }
            };
            changes.insert(document.uri.clone().into(), edits);
        }
        Some(WorkspaceEdit::new(changes))
    }
}

fn find_key(tree: &SyntaxTree, position: Position) -> Option<&Span> {
    match tree {
        SyntaxTree::Latex(tree) => {
            for citation in &tree.citations {
                let keys = citation.keys();
                for key in keys {
                    if key.range().contains(position) {
                        return Some(&key.span);
                    }
                }
            }
            None
        }
        SyntaxTree::Bibtex(tree) => {
            for entry in tree.entries() {
                if let Some(key) = &entry.key {
                    if key.range().contains(position) {
                        return Some(&key.span);
                    }
                }
            }
            None
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use lsp_types::Position;

    #[test]
    fn test_entry() {
        let edit = test_feature(
            BibtexEntryRenameProvider,
            FeatureSpec {
                files: vec![
                    FeatureSpec::file("foo.bib", "@article{foo, bar = baz}"),
                    FeatureSpec::file("bar.tex", "\\addbibresource{foo.bib}\n\\cite{foo}"),
                ],
                main_file: "foo.bib",
                position: Position::new(0, 9),
                new_name: "qux",
                ..FeatureSpec::default()
            },
        );
        let mut changes = HashMap::new();
        changes.insert(
            FeatureSpec::uri("foo.bib"),
            vec![TextEdit::new(Range::new_simple(0, 9, 0, 12), "qux".into())],
        );
        changes.insert(
            FeatureSpec::uri("bar.tex"),
            vec![TextEdit::new(Range::new_simple(1, 6, 1, 9), "qux".into())],
        );
        assert_eq!(edit, Some(WorkspaceEdit::new(changes)));
    }

    #[test]
    fn test_citation() {
        let edit = test_feature(
            BibtexEntryRenameProvider,
            FeatureSpec {
                files: vec![
                    FeatureSpec::file("foo.bib", "@article{foo, bar = baz}"),
                    FeatureSpec::file("bar.tex", "\\addbibresource{foo.bib}\n\\cite{foo}"),
                ],
                main_file: "bar.tex",
                position: Position::new(1, 6),
                new_name: "qux",
                ..FeatureSpec::default()
            },
        );
        let mut changes = HashMap::new();
        changes.insert(
            FeatureSpec::uri("foo.bib"),
            vec![TextEdit::new(Range::new_simple(0, 9, 0, 12), "qux".into())],
        );
        changes.insert(
            FeatureSpec::uri("bar.tex"),
            vec![TextEdit::new(Range::new_simple(1, 6, 1, 9), "qux".into())],
        );
        assert_eq!(edit, Some(WorkspaceEdit::new(changes)));
    }

    #[test]
    fn test_field_name() {
        let edit = test_feature(
            BibtexEntryRenameProvider,
            FeatureSpec {
                files: vec![FeatureSpec::file("foo.bib", "@article{foo, bar = baz}")],
                main_file: "foo.bib",
                position: Position::new(0, 14),
                new_name: "qux",
                ..FeatureSpec::default()
            },
        );
        assert_eq!(edit, None);
    }
}