Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to define a deadline for inline changes #61

Merged
merged 3 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
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
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)
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