diff options
author | 2025-01-28 06:23:38 +0100 | |
---|---|---|
committer | 2025-01-28 06:23:38 +0100 | |
commit | 87165ccd29f0e85a26774ec090d8400274c6d04e (patch) | |
tree | d057c83712b9228b787adb8ac327b25bbf95795c /core | |
parent | 00a048677fc43a5e3106dff17e0c1c490fdc5ce1 (diff) | |
download | iced-87165ccd29f0e85a26774ec090d8400274c6d04e.tar.gz iced-87165ccd29f0e85a26774ec090d8400274c6d04e.tar.bz2 iced-87165ccd29f0e85a26774ec090d8400274c6d04e.zip |
Introduce `LineEnding` to `editor` and fix inconsistencies
Diffstat (limited to 'core')
-rw-r--r-- | core/src/renderer/null.rs | 2 | ||||
-rw-r--r-- | core/src/text/editor.rs | 41 |
2 files changed, 41 insertions, 2 deletions
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<text::editor::Line<'_>> { 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<String>; /// 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<Line<'_>>; /// 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<Rectangle>), } + +/// 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 => "", + } + } +} |