Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,17 @@ impl<'a> LineCursor<'a> {
indent_columns(self.current_line_text())
}

/// Whether the current line is blank (empty or whitespace-only).
pub fn current_is_blank(&self) -> bool {
self.current_line_text().trim().is_empty()
/// A [`TextRange`] spanning the trimmed (non-whitespace) content of
/// the current line.
///
/// Equivalent to
/// `make_line_range(line, current_indent(), current_trimmed().len())`.
pub fn current_trimmed_range(&self) -> TextRange {
self.make_line_range(
self.line,
self.current_indent(),
self.current_trimmed().len(),
)
}

// ── Arbitrary-line helpers ──────────────────────────────────────
Expand Down Expand Up @@ -159,6 +167,27 @@ impl<'a> LineCursor<'a> {
TextRange::from_offset_len(start, len)
}

/// Build a [`TextRange`] from a starting offset to the end of the
/// last non-blank line before the current cursor position.
///
/// Walks backwards from `cursor.line - 1`, skipping trailing blank
/// lines, to find the true content end. The returned range starts at
/// `start_offset` and ends at the last non-whitespace byte on the
/// found line.
pub fn span_back_from_cursor(&self, start_offset: usize) -> TextRange {
let (start_line, start_col) = self.offset_to_line_col(start_offset);
let mut end_line = self.line.saturating_sub(1);
while end_line > start_line {
if !self.line_text(end_line).trim().is_empty() {
break;
}
end_line -= 1;
}
let end_text = self.line_text(end_line);
let end_col = indent_len(end_text) + end_text.trim().len();
self.make_range(start_line, start_col, end_line, end_col)
}

// ── Offset utilities ───────────────────────────────────────────

/// Convert a byte offset to `(line, col)`.
Expand Down
Loading