nimpretty improvements

This commit is contained in:
Andreas Rumpf 2018-06-19 09:42:33 +02:00
commit dd81d9d5b7
3 changed files with 60 additions and 43 deletions

View file

@ -7,10 +7,7 @@
# distribution, for details about the copyright. # distribution, for details about the copyright.
# #
## Layouter for nimpretty. Still primitive but useful. ## Layouter for nimpretty.
## TODO
## - Make indentations consistent.
## - Align 'if' and 'case' expressions properly.
import idents, lexer, lineinfos, llstream, options, msgs, strutils import idents, lexer, lineinfos, llstream, options, msgs, strutils
from os import changeFileExt from os import changeFileExt
@ -30,14 +27,20 @@ type
lastTok: TTokType lastTok: TTokType
inquote: bool inquote: bool
col, lastLineNumber, lineSpan, indentLevel, indWidth: int col, lastLineNumber, lineSpan, indentLevel, indWidth: int
lastIndent: int nested: int
doIndentMore*: int doIndentMore*: int
content: string content: string
indentStack: seq[int]
fixedUntil: int # marks where we must not go in the content fixedUntil: int # marks where we must not go in the content
altSplitPos: array[SplitKind, int] # alternative split positions altSplitPos: array[SplitKind, int] # alternative split positions
proc openEmitter*(em: var Emitter, config: ConfigRef, fileIdx: FileIndex) = proc openEmitter*(em: var Emitter, cache: IdentCache;
let outfile = changeFileExt(config.toFullPath(fileIdx), ".pretty.nim") 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.f = llStreamOpen(outfile, fmWrite)
em.config = config em.config = config
em.fid = fileIdx em.fid = fileIdx
@ -45,6 +48,8 @@ proc openEmitter*(em: var Emitter, config: ConfigRef, fileIdx: FileIndex) =
em.inquote = false em.inquote = false
em.col = 0 em.col = 0
em.content = newStringOfCap(16_000) em.content = newStringOfCap(16_000)
em.indentStack = newSeqOfCap[int](30)
em.indentStack.add 0
if em.f == nil: if em.f == nil:
rawMessage(config, errGenerated, "cannot open file: " & outfile) rawMessage(config, errGenerated, "cannot open file: " & outfile)
@ -74,14 +79,15 @@ const
splitters = {tkComma, tkSemicolon, tkParLe, tkParDotLe, splitters = {tkComma, tkSemicolon, tkParLe, tkParDotLe,
tkBracketLe, tkBracketLeColon, tkCurlyDotLe, tkBracketLe, tkBracketLeColon, tkCurlyDotLe,
tkCurlyLe} 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) = template rememberSplit(kind) =
if goodCol(em.col): if goodCol(em.col):
em.altSplitPos[kind] = em.content.len em.altSplitPos[kind] = em.content.len
template moreIndent(em): int = 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) = proc softLinebreak(em: var Emitter, lit: string) =
# XXX Use an algorithm that is outlined here: # 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: # search backwards for a good split position:
for a in em.altSplitPos: for a in em.altSplitPos:
if a > em.fixedUntil: 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.col = em.content.len - a
em.content.insert(ws, a) em.content.insert(ws, a)
break break
@ -119,10 +126,6 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) =
wr lit wr lit
var preventComment = false 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: 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: # we have an inline comment so handle it before the indentation token:
emitComment(em, tok) emitComment(em, tok)
@ -130,14 +133,17 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) =
em.fixedUntil = em.content.high em.fixedUntil = em.content.high
elif tok.indent >= 0: elif tok.indent >= 0:
em.indentLevel = tok.indent if em.lastTok in (splitters + oprSet):
# remove trailing whitespace: em.indentLevel = tok.indent
while em.content.len > 0 and em.content[em.content.high] == ' ': else:
setLen(em.content, em.content.len-1) if tok.indent > em.indentStack[^1]:
wr("\L") em.indentStack.add tok.indent
for i in 2..tok.line - em.lastLineNumber: wr("\L") else:
em.col = 0 # dedent?
#[ we only correct the indentation if it is slightly off, 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 so that code like
const splitters = {tkComma, tkSemicolon, tkParLe, tkParDotLe, const splitters = {tkComma, tkSemicolon, tkParLe, tkParDotLe,
@ -146,20 +152,15 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) =
is not touched. is not touched.
]# ]#
when false: # remove trailing whitespace:
if tok.indent > em.lastIndent and em.indWidth > 1 and (tok.indent mod em.indWidth) == 1: while em.content.len > 0 and em.content[em.content.high] == ' ':
em.indentLevel = 0 setLen(em.content, em.content.len-1)
while em.indentLevel < tok.indent-1: wr("\L")
inc em.indentLevel, em.indWidth for i in 2..tok.line - em.lastLineNumber: wr("\L")
when false: em.col = 0
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
for i in 1..em.indentLevel: for i in 1..em.indentLevel:
wr(" ") wr(" ")
em.fixedUntil = em.content.high em.fixedUntil = em.content.high
em.lastIndent = tok.indent
case tok.tokType case tok.tokType
of tokKeywordLow..tokKeywordHigh: of tokKeywordLow..tokKeywordHigh:
@ -168,15 +169,19 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) =
elif not em.inquote and not endsInWhite(em): elif not em.inquote and not endsInWhite(em):
wr(" ") wr(" ")
wr(TokTypeToStr[tok.tokType]) if not em.inquote:
wr(TokTypeToStr[tok.tokType])
case tok.tokType case tok.tokType
of tkAnd: rememberSplit(splitAnd) of tkAnd: rememberSplit(splitAnd)
of tkOr: rememberSplit(splitOr) of tkOr: rememberSplit(splitOr)
of tkIn, tkNotin: of tkIn, tkNotin:
rememberSplit(splitIn) rememberSplit(splitIn)
wr(" ") wr(" ")
else: discard else: discard
else:
# keywords in backticks are not normalized:
wr(tok.ident.s)
of tkColon: of tkColon:
wr(TokTypeToStr[tok.tokType]) wr(TokTypeToStr[tok.tokType])

View file

@ -867,7 +867,7 @@ proc getOperator(L: var TLexer, tok: var TToken) =
if buf[pos] in {CR, LF, nimlexbase.EndOfFile}: if buf[pos] in {CR, LF, nimlexbase.EndOfFile}:
tok.strongSpaceB = -1 tok.strongSpaceB = -1
proc newlineFollows*(L: var TLexer): bool = proc newlineFollows*(L: TLexer): bool =
var pos = L.bufpos var pos = L.bufpos
var buf = L.buf var buf = L.buf
while true: while true:
@ -1220,3 +1220,15 @@ proc rawGetTok*(L: var TLexer, tok: var TToken) =
lexMessage(L, errGenerated, "invalid token: " & c & " (\\" & $(ord(c)) & ')') lexMessage(L, errGenerated, "invalid token: " & c & " (\\" & $(ord(c)) & ')')
inc(L.bufpos) inc(L.bufpos)
atTokenEnd() 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)

View file

@ -102,7 +102,7 @@ proc openParser*(p: var TParser, fileIdx: FileIndex, inputStream: PLLStream,
initToken(p.tok) initToken(p.tok)
openLexer(p.lex, fileIdx, inputStream, cache, config) openLexer(p.lex, fileIdx, inputStream, cache, config)
when defined(nimpretty2): when defined(nimpretty2):
openEmitter(p.em, config, fileIdx) openEmitter(p.em, cache, config, fileIdx)
getTok(p) # read the first token getTok(p) # read the first token
p.firstTok = true p.firstTok = true
p.strongSpaces = strongSpaces p.strongSpaces = strongSpaces