Add a way to define a deadline for inline changes (#61)
This commit is contained in:
parent
ace8f34a27
commit
75d40b041b
3 changed files with 26 additions and 4 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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<Instant>,
|
||||
) -> impl Iterator<Item = InlineChange<'x, T>> + '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 {
|
||||
|
|
|
|||
|
|
@ -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<Instant>,
|
||||
) -> impl Iterator<Item = InlineChange<'slf, T>> + '_
|
||||
where
|
||||
'slf: 'old + 'new,
|
||||
{
|
||||
inline::iter_inline_changes(self, op, deadline)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue