From 11751d8718cf55ee84744262a6dd165d32adfb19 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Mon, 15 Mar 2021 13:14:13 -0700 Subject: [PATCH] implement --spellsuggest with 0 arguments --- compiler/commands.nim | 5 ++-- compiler/lookups.nim | 5 +++- compiler/options.nim | 1 + doc/advopt.txt | 4 +++- tests/misc/tspellsuggest.nim | 2 +- tests/misc/tspellsuggest2.nim | 45 +++++++++++++++++++++++++++++++++++ 6 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 tests/misc/tspellsuggest2.nim diff --git a/compiler/commands.nim b/compiler/commands.nim index eefaa35cb2ae..f36a4f515606 100644 --- a/compiler/commands.nim +++ b/compiler/commands.nim @@ -864,8 +864,9 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo; of "listfullpaths": processOnOffSwitchG(conf, {optListFullPaths}, arg, pass, info) of "spellsuggest": - expectArg(conf, switch, arg, pass, info) - conf.spellSuggestMax = parseInt(arg) + if arg.len == 0: conf.spellSuggestMax = spellSuggestSecretSauce + elif arg == "auto": conf.spellSuggestMax = spellSuggestSecretSauce + else: conf.spellSuggestMax = parseInt(arg) of "declaredlocs": processOnOffSwitchG(conf, {optDeclaredLocs}, arg, pass, info) of "dynliboverride": diff --git a/compiler/lookups.nim b/compiler/lookups.nim index 2cca964bfe36..ee446dc34a38 100644 --- a/compiler/lookups.nim +++ b/compiler/lookups.nim @@ -403,8 +403,11 @@ proc fixSpelling(c: PContext, n: PNode, ident: PIdent, result: var string) = var count = 0 while true: # pending https://github.com/timotheecour/Nim/issues/373 use more efficient `itemsSorted`. - if count >= c.config.spellSuggestMax or list.len == 0: break + if list.len == 0: break let e = list.pop() + if c.config.spellSuggestMax == spellSuggestSecretSauce: + if e.dist > e0.dist: break + elif count >= c.config.spellSuggestMax: break if count == 0: result.add "\ncandidate misspellings (edit distance, lexical scope distance): " result.add "\n ($1, $2): '$3'" % [$e.dist, $e.depth, e.sym.name.s] diff --git a/compiler/options.nim b/compiler/options.nim index 55cb58e7fb5d..6aa7533f7668 100644 --- a/compiler/options.nim +++ b/compiler/options.nim @@ -576,6 +576,7 @@ const htmldocsDir* = htmldocsDirname.RelativeDir docRootDefault* = "@default" # using `@` instead of `$` to avoid shell quoting complications oKeepVariableNames* = true + spellSuggestSecretSauce* = -1 proc mainCommandArg*(conf: ConfigRef): string = ## This is intended for commands like check or parse diff --git a/doc/advopt.txt b/doc/advopt.txt index db36596c5a5c..e31fbd834dc5 100644 --- a/doc/advopt.txt +++ b/doc/advopt.txt @@ -35,7 +35,9 @@ Advanced options: --colors:on|off turn compiler messages coloring on|off --listFullPaths:on|off list full paths in messages --declaredLocs:on|off show declaration locations in messages - --spellSuggest:num show at most `num` spelling suggestions on typos + --spellSuggest|:num show at most `num >=0` spelling suggestions on typos. + if `num` is not specified (or `auto`), return + an implementation defined set of suggestions. -w:on|off|list, --warnings:on|off|list turn all warnings on|off or list all available --warning[X]:on|off turn specific warning X on|off diff --git a/tests/misc/tspellsuggest.nim b/tests/misc/tspellsuggest.nim index 4b93e76c89da..938be34607f3 100644 --- a/tests/misc/tspellsuggest.nim +++ b/tests/misc/tspellsuggest.nim @@ -23,7 +23,7 @@ candidate misspellings (edit distance, lexical scope distance): ''' """ -# tests `--spellsuggest` +# tests `--spellsuggest:num` diff --git a/tests/misc/tspellsuggest2.nim b/tests/misc/tspellsuggest2.nim new file mode 100644 index 000000000000..fc3d9668a896 --- /dev/null +++ b/tests/misc/tspellsuggest2.nim @@ -0,0 +1,45 @@ +discard """ + # pending bug #16521 (bug 12) use `matrix` + cmd: "nim c --spellsuggest --hints:off $file" + action: "reject" + nimout: ''' +tspellsuggest2.nim(45, 13) Error: undeclared identifier: 'fooBar' +candidate misspellings (edit distance, lexical scope distance): + (1, 0): 'fooBar8' [var declared in tspellsuggest2.nim(43, 9)] + (1, 1): 'fooBar7' [var declared in tspellsuggest2.nim(41, 7)] + (1, 3): 'fooBar1' [var declared in tspellsuggest2.nim(33, 5)] + (1, 3): 'fooBar2' [let declared in tspellsuggest2.nim(34, 5)] + (1, 3): 'fooBar3' [const declared in tspellsuggest2.nim(35, 7)] + (1, 3): 'fooBar4' [proc declared in tspellsuggest2.nim(36, 6)] + (1, 3): 'fooBar5' [template declared in tspellsuggest2.nim(37, 10)] + (1, 3): 'fooBar6' [macro declared in tspellsuggest2.nim(38, 7)] + (1, 5): 'FooBar' [type declared in mspellsuggest.nim(5, 6)] + (1, 5): 'fooBar4' [proc declared in mspellsuggest.nim(1, 6)] + (1, 5): 'fooBar9' [var declared in mspellsuggest.nim(2, 5)] + (1, 5): 'fooCar' [var declared in mspellsuggest.nim(4, 5)] +''' +""" + +# tests `--spellsuggest` + + + + + + +# line 30 +import ./mspellsuggest + +var fooBar1 = 0 +let fooBar2 = 0 +const fooBar3 = 0 +proc fooBar4() = discard +template fooBar5() = discard +macro fooBar6() = discard + +proc main = + var fooBar7 = 0 + block: + var fooBar8 = 0 + const fooBarBaz = 0 + let x = fooBar