Add mergeTextWithHistory function

This commit is contained in:
Andras Schmelczer 2025-06-22 20:49:11 +01:00
parent c0333c1146
commit 779579d38f
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
18 changed files with 285 additions and 100 deletions

15
src/utils/history.rs Normal file
View file

@ -0,0 +1,15 @@
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;
#[cfg_attr(feature = "wasm", wasm_bindgen)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum History {
Unchanged = "Unchanged",
AddedFromLeft = "AddedFromLeft",
AddedFromRight = "AddedFromRight",
RemovedFromLeft = "RemovedFromLeft",
RemovedFromRight = "RemovedFromRight",
}

View file

@ -1,5 +1,9 @@
use std::fmt::Display;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Side {
Left,

View file

@ -35,7 +35,8 @@ impl StringBuilder<'_> {
self.original.nth(length - 1);
if cfg!(debug_assertions) {
#[cfg(debug_assertions)]
{
self.remaining = self.remaining.chars().skip(length).collect();
}
}
@ -44,20 +45,28 @@ impl StringBuilder<'_> {
pub fn retain(&mut self, length: usize) {
self.buffer.extend(self.original.by_ref().take(length));
if cfg!(debug_assertions) {
#[cfg(debug_assertions)]
{
self.remaining = self.remaining.chars().skip(length).collect();
}
}
/// Returns the currently built buffer and clears it.
pub fn take(&mut self) -> String {
let result = self.buffer.clone();
self.buffer.clear();
result
}
/// Finish building the string after copying the remaining original string
/// since the last insertion or deletion.
pub fn build(self) -> String { self.buffer }
#[cfg(debug_assertions)]
/// Get a slice of the remaining original string. The slice starts from
/// where the next delete/retain operation would start and is of length
/// `length`. The implementation is quite suboptimal but it's only used
/// for debugging.
#[cfg(debug_assertions)]
pub fn get_slice_from_remaining(&self, length: usize) -> String {
let result = self.remaining.chars().take(length).collect::<String>();