Skip to content

Commit

Permalink
implement --spellsuggest with 0 arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Mar 15, 2021
1 parent f62ba97 commit 11751d8
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 5 deletions.
5 changes: 3 additions & 2 deletions compiler/commands.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
5 changes: 4 additions & 1 deletion compiler/lookups.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions compiler/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion doc/advopt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/misc/tspellsuggest.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ candidate misspellings (edit distance, lexical scope distance):
'''
"""

# tests `--spellsuggest`
# tests `--spellsuggest:num`



Expand Down
45 changes: 45 additions & 0 deletions tests/misc/tspellsuggest2.nim
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 11751d8

Please sign in to comment.