improvements for nimfix
This commit is contained in:
parent
344488fa0d
commit
611d5d7766
3 changed files with 41 additions and 27 deletions
|
|
@ -21,9 +21,11 @@ Usage:
|
||||||
Options:
|
Options:
|
||||||
--overwriteFiles:on|off overwrite the original nim files.
|
--overwriteFiles:on|off overwrite the original nim files.
|
||||||
DEFAULT is ON!
|
DEFAULT is ON!
|
||||||
|
--onlyMainfile overwrite only the main file.
|
||||||
--checkExtern:on|off style check also extern names
|
--checkExtern:on|off style check also extern names
|
||||||
--styleCheck:on|off performs style checking for identifiers
|
--styleCheck:on|off|auto performs style checking for identifiers
|
||||||
and suggests an alternative spelling.
|
and suggests an alternative spelling;
|
||||||
|
'auto' corrects the spelling.
|
||||||
|
|
||||||
In addition, all command line options of Nim are supported.
|
In addition, all command line options of Nim are supported.
|
||||||
"""
|
"""
|
||||||
|
|
@ -63,8 +65,10 @@ proc processCmdLine*(pass: TCmdLinePass, cmd: string) =
|
||||||
of "stylecheck":
|
of "stylecheck":
|
||||||
case p.val.normalize
|
case p.val.normalize
|
||||||
of "off": gStyleCheck = StyleCheck.None
|
of "off": gStyleCheck = StyleCheck.None
|
||||||
of "on": gStyleCheck = StyleCheck.Auto
|
of "on": gStyleCheck = StyleCheck.Warn
|
||||||
|
of "auto": gStyleCheck = StyleCheck.Auto
|
||||||
else: localError(gCmdLineInfo, errOnOrOffExpected)
|
else: localError(gCmdLineInfo, errOnOrOffExpected)
|
||||||
|
of "onlymainfile": gOnlyMainfile = true
|
||||||
else:
|
else:
|
||||||
processSwitch(pass, p)
|
processSwitch(pass, p)
|
||||||
of cmdArgument:
|
of cmdArgument:
|
||||||
|
|
|
||||||
|
|
@ -15,17 +15,18 @@ import
|
||||||
intsets, strtabs, semdata, prettybase
|
intsets, strtabs, semdata, prettybase
|
||||||
|
|
||||||
type
|
type
|
||||||
StyleCheck* {.pure.} = enum None, Confirm, Auto
|
StyleCheck* {.pure.} = enum None, Warn, Auto
|
||||||
|
|
||||||
var
|
var
|
||||||
gOverWrite* = true
|
gOverWrite* = true
|
||||||
gStyleCheck*: StyleCheck
|
gStyleCheck*: StyleCheck
|
||||||
gCheckExtern*: bool
|
gCheckExtern*, gOnlyMainfile*: bool
|
||||||
|
|
||||||
proc overwriteFiles*() =
|
proc overwriteFiles*() =
|
||||||
let doStrip = options.getConfigVar("pretty.strip").normalize == "on"
|
let doStrip = options.getConfigVar("pretty.strip").normalize == "on"
|
||||||
for i in 0 .. high(gSourceFiles):
|
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
|
let newFile = if gOverWrite: gSourceFiles[i].fullpath
|
||||||
else: gSourceFiles[i].fullpath.changeFileExt(".pretty.nim")
|
else: gSourceFiles[i].fullpath.changeFileExt(".pretty.nim")
|
||||||
try:
|
try:
|
||||||
|
|
@ -89,17 +90,39 @@ proc beautifyName(s: string, k: TSymKind): string =
|
||||||
result.add s[i]
|
result.add s[i]
|
||||||
inc 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)
|
let beau = beautifyName(s, k)
|
||||||
if s != beau:
|
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) =
|
proc styleCheckDefImpl(info: TLineInfo; s: PSym; k: TSymKind) =
|
||||||
# operators stay as they are:
|
# operators stay as they are:
|
||||||
if k in {skResult, skTemp} or s.name.s[0] notin prettybase.Letters: return
|
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 k in {skType, skGenericParam} and sfAnon in s.flags: return
|
||||||
if {sfImportc, sfExportc} * s.flags == {} or gCheckExtern:
|
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) =
|
template styleCheckDef*(info: TLineInfo; s: PSym; k: TSymKind) =
|
||||||
when defined(nimfix):
|
when defined(nimfix):
|
||||||
|
|
@ -121,23 +144,8 @@ proc styleCheckUseImpl(info: TLineInfo; s: PSym) =
|
||||||
if s.kind in {skType, skGenericParam} and sfAnon in s.flags: return
|
if s.kind in {skType, skGenericParam} and sfAnon in s.flags: return
|
||||||
let newName = s.name.s
|
let newName = s.name.s
|
||||||
|
|
||||||
loadFile(info)
|
replaceInFile(info, newName)
|
||||||
|
#if newName == "File": writeStackTrace()
|
||||||
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()
|
|
||||||
|
|
||||||
template styleCheckUse*(info: TLineInfo; s: PSym) =
|
template styleCheckUse*(info: TLineInfo; s: PSym) =
|
||||||
when defined(nimfix):
|
when defined(nimfix):
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ type
|
||||||
lines*: seq[string]
|
lines*: seq[string]
|
||||||
dirty*, isNimfixFile*: bool
|
dirty*, isNimfixFile*: bool
|
||||||
fullpath*, newline*: string
|
fullpath*, newline*: string
|
||||||
|
fileIdx*: int32
|
||||||
|
|
||||||
var
|
var
|
||||||
gSourceFiles*: seq[TSourceFile] = @[]
|
gSourceFiles*: seq[TSourceFile] = @[]
|
||||||
|
|
@ -24,6 +25,7 @@ proc loadFile*(info: TLineInfo) =
|
||||||
if i >= gSourceFiles.len:
|
if i >= gSourceFiles.len:
|
||||||
gSourceFiles.setLen(i+1)
|
gSourceFiles.setLen(i+1)
|
||||||
if gSourceFiles[i].lines.isNil:
|
if gSourceFiles[i].lines.isNil:
|
||||||
|
gSourceFiles[i].fileIdx = info.fileIndex
|
||||||
gSourceFiles[i].lines = @[]
|
gSourceFiles[i].lines = @[]
|
||||||
let path = info.toFullPath
|
let path = info.toFullPath
|
||||||
gSourceFiles[i].fullpath = path
|
gSourceFiles[i].fullpath = path
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue