This commit is contained in:
koe 2024-08-31 13:27:48 -05:00 committed by Jeremy Soller
parent b68f4ad5c6
commit f89e64aa76
3 changed files with 14 additions and 6 deletions

View file

@ -766,7 +766,7 @@ impl Buffer {
loop { loop {
let (Some(line_range), Some((attrs, span_range))) = (&maybe_line, &maybe_span) else { let (Some(line_range), Some((attrs, span_range))) = (&maybe_line, &maybe_span) else {
// this is reached only if this text is empty // this is reached only if this text is empty
if self.lines.len() <= line_count { if self.lines.len() == line_count {
self.lines.push(BufferLine::empty()); self.lines.push(BufferLine::empty());
} }
self.lines[line_count].reset_new( self.lines[line_count].reset_new(
@ -817,7 +817,7 @@ impl Buffer {
.unwrap_or_default(); .unwrap_or_default();
let prev_attrs_list = core::mem::replace(&mut attrs_list, next_attrs_list); let prev_attrs_list = core::mem::replace(&mut attrs_list, next_attrs_list);
let prev_line_string = core::mem::replace(&mut line_string, next_line_string); let prev_line_string = core::mem::replace(&mut line_string, next_line_string);
if self.lines.len() <= line_count { if self.lines.len() == line_count {
self.lines.push(BufferLine::empty()); self.lines.push(BufferLine::empty());
} }
self.lines[line_count].reset_new( self.lines[line_count].reset_new(
@ -829,7 +829,7 @@ impl Buffer {
line_count += 1; line_count += 1;
} else { } else {
// finalize the final line // finalize the final line
if self.lines.len() <= line_count { if self.lines.len() == line_count {
self.lines.push(BufferLine::empty()); self.lines.push(BufferLine::empty());
} }
self.lines[line_count].reset_new(line_string, line_ending, attrs_list, shaping); self.lines[line_count].reset_new(line_string, line_ending, attrs_list, shaping);

View file

@ -309,7 +309,7 @@ impl BufferLine {
/// Makes an empty buffer line. /// Makes an empty buffer line.
/// ///
/// The buffer line is in an invalid state after this is called. See [`Self::reclaim_new`]. /// The buffer line is in an invalid state after this is called. See [`Self::reset_new`].
pub(crate) fn empty() -> Self { pub(crate) fn empty() -> Self {
Self { Self {
text: String::default(), text: String::default(),
@ -325,14 +325,14 @@ impl BufferLine {
/// Reclaim attributes list memory that isn't needed any longer. /// Reclaim attributes list memory that isn't needed any longer.
/// ///
/// The buffer line is in an invalid state after this is called. See [`Self::reclaim_new`]. /// The buffer line is in an invalid state after this is called. See [`Self::reset_new`].
pub(crate) fn reclaim_attrs(&mut self) -> AttrsList { pub(crate) fn reclaim_attrs(&mut self) -> AttrsList {
std::mem::replace(&mut self.attrs_list, AttrsList::new(Attrs::new())) std::mem::replace(&mut self.attrs_list, AttrsList::new(Attrs::new()))
} }
/// Reclaim text memory that isn't needed any longer. /// Reclaim text memory that isn't needed any longer.
/// ///
/// The buffer line is in an invalid state after this is called. See [`Self::reclaim_new`]. /// The buffer line is in an invalid state after this is called. See [`Self::reset_new`].
pub(crate) fn reclaim_text(&mut self) -> String { pub(crate) fn reclaim_text(&mut self) -> String {
let mut text = std::mem::take(&mut self.text); let mut text = std::mem::take(&mut self.text);
text.clear(); text.clear();

View file

@ -9,6 +9,7 @@ pub enum Cached<T: Clone + Debug> {
} }
impl<T: Clone + Debug> Cached<T> { impl<T: Clone + Debug> Cached<T> {
/// Gets the value if in state `Self::Used`.
pub fn get(&self) -> Option<&T> { pub fn get(&self) -> Option<&T> {
match self { match self {
Self::Empty | Self::Unused(_) => None, Self::Empty | Self::Unused(_) => None,
@ -16,6 +17,7 @@ impl<T: Clone + Debug> Cached<T> {
} }
} }
/// Gets the value mutably if in state `Self::Used`.
pub fn get_mut(&mut self) -> Option<&mut T> { pub fn get_mut(&mut self) -> Option<&mut T> {
match self { match self {
Self::Empty | Self::Unused(_) => None, Self::Empty | Self::Unused(_) => None,
@ -23,6 +25,7 @@ impl<T: Clone + Debug> Cached<T> {
} }
} }
/// Checks if the value is empty or unused.
pub fn is_unused(&self) -> bool { pub fn is_unused(&self) -> bool {
match self { match self {
Self::Empty | Self::Unused(_) => true, Self::Empty | Self::Unused(_) => true,
@ -30,6 +33,7 @@ impl<T: Clone + Debug> Cached<T> {
} }
} }
/// Checks if the value is used (i.e. cached for access).
pub fn is_used(&self) -> bool { pub fn is_used(&self) -> bool {
match self { match self {
Self::Empty | Self::Unused(_) => false, Self::Empty | Self::Unused(_) => false,
@ -37,6 +41,7 @@ impl<T: Clone + Debug> Cached<T> {
} }
} }
/// Takes the buffered value if in state `Self::Unused`.
pub fn take_unused(&mut self) -> Option<T> { pub fn take_unused(&mut self) -> Option<T> {
if matches!(*self, Self::Unused(_)) { if matches!(*self, Self::Unused(_)) {
let Self::Unused(val) = std::mem::replace(self, Self::Empty) else { let Self::Unused(val) = std::mem::replace(self, Self::Empty) else {
@ -48,6 +53,7 @@ impl<T: Clone + Debug> Cached<T> {
} }
} }
/// Takes the cached value if in state `Self::Used`.
pub fn take_used(&mut self) -> Option<T> { pub fn take_used(&mut self) -> Option<T> {
if matches!(*self, Self::Used(_)) { if matches!(*self, Self::Used(_)) {
let Self::Used(val) = std::mem::replace(self, Self::Empty) else { let Self::Used(val) = std::mem::replace(self, Self::Empty) else {
@ -59,12 +65,14 @@ impl<T: Clone + Debug> Cached<T> {
} }
} }
/// Moves the value from `Self::Used` to `Self::Unused`.
pub fn set_unused(&mut self) { pub fn set_unused(&mut self) {
if matches!(*self, Self::Used(_)) { if matches!(*self, Self::Used(_)) {
*self = Self::Unused(self.take_used().unwrap()); *self = Self::Unused(self.take_used().unwrap());
} }
} }
/// Sets the value to `Self::Used`.
pub fn set_used(&mut self, val: T) { pub fn set_used(&mut self, val: T) {
*self = Self::Used(val); *self = Self::Used(val);
} }