Skip to content

Commit 1425c18

Browse files
committed
[ty] Add code folding support
This PR implements the `textDocument/foldingRange` LSP request, enabling code folding in editors. We also support tagging each folding range with its "kind." So for example, this enables one to ask your editor to "collapse all block comments." The implementation works by doing a simple AST traversal to identify "blocks" in a Python program. We also do a line oriented search to extract ranges that are more difficult to do from the AST: blocks of comments, blocks of imports and special custom "regions." Closes astral-sh/ty#2588
1 parent 97acaae commit 1425c18

12 files changed

Lines changed: 1926 additions & 8 deletions

crates/ruff_text_size/src/range.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,52 @@ impl TextRange {
345345
}
346346
}
347347

348+
/// Returns a new range with the start offset set to the
349+
/// value given and the end offset unchanged from this
350+
/// range.
351+
///
352+
/// ## Panics
353+
///
354+
/// When `offset > self.end()`.
355+
///
356+
/// ## Examples
357+
///
358+
/// ```
359+
/// use ruff_text_size::{Ranged, TextRange, TextSize};
360+
///
361+
/// let range = TextRange::new(TextSize::from(5), TextSize::from(10));
362+
/// let new = range.with_start(TextSize::from(8));
363+
/// assert_eq!(new, TextRange::new(TextSize::from(8), TextSize::from(10)));
364+
/// ```
365+
#[inline]
366+
#[must_use]
367+
pub fn with_start(&self, offset: TextSize) -> TextRange {
368+
TextRange::new(offset, self.end())
369+
}
370+
371+
/// Returns a new range with the end offset set to the
372+
/// value given and the start offset unchanged from this
373+
/// range.
374+
///
375+
/// ## Panics
376+
///
377+
/// When `offset < self.start()`.
378+
///
379+
/// ## Examples
380+
///
381+
/// ```
382+
/// use ruff_text_size::{Ranged, TextRange, TextSize};
383+
///
384+
/// let range = TextRange::new(TextSize::from(5), TextSize::from(10));
385+
/// let new = range.with_end(TextSize::from(8));
386+
/// assert_eq!(new, TextRange::new(TextSize::from(5), TextSize::from(8)));
387+
/// ```
388+
#[inline]
389+
#[must_use]
390+
pub fn with_end(&self, offset: TextSize) -> TextRange {
391+
TextRange::new(self.start(), offset)
392+
}
393+
348394
/// Subtracts an offset from the start position.
349395
///
350396
///

0 commit comments

Comments
 (0)