compiler: clean up the typo check
parent
9dd86f6fb8
commit
5aaa794519
|
@ -1018,21 +1018,17 @@ fn (f &Fn) str_args(table &Table) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// find local function variable with closest name to `name`
|
// find local function variable with closest name to `name`
|
||||||
fn (f &Fn) find_misspelled_local_var(name string, min_match f64) string {
|
fn (f &Fn) find_misspelled_local_var(name string, min_match f32) string {
|
||||||
mut closest := f64(0)
|
mut closest := f32(0)
|
||||||
mut closest_var := ''
|
mut closest_var := ''
|
||||||
for var in f.local_vars {
|
for var in f.local_vars {
|
||||||
n := '${f.mod}.$var.name'
|
n := '${f.mod}.$var.name'
|
||||||
if var.name == '' || !name.starts_with(f.mod) || (n.len - name.len > 3 || name.len - n.len > 3) { continue }
|
if var.name == '' || !name.starts_with(f.mod) || (n.len - name.len > 3 || name.len - n.len > 3) { continue }
|
||||||
p := strings.dice_coefficient(name, n)
|
p := strings.dice_coefficient(name, n)
|
||||||
println(' ## $name - $n: $p')
|
|
||||||
if p > closest {
|
if p > closest {
|
||||||
closest = p
|
closest = p
|
||||||
closest_var = n
|
closest_var = n
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if closest >= min_match {
|
return if closest >= min_match { closest_var } else { '' }
|
||||||
return closest_var
|
|
||||||
}
|
|
||||||
return ''
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -955,8 +955,8 @@ fn (table &Table) identify_typo(name string, current_fn &Fn, fit &FileImportTabl
|
||||||
}
|
}
|
||||||
|
|
||||||
// find function with closest name to `name`
|
// find function with closest name to `name`
|
||||||
fn (table &Table) find_misspelled_fn(name string, min_match f64) string {
|
fn (table &Table) find_misspelled_fn(name string, min_match f32) string {
|
||||||
mut closest := f64(0)
|
mut closest := f32(0)
|
||||||
mut closest_fn := ''
|
mut closest_fn := ''
|
||||||
for _, f in table.fns {
|
for _, f in table.fns {
|
||||||
n := '${f.mod}.$f.name'
|
n := '${f.mod}.$f.name'
|
||||||
|
@ -967,15 +967,12 @@ fn (table &Table) find_misspelled_fn(name string, min_match f64) string {
|
||||||
closest_fn = n
|
closest_fn = n
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if closest >= min_match {
|
return if closest >= min_match { closest_fn } else { '' }
|
||||||
return closest_fn
|
|
||||||
}
|
|
||||||
return ''
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// find imported module with closest name to `name`
|
// find imported module with closest name to `name`
|
||||||
fn (table &Table) find_misspelled_imported_mod(name string, fit &FileImportTable, min_match f64) string {
|
fn (table &Table) find_misspelled_imported_mod(name string, fit &FileImportTable, min_match f32) string {
|
||||||
mut closest := f64(0)
|
mut closest := f32(0)
|
||||||
mut closest_mod := ''
|
mut closest_mod := ''
|
||||||
for alias, mod in fit.imports {
|
for alias, mod in fit.imports {
|
||||||
n := '${fit.module_name}.$alias'
|
n := '${fit.module_name}.$alias'
|
||||||
|
@ -986,8 +983,5 @@ fn (table &Table) find_misspelled_imported_mod(name string, fit &FileImportTable
|
||||||
closest_mod = '$alias ($mod)'
|
closest_mod = '$alias ($mod)'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if closest >= min_match {
|
return if closest >= min_match { closest_mod } else { '' }
|
||||||
return closest_mod
|
|
||||||
}
|
|
||||||
return ''
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,16 +25,16 @@ pub fn levenshtein_distance(a, b string) int {
|
||||||
|
|
||||||
// use levenshtein distance algorithm to calculate
|
// use levenshtein distance algorithm to calculate
|
||||||
// how similar two strings are as a percentage (higher is closer)
|
// how similar two strings are as a percentage (higher is closer)
|
||||||
pub fn levenshtein_distance_percentage(a, b string) f64 {
|
pub fn levenshtein_distance_percentage(a, b string) f32 {
|
||||||
d := levenshtein_distance(a, b)
|
d := levenshtein_distance(a, b)
|
||||||
l := if a.len >= b.len { a.len } else { b.len }
|
l := if a.len >= b.len { a.len } else { b.len }
|
||||||
return (1.00 - f64(d)/f64(l)) * 100.00
|
return (1.00 - f32(d)/f32(l)) * 100.00
|
||||||
}
|
}
|
||||||
|
|
||||||
// implementation of Sørensen–Dice coefficient.
|
// implementation of Sørensen–Dice coefficient.
|
||||||
// find the similarity between two strings.
|
// find the similarity between two strings.
|
||||||
// returns f64 between 0.0 (not similar) and 1.0 (exact match).
|
// returns f64 between 0.0 (not similar) and 1.0 (exact match).
|
||||||
pub fn dice_coefficient(s1, s2 string) f64 {
|
pub fn dice_coefficient(s1, s2 string) f32 {
|
||||||
if s1.len == 0 || s2.len == 0 { return 0.0 }
|
if s1.len == 0 || s2.len == 0 { return 0.0 }
|
||||||
if s1 == s2 { return 1.0 }
|
if s1 == s2 { return 1.0 }
|
||||||
if s1.len < 2 || s2.len < 2 { return 0.0 }
|
if s1.len < 2 || s2.len < 2 { return 0.0 }
|
||||||
|
@ -55,5 +55,5 @@ pub fn dice_coefficient(s1, s2 string) f64 {
|
||||||
intersection_size++
|
intersection_size++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (2.0 * intersection_size) / (f64(s1.len) + f64(s2.len) - 2)
|
return (2.0 * intersection_size) / (f32(s1.len) + f32(s2.len) - 2)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue