diff options
author | Alex Zatelepin <[email protected]> | 2019-10-21 00:04:55 +0100 |
---|---|---|
committer | Alex Zatelepin <[email protected]> | 2019-10-21 19:35:42 +0100 |
commit | 9d5e9326266d6b064c6d0f5d78ba2fae4d78e8fb (patch) | |
tree | 8f68f7083cad16640f4f99b1ce6e77315e5bd5f4 /crates/ra_lsp_server/src/conv.rs | |
parent | 6b9bd7bdd2712a7e85d6bfc70c231dbe36c2e585 (diff) |
fixup folding ranges for "lineFoldingOnly" clients #2033
Diffstat (limited to 'crates/ra_lsp_server/src/conv.rs')
-rw-r--r-- | crates/ra_lsp_server/src/conv.rs | 63 |
1 files changed, 49 insertions, 14 deletions
diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 1318a1738..173580dee 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs | |||
@@ -227,22 +227,57 @@ impl ConvWith<(&LineIndex, LineEndings)> for &AtomTextEdit { | |||
227 | } | 227 | } |
228 | } | 228 | } |
229 | 229 | ||
230 | impl ConvWith<&LineIndex> for Fold { | 230 | pub(crate) struct FoldConvCtx<'a> { |
231 | pub(crate) text: &'a str, | ||
232 | pub(crate) line_index: &'a LineIndex, | ||
233 | pub(crate) line_folding_only: bool, | ||
234 | } | ||
235 | |||
236 | impl ConvWith<&FoldConvCtx<'_>> for Fold { | ||
231 | type Output = lsp_types::FoldingRange; | 237 | type Output = lsp_types::FoldingRange; |
232 | 238 | ||
233 | fn conv_with(self, line_index: &LineIndex) -> lsp_types::FoldingRange { | 239 | fn conv_with(self, ctx: &FoldConvCtx) -> lsp_types::FoldingRange { |
234 | let range = self.range.conv_with(&line_index); | 240 | let kind = match self.kind { |
235 | lsp_types::FoldingRange { | 241 | FoldKind::Comment => Some(lsp_types::FoldingRangeKind::Comment), |
236 | start_line: range.start.line, | 242 | FoldKind::Imports => Some(lsp_types::FoldingRangeKind::Imports), |
237 | start_character: Some(range.start.character), | 243 | FoldKind::Mods => None, |
238 | end_line: range.end.line, | 244 | FoldKind::Block => None, |
239 | end_character: Some(range.end.character), | 245 | }; |
240 | kind: match self.kind { | 246 | |
241 | FoldKind::Comment => Some(lsp_types::FoldingRangeKind::Comment), | 247 | let range = self.range.conv_with(&ctx.line_index); |
242 | FoldKind::Imports => Some(lsp_types::FoldingRangeKind::Imports), | 248 | |
243 | FoldKind::Mods => None, | 249 | if ctx.line_folding_only { |
244 | FoldKind::Block => None, | 250 | // Clients with line_folding_only == true (such as VSCode) will fold the whole end line |
245 | }, | 251 | // even if it contains text not in the folding range. To prevent that we exclude |
252 | // range.end.line from the folding region if there is more text after range.end | ||
253 | // on the same line. | ||
254 | let has_more_text_on_end_line = ctx.text | ||
255 | [TextRange::from_to(self.range.end(), TextUnit::of_str(ctx.text))] | ||
256 | .chars() | ||
257 | .take_while(|it| *it != '\n') | ||
258 | .any(|it| !it.is_whitespace()); | ||
259 | |||
260 | let end_line = if has_more_text_on_end_line { | ||
261 | range.end.line.saturating_sub(1) | ||
262 | } else { | ||
263 | range.end.line | ||
264 | }; | ||
265 | |||
266 | lsp_types::FoldingRange { | ||
267 | start_line: range.start.line, | ||
268 | start_character: None, | ||
269 | end_line, | ||
270 | end_character: None, | ||
271 | kind, | ||
272 | } | ||
273 | } else { | ||
274 | lsp_types::FoldingRange { | ||
275 | start_line: range.start.line, | ||
276 | start_character: Some(range.start.character), | ||
277 | end_line: range.end.line, | ||
278 | end_character: Some(range.end.character), | ||
279 | kind, | ||
280 | } | ||
246 | } | 281 | } |
247 | } | 282 | } |
248 | } | 283 | } |