forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfolding_range.rs
More file actions
203 lines (177 loc) · 5.78 KB
/
Copy pathfolding_range.rs
File metadata and controls
203 lines (177 loc) · 5.78 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use async_lsp::ResponseError;
use async_lsp::lsp_types::{FoldingRange, FoldingRangeKind, FoldingRangeParams};
use common::InputDb;
use hir::{hir_def::ItemKind, lower::map_file_to_mod, span::LazySpan};
use crate::{backend::Backend, util::to_lsp_range_from_span};
/// Handle textDocument/foldingRange.
pub async fn handle_folding_range(
backend: &Backend,
params: FoldingRangeParams,
) -> Result<Option<Vec<FoldingRange>>, ResponseError> {
let internal_url = backend.map_client_uri_to_internal(params.text_document.uri);
let Some(file) = backend.db.workspace().get(&backend.db, &internal_url) else {
return Ok(None);
};
let top_mod = map_file_to_mod(&backend.db, file);
let scope_graph = top_mod.scope_graph(&backend.db);
let mut ranges = Vec::new();
for item in scope_graph.items_dfs(&backend.db) {
let item_span: hir::span::DynLazySpan = item.span().into();
let Some(span) = item_span.resolve(&backend.db) else {
continue;
};
let Ok(range) = to_lsp_range_from_span(span, &backend.db) else {
continue;
};
// If the span ends at column 0, the actual content ends on the
// previous line (trailing newline included in the parser span).
let end_line = if range.end.character == 0 && range.end.line > 0 {
range.end.line - 1
} else {
range.end.line
};
// Only fold multi-line items
if range.start.line == end_line {
continue;
}
let kind = match item {
ItemKind::Func(_)
| ItemKind::Struct(_)
| ItemKind::Enum(_)
| ItemKind::Trait(_)
| ItemKind::Impl(_)
| ItemKind::ImplTrait(_) => Some(FoldingRangeKind::Region),
_ => None,
};
ranges.push(FoldingRange {
start_line: range.start.line,
start_character: Some(range.start.character),
end_line,
end_character: Some(range.end.character),
kind,
collapsed_text: None,
});
}
if ranges.is_empty() {
Ok(None)
} else {
Ok(Some(ranges))
}
}
#[cfg(test)]
mod tests {
use common::InputDb;
use driver::DriverDataBase;
use hir::{lower::map_file_to_mod, span::LazySpan};
use url::Url;
use crate::util::to_lsp_range_from_span;
fn collect_folding_ranges(
db: &DriverDataBase,
top_mod: hir::hir_def::TopLevelMod,
) -> Vec<(u32, u32)> {
let scope_graph = top_mod.scope_graph(db);
let mut ranges = Vec::new();
for item in scope_graph.items_dfs(db) {
let item_span: hir::span::DynLazySpan = item.span().into();
let Some(span) = item_span.resolve(db) else {
continue;
};
let Ok(range) = to_lsp_range_from_span(span, db) else {
continue;
};
let end_line = if range.end.character == 0 && range.end.line > 0 {
range.end.line - 1
} else {
range.end.line
};
if range.start.line != end_line {
ranges.push((range.start.line, end_line));
}
}
ranges
}
#[test]
fn test_folding_basic_function() {
let mut db = DriverDataBase::default();
let code = "fn foo() -> i32 {\n 42\n}\n";
let file = db.workspace().touch(
&mut db,
Url::parse("file:///test.fe").unwrap(),
Some(code.to_string()),
);
let top_mod = map_file_to_mod(&db, file);
let ranges = collect_folding_ranges(&db, top_mod);
assert!(!ranges.is_empty(), "should have folding range for function");
assert_eq!(ranges[0], (0, 2), "function should fold from line 0 to 2");
}
#[test]
fn test_folding_struct_and_impl() {
let mut db = DriverDataBase::default();
let code = r#"struct Point {
x: i32
y: i32
}
impl Point {
fn sum(self) -> i32 {
self.x
}
}
"#;
let file = db.workspace().touch(
&mut db,
Url::parse("file:///test.fe").unwrap(),
Some(code.to_string()),
);
let top_mod = map_file_to_mod(&db, file);
let ranges = collect_folding_ranges(&db, top_mod);
// Should have ranges for: struct, impl, fn sum
assert!(
ranges.len() >= 3,
"should have at least 3 folding ranges, got {}",
ranges.len()
);
}
#[test]
fn test_folding_single_line_no_fold() {
let mut db = DriverDataBase::default();
// A single-line function shouldn't produce a folding range
let code = "fn x() -> i32 { 1 }\n";
let file = db.workspace().touch(
&mut db,
Url::parse("file:///test.fe").unwrap(),
Some(code.to_string()),
);
let top_mod = map_file_to_mod(&db, file);
let ranges = collect_folding_ranges(&db, top_mod);
assert!(ranges.is_empty(), "single-line items should not fold");
}
#[test]
fn test_folding_trait_and_impl_trait() {
let mut db = DriverDataBase::default();
let code = r#"trait Runnable {
fn run(self) -> i32
}
struct Task {
id: i32
}
impl Runnable for Task {
fn run(self) -> i32 {
self.id
}
}
"#;
let file = db.workspace().touch(
&mut db,
Url::parse("file:///test.fe").unwrap(),
Some(code.to_string()),
);
let top_mod = map_file_to_mod(&db, file);
let ranges = collect_folding_ranges(&db, top_mod);
// trait, struct, impl trait, fn run (at minimum)
assert!(
ranges.len() >= 4,
"should have at least 4 folding ranges, got {}",
ranges.len()
);
}
}