summaryrefslogtreecommitdiff
path: root/support/texlab/src/rename/latex_command.rs
blob: 52bac7a8c946755011b4b4a352be5815f63631f8 (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
use crate::syntax::*;
use crate::workspace::*;
use futures_boxed::boxed;
use lsp_types::*;
use std::collections::HashMap;
use std::sync::Arc;

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

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

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

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

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

    #[boxed]
    async fn execute<'a>(
        &'a self,
        request: &'a FeatureRequest<RenameParams>,
    ) -> Option<WorkspaceEdit> {
        let command = find_command(
            &request.document().tree,
            request.params.text_document_position.position,
        )?;
        let mut changes = HashMap::new();
        for document in request.related_documents() {
            if let SyntaxTree::Latex(tree) = &document.tree {
                let edits: Vec<TextEdit> = tree
                    .commands
                    .iter()
                    .filter(|cmd| cmd.name.text() == command.name.text())
                    .map(|cmd| {
                        TextEdit::new(cmd.name.range(), format!("\\{}", request.params.new_name))
                    })
                    .collect();
                changes.insert(document.uri.clone().into(), edits);
            }
        }
        Some(WorkspaceEdit::new(changes))
    }
}

fn find_command(tree: &SyntaxTree, position: Position) -> Option<Arc<LatexCommand>> {
    if let SyntaxTree::Latex(tree) = tree {
        tree.find_command_by_name(position)
    } else {
        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::range::RangeExt;
    use lsp_types::{Position, Range};

    #[test]
    fn test() {
        let edit = test_feature(
            LatexCommandRenameProvider,
            FeatureSpec {
                files: vec![
                    FeatureSpec::file("foo.tex", "\\include{bar.tex}\n\\baz"),
                    FeatureSpec::file("bar.tex", "\\baz"),
                ],
                main_file: "foo.tex",
                position: Position::new(1, 2),
                new_name: "qux",
                ..FeatureSpec::default()
            },
        );
        let mut changes = HashMap::new();
        changes.insert(
            FeatureSpec::uri("foo.tex"),
            vec![TextEdit::new(Range::new_simple(1, 0, 1, 4), "\\qux".into())],
        );
        changes.insert(
            FeatureSpec::uri("bar.tex"),
            vec![TextEdit::new(Range::new_simple(0, 0, 0, 4), "\\qux".into())],
        );
        assert_eq!(edit, Some(WorkspaceEdit::new(changes)));
    }

    #[test]
    fn test_bibtex() {
        let edit = test_feature(
            LatexCommandRenameProvider,
            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);
    }
}