mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-26 06:01:48 +00:00
refactor(revset): move collect_similar
from cli_util
to revset
This commit is contained in:
parent
0fff404fb2
commit
f61cbae022
5 changed files with 18 additions and 16 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -865,7 +865,6 @@ dependencies = [
|
|||
"rpassword",
|
||||
"serde",
|
||||
"slab",
|
||||
"strsim",
|
||||
"tempfile",
|
||||
"testutils",
|
||||
"textwrap",
|
||||
|
@ -905,6 +904,7 @@ dependencies = [
|
|||
"rustix",
|
||||
"serde_json",
|
||||
"smallvec",
|
||||
"strsim",
|
||||
"tempfile",
|
||||
"test-case",
|
||||
"testutils",
|
||||
|
|
|
@ -56,7 +56,6 @@ regex = "1.8.3"
|
|||
rpassword = "7.2.0"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
slab = "0.4.8"
|
||||
strsim = "0.10.0"
|
||||
tempfile = "3.5.0"
|
||||
textwrap = "0.16.0"
|
||||
thiserror = "1.0.40"
|
||||
|
|
|
@ -41,6 +41,7 @@ rand_chacha = "0.3.1"
|
|||
regex = "1.8.3"
|
||||
serde_json = "1.0.96"
|
||||
smallvec = { version = "1.10.0", features = ["const_generics", "const_new", "union"] }
|
||||
strsim = "0.10.0"
|
||||
tempfile = "3.5.0"
|
||||
thiserror = "1.0.40"
|
||||
tracing = "0.1.37"
|
||||
|
|
|
@ -836,7 +836,7 @@ fn parse_function_expression(
|
|||
Err(RevsetParseError::with_span(
|
||||
RevsetParseErrorKind::NoSuchFunction {
|
||||
name: name.to_owned(),
|
||||
candidates: collect_function_names(state.aliases_map),
|
||||
candidates: collect_similar(name, &collect_function_names(state.aliases_map)),
|
||||
},
|
||||
name_pair.as_span(),
|
||||
))
|
||||
|
@ -854,6 +854,17 @@ fn collect_function_names(aliases_map: &RevsetAliasesMap) -> Vec<String> {
|
|||
names
|
||||
}
|
||||
|
||||
fn collect_similar(name: &str, candidates: &[impl AsRef<str>]) -> Vec<String> {
|
||||
candidates
|
||||
.iter()
|
||||
.filter_map(|cand| {
|
||||
// The parameter is borrowed from clap f5540d26
|
||||
(strsim::jaro(name, cand.as_ref()) > 0.7).then_some(cand)
|
||||
})
|
||||
.map(|s| s.as_ref().to_owned())
|
||||
.collect_vec()
|
||||
}
|
||||
|
||||
type RevsetFunction =
|
||||
fn(&str, Pair<Rule>, ParseState) -> Result<Rc<RevsetExpression>, RevsetParseError>;
|
||||
|
||||
|
|
|
@ -101,16 +101,6 @@ pub fn user_error_with_hint(message: impl Into<String>, hint: impl Into<String>)
|
|||
}
|
||||
}
|
||||
|
||||
fn collect_similar<'a, S: AsRef<str>>(name: &str, candidates: &'a [S]) -> Vec<&'a S> {
|
||||
candidates
|
||||
.iter()
|
||||
.filter_map(|cand| {
|
||||
// The parameter is borrowed from clap f5540d26
|
||||
(strsim::jaro(name, cand.as_ref()) > 0.7).then_some(cand)
|
||||
})
|
||||
.collect_vec()
|
||||
}
|
||||
|
||||
fn format_similarity_hint<S: AsRef<str>>(candidates: &[S]) -> Option<String> {
|
||||
match candidates {
|
||||
[] => None,
|
||||
|
@ -274,9 +264,10 @@ impl From<RevsetParseError> for CommandError {
|
|||
similar_op,
|
||||
description,
|
||||
} => Some(format!("Did you mean '{similar_op}' for {description}?")),
|
||||
RevsetParseErrorKind::NoSuchFunction { name, candidates } => {
|
||||
format_similarity_hint(&collect_similar(name, candidates))
|
||||
}
|
||||
RevsetParseErrorKind::NoSuchFunction {
|
||||
name: _,
|
||||
candidates,
|
||||
} => format_similarity_hint(candidates),
|
||||
_ => None,
|
||||
};
|
||||
CommandError::UserError {
|
||||
|
|
Loading…
Reference in a new issue