summaryrefslogtreecommitdiff
path: root/support/texlab/tests/integration/lsp/text_document/definition.rs
blob: 203c350464c323f0372d0777728ee6faf3bb8a2c (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
use anyhow::Result;
use assert_unordered::assert_eq_unordered;
use lsp_types::{
    request::GotoDefinition, ClientCapabilities, GotoDefinitionParams, GotoDefinitionResponse,
    LocationLink,
};

use crate::lsp::{client::Client, fixture};

fn check(fixture: &str) -> Result<()> {
    let mut client = Client::spawn()?;
    client.initialize(ClientCapabilities::default(), None)?;

    let fixture = fixture::parse(fixture);
    for file in fixture.files {
        client.open(file.name, file.lang, file.text)?;
    }

    let mut expected_links = Vec::new();
    for ranges in fixture.ranges.values() {
        expected_links.push(LocationLink {
            origin_selection_range: Some(ranges[&1].range),
            target_uri: client.uri(ranges[&2].name)?,
            target_range: ranges[&2].range,
            target_selection_range: ranges[&3].range,
        });
    }

    let actual_links = client
        .request::<GotoDefinition>(GotoDefinitionParams {
            text_document_position_params: fixture.cursor.unwrap().into_params(&client)?,
            partial_result_params: Default::default(),
            work_done_progress_params: Default::default(),
        })?
        .map_or(Vec::new(), |actual| match actual {
            GotoDefinitionResponse::Link(links) => links,
            GotoDefinitionResponse::Array(_) | GotoDefinitionResponse::Scalar(_) => unreachable!(),
        });

    client.shutdown()?;

    assert_eq_unordered!(actual_links, expected_links);
    Ok(())
}

#[test]
fn command_definition() -> Result<()> {
    check(
        r#"
%TEX main.tex
%SRC \DeclareMathOperator{\foo}{foo}
%1.3                      ^^^^
%1.2 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
%SRC \foo
%CUR   ^
%1.1 ^^^^
"#,
    )
}

#[test]
fn document() -> Result<()> {
    check(
        r#"
%TEX foo.tex
%SRC \addbibresource{baz.bib}
%CUR                   ^
%1.1                 ^^^^^^^

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

%TEX baz.bib
%SRC @article{foo, bar = {baz}}
%1.3 
%1.2 
"#,
    )
}

#[test]
fn entry() -> Result<()> {
    check(
        r#"
%TEX foo.tex
%SRC \addbibresource{baz.bib}
%SRC \cite{foo}
%CUR       ^
%1.1       ^^^

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

%BIB baz.bib
%SRC @article{foo, bar = {baz}}
%1.3          ^^^
%1.2 ^^^^^^^^^^^^^^^^^^^^^^^^^^
"#,
    )
}

#[test]
fn string_simple() -> Result<()> {
    check(
        r#"
%BIB main.bib
%SRC @string{foo = {bar}}
%1.3         ^^^
%1.2 ^^^^^^^^^^^^^^^^^^^^
%SRC @article{bar, author = foo}
%CUR                         ^
%1.1                        ^^^
"#,
    )
}

#[test]
fn string_join() -> Result<()> {
    check(
        r#"
%BIB main.bib
%SRC @string{foo = {bar}}
%1.3         ^^^
%1.2 ^^^^^^^^^^^^^^^^^^^^
%SRC @article{bar, author = foo # "bar"}
%CUR                         ^
%1.1                        ^^^
"#,
    )
}

#[test]
fn string_field() -> Result<()> {
    check(
        r#"
%BIB main.bib
%SRC @string{foo = {bar}}
%SRC @article{bar, author = foo # "bar"}
%CUR                 ^
"#,
    )
}