From 75d40b041bea7c82003a360f2dee956ddba893bb Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Thu, 28 Mar 2024 21:52:56 +0100 Subject: [PATCH] Add a way to define a deadline for inline changes (#61) --- CHANGELOG.md | 4 ++++ src/text/inline.rs | 6 +++--- src/text/mod.rs | 20 +++++++++++++++++++- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b69881a..488a6d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to similar are documented here. +## 2.5.0 + +* Added support for `TextDiff::iter_inline_changes_deadline`. #61 + ## 2.4.0 * Fixed a bug where the LCS diff algorithm didn't always call `D::finish`. (#58) diff --git a/src/text/inline.rs b/src/text/inline.rs index 0b252ac..34cfd39 100644 --- a/src/text/inline.rs +++ b/src/text/inline.rs @@ -7,7 +7,7 @@ use crate::types::{Algorithm, Change, ChangeTag, DiffOp, DiffTag}; use crate::{capture_diff_deadline, get_diff_ratio}; use std::ops::Index; -use std::time::{Duration, Instant}; +use std::time::Instant; use super::utils::upper_seq_ratio; @@ -195,11 +195,11 @@ impl<'s, T: DiffableStr + ?Sized> fmt::Display for InlineChange<'s, T> { } const MIN_RATIO: f32 = 0.5; -const TIMEOUT_MS: u64 = 500; pub(crate) fn iter_inline_changes<'x, 'diff, 'old, 'new, 'bufs, T>( diff: &'diff TextDiff<'old, 'new, 'bufs, T>, op: &DiffOp, + deadline: Option, ) -> impl Iterator> + 'diff where T: DiffableStr + ?Sized, @@ -231,7 +231,7 @@ where 0..old_lookup.len(), &new_lookup, 0..new_lookup.len(), - Some(Instant::now() + Duration::from_millis(TIMEOUT_MS)), + deadline, ); if get_diff_ratio(&ops, old_lookup.len(), new_lookup.len()) < MIN_RATIO { diff --git a/src/text/mod.rs b/src/text/mod.rs index 0a441d1..84e300c 100644 --- a/src/text/mod.rs +++ b/src/text/mod.rs @@ -531,6 +531,9 @@ impl<'old, 'new, 'bufs, T: DiffableStr + ?Sized + 'old + 'new> TextDiff<'old, 'n /// this function with regards to how it detects those inline changes /// is currently not defined and will likely change over time. /// + /// This method has a hardcoded 500ms deadline which is often not ideal. For + /// fine tuning use [`iter_inline_changes_deadline`](Self::iter_inline_changes_deadline). + /// /// As of similar 1.2.0 the behavior of this function changes depending on /// if the `unicode` feature is enabled or not. It will prefer unicode word /// splitting over word splitting depending on the feature flag. @@ -544,7 +547,22 @@ impl<'old, 'new, 'bufs, T: DiffableStr + ?Sized + 'old + 'new> TextDiff<'old, 'n where 'slf: 'old + 'new, { - inline::iter_inline_changes(self, op) + inline::iter_inline_changes(self, op, Some(Instant::now() + Duration::from_millis(500))) + } + + /// Iterates over the changes the op expands to with inline emphasis with a deadline. + /// + /// Like [`iter_inline_changes`](Self::iter_inline_changes) but with an explicit deadline. + #[cfg(feature = "inline")] + pub fn iter_inline_changes_deadline<'slf>( + &'slf self, + op: &DiffOp, + deadline: Option, + ) -> impl Iterator> + '_ + where + 'slf: 'old + 'new, + { + inline::iter_inline_changes(self, op, deadline) } }