Skip to content

Commit

Permalink
Add a way to define a deadline for inline changes (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko authored Mar 28, 2024
1 parent ace8f34 commit 75d40b0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/text/inline.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)
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<Instant>,
) -> impl Iterator<Item = InlineChange<'slf, T>> + '_
where
'slf: 'old + 'new,
{
inline::iter_inline_changes(self, op, deadline)
}
}

Expand Down

0 comments on commit 75d40b0

Please sign in to comment.