summaryrefslogtreecommitdiff
path: root/support/texlab/tests/lsp/text_document/formatting.rs
blob: 295022c76a91e4c710da2bf332efe0a36863e225 (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
use insta::assert_snapshot;
use lsp_types::{
    request::Formatting, ClientCapabilities, DocumentFormattingParams, FormattingOptions,
};
use texlab::util::{line_index::LineIndex, line_index_ext::LineIndexExt};

use crate::fixture::TestBed;

fn format(fixture: &str) -> String {
    let test_bed = TestBed::new(fixture).unwrap();
    test_bed.initialize(ClientCapabilities::default()).unwrap();

    let text_document = test_bed.cursor().unwrap().text_document;
    let edits = test_bed
        .client()
        .send_request::<Formatting>(DocumentFormattingParams {
            text_document,
            work_done_progress_params: Default::default(),
            options: FormattingOptions {
                insert_spaces: true,
                tab_size: 4,
                ..Default::default()
            },
        })
        .unwrap()
        .unwrap_or_default();

    let old_text = &test_bed.documents()[0].text;
    let line_index = LineIndex::new(old_text);
    let mut new_text = String::from(old_text);
    for edit in edits.into_iter().rev() {
        let range = line_index.offset_lsp_range(edit.range);
        new_text.replace_range::<std::ops::Range<usize>>(range.into(), &edit.new_text);
    }

    new_text
}

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

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

@article{foo, bar = {Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, 
consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.},}"
|"#,
    ));
}

#[test]
fn bibtex_internal_trailing_comma() {
    assert_snapshot!(format(
        r#"
%! main.bib
@article{foo, bar = baz}
|"#,
    ));
}

#[test]
fn bibtex_internal_insert_braces() {
    assert_snapshot!(format(
        r#"
%! main.bib
@article{foo, bar = baz,
|"#,
    ));
}

#[test]
fn bibtex_internal_command() {
    assert_snapshot!(format(
        r#"
%! main.bib
@article{foo, bar = "\baz",}
|"#,
    ));
}

#[test]
fn bibtex_internal_join_strings() {
    assert_snapshot!(format(
        r#"
%! main.bib
@article{foo, bar = "baz" # "qux"}
|"#,
    ));
}

#[test]
fn bibtex_internal_parens() {
    assert_snapshot!(format(
        r#"
%! main.bib
@article(foo,)
|"#,
    ));
}

#[test]
fn bibtex_internal_string() {
    assert_snapshot!(format(
        r#"
%! main.bib
@string{foo="bar"}
|"#,
    ));
}

#[test]
fn bibtex_internal_preamble() {
    assert_snapshot!(format(
        r#"
%! main.bib
@preamble{
    "foo bar baz" }
|"#,
    ));
}