summaryrefslogtreecommitdiff
path: root/support/texlab/src/completion/latex/combinators.rs
blob: 74337f85b6a708a9b0c63ee910085e96e2061deb (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
use crate::range::RangeExt;
use crate::syntax::*;
use crate::workspace::*;
use lsp_types::*;
use std::future::Future;
use std::sync::Arc;

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct Parameter<'a> {
    pub name: &'a str,
    pub index: usize,
}

impl<'a> Parameter<'a> {
    pub fn new(name: &'a str, index: usize) -> Self {
        Self { name, index }
    }
}

pub async fn command<E, F>(
    request: &FeatureRequest<CompletionParams>,
    execute: E,
) -> Vec<CompletionItem>
where
    E: FnOnce(Arc<LatexCommand>) -> F,
    F: Future<Output = Vec<CompletionItem>>,
{
    if let SyntaxTree::Latex(tree) = &request.document().tree {
        if let Some(command) =
            tree.find_command_by_name(request.params.text_document_position.position)
        {
            return execute(command).await;
        }
    }
    Vec::new()
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ArgumentContext<'a> {
    pub parameter: Parameter<'a>,
    pub command: Arc<LatexCommand>,
    pub range: Range,
}

pub async fn argument<'a, I, E, F>(
    request: &'a FeatureRequest<CompletionParams>,
    mut parameters: I,
    execute: E,
) -> Vec<CompletionItem>
where
    I: Iterator<Item = Parameter<'a>>,
    E: FnOnce(ArgumentContext<'a>) -> F,
    F: Future<Output = Vec<CompletionItem>>,
{
    if let SyntaxTree::Latex(tree) = &request.document().tree {
        let position = request.params.text_document_position.position;
        if let Some(command) = find_command(tree, position) {
            for parameter in parameters.by_ref() {
                if command.name.text() != parameter.name {
                    continue;
                }

                if let Some(args) = command.args.get(parameter.index) {
                    if args.right.is_some() && !args.range().contains_exclusive(position) {
                        continue;
                    }

                    let mut range = None;
                    for child in &args.children {
                        if let LatexContent::Text(text) = &child {
                            for word in &text.words {
                                if word.range().contains(position) {
                                    range = Some(word.range());
                                    break;
                                }
                            }
                        }
                    }
                    let text_range = range.unwrap_or_else(|| Range::new(position, position));
                    let context = ArgumentContext {
                        parameter,
                        command: Arc::clone(&command),
                        range: text_range,
                    };
                    return execute(context).await;
                }
            }
        }
    }
    Vec::new()
}

pub async fn argument_word<'a, I, E, F>(
    request: &'a FeatureRequest<CompletionParams>,
    mut parameters: I,
    execute: E,
) -> Vec<CompletionItem>
where
    I: Iterator<Item = Parameter<'a>>,
    E: FnOnce(Arc<LatexCommand>, usize) -> F,
    F: Future<Output = Vec<CompletionItem>>,
{
    if let SyntaxTree::Latex(tree) = &request.document().tree {
        let position = request.params.text_document_position.position;
        if let Some(command) = find_command(tree, position) {
            for parameter in parameters.by_ref() {
                if command.name.text() != parameter.name {
                    continue;
                }

                if let Some(args) = command.args.get(parameter.index) {
                    if args.right.is_some() && !args.range().contains_exclusive(position) {
                        continue;
                    }

                    if !args.children.is_empty() && !command.has_word(parameter.index) {
                        continue;
                    }

                    return execute(Arc::clone(&command), parameter.index).await;
                }
            }
        }
    }
    Vec::new()
}

pub async fn environment<'a, E, F>(
    request: &'a FeatureRequest<CompletionParams>,
    execute: E,
) -> Vec<CompletionItem>
where
    E: FnOnce(ArgumentContext<'a>) -> F,
    F: Future<Output = Vec<CompletionItem>>,
{
    let parameters = LANGUAGE_DATA
        .environment_commands
        .iter()
        .map(|cmd| Parameter::new(&cmd.name, cmd.index));
    argument(request, parameters, execute).await
}

fn find_command(tree: &LatexSyntaxTree, position: Position) -> Option<Arc<LatexCommand>> {
    let mut nodes = tree.find(position);
    nodes.reverse();
    for node in nodes {
        if let LatexNode::Command(command) = node {
            return Some(command);
        }
    }
    None
}