Skip to main content

redox_core/buffer/text_buffer/
lines.rs

1//! Line-oriented helpers for `TextBuffer`.
2//!
3//! This file is intended to be included by the parent `text_buffer` module, and
4//! adds line/indexing utilities as an inherent `impl` on `TextBuffer`.
5//!
6//! Design notes
7//! - These APIs use **char indices** (Unicode scalar values), matching `ropey`.
8//! - Treats the trailing `'\n'` as *not part of the editable line*, so
9//!   `line_len_chars()` excludes it when present.
10//! - All functions are defensive, meaning they clamp out-of-range inputs.
11
12use std::cmp::min;
13
14use ropey::RopeSlice;
15
16use crate::buffer::TextBuffer;
17
18impl TextBuffer {
19    /// Number of lines in the buffer.
20    ///
21    /// Ropey counts lines by `'\n'` boundaries and always reports at least 1 line,
22    /// even for empty text.
23    #[inline]
24    pub fn len_lines(&self) -> usize {
25        self.rope.len_lines()
26    }
27
28    /// Clamp a line index to the valid range `[0, len_lines - 1]`.
29    ///
30    /// If the buffer is empty, Ropey still reports `len_lines() == 1`, so this
31    /// always returns a valid line index.
32    #[inline]
33    pub fn clamp_line(&self, line: usize) -> usize {
34        let last = self.len_lines().saturating_sub(1);
35        min(line, last)
36    }
37
38    /// Returns the absolute char index at the start of `line`.
39    ///
40    /// `line` is clamped into a valid range.
41    #[inline]
42    pub fn line_to_char(&self, line: usize) -> usize {
43        let line = self.clamp_line(line);
44        self.rope.line_to_char(line)
45    }
46
47    /// Returns the line index containing `char_idx`.
48    ///
49    /// `char_idx` is clamped to `[0, len_chars]`.
50    #[inline]
51    pub fn char_to_line(&self, char_idx: usize) -> usize {
52        let c = min(char_idx, self.len_chars());
53        self.rope.char_to_line(c)
54    }
55
56    /// Returns the length of `line` in chars, excluding a trailing `'\n'` if present.
57    ///
58    /// This corresponds to the number of valid "columns" for a `(line, col)` cursor
59    /// model where the newline is not considered part of the line.
60    pub fn line_len_chars(&self, line: usize) -> usize {
61        let line = self.clamp_line(line);
62        let slice = self.rope.line(line);
63
64        // Ropey line slices typically include the newline if present.
65        let mut len = slice.len_chars();
66        if len > 0 && slice.char(len - 1) == '\n' {
67            len -= 1;
68        }
69
70        len
71    }
72
73    /// Returns the line content as a `String`, excluding a trailing `'\n'` if present.
74    pub fn line_string(&self, line: usize) -> String {
75        self.line_slice(line).to_string()
76    }
77
78    /// Returns a non-allocating line slice excluding a trailing `'\n'` if present.
79    pub fn line_slice(&self, line: usize) -> RopeSlice<'_> {
80        let line = self.clamp_line(line);
81        let range = self.line_char_range(line);
82        self.rope.slice(range)
83    }
84
85    /// Returns the char range `[start, end)` for the line content, excluding a trailing `'\n'`.
86    ///
87    /// This will be useful for operations like "delete to end of line" or yanking the line
88    /// content without the newline.
89    pub fn line_char_range(&self, line: usize) -> std::ops::Range<usize> {
90        let line = self.clamp_line(line);
91        let start = self.rope.line_to_char(line);
92
93        // `line(line).len_chars()` includes the newline if present.
94        let end_including_newline = start + self.rope.line(line).len_chars();
95
96        // Drop exactly one trailing '\n' if present.
97        let end =
98            if end_including_newline > start && self.rope.char(end_including_newline - 1) == '\n' {
99                end_including_newline - 1
100            } else {
101                end_including_newline
102            };
103
104        start..end
105    }
106
107    /// Returns the absolute char index of the end of `line`, including a trailing
108    /// `'\n'` when one exists.
109    ///
110    /// This is useful for line-block transforms that need stable slice boundaries
111    /// across both newline-terminated and non-terminated final lines.
112    pub fn line_full_end_char(&self, line: usize) -> usize {
113        let line = self.clamp_line(line);
114        if line + 1 < self.len_lines() {
115            self.rope.line_to_char(line + 1)
116        } else {
117            self.rope.line_to_char(line) + self.line_len_chars(line)
118        }
119    }
120}