Introduce max_suggestion_distance function to avoid duplicating the heuristic
This commit is contained in:
parent
9ba657cad5
commit
4bb7cf11dc
3 changed files with 12 additions and 10 deletions
|
|
@ -72,7 +72,7 @@ use syntax::ext::mtwt;
|
|||
use syntax::parse::token::{self, special_names, special_idents};
|
||||
use syntax::ptr::P;
|
||||
use syntax::codemap::{self, Span, Pos};
|
||||
use syntax::util::lev_distance::lev_distance;
|
||||
use syntax::util::lev_distance::{lev_distance, max_suggestion_distance};
|
||||
|
||||
use rustc_front::intravisit::{self, FnKind, Visitor};
|
||||
use rustc_front::hir;
|
||||
|
|
@ -3384,11 +3384,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
// As a loose rule to avoid obviously incorrect suggestions, clamp the
|
||||
// maximum edit distance we will accept for a suggestion to one third of
|
||||
// the typo'd name's length.
|
||||
let max_distance = std::cmp::max(name.len(), 3) / 3;
|
||||
|
||||
let max_distance = max_suggestion_distance(name);
|
||||
if !values.is_empty() && values[smallest] <= max_distance && name != &maybes[smallest][..] {
|
||||
|
||||
SuggestionType::Function(maybes[smallest].to_string())
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ use parse::token;
|
|||
use parse::token::{InternedString, intern, str_to_ident};
|
||||
use ptr::P;
|
||||
use util::small_vector::SmallVector;
|
||||
use util::lev_distance::lev_distance;
|
||||
use util::lev_distance::{lev_distance, max_suggestion_distance};
|
||||
use ext::mtwt;
|
||||
use fold::Folder;
|
||||
|
||||
|
|
@ -779,10 +779,8 @@ impl<'a> ExtCtxt<'a> {
|
|||
}
|
||||
|
||||
pub fn suggest_macro_name(&mut self, name: &str, span: Span) {
|
||||
use std::cmp::max;
|
||||
|
||||
let mut min: Option<(Name, usize)> = None;
|
||||
let max_dist = max(name.len() / 3, 1);
|
||||
let max_dist = max_suggestion_distance(name);
|
||||
for macro_name in self.syntax_env.names.iter() {
|
||||
let dist = lev_distance(name, ¯o_name.as_str());
|
||||
if dist <= max_dist && (min.is_none() || min.unwrap().1 > dist) {
|
||||
|
|
|
|||
|
|
@ -41,6 +41,14 @@ pub fn lev_distance(me: &str, t: &str) -> usize {
|
|||
dcol[t_last + 1]
|
||||
}
|
||||
|
||||
pub fn max_suggestion_distance(name: &str) -> usize {
|
||||
use std::cmp::max;
|
||||
// As a loose rule to avoid obviously incorrect suggestions, clamp the
|
||||
// maximum edit distance we will accept for a suggestion to one third of
|
||||
// the typo'd name's length.
|
||||
max(name.len(), 3) / 3
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_lev_distance() {
|
||||
use std::char::{ from_u32, MAX };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue