mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-26 22:10:52 +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",
|
"rpassword",
|
||||||
"serde",
|
"serde",
|
||||||
"slab",
|
"slab",
|
||||||
"strsim",
|
|
||||||
"tempfile",
|
"tempfile",
|
||||||
"testutils",
|
"testutils",
|
||||||
"textwrap",
|
"textwrap",
|
||||||
|
@ -905,6 +904,7 @@ dependencies = [
|
||||||
"rustix",
|
"rustix",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"smallvec",
|
"smallvec",
|
||||||
|
"strsim",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
"test-case",
|
"test-case",
|
||||||
"testutils",
|
"testutils",
|
||||||
|
|
|
@ -56,7 +56,6 @@ regex = "1.8.3"
|
||||||
rpassword = "7.2.0"
|
rpassword = "7.2.0"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
slab = "0.4.8"
|
slab = "0.4.8"
|
||||||
strsim = "0.10.0"
|
|
||||||
tempfile = "3.5.0"
|
tempfile = "3.5.0"
|
||||||
textwrap = "0.16.0"
|
textwrap = "0.16.0"
|
||||||
thiserror = "1.0.40"
|
thiserror = "1.0.40"
|
||||||
|
|
|
@ -41,6 +41,7 @@ rand_chacha = "0.3.1"
|
||||||
regex = "1.8.3"
|
regex = "1.8.3"
|
||||||
serde_json = "1.0.96"
|
serde_json = "1.0.96"
|
||||||
smallvec = { version = "1.10.0", features = ["const_generics", "const_new", "union"] }
|
smallvec = { version = "1.10.0", features = ["const_generics", "const_new", "union"] }
|
||||||
|
strsim = "0.10.0"
|
||||||
tempfile = "3.5.0"
|
tempfile = "3.5.0"
|
||||||
thiserror = "1.0.40"
|
thiserror = "1.0.40"
|
||||||
tracing = "0.1.37"
|
tracing = "0.1.37"
|
||||||
|
|
|
@ -836,7 +836,7 @@ fn parse_function_expression(
|
||||||
Err(RevsetParseError::with_span(
|
Err(RevsetParseError::with_span(
|
||||||
RevsetParseErrorKind::NoSuchFunction {
|
RevsetParseErrorKind::NoSuchFunction {
|
||||||
name: name.to_owned(),
|
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(),
|
name_pair.as_span(),
|
||||||
))
|
))
|
||||||
|
@ -854,6 +854,17 @@ fn collect_function_names(aliases_map: &RevsetAliasesMap) -> Vec<String> {
|
||||||
names
|
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 =
|
type RevsetFunction =
|
||||||
fn(&str, Pair<Rule>, ParseState) -> Result<Rc<RevsetExpression>, RevsetParseError>;
|
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> {
|
fn format_similarity_hint<S: AsRef<str>>(candidates: &[S]) -> Option<String> {
|
||||||
match candidates {
|
match candidates {
|
||||||
[] => None,
|
[] => None,
|
||||||
|
@ -274,9 +264,10 @@ impl From<RevsetParseError> for CommandError {
|
||||||
similar_op,
|
similar_op,
|
||||||
description,
|
description,
|
||||||
} => Some(format!("Did you mean '{similar_op}' for {description}?")),
|
} => Some(format!("Did you mean '{similar_op}' for {description}?")),
|
||||||
RevsetParseErrorKind::NoSuchFunction { name, candidates } => {
|
RevsetParseErrorKind::NoSuchFunction {
|
||||||
format_similarity_hint(&collect_similar(name, candidates))
|
name: _,
|
||||||
}
|
candidates,
|
||||||
|
} => format_similarity_hint(candidates),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
CommandError::UserError {
|
CommandError::UserError {
|
||||||
|
|
Loading…
Reference in a new issue