Skip to content

Commit

Permalink
Add a way to define a deadline for inline changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Mar 28, 2024
1 parent ace8f34 commit f2a7c87
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/text/inline.rs
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Instant>,
) -> impl Iterator<Item = InlineChange<'x, T>> + 'diff
where
T: DiffableStr + ?Sized,
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 19 additions & 1 deletion src/text/mod.rs
Expand Up @@ -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.
Expand All @@ -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)
self.iter_inline_changes_deadline(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<Instant>,
) -> impl Iterator<Item = InlineChange<'slf, T>> + '_
where
'slf: 'old + 'new,
{
inline::iter_inline_changes(self, op, deadline)
}
}

Expand Down

0 comments on commit f2a7c87

Please sign in to comment.