nimpretty: next steps
This commit is contained in:
parent
a230307b48
commit
9bc963508f
5 changed files with 34 additions and 17 deletions
|
|
@ -1080,7 +1080,9 @@ proc rawGetTok*(L: var TLexer, tok: var TToken) =
|
||||||
tok.indent = -1
|
tok.indent = -1
|
||||||
skip(L, tok)
|
skip(L, tok)
|
||||||
when defined(nimpretty):
|
when defined(nimpretty):
|
||||||
if tok.tokType == tkComment: return
|
if tok.tokType == tkComment:
|
||||||
|
L.indentAhead = L.currLineIndent
|
||||||
|
return
|
||||||
var c = L.buf[L.bufpos]
|
var c = L.buf[L.bufpos]
|
||||||
tok.line = L.lineNumber
|
tok.line = L.lineNumber
|
||||||
tok.col = getColNumber(L, L.bufpos)
|
tok.col = getColNumber(L, L.bufpos)
|
||||||
|
|
|
||||||
|
|
@ -495,7 +495,9 @@ type
|
||||||
# and parsed; usually 'nil' but is used
|
# and parsed; usually 'nil' but is used
|
||||||
# for 'nimsuggest'
|
# for 'nimsuggest'
|
||||||
hash*: string # the checksum of the file
|
hash*: string # the checksum of the file
|
||||||
|
when defined(nimpretty):
|
||||||
|
fullContent*: string
|
||||||
|
FileIndex* = int32 # XXX will make this 'distinct' later
|
||||||
TLineInfo* = object # This is designed to be as small as possible,
|
TLineInfo* = object # This is designed to be as small as possible,
|
||||||
# because it is used
|
# because it is used
|
||||||
# in syntax nodes. We save space here by using
|
# in syntax nodes. We save space here by using
|
||||||
|
|
@ -503,7 +505,7 @@ type
|
||||||
# On 64 bit and on 32 bit systems this is
|
# On 64 bit and on 32 bit systems this is
|
||||||
# only 8 bytes.
|
# only 8 bytes.
|
||||||
line*, col*: int16
|
line*, col*: int16
|
||||||
fileIndex*: int32
|
fileIndex*: FileIndex
|
||||||
when defined(nimpretty):
|
when defined(nimpretty):
|
||||||
offsetA*, offsetB*: int
|
offsetA*, offsetB*: int
|
||||||
commentOffsetA*, commentOffsetB*: int
|
commentOffsetA*, commentOffsetB*: int
|
||||||
|
|
@ -583,6 +585,18 @@ proc newFileInfo(fullPath, projPath: string): TFileInfo =
|
||||||
result.quotedFullName = fullPath.makeCString
|
result.quotedFullName = fullPath.makeCString
|
||||||
if optEmbedOrigSrc in gGlobalOptions or true:
|
if optEmbedOrigSrc in gGlobalOptions or true:
|
||||||
result.lines = @[]
|
result.lines = @[]
|
||||||
|
when defined(nimpretty):
|
||||||
|
if result.fullPath.len > 0:
|
||||||
|
try:
|
||||||
|
result.fullContent = readFile(result.fullPath)
|
||||||
|
except IOError:
|
||||||
|
#rawMessage(errCannotOpenFile, result.fullPath)
|
||||||
|
# XXX fixme
|
||||||
|
result.fullContent = ""
|
||||||
|
|
||||||
|
when defined(nimpretty):
|
||||||
|
proc fileSection*(fid: FileIndex; a, b: int): string =
|
||||||
|
substr(fileInfos[fid].fullContent, a, b)
|
||||||
|
|
||||||
proc fileInfoKnown*(filename: string): bool =
|
proc fileInfoKnown*(filename: string): bool =
|
||||||
var
|
var
|
||||||
|
|
|
||||||
|
|
@ -125,6 +125,12 @@ proc rawSkipComment(p: var TParser, node: PNode) =
|
||||||
if p.tok.tokType == tkComment:
|
if p.tok.tokType == tkComment:
|
||||||
if node != nil:
|
if node != nil:
|
||||||
if node.comment == nil: node.comment = ""
|
if node.comment == nil: node.comment = ""
|
||||||
|
when defined(nimpretty):
|
||||||
|
if p.tok.commentOffsetB > p.tok.commentOffsetA:
|
||||||
|
add node.comment, fileSection(p.lex.fileIdx, p.tok.commentOffsetA, p.tok.commentOffsetB)
|
||||||
|
else:
|
||||||
|
add node.comment, p.tok.literal
|
||||||
|
else:
|
||||||
add(node.comment, p.tok.literal)
|
add(node.comment, p.tok.literal)
|
||||||
else:
|
else:
|
||||||
parMessage(p, errInternal, "skipComment")
|
parMessage(p, errInternal, "skipComment")
|
||||||
|
|
|
||||||
|
|
@ -39,8 +39,7 @@ type
|
||||||
inPragma: int
|
inPragma: int
|
||||||
when defined(nimpretty):
|
when defined(nimpretty):
|
||||||
pendingNewlineCount: int
|
pendingNewlineCount: int
|
||||||
origContent: string
|
fid*: FileIndex
|
||||||
|
|
||||||
|
|
||||||
# We render the source code in a two phases: The first
|
# We render the source code in a two phases: The first
|
||||||
# determines how long the subtree will likely be, the second
|
# determines how long the subtree will likely be, the second
|
||||||
|
|
@ -354,13 +353,13 @@ proc ulitAux(g: TSrcGen; n: PNode, x: BiggestInt, size: int): string =
|
||||||
proc atom(g: TSrcGen; n: PNode): string =
|
proc atom(g: TSrcGen; n: PNode): string =
|
||||||
when defined(nimpretty):
|
when defined(nimpretty):
|
||||||
let comment = if n.info.commentOffsetA < n.info.commentOffsetB:
|
let comment = if n.info.commentOffsetA < n.info.commentOffsetB:
|
||||||
" " & substr(g.origContent, n.info.commentOffsetA, n.info.commentOffsetB)
|
" " & fileSection(g.fid, n.info.commentOffsetA, n.info.commentOffsetB)
|
||||||
else:
|
else:
|
||||||
""
|
""
|
||||||
if n.info.offsetA <= n.info.offsetB:
|
if n.info.offsetA <= n.info.offsetB:
|
||||||
# for some constructed tokens this can not be the case and we're better
|
# for some constructed tokens this can not be the case and we're better
|
||||||
# off to not mess with the offset then.
|
# off to not mess with the offset then.
|
||||||
return substr(g.origContent, n.info.offsetA, n.info.offsetB) & comment
|
return fileSection(g.fid, n.info.offsetA, n.info.offsetB) & comment
|
||||||
var f: float32
|
var f: float32
|
||||||
case n.kind
|
case n.kind
|
||||||
of nkEmpty: result = ""
|
of nkEmpty: result = ""
|
||||||
|
|
@ -1460,17 +1459,13 @@ proc renderTree*(n: PNode, renderFlags: TRenderFlags = {}): string =
|
||||||
proc `$`*(n: PNode): string = n.renderTree
|
proc `$`*(n: PNode): string = n.renderTree
|
||||||
|
|
||||||
proc renderModule*(n: PNode, infile, outfile: string,
|
proc renderModule*(n: PNode, infile, outfile: string,
|
||||||
renderFlags: TRenderFlags = {}) =
|
renderFlags: TRenderFlags = {};
|
||||||
|
fid = FileIndex(-1)) =
|
||||||
var
|
var
|
||||||
f: File
|
f: File
|
||||||
g: TSrcGen
|
g: TSrcGen
|
||||||
initSrcGen(g, renderFlags)
|
initSrcGen(g, renderFlags)
|
||||||
when defined(nimpretty):
|
g.fid = fid
|
||||||
try:
|
|
||||||
g.origContent = readFile(infile)
|
|
||||||
except IOError:
|
|
||||||
rawMessage(errCannotOpenFile, infile)
|
|
||||||
|
|
||||||
for i in countup(0, sonsLen(n) - 1):
|
for i in countup(0, sonsLen(n) - 1):
|
||||||
gsub(g, n.sons[i])
|
gsub(g, n.sons[i])
|
||||||
optNL(g)
|
optNL(g)
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ const
|
||||||
Usage:
|
Usage:
|
||||||
nimpretty [options] file.nim
|
nimpretty [options] file.nim
|
||||||
Options:
|
Options:
|
||||||
--backup:ON|OFF create a backup file before overwritting (default: ON)
|
--backup:on|off create a backup file before overwritting (default: ON)
|
||||||
--version show the version
|
--version show the version
|
||||||
--help show this help
|
--help show this help
|
||||||
"""
|
"""
|
||||||
|
|
@ -43,7 +43,7 @@ proc prettyPrint(infile: string) =
|
||||||
let fileIdx = fileInfoIdx(infile)
|
let fileIdx = fileInfoIdx(infile)
|
||||||
let tree = parseFile(fileIdx, newIdentCache())
|
let tree = parseFile(fileIdx, newIdentCache())
|
||||||
let outfile = changeFileExt(infile, ".pretty.nim")
|
let outfile = changeFileExt(infile, ".pretty.nim")
|
||||||
renderModule(tree, infile, outfile, {})
|
renderModule(tree, infile, outfile, {}, fileIdx)
|
||||||
|
|
||||||
proc main =
|
proc main =
|
||||||
var infile: string
|
var infile: string
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue