From bdee5e7995b29e1c5d431b4339a598551ba36424 Mon Sep 17 00:00:00 2001 From: KtorZ Date: Wed, 22 Feb 2023 13:29:39 +0100 Subject: [PATCH] Use variable-length threshold for levenshtein distance Fixes #348 --- crates/aiken-lang/src/tipo/error.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/aiken-lang/src/tipo/error.rs b/crates/aiken-lang/src/tipo/error.rs index 4bb87529..e73622c7 100644 --- a/crates/aiken-lang/src/tipo/error.rs +++ b/crates/aiken-lang/src/tipo/error.rs @@ -875,11 +875,12 @@ fn suggest_neighbor<'a>( items: impl Iterator, default: &'a str, ) -> String { + let threshold = (name.len() as f64).sqrt().round() as usize; items .map(|s| (s, levenshtein::distance(name, s))) .min_by(|(_, a), (_, b)| a.cmp(b)) .and_then(|(suggestion, distance)| { - if distance <= 4 { + if distance <= threshold { Some(format!("Did you mean '{}'?", suggestion.yellow())) } else { None