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
|
use crate::syntax::*;
use crate::workspace::*;
use futures_boxed::boxed;
use lsp_types::{FoldingRange, FoldingRangeKind, FoldingRangeParams};
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct LatexSectionFoldingProvider;
impl FeatureProvider for LatexSectionFoldingProvider {
type Params = FoldingRangeParams;
type Output = Vec<FoldingRange>;
#[boxed]
async fn execute<'a>(
&'a self,
request: &'a FeatureRequest<FoldingRangeParams>,
) -> Vec<FoldingRange> {
let mut foldings = Vec::new();
if let SyntaxTree::Latex(tree) = &request.document().tree {
let sections = &tree.structure.sections;
for i in 0..sections.len() {
let current = §ions[i];
let next = sections
.iter()
.skip(i + 1)
.find(|sec| current.level >= sec.level);
if let Some(next) = next {
if next.command.start().line > 0 {
let folding = FoldingRange {
start_line: current.command.end().line,
start_character: Some(current.command.end().character),
end_line: next.command.start().line - 1,
end_character: Some(0),
kind: Some(FoldingRangeKind::Region),
};
foldings.push(folding);
}
}
}
}
foldings
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_nesting() {
let foldings = test_feature(
LatexSectionFoldingProvider,
FeatureSpec {
files: vec![FeatureSpec::file("foo.tex", "\\section{Foo}\nfoo\n\\subsection{Bar}\nbar\n\\section{Baz}\nbaz\n\\section{Qux}")],
main_file: "foo.tex",
..FeatureSpec::default()
}
);
assert_eq!(
foldings,
vec![
FoldingRange {
start_line: 0,
start_character: Some(13),
end_line: 3,
end_character: Some(0),
kind: Some(FoldingRangeKind::Region),
},
FoldingRange {
start_line: 2,
start_character: Some(16),
end_line: 3,
end_character: Some(0),
kind: Some(FoldingRangeKind::Region),
},
FoldingRange {
start_line: 4,
start_character: Some(13),
end_line: 5,
end_character: Some(0),
kind: Some(FoldingRangeKind::Region),
}
]
);
}
#[test]
fn test_bibtex() {
let foldings = test_feature(
LatexSectionFoldingProvider,
FeatureSpec {
files: vec![FeatureSpec::file("foo.bib", "@article{foo, bar = baz}")],
main_file: "foo.bib",
..FeatureSpec::default()
},
);
assert!(foldings.is_empty());
}
}
|