Lines
99.22 %
Functions
50 %
Branches
100 %
//! Line-oriented behaviour of a multi-line [`Editor`]. Lines end in `\n` or
//! `\r\n`; the terminator a text was loaded with is never rewritten, and the
//! cursor never rests between the `\r` and the `\n`.
use std::ops::Range;
use unicode_width::UnicodeWidthChar;
use super::Editor;
/// Height of the text area, in lines.
pub const TEXT_AREA_ROWS: usize = 5;
fn width(c: char) -> usize {
c.width().unwrap_or(0)
}
fn line_ranges(chars: &[char]) -> Vec<Range<usize>> {
let mut ranges = Vec::new();
let mut start = 0;
for i in (0..chars.len()).filter(|&i| chars[i] == '\n') {
let end = if i > start && chars[i - 1] == '\r' {
i - 1
} else {
i
};
ranges.push(start..end);
start = i + 1;
ranges.push(start..chars.len());
ranges
fn line_of(ranges: &[Range<usize>], cursor: usize) -> usize {
.iter()
.rposition(|range| range.start <= cursor)
.unwrap_or(0)
impl Editor {
#[must_use]
pub fn multiline(mut self, on: bool) -> Self {
self.multiline = on;
if on {
self.cursor = 0;
self
pub fn is_multiline(&self) -> bool {
self.multiline
/// Types a newline (always LF); a no-op for a single-line editor.
pub fn insert_newline(&mut self) {
if self.multiline {
self.insert_char('\n');
pub fn at_first_line(&self) -> bool {
line_of(&line_ranges(&self.chars()), self.cursor) == 0
pub fn at_last_line(&self) -> bool {
let ranges = line_ranges(&self.chars());
line_of(&ranges, self.cursor) + 1 == ranges.len()
pub fn move_up(&mut self) {
self.move_lines(-1);
pub fn move_down(&mut self) {
self.move_lines(1);
pub fn page_up(&mut self) {
self.move_lines(-(TEXT_AREA_ROWS as isize));
pub fn page_down(&mut self) {
self.move_lines(TEXT_AREA_ROWS as isize);
/// Moves by `delta` lines, keeping the display column where the target
/// line is long enough and landing on its end otherwise.
fn move_lines(&mut self, delta: isize) {
let chars = self.chars();
let ranges = line_ranges(&chars);
let line = line_of(&ranges, self.cursor);
let target = line
.saturating_add_signed(delta)
.min(ranges.len().saturating_sub(1));
if target == line {
return;
let column: usize = chars[ranges[line].start..self.cursor]
.map(|&c| width(c))
.sum();
let mut used = 0;
let mut at = ranges[target].start;
for &c in &chars[ranges[target].clone()] {
if used + width(c) > column {
break;
used += width(c);
at += 1;
self.cursor = at;
pub(super) fn home_target(&self) -> usize {
if !self.multiline {
return 0;
ranges[line_of(&ranges, self.cursor)].start
pub(super) fn end_target(&self) -> usize {
return chars.len();
ranges[line_of(&ranges, self.cursor)].end
pub(super) fn inside_crlf(&self) -> bool {
&& self.cursor > 0
&& chars.get(self.cursor - 1) == Some(&'\r')
&& chars.get(self.cursor) == Some(&'\n')
/// Length of the `\r\n` ending (backward) or starting (forward) at `at`
/// in a multi-line editor.
pub(super) fn terminator_span(
&self,
chars: &[char],
at: usize,
backward: bool,
) -> Option<usize> {
let pair = if backward {
chars.get(at.checked_sub(2)?..at)?
chars.get(at..at + 2)?
(self.multiline && pair == ['\r', '\n']).then_some(2)
/// The lines of the text, without their terminators.
pub fn text_lines(&self) -> Vec<&str> {
let lines = self.buffer.split('\n');
lines
.map(|line| line.strip_suffix('\r').unwrap_or(line))
.collect()
/// Puts the cursor at character `col` of `line`, clamped to the text and to the line's end.
pub fn set_cursor_position(&mut self, (line, col): (usize, usize)) {
let Some(range) = ranges.get(line).or(ranges.last()) else {
self.cursor = range.start + col.min(range.len());
/// The line of the cursor and its character index in that line.
pub fn cursor_position(&self) -> (usize, usize) {
let before = self.buffer.chars().take(self.cursor);
before.fold((0, 0), |(line, col), ch| match ch {
'\n' => (line + 1, 0),
_ => (line, col + 1),
})
#[cfg(test)]
#[path = "lines_tests.rs"]
mod tests;