summaryrefslogtreecommitdiff
path: root/support/texlab/src/features/definition.rs
blob: 20da72a14231f6875de6bea771c8438bfede17eb (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
mod command;
mod document;
mod entry;
mod label;
mod string;

use std::sync::Arc;

use lsp_types::{GotoDefinitionParams, GotoDefinitionResponse, LocationLink, Url};
use rowan::TextRange;

use crate::LineIndexExt;

use self::{
    command::goto_command_definition, document::goto_document_definition,
    entry::goto_entry_definition, label::goto_label_definition, string::goto_string_definition,
};

use super::{cursor::CursorContext, FeatureRequest};

pub fn goto_definition(
    request: FeatureRequest<GotoDefinitionParams>,
) -> Option<GotoDefinitionResponse> {
    let context = CursorContext::new(request);
    log::debug!("[Definition] Cursor: {:?}", context.cursor);

    let origin_document = context.request.main_document();
    let links: Vec<_> = goto_command_definition(&context)
        .or_else(|| goto_document_definition(&context))
        .or_else(|| goto_entry_definition(&context))
        .or_else(|| goto_label_definition(&context))
        .or_else(|| goto_string_definition(&context))?
        .into_iter()
        .map(|result| {
            let origin_selection_range = Some(
                origin_document
                    .line_index
                    .line_col_lsp_range(result.origin_selection_range),
            );

            let target_document = &context.request.workspace.documents_by_uri[&result.target_uri];
            let target_uri = result.target_uri.as_ref().clone();
            let target_range = target_document
                .line_index
                .line_col_lsp_range(result.target_range);
            let target_selection_range = target_document
                .line_index
                .line_col_lsp_range(result.target_selection_range);

            LocationLink {
                origin_selection_range,
                target_uri,
                target_range,
                target_selection_range,
            }
        })
        .collect();

    Some(GotoDefinitionResponse::Link(links))
}

#[derive(Debug, Clone)]
struct DefinitionResult {
    origin_selection_range: TextRange,
    target_uri: Arc<Url>,
    target_range: TextRange,
    target_selection_range: TextRange,
}