mirror of
https://github.com/nim-lang/Nim.git
synced 2026-07-21 16:31:39 +00:00
nimpretty improvements
This commit is contained in:
@@ -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
|
||||
@@ -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])
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user