Skip to content

Commit 1f12b88

Browse files
committed
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`
2 parents 47b869c + f0992e3 commit 1f12b88

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/cargo/util/lev_distance.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
use std::cmp;
22

33
pub fn lev_distance(me: &str, t: &str) -> usize {
4+
// Comparing the strings lowercased will result in a difference in capitalization being less distance away
5+
// than being a completely different letter. Otherwise `CHECK` is as far away from `check` as it
6+
// is from `build` (both with a distance of 5). For a single letter shortcut (e.g. `b` or `c`), they will
7+
// all be as far away from any capital single letter entry (all with a distance of 1).
8+
// By first lowercasing the strings, `C` and `c` are closer than `C` and `b`, for example.
9+
let me = me.to_lowercase();
10+
let t = t.to_lowercase();
11+
412
let t_len = t.chars().count();
513
if me.is_empty() {
614
return t_len;

tests/testsuite/cargo_command.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,34 @@ fn list_command_resolves_symlinks() {
147147
);
148148
}
149149

150+
#[cargo_test]
151+
fn find_closest_capital_c_to_c() {
152+
cargo_process("C")
153+
.with_status(101)
154+
.with_stderr_contains(
155+
"\
156+
error: no such subcommand: `C`
157+
158+
<tab>Did you mean `c`?
159+
",
160+
)
161+
.run();
162+
}
163+
164+
#[cargo_test]
165+
fn find_closest_captial_b_to_b() {
166+
cargo_process("B")
167+
.with_status(101)
168+
.with_stderr_contains(
169+
"\
170+
error: no such subcommand: `B`
171+
172+
<tab>Did you mean `b`?
173+
",
174+
)
175+
.run();
176+
}
177+
150178
#[cargo_test]
151179
fn find_closest_biuld_to_build() {
152180
cargo_process("biuld")

0 commit comments

Comments
 (0)