summaryrefslogtreecommitdiff
path: root/support/texlab/tests/lsp/text_document/definition.rs
blob: 78eed633c82338bede35f8e9e4d7464a9ec15156 (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
use itertools::Itertools;
use lsp_types::{
    request::GotoDefinition, ClientCapabilities, GotoDefinitionParams, GotoDefinitionResponse,
    LocationLink,
};

use crate::fixture::TestBed;

fn check(fixture: &str) {
    let test_bed = TestBed::new(fixture).unwrap();
    test_bed.initialize(ClientCapabilities::default()).unwrap();

    let text_document_position_params = test_bed.cursor().unwrap();
    let cursor = text_document_position_params.position;

    let origin_selection = test_bed
        .locations()
        .iter()
        .filter(|location| location.uri == text_document_position_params.text_document.uri)
        .find(|location| cursor >= location.range.start && cursor <= location.range.end);

    let mut expected_links: Vec<_> = test_bed
        .locations()
        .iter()
        .filter(|location| Some(*location) != origin_selection)
        .batching(|it| {
            let target_selection_range = it.next()?.range;
            let target = it.next()?;
            Some(LocationLink {
                origin_selection_range: origin_selection.map(|sel| sel.range),
                target_uri: target.uri.clone(),
                target_range: target.range,
                target_selection_range,
            })
        })
        .collect();

    let mut actual_links = match test_bed
        .client()
        .send_request::<GotoDefinition>(GotoDefinitionParams {
            text_document_position_params,
            partial_result_params: Default::default(),
            work_done_progress_params: Default::default(),
        })
        .unwrap()
    {
        Some(GotoDefinitionResponse::Link(links)) => links,
        Some(GotoDefinitionResponse::Array(_)) => unreachable!(),
        Some(GotoDefinitionResponse::Scalar(_)) => unreachable!(),
        None => Vec::new(),
    };

    sort_links(&mut actual_links);
    sort_links(&mut expected_links);
    assert_eq!(actual_links, expected_links);
}

fn sort_links(links: &mut Vec<LocationLink>) {
    links.sort_by(|a, b| {
        let left = (&a.target_uri, a.target_range.start);
        let right = (&b.target_uri, b.target_range.start);
        left.cmp(&right)
    });
}

#[test]
fn command_definition() {
    check(
        r#"
%! main.tex
\DeclareMathOperator{\foo}{foo}
                     ^^^^
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
\foo
  |
^^^^"#,
    )
}

#[test]
fn document() {
    check(
        r#"
%! foo.tex
\addbibresource{baz.bib}
                  |
                ^^^^^^^

%! bar.bib
@article{foo, bar = {baz}}

%! baz.bib
@article{foo, bar = {baz}}
!
!"#,
    )
}

#[test]
fn entry() {
    check(
        r#"
%! foo.tex
\addbibresource{baz.bib}
\cite{foo}
      |
      ^^^

%! bar.bib
@article{foo, bar = {baz}}

%! baz.bib
@article{foo, bar = {baz}}
         ^^^
^^^^^^^^^^^^^^^^^^^^^^^^^^"#,
    )
}

#[test]
fn string_simple() {
    check(
        r#"
%! main.bib
@string{foo = {bar}}
        ^^^
^^^^^^^^^^^^^^^^^^^^
@article{bar, author = foo}
                        |
                       ^^^"#,
    )
}

#[test]
fn string_join() {
    check(
        r#"
%! main.bib
@string{foo = {bar}}
        ^^^
^^^^^^^^^^^^^^^^^^^^
@article{bar, author = foo # "bar"}
                        |
                       ^^^"#,
    )
}

#[test]
fn string_field() {
    check(
        r#"
%! main.bib
@string{foo = {bar}}
@article{bar, author = foo # "bar"}
                |"#,
    )
}