summaryrefslogtreecommitdiff
path: root/support/texlab/src/symbol/mod.rs
blob: b292f15bb23d349f7c59483ae648330a9d7eaeeb (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
173
174
175
176
177
178
mod bibtex_entry;
mod bibtex_string;
mod latex_section;
mod project_order;
mod types;

pub use self::latex_section::{build_section_tree, LatexSectionNode, LatexSectionTree};

use self::{
    bibtex_entry::BibtexEntrySymbolProvider, bibtex_string::BibtexStringSymbolProvider,
    latex_section::LatexSectionSymbolProvider, project_order::ProjectOrdering, types::LatexSymbol,
};
use crate::{
    feature::{ConcatProvider, DocumentView, FeatureProvider, FeatureRequest},
    protocol::{
        ClientCapabilities, ClientCapabilitiesExt, DocumentSymbolParams, DocumentSymbolResponse,
        Options, PartialResultParams, SymbolInformation, TextDocumentIdentifier, Uri,
        WorkDoneProgressParams, WorkspaceSymbolParams,
    },
    tex::Distribution,
    workspace::Snapshot,
};
use async_trait::async_trait;
use std::{
    cmp::Reverse,
    path::{Path, PathBuf},
    sync::Arc,
};

pub struct SymbolProvider {
    provider: ConcatProvider<DocumentSymbolParams, LatexSymbol>,
}

impl SymbolProvider {
    pub fn new() -> Self {
        Self {
            provider: ConcatProvider::new(vec![
                Box::new(BibtexEntrySymbolProvider),
                Box::new(BibtexStringSymbolProvider),
                Box::new(LatexSectionSymbolProvider),
            ]),
        }
    }
}

impl Default for SymbolProvider {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl FeatureProvider for SymbolProvider {
    type Params = DocumentSymbolParams;
    type Output = Vec<LatexSymbol>;

    async fn execute<'a>(&'a self, req: &'a FeatureRequest<Self::Params>) -> Self::Output {
        self.provider.execute(req).await
    }
}

pub fn document_symbols(
    client_capabilities: &ClientCapabilities,
    snapshot: &Snapshot,
    uri: &Uri,
    options: &Options,
    current_dir: &Path,
    symbols: Vec<LatexSymbol>,
) -> DocumentSymbolResponse {
    if client_capabilities.has_hierarchical_document_symbol_support() {
        DocumentSymbolResponse::Nested(symbols.into_iter().map(Into::into).collect())
    } else {
        let mut buffer = Vec::new();
        for symbol in symbols {
            symbol.flatten(&mut buffer);
        }
        let mut buffer = buffer
            .into_iter()
            .map(|symbol| symbol.into_symbol_info(uri.clone()))
            .collect();
        sort_symbols(snapshot, options, &current_dir, &mut buffer);
        DocumentSymbolResponse::Flat(buffer)
    }
}

struct WorkspaceSymbol {
    info: SymbolInformation,
    search_text: String,
}

pub async fn workspace_symbols<'a>(
    distro: Arc<dyn Distribution>,
    client_capabilities: Arc<ClientCapabilities>,
    snapshot: Arc<Snapshot>,
    options: &'a Options,
    current_dir: Arc<PathBuf>,
    params: &'a WorkspaceSymbolParams,
) -> Vec<SymbolInformation> {
    let provider = SymbolProvider::new();
    let mut symbols = Vec::new();

    for doc in &snapshot.0 {
        let uri: Uri = doc.uri.clone();
        let req = FeatureRequest {
            params: DocumentSymbolParams {
                text_document: TextDocumentIdentifier::new(uri.clone().into()),
                work_done_progress_params: WorkDoneProgressParams::default(),
                partial_result_params: PartialResultParams::default(),
            },
            view: DocumentView::analyze(
                Arc::clone(&snapshot),
                Arc::clone(&doc),
                &options,
                &current_dir,
            ),
            distro: distro.clone(),
            client_capabilities: Arc::clone(&client_capabilities),
            options: options.clone(),
            current_dir: Arc::clone(&current_dir),
        };

        let mut buffer = Vec::new();
        for symbol in provider.execute(&req).await {
            symbol.flatten(&mut buffer);
        }

        for symbol in buffer {
            symbols.push(WorkspaceSymbol {
                search_text: symbol.search_text(),
                info: symbol.into_symbol_info(uri.clone()),
            });
        }
    }

    let query_words: Vec<_> = params
        .query
        .split_whitespace()
        .map(str::to_lowercase)
        .collect();
    let mut filtered = Vec::new();
    for symbol in symbols {
        let mut included = true;
        for word in &query_words {
            if !symbol.search_text.contains(word) {
                included = false;
                break;
            }
        }

        if included {
            filtered.push(symbol.info);
        }
    }
    sort_symbols(&snapshot, options, &current_dir, &mut filtered);
    filtered
}

fn sort_symbols(
    snapshot: &Snapshot,
    options: &Options,
    current_dir: &Path,
    symbols: &mut Vec<SymbolInformation>,
) {
    let ordering = ProjectOrdering::analyze(snapshot, options, current_dir);
    symbols.sort_by(|left, right| {
        let left_key = (
            ordering.get(&Uri::from(left.location.uri.clone())),
            left.location.range.start,
            Reverse(left.location.range.end),
        );
        let right_key = (
            ordering.get(&Uri::from(right.location.uri.clone())),
            right.location.range.start,
            Reverse(right.location.range.end),
        );
        left_key.cmp(&right_key)
    });
}