nimpretty: next steps

This commit is contained in:
Araq
2018-04-17 11:09:23 +02:00
parent a230307b48
commit 9bc963508f
5 changed files with 34 additions and 17 deletions

View File

@@ -1080,7 +1080,9 @@ proc rawGetTok*(L: var TLexer, tok: var TToken) =
tok.indent = -1
skip(L, tok)
when defined(nimpretty):
if tok.tokType == tkComment: return
if tok.tokType == tkComment:
L.indentAhead = L.currLineIndent
return
var c = L.buf[L.bufpos]
tok.line = L.lineNumber
tok.col = getColNumber(L, L.bufpos)

View File

@@ -495,7 +495,9 @@ type
# and parsed; usually 'nil' but is used
# for 'nimsuggest'
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,
# because it is used
# in syntax nodes. We save space here by using
@@ -503,7 +505,7 @@ type
# On 64 bit and on 32 bit systems this is
# only 8 bytes.
line*, col*: int16
fileIndex*: int32
fileIndex*: FileIndex
when defined(nimpretty):
offsetA*, offsetB*: int
commentOffsetA*, commentOffsetB*: int
@@ -583,6 +585,18 @@ proc newFileInfo(fullPath, projPath: string): TFileInfo =
result.quotedFullName = fullPath.makeCString
if optEmbedOrigSrc in gGlobalOptions or true:
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 =
var

View File

@@ -125,7 +125,13 @@ proc rawSkipComment(p: var TParser, node: PNode) =
if p.tok.tokType == tkComment:
if node != nil:
if node.comment == nil: node.comment = ""
add(node.comment, p.tok.literal)
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)
else:
parMessage(p, errInternal, "skipComment")
getTok(p)

View File

@@ -39,8 +39,7 @@ type
inPragma: int
when defined(nimpretty):
pendingNewlineCount: int
origContent: string
fid*: FileIndex
# We render the source code in a two phases: The first
# 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 =
when defined(nimpretty):
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:
""
if n.info.offsetA <= n.info.offsetB:
# for some constructed tokens this can not be the case and we're better
# 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
case n.kind
of nkEmpty: result = ""
@@ -1460,17 +1459,13 @@ proc renderTree*(n: PNode, renderFlags: TRenderFlags = {}): string =
proc `$`*(n: PNode): string = n.renderTree
proc renderModule*(n: PNode, infile, outfile: string,
renderFlags: TRenderFlags = {}) =
renderFlags: TRenderFlags = {};
fid = FileIndex(-1)) =
var
f: File
g: TSrcGen
initSrcGen(g, renderFlags)
when defined(nimpretty):
try:
g.origContent = readFile(infile)
except IOError:
rawMessage(errCannotOpenFile, infile)
g.fid = fid
for i in countup(0, sonsLen(n) - 1):
gsub(g, n.sons[i])
optNL(g)

View File

@@ -24,7 +24,7 @@ const
Usage:
nimpretty [options] file.nim
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
--help show this help
"""
@@ -43,7 +43,7 @@ proc prettyPrint(infile: string) =
let fileIdx = fileInfoIdx(infile)
let tree = parseFile(fileIdx, newIdentCache())
let outfile = changeFileExt(infile, ".pretty.nim")
renderModule(tree, infile, outfile, {})
renderModule(tree, infile, outfile, {}, fileIdx)
proc main =
var infile: string