diff options
-rw-r--r-- | compiler/lexer.nim | 4 | ||||
-rw-r--r-- | compiler/msgs.nim | 18 | ||||
-rw-r--r-- | compiler/parser.nim | 8 | ||||
-rw-r--r-- | compiler/renderer.nim | 17 | ||||
-rw-r--r-- | tools/nimpretty.nim | 4 |
5 files changed, 34 insertions, 17 deletions
diff --git a/compiler/lexer.nim b/compiler/lexer.nim index 1d9279d02..a4a2615bd 100644 --- a/compiler/lexer.nim +++ b/compiler/lexer.nim @@ -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) diff --git a/compiler/msgs.nim b/compiler/msgs.nim index 70504cfc9..818ab0c05 100644 --- a/compiler/msgs.nim +++ b/compiler/msgs.nim @@ -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 diff --git a/compiler/parser.nim b/compiler/parser.nim index 0019b7acb..3bf75c6f7 100644 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -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) diff --git a/compiler/renderer.nim b/compiler/renderer.nim index 7d513afb1..0e631a898 100644 --- a/compiler/renderer.nim +++ b/compiler/renderer.nim @@ -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) diff --git a/tools/nimpretty.nim b/tools/nimpretty.nim index 36d1382cf..396f17b0b 100644 --- a/tools/nimpretty.nim +++ b/tools/nimpretty.nim @@ -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 |