@@ -926,3 +926,66 @@ fn (t &Type) contains_field_type(typ string) bool {
926926 }
927927 return false
928928}
929+
930+ // check for a function / variable / module typo in `name`
931+ fn (table &Table) identify_typo (name string , current_fn & Fn, fit & FileImportTable) string {
932+ // dont check if so short
933+ if name.len < 2 { return '' }
934+ min_match := 0.8 // for dice coefficient between 0.0 - 1.0
935+ name_orig := name.replace ('__' , '.' ).replace ('_dot_' , '.' )
936+ mut output := ''
937+ // check functions
938+ mut n := table.find_misspelled_fn (name_orig, min_match)
939+ if n != '' {
940+ output + = '\n * function: `$n `'
941+ }
942+ // check function local variables
943+ n = current_fn.find_misspelled_local_var (name_orig, min_match)
944+ if n != '' {
945+ output + = '\n * variable: `$n `'
946+ }
947+ // check imported modules
948+ n = table.find_misspelled_imported_mod (name_orig, fit, min_match)
949+ if n != '' {
950+ output + = '\n * module: `$n `'
951+ }
952+ return output
953+ }
954+
955+ // find function with closest name to `name`
956+ fn (table &Table) find_misspelled_fn (name string , min_match f64 ) string {
957+ mut closest := f64 (0 )
958+ mut closest_fn := ''
959+ for _, f in table.fns {
960+ n := '${f.mod} .$f.name '
961+ if ! name.starts_with (f.mod) || (n.len - name.len > 3 || name.len - n.len > 3 ) { continue }
962+ p := strings.dice_coefficient (name, n)
963+ if p > closest {
964+ closest = p
965+ closest_fn = n
966+ }
967+ }
968+ if closest > = min_match {
969+ return closest_fn
970+ }
971+ return ''
972+ }
973+
974+ // find imported module with closest name to `name`
975+ fn (table &Table) find_misspelled_imported_mod (name string , fit & FileImportTable, min_match f64 ) string {
976+ mut closest := f64 (0 )
977+ mut closest_mod := ''
978+ for alias, mod in fit.imports {
979+ n := '${fit.module_name} .$alias '
980+ if ! name.starts_with (fit.module_name) || (n.len - name.len > 3 || name.len - n.len > 3 ) { continue }
981+ p := strings.dice_coefficient (name, n)
982+ if p > closest {
983+ closest = p
984+ closest_mod = '$alias ($mod )'
985+ }
986+ }
987+ if closest > = min_match {
988+ return closest_mod
989+ }
990+ return ''
991+ }
0 commit comments