mirror of
https://github.com/rust-lang/cargo.git
synced 2025-10-01 11:30:39 +00:00
Auto merge of #10224 - MidasLamb:find-closest-capitalization, r=joshtriplett
Make levenshtein distance case insensitive. When typing in a single character shortcut as a capital, it always returns `b` as the suggestion as every one-letter abbreviation is a lev distance 1 away from the capitalized one. By making the levenshtein distance case insensitive, the case-mismatched one-letter abbriviation (e.g. `C` to `c`) will be suggested, rather than `b`
This commit is contained in:
commit
1f12b88497
@ -1,6 +1,14 @@
|
|||||||
use std::cmp;
|
use std::cmp;
|
||||||
|
|
||||||
pub fn lev_distance(me: &str, t: &str) -> usize {
|
pub fn lev_distance(me: &str, t: &str) -> usize {
|
||||||
|
// Comparing the strings lowercased will result in a difference in capitalization being less distance away
|
||||||
|
// than being a completely different letter. Otherwise `CHECK` is as far away from `check` as it
|
||||||
|
// is from `build` (both with a distance of 5). For a single letter shortcut (e.g. `b` or `c`), they will
|
||||||
|
// all be as far away from any capital single letter entry (all with a distance of 1).
|
||||||
|
// By first lowercasing the strings, `C` and `c` are closer than `C` and `b`, for example.
|
||||||
|
let me = me.to_lowercase();
|
||||||
|
let t = t.to_lowercase();
|
||||||
|
|
||||||
let t_len = t.chars().count();
|
let t_len = t.chars().count();
|
||||||
if me.is_empty() {
|
if me.is_empty() {
|
||||||
return t_len;
|
return t_len;
|
||||||
|
@ -147,6 +147,34 @@ fn list_command_resolves_symlinks() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cargo_test]
|
||||||
|
fn find_closest_capital_c_to_c() {
|
||||||
|
cargo_process("C")
|
||||||
|
.with_status(101)
|
||||||
|
.with_stderr_contains(
|
||||||
|
"\
|
||||||
|
error: no such subcommand: `C`
|
||||||
|
|
||||||
|
<tab>Did you mean `c`?
|
||||||
|
",
|
||||||
|
)
|
||||||
|
.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cargo_test]
|
||||||
|
fn find_closest_captial_b_to_b() {
|
||||||
|
cargo_process("B")
|
||||||
|
.with_status(101)
|
||||||
|
.with_stderr_contains(
|
||||||
|
"\
|
||||||
|
error: no such subcommand: `B`
|
||||||
|
|
||||||
|
<tab>Did you mean `b`?
|
||||||
|
",
|
||||||
|
)
|
||||||
|
.run();
|
||||||
|
}
|
||||||
|
|
||||||
#[cargo_test]
|
#[cargo_test]
|
||||||
fn find_closest_biuld_to_build() {
|
fn find_closest_biuld_to_build() {
|
||||||
cargo_process("biuld")
|
cargo_process("biuld")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user