summaryrefslogtreecommitdiff
path: root/support/texlab/src/rename/latex_env.rs
blob: 6e302ef8b61614f6f02eb1203d27835e3d72720d (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
use crate::{
    feature::{FeatureProvider, FeatureRequest},
    protocol::{
        Position, Range, RangeExt, RenameParams, TextDocumentPositionParams, TextEdit,
        WorkspaceEdit,
    },
    syntax::{latex, SyntaxNode},
    workspace::DocumentContent,
};
use async_trait::async_trait;
use std::collections::HashMap;

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

#[async_trait]
impl FeatureProvider for LatexEnvironmentPrepareRenameProvider {
    type Params = TextDocumentPositionParams;
    type Output = Option<Range>;

    async fn execute<'a>(&'a self, req: &'a FeatureRequest<Self::Params>) -> Self::Output {
        let pos = req.params.position;
        let (left_name, right_name) = find_environment(&req.current().content, pos)?;
        let range = if left_name.range().contains(pos) {
            left_name.range()
        } else {
            right_name.range()
        };
        Some(range)
    }
}

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

#[async_trait]
impl FeatureProvider for LatexEnvironmentRenameProvider {
    type Params = RenameParams;
    type Output = Option<WorkspaceEdit>;

    async fn execute<'a>(&'a self, req: &'a FeatureRequest<Self::Params>) -> Self::Output {
        let (left_name, right_name) = find_environment(
            &req.current().content,
            req.params.text_document_position.position,
        )?;
        let edits = vec![
            TextEdit::new(left_name.range(), req.params.new_name.clone()),
            TextEdit::new(right_name.range(), req.params.new_name.clone()),
        ];
        let mut changes = HashMap::new();
        changes.insert(req.current().uri.clone().into(), edits);
        Some(WorkspaceEdit::new(changes))
    }
}

fn find_environment(
    content: &DocumentContent,
    pos: Position,
) -> Option<(&latex::Token, &latex::Token)> {
    if let DocumentContent::Latex(table) = content {
        for env in &table.environments {
            if let Some(left_name) = env.left.name(&table) {
                if let Some(right_name) = env.right.name(&table) {
                    if left_name.range().contains(pos) || right_name.range().contains(pos) {
                        return Some((left_name, right_name));
                    }
                }
            }
        }
    }
    None
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::feature::FeatureTester;
    use indoc::indoc;

    #[tokio::test]
    async fn environment() {
        let actual_edit = FeatureTester::new()
            .file(
                "main.tex",
                indoc!(
                    r#"
                        \begin{foo}
                        \end{bar}
                    "#
                ),
            )
            .main("main.tex")
            .position(0, 8)
            .new_name("baz")
            .test_rename(LatexEnvironmentRenameProvider)
            .await
            .unwrap();

        let mut expected_changes = HashMap::new();
        expected_changes.insert(
            FeatureTester::uri("main.tex").into(),
            vec![
                TextEdit::new(Range::new_simple(0, 7, 0, 10), "baz".into()),
                TextEdit::new(Range::new_simple(1, 5, 1, 8), "baz".into()),
            ],
        );

        assert_eq!(actual_edit, WorkspaceEdit::new(expected_changes));
    }

    #[tokio::test]
    async fn command() {
        let actual_edit = FeatureTester::new()
            .file(
                "main.tex",
                indoc!(
                    r#"
                    \begin{foo}
                    \end{bar}
                "#
                ),
            )
            .main("main.tex")
            .position(0, 5)
            .new_name("baz")
            .test_rename(LatexEnvironmentRenameProvider)
            .await;

        assert_eq!(actual_edit, None);
    }

    #[tokio::test]
    async fn empty_latex_document() {
        let actual_edit = FeatureTester::new()
            .file("main.tex", "")
            .main("main.tex")
            .position(0, 0)
            .new_name("")
            .test_rename(LatexEnvironmentRenameProvider)
            .await;

        assert_eq!(actual_edit, None);
    }

    #[tokio::test]
    async fn empty_bibtex_document() {
        let actual_edit = FeatureTester::new()
            .file("main.bib", "")
            .main("main.bib")
            .position(0, 0)
            .new_name("")
            .test_rename(LatexEnvironmentRenameProvider)
            .await;

        assert_eq!(actual_edit, None);
    }
}