From 611d5d7766e682ab7b05bd3896221ce8764bddf7 Mon Sep 17 00:00:00 2001 From: Araq Date: Wed, 17 Sep 2014 01:45:27 +0200 Subject: [PATCH] improvements for nimfix --- compiler/nimfix.nim | 10 +++++--- compiler/pretty.nim | 56 +++++++++++++++++++++++------------------ compiler/prettybase.nim | 2 ++ 3 files changed, 41 insertions(+), 27 deletions(-) diff --git a/compiler/nimfix.nim b/compiler/nimfix.nim index 7e546fe0f..15174fd48 100644 --- a/compiler/nimfix.nim +++ b/compiler/nimfix.nim @@ -21,9 +21,11 @@ Usage: Options: --overwriteFiles:on|off overwrite the original nim files. DEFAULT is ON! + --onlyMainfile overwrite only the main file. --checkExtern:on|off style check also extern names - --styleCheck:on|off performs style checking for identifiers - and suggests an alternative spelling. + --styleCheck:on|off|auto performs style checking for identifiers + and suggests an alternative spelling; + 'auto' corrects the spelling. In addition, all command line options of Nim are supported. """ @@ -63,8 +65,10 @@ proc processCmdLine*(pass: TCmdLinePass, cmd: string) = of "stylecheck": case p.val.normalize of "off": gStyleCheck = StyleCheck.None - of "on": gStyleCheck = StyleCheck.Auto + of "on": gStyleCheck = StyleCheck.Warn + of "auto": gStyleCheck = StyleCheck.Auto else: localError(gCmdLineInfo, errOnOrOffExpected) + of "onlymainfile": gOnlyMainfile = true else: processSwitch(pass, p) of cmdArgument: diff --git a/compiler/pretty.nim b/compiler/pretty.nim index ec9322309..acac574af 100644 --- a/compiler/pretty.nim +++ b/compiler/pretty.nim @@ -15,17 +15,18 @@ import intsets, strtabs, semdata, prettybase type - StyleCheck* {.pure.} = enum None, Confirm, Auto + StyleCheck* {.pure.} = enum None, Warn, Auto var gOverWrite* = true gStyleCheck*: StyleCheck - gCheckExtern*: bool + gCheckExtern*, gOnlyMainfile*: bool proc overwriteFiles*() = let doStrip = options.getConfigVar("pretty.strip").normalize == "on" for i in 0 .. high(gSourceFiles): - if gSourceFiles[i].dirty and not gSourceFiles[i].isNimfixFile: + if gSourceFiles[i].dirty and not gSourceFiles[i].isNimfixFile and + (not gOnlyMainfile or gSourceFiles[i].fileIdx == gProjectMainIdx): let newFile = if gOverWrite: gSourceFiles[i].fullpath else: gSourceFiles[i].fullpath.changeFileExt(".pretty.nim") try: @@ -89,17 +90,39 @@ proc beautifyName(s: string, k: TSymKind): string = result.add s[i] inc i -proc checkStyle(info: TLineInfo, s: string, k: TSymKind) = +proc replaceInFile(info: TLineInfo; newName: string) = + loadFile(info) + + let line = gSourceFiles[info.fileIndex].lines[info.line-1] + var first = min(info.col.int, line.len) + if first < 0: return + #inc first, skipIgnoreCase(line, "proc ", first) + while first > 0 and line[first-1] in prettybase.Letters: dec first + if first < 0: return + if line[first] == '`': inc first + + let last = first+identLen(line, first)-1 + if differ(line, first, last, newName): + # last-first+1 != newName.len or + var x = line.substr(0, first-1) & newName & line.substr(last+1) + system.shallowCopy(gSourceFiles[info.fileIndex].lines[info.line-1], x) + gSourceFiles[info.fileIndex].dirty = true + +proc checkStyle(info: TLineInfo, s: string, k: TSymKind; sym: PSym) = let beau = beautifyName(s, k) if s != beau: - message(info, hintName, beau) + if gStyleCheck == StyleCheck.Auto: + sym.name = getIdent(beau) + replaceInFile(info, beau) + else: + message(info, hintName, beau) proc styleCheckDefImpl(info: TLineInfo; s: PSym; k: TSymKind) = # operators stay as they are: if k in {skResult, skTemp} or s.name.s[0] notin prettybase.Letters: return if k in {skType, skGenericParam} and sfAnon in s.flags: return if {sfImportc, sfExportc} * s.flags == {} or gCheckExtern: - checkStyle(info, s.name.s, k) + checkStyle(info, s.name.s, k, s) template styleCheckDef*(info: TLineInfo; s: PSym; k: TSymKind) = when defined(nimfix): @@ -120,24 +143,9 @@ proc styleCheckUseImpl(info: TLineInfo; s: PSym) = return if s.kind in {skType, skGenericParam} and sfAnon in s.flags: return let newName = s.name.s - - loadFile(info) - - let line = gSourceFiles[info.fileIndex].lines[info.line-1] - var first = min(info.col.int, line.len) - if first < 0: return - #inc first, skipIgnoreCase(line, "proc ", first) - while first > 0 and line[first-1] in prettybase.Letters: dec first - if first < 0: return - if line[first] == '`': inc first - - let last = first+identLen(line, first)-1 - if differ(line, first, last, newName): - # last-first+1 != newName.len or - var x = line.substr(0, first-1) & newName & line.substr(last+1) - system.shallowCopy(gSourceFiles[info.fileIndex].lines[info.line-1], x) - gSourceFiles[info.fileIndex].dirty = true - #if newName == "File": writeStackTrace() + + replaceInFile(info, newName) + #if newName == "File": writeStackTrace() template styleCheckUse*(info: TLineInfo; s: PSym) = when defined(nimfix): diff --git a/compiler/prettybase.nim b/compiler/prettybase.nim index 21613396a..225b78479 100644 --- a/compiler/prettybase.nim +++ b/compiler/prettybase.nim @@ -15,6 +15,7 @@ type lines*: seq[string] dirty*, isNimfixFile*: bool fullpath*, newline*: string + fileIdx*: int32 var gSourceFiles*: seq[TSourceFile] = @[] @@ -24,6 +25,7 @@ proc loadFile*(info: TLineInfo) = if i >= gSourceFiles.len: gSourceFiles.setLen(i+1) if gSourceFiles[i].lines.isNil: + gSourceFiles[i].fileIdx = info.fileIndex gSourceFiles[i].lines = @[] let path = info.toFullPath gSourceFiles[i].fullpath = path