From 87165ccd29f0e85a26774ec090d8400274c6d04e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 28 Jan 2025 06:23:38 +0100 Subject: Introduce `LineEnding` to `editor` and fix inconsistencies --- core/src/renderer/null.rs | 2 +- core/src/text/editor.rs | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) (limited to 'core') diff --git a/core/src/renderer/null.rs b/core/src/renderer/null.rs index bbcdd8ff..5732c41b 100644 --- a/core/src/renderer/null.rs +++ b/core/src/renderer/null.rs @@ -137,7 +137,7 @@ impl text::Editor for () { None } - fn line(&self, _index: usize) -> Option<&str> { + fn line(&self, _index: usize) -> Option> { None } diff --git a/core/src/text/editor.rs b/core/src/text/editor.rs index cd30db3a..6921c61c 100644 --- a/core/src/text/editor.rs +++ b/core/src/text/editor.rs @@ -3,6 +3,7 @@ use crate::text::highlighter::{self, Highlighter}; use crate::text::{LineHeight, Wrapping}; use crate::{Pixels, Point, Rectangle, Size}; +use std::borrow::Cow; use std::sync::Arc; /// A component that can be used by widgets to edit multi-line text. @@ -28,7 +29,7 @@ pub trait Editor: Sized + Default { fn selection(&self) -> Option; /// Returns the text of the given line in the [`Editor`], if it exists. - fn line(&self, index: usize) -> Option<&str>; + fn line(&self, index: usize) -> Option>; /// Returns the amount of lines in the [`Editor`]. fn line_count(&self) -> usize; @@ -189,3 +190,41 @@ pub enum Cursor { /// Cursor selecting a range of text Selection(Vec), } + +/// A line of an [`Editor`]. +#[derive(Clone, Debug, Default, Eq, PartialEq)] +pub struct Line<'a> { + /// The raw text of the [`Line`]. + pub text: Cow<'a, str>, + /// The line ending of the [`Line`]. + pub ending: LineEnding, +} + +/// The line ending of a [`Line`]. +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] +pub enum LineEnding { + /// Use `\n` for line ending (POSIX-style) + #[default] + Lf, + /// Use `\r\n` for line ending (Windows-style) + CrLf, + /// Use `\r` for line ending (many legacy systems) + Cr, + /// Use `\n\r` for line ending (some legacy systems) + LfCr, + /// No line ending + None, +} + +impl LineEnding { + /// Gets the string representation of the [`LineEnding`]. + pub fn as_str(self) -> &'static str { + match self { + Self::Lf => "\n", + Self::CrLf => "\r\n", + Self::Cr => "\r", + Self::LfCr => "\n\r", + Self::None => "", + } + } +} -- cgit