diff options
-rw-r--r-- | compiler/layouter.nim | 109 | ||||
-rw-r--r-- | compiler/lexer.nim | 14 | ||||
-rw-r--r-- | compiler/parser.nim | 2 |
3 files changed, 73 insertions, 52 deletions
diff --git a/compiler/layouter.nim b/compiler/layouter.nim index ec5d3d088..e07bde786 100644 --- a/compiler/layouter.nim +++ b/compiler/layouter.nim @@ -7,10 +7,7 @@ # distribution, for details about the copyright. # -## Layouter for nimpretty. Still primitive but useful. -## TODO -## - Make indentations consistent. -## - Align 'if' and 'case' expressions properly. +## Layouter for nimpretty. import idents, lexer, lineinfos, llstream, options, msgs, strutils from os import changeFileExt @@ -30,14 +27,20 @@ type lastTok: TTokType inquote: bool col, lastLineNumber, lineSpan, indentLevel, indWidth: int - lastIndent: int + nested: int doIndentMore*: int content: string + indentStack: seq[int] fixedUntil: int # marks where we must not go in the content altSplitPos: array[SplitKind, int] # alternative split positions -proc openEmitter*(em: var Emitter, config: ConfigRef, fileIdx: FileIndex) = - let outfile = changeFileExt(config.toFullPath(fileIdx), ".pretty.nim") +proc openEmitter*(em: var Emitter, cache: IdentCache; + config: ConfigRef, fileIdx: FileIndex) = + let fullPath = config.toFullPath(fileIdx) + em.indWidth = getIndentWidth(fileIdx, llStreamOpen(fullPath, fmRead), + cache, config) + if em.indWidth == 0: em.indWidth = 2 + let outfile = changeFileExt(fullPath, ".pretty.nim") em.f = llStreamOpen(outfile, fmWrite) em.config = config em.fid = fileIdx @@ -45,6 +48,8 @@ proc openEmitter*(em: var Emitter, config: ConfigRef, fileIdx: FileIndex) = em.inquote = false em.col = 0 em.content = newStringOfCap(16_000) + em.indentStack = newSeqOfCap[int](30) + em.indentStack.add 0 if em.f == nil: rawMessage(config, errGenerated, "cannot open file: " & outfile) @@ -74,14 +79,15 @@ const splitters = {tkComma, tkSemicolon, tkParLe, tkParDotLe, tkBracketLe, tkBracketLeColon, tkCurlyDotLe, tkCurlyLe} - sectionKeywords = {tkType, tkVar, tkConst, tkLet, tkUsing} + oprSet = {tkOpr, tkDiv, tkMod, tkShl, tkShr, tkIn, tkNotin, tkIs, + tkIsnot, tkNot, tkOf, tkAs, tkDotDot, tkAnd, tkOr, tkXor} template rememberSplit(kind) = if goodCol(em.col): em.altSplitPos[kind] = em.content.len template moreIndent(em): int = - max(if em.doIndentMore > 0: em.indWidth*2 else: em.indWidth, 2) + (if em.doIndentMore > 0: em.indWidth*2 else: em.indWidth) proc softLinebreak(em: var Emitter, lit: string) = # XXX Use an algorithm that is outlined here: @@ -96,7 +102,8 @@ proc softLinebreak(em: var Emitter, lit: string) = # search backwards for a good split position: for a in em.altSplitPos: if a > em.fixedUntil: - let ws = "\L" & repeat(' ',em.indentLevel+moreIndent(em)) + let ws = "\L" & repeat(' ',em.indentLevel+moreIndent(em) - + ord(em.content[a] == ' ')) em.col = em.content.len - a em.content.insert(ws, a) break @@ -104,7 +111,7 @@ proc softLinebreak(em: var Emitter, lit: string) = proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) = template endsInWhite(em): bool = - em.content.len > 0 and em.content[em.content.high] in {' ', '\L'} + em.content.len == 0 or em.content[em.content.high] in {' ', '\L'} template endsInAlpha(em): bool = em.content.len > 0 and em.content[em.content.high] in SymChars+{'_'} @@ -119,10 +126,6 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) = wr lit var preventComment = false - if em.indWidth == 0 and tok.indent > 0: - # first indentation determines how many number of spaces to use: - em.indWidth = tok.indent - if tok.tokType == tkComment and tok.line == em.lastLineNumber and tok.indent >= 0: # we have an inline comment so handle it before the indentation token: emitComment(em, tok) @@ -130,14 +133,17 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) = em.fixedUntil = em.content.high elif tok.indent >= 0: - em.indentLevel = tok.indent - # remove trailing whitespace: - while em.content.len > 0 and em.content[em.content.high] == ' ': - setLen(em.content, em.content.len-1) - wr("\L") - for i in 2..tok.line - em.lastLineNumber: wr("\L") - em.col = 0 - #[ we only correct the indentation if it is slightly off, + if em.lastTok in (splitters + oprSet): + em.indentLevel = tok.indent + else: + if tok.indent > em.indentStack[^1]: + em.indentStack.add tok.indent + else: + # dedent? + while em.indentStack.len > 1 and em.indentStack[^1] > tok.indent: + discard em.indentStack.pop() + em.indentLevel = em.indentStack.high * em.indWidth + #[ we only correct the indentation if it is not in an expression context, so that code like const splitters = {tkComma, tkSemicolon, tkParLe, tkParDotLe, @@ -146,20 +152,15 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) = is not touched. ]# - when false: - if tok.indent > em.lastIndent and em.indWidth > 1 and (tok.indent mod em.indWidth) == 1: - em.indentLevel = 0 - while em.indentLevel < tok.indent-1: - inc em.indentLevel, em.indWidth - when false: - if em.indWidth != 0 and - abs(tok.indent - em.nested*em.indWidth) <= em.indWidth and - em.lastTok notin sectionKeywords: - em.indentLevel = em.nested*em.indWidth + # remove trailing whitespace: + while em.content.len > 0 and em.content[em.content.high] == ' ': + setLen(em.content, em.content.len-1) + wr("\L") + for i in 2..tok.line - em.lastLineNumber: wr("\L") + em.col = 0 for i in 1..em.indentLevel: wr(" ") em.fixedUntil = em.content.high - em.lastIndent = tok.indent case tok.tokType of tokKeywordLow..tokKeywordHigh: @@ -168,15 +169,19 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) = elif not em.inquote and not endsInWhite(em): wr(" ") - wr(TokTypeToStr[tok.tokType]) + if not em.inquote: + wr(TokTypeToStr[tok.tokType]) - case tok.tokType - of tkAnd: rememberSplit(splitAnd) - of tkOr: rememberSplit(splitOr) - of tkIn, tkNotin: - rememberSplit(splitIn) - wr(" ") - else: discard + case tok.tokType + of tkAnd: rememberSplit(splitAnd) + of tkOr: rememberSplit(splitOr) + of tkIn, tkNotin: + rememberSplit(splitIn) + wr(" ") + else: discard + else: + # keywords in backticks are not normalized: + wr(tok.ident.s) of tkColon: wr(TokTypeToStr[tok.tokType]) @@ -203,14 +208,18 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) = wr(TokTypeToStr[tok.tokType]) wr(" ") of tkOpr, tkDotDot: - if not em.endsInWhite: wr(" ") - wr(tok.ident.s) - template isUnary(tok): bool = - tok.strongSpaceB == 0 and tok.strongSpaceA > 0 - - if not isUnary(tok) or em.lastTok in {tkOpr, tkDotDot}: - wr(" ") - rememberSplit(splitBinary) + if tok.strongSpaceA == 0 and tok.strongSpaceB == 0: + # if not surrounded by whitespace, don't produce any whitespace either: + wr(tok.ident.s) + else: + if not em.endsInWhite: wr(" ") + wr(tok.ident.s) + template isUnary(tok): bool = + tok.strongSpaceB == 0 and tok.strongSpaceA > 0 + + if not isUnary(tok) or em.lastTok in {tkOpr, tkDotDot}: + wr(" ") + rememberSplit(splitBinary) of tkAccent: wr(TokTypeToStr[tok.tokType]) em.inquote = not em.inquote diff --git a/compiler/lexer.nim b/compiler/lexer.nim index 13460f7c1..c5afa6e97 100644 --- a/compiler/lexer.nim +++ b/compiler/lexer.nim @@ -867,7 +867,7 @@ proc getOperator(L: var TLexer, tok: var TToken) = if buf[pos] in {CR, LF, nimlexbase.EndOfFile}: tok.strongSpaceB = -1 -proc newlineFollows*(L: var TLexer): bool = +proc newlineFollows*(L: TLexer): bool = var pos = L.bufpos var buf = L.buf while true: @@ -1220,3 +1220,15 @@ proc rawGetTok*(L: var TLexer, tok: var TToken) = lexMessage(L, errGenerated, "invalid token: " & c & " (\\" & $(ord(c)) & ')') inc(L.bufpos) atTokenEnd() + +proc getIndentWidth*(fileIdx: FileIndex, inputstream: PLLStream; + cache: IdentCache; config: ConfigRef): int = + var lex: TLexer + var tok: TToken + initToken(tok) + openLexer(lex, fileIdx, inputstream, cache, config) + while true: + rawGetTok(lex, tok) + result = tok.indent + if result > 0 or tok.tokType == tkEof: break + closeLexer(lex) diff --git a/compiler/parser.nim b/compiler/parser.nim index 649f9d506..f575f3d7e 100644 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -102,7 +102,7 @@ proc openParser*(p: var TParser, fileIdx: FileIndex, inputStream: PLLStream, initToken(p.tok) openLexer(p.lex, fileIdx, inputStream, cache, config) when defined(nimpretty2): - openEmitter(p.em, config, fileIdx) + openEmitter(p.em, cache, config, fileIdx) getTok(p) # read the first token p.firstTok = true p.strongSpaces = strongSpaces |