From 5971faa53c072b16b5cf13f4ff8116db753e3c64 Mon Sep 17 00:00:00 2001 From: Wojciech Danilo Date: Tue, 4 Oct 2022 15:29:35 +0200 Subject: [PATCH] Fixes --- .../ensogl/component/text/src/buffer/rope.rs | 3 +++ .../component/text/src/component/text.rs | 23 ++++++++++++++----- lib/rust/ensogl/component/text/src/lib.rs | 3 +++ lib/rust/prelude/src/data/non_empty_vec.rs | 6 +++++ lib/rust/prelude/src/data/vec_indexed_by.rs | 10 ++++++++ 5 files changed, 39 insertions(+), 6 deletions(-) diff --git a/lib/rust/ensogl/component/text/src/buffer/rope.rs b/lib/rust/ensogl/component/text/src/buffer/rope.rs index ae452b6b16..989430ab71 100644 --- a/lib/rust/ensogl/component/text/src/buffer/rope.rs +++ b/lib/rust/ensogl/component/text/src/buffer/rope.rs @@ -8,3 +8,6 @@ pub mod formatted; pub mod word; + + + diff --git a/lib/rust/ensogl/component/text/src/component/text.rs b/lib/rust/ensogl/component/text/src/component/text.rs index 24a0d41193..501eb47505 100644 --- a/lib/rust/ensogl/component/text/src/component/text.rs +++ b/lib/rust/ensogl/component/text/src/component/text.rs @@ -1675,19 +1675,30 @@ impl TextModel { if range.single_line() { let view_line = ViewLine::from_in_context_snapped(self, range.start.line); let line = &mut lines[view_line]; - for glyph in &mut line.glyphs[range.start.offset..range.end.offset] { - glyph.set_property(property); + if let Some(last_index) = line.glyphs.last_valid_index() { + let start = std::cmp::min(last_index, range.start.offset); + let end = std::cmp::min(last_index + Column(1), range.end.offset); + for glyph in &mut line.glyphs[start..end] { + glyph.set_property(property); + } } } else { let view_line = ViewLine::from_in_context_snapped(self, range.start.line); let first_line = &mut lines[view_line]; - for glyph in &mut first_line.glyphs[range.start.offset..] { - glyph.set_property(property); + if let Some(last_index) = first_line.glyphs.last_valid_index() { + let start = std::cmp::min(last_index, range.start.offset); + for glyph in &mut first_line.glyphs[start..] { + glyph.set_property(property); + } } + let view_line = ViewLine::from_in_context_snapped(self, range.end.line); let last_line = &mut lines[view_line]; - for glyph in &mut last_line.glyphs[..range.end.offset] { - glyph.set_property(property); + if let Some(last_index) = last_line.glyphs.last_valid_index() { + let end = std::cmp::min(last_index + Column(1), range.end.offset); + for glyph in &mut last_line.glyphs[..end] { + glyph.set_property(property); + } } for line_index in range.start.line.value + 1..range.end.line.value { let view_line = ViewLine::from_in_context_snapped(self, Line(line_index)); diff --git a/lib/rust/ensogl/component/text/src/lib.rs b/lib/rust/ensogl/component/text/src/lib.rs index bf5194ede4..58fe0fc122 100644 --- a/lib/rust/ensogl/component/text/src/lib.rs +++ b/lib/rust/ensogl/component/text/src/lib.rs @@ -4,6 +4,7 @@ //! of [`enso_text`] crate carefully. #![recursion_limit = "1024"] + // === Features === #![feature(const_trait_impl)] #![feature(trait_alias)] @@ -15,10 +16,12 @@ #![feature(allocator_api)] #![feature(let_chains)] #![feature(step_trait)] + // === Standard Linter Configuration === #![deny(non_ascii_idents)] #![warn(unsafe_code)] #![allow(clippy::let_and_return)] + // === Non-Standard Linter Configuration === #![warn(missing_copy_implementations)] #![warn(missing_debug_implementations)] diff --git a/lib/rust/prelude/src/data/non_empty_vec.rs b/lib/rust/prelude/src/data/non_empty_vec.rs index 64b3ebfb54..fd60d77f4d 100644 --- a/lib/rust/prelude/src/data/non_empty_vec.rs +++ b/lib/rust/prelude/src/data/non_empty_vec.rs @@ -63,6 +63,12 @@ impl NonEmptyVec { self.elems.len() } + /// Return the last valid index. + pub fn last_valid_index(&self) -> I + where I: From { + (self.len() - 1).into() + } + /// Construct a `NonEmptyVec` containing a single element. /// /// # Examples diff --git a/lib/rust/prelude/src/data/vec_indexed_by.rs b/lib/rust/prelude/src/data/vec_indexed_by.rs index 4d2a0d12fe..c9774f4f88 100644 --- a/lib/rust/prelude/src/data/vec_indexed_by.rs +++ b/lib/rust/prelude/src/data/vec_indexed_by.rs @@ -76,6 +76,16 @@ where A: Allocator self.vec.len() } + /// Return the last valid index, if any. + pub fn last_valid_index(&self) -> Option + where I: From { + if self.vec.is_empty() { + None + } else { + Some((self.len() - 1).into()) + } + } + /// Check if the vector is empty. pub fn is_empty(&self) -> bool { self.vec.is_empty()