summaryrefslogtreecommitdiff
path: root/support/texlab/tests/integration/lsp/text_document/formatting.rs
blob: a0c02bf4b1eeda4fd55a88b51391937cc583e439 (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
use anyhow::Result;
use insta::assert_snapshot;
use lsp_types::{
    request::Formatting, ClientCapabilities, DocumentFormattingParams, FormattingOptions,
    TextDocumentIdentifier,
};
use texlab::{LineIndex, LineIndexExt};

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

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

    let fixture = fixture::parse(fixture);
    let file = fixture.files.into_iter().next().unwrap();
    let old_text = file.text.clone();
    client.open(file.name, file.lang, file.text)?;

    let actual_edits = client
        .request::<Formatting>(DocumentFormattingParams {
            text_document: TextDocumentIdentifier::new(client.uri(file.name)?),
            work_done_progress_params: Default::default(),
            options: FormattingOptions {
                insert_spaces: true,
                tab_size: 4,
                ..Default::default()
            },
        })?
        .unwrap_or_default();

    client.shutdown()?;

    let line_index = LineIndex::new(&old_text);
    let mut actual_text = old_text;
    for edit in actual_edits.into_iter().rev() {
        let range = line_index.offset_lsp_range(edit.range);
        actual_text.replace_range::<std::ops::Range<usize>>(range.into(), &edit.new_text);
    }

    Ok(actual_text)
}

#[test]
fn bibtex_internal_wrap_long_lines() -> Result<()> {
    assert_snapshot!(format(
        r#"
%BIB main.bib
%SRC @article{foo, bar = {Lorem ipsum dolor sit amet, consectetur adipiscing elit.
%SRC Lorem ipsum dolor sit amet,
%SRC consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.},}"#,
    )?);

    Ok(())
}

#[test]
fn bibtex_internal_multiple_entries() -> Result<()> {
    assert_snapshot!(format(
        r#"
%BIB main.bib
%SRC @article{foo, bar = {Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, 
%SRC consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.},}
%SRC 
%SRC @article{foo, bar = {Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, 
%SRC consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.},}""#,
    )?);

    Ok(())
}

#[test]
fn bibtex_internal_trailing_comma() -> Result<()> {
    assert_snapshot!(format(
        r#"
%BIB main.bib
%SRC @article{foo, bar = baz}"#,
    )?);

    Ok(())
}

#[test]
fn bibtex_internal_insert_braces() -> Result<()> {
    assert_snapshot!(format(
        r#"
%BIB main.bib
%SRC @article{foo, bar = baz,"#,
    )?);

    Ok(())
}

#[test]
fn bibtex_internal_command() -> Result<()> {
    assert_snapshot!(format(
        r#"
%BIB main.bib
%SRC @article{foo, bar = "\baz",}"#,
    )?);

    Ok(())
}

#[test]
fn bibtex_internal_join_strings() -> Result<()> {
    assert_snapshot!(format(
        r#"
%BIB main.bib
%SRC @article{foo, bar = "baz" # "qux"}"#,
    )?);

    Ok(())
}

#[test]
fn bibtex_internal_parens() -> Result<()> {
    assert_snapshot!(format(
        r#"
%BIB main.bib
%SRC @article(foo,)"#,
    )?);

    Ok(())
}

#[test]
fn bibtex_internal_string() -> Result<()> {
    assert_snapshot!(format(
        r#"
%BIB main.bib
%SRC @string{foo="bar"}"#,
    )?);

    Ok(())
}

#[test]
fn bibtex_internal_preamble() -> Result<()> {
    assert_snapshot!(format(
        r#"
%BIB main.bib
%SRC @preamble{
%SRC     "foo bar baz" }"#,
    )?);

    Ok(())
}