fixes a regression
This commit is contained in:
parent
fe187719ab
commit
0ac74ddd74
2 changed files with 19 additions and 10 deletions
|
|
@ -62,6 +62,7 @@ type
|
||||||
tkComma, tkSemiColon,
|
tkComma, tkSemiColon,
|
||||||
tkColon, tkColonColon, tkEquals, tkDot, tkDotDot, tkBracketLeColon,
|
tkColon, tkColonColon, tkEquals, tkDot, tkDotDot, tkBracketLeColon,
|
||||||
tkOpr, tkComment, tkAccent,
|
tkOpr, tkComment, tkAccent,
|
||||||
|
# these are fake tokens used by renderer.nim
|
||||||
tkSpaces, tkInfixOpr, tkPrefixOpr, tkPostfixOpr
|
tkSpaces, tkInfixOpr, tkPrefixOpr, tkPostfixOpr
|
||||||
|
|
||||||
TokTypes* = set[TokType]
|
TokTypes* = set[TokType]
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,7 @@ type
|
||||||
TSrcGen* = object
|
TSrcGen* = object
|
||||||
indent*: int
|
indent*: int
|
||||||
lineLen*: int
|
lineLen*: int
|
||||||
|
col: int
|
||||||
pos*: int # current position for iteration over the buffer
|
pos*: int # current position for iteration over the buffer
|
||||||
idx*: int # current token index for iteration over the buffer
|
idx*: int # current token index for iteration over the buffer
|
||||||
tokens*: TRenderTokSeq
|
tokens*: TRenderTokSeq
|
||||||
|
|
@ -119,11 +120,10 @@ proc initSrcGen(g: var TSrcGen, renderFlags: TRenderFlags; config: ConfigRef) =
|
||||||
g.config = config
|
g.config = config
|
||||||
|
|
||||||
proc addTok(g: var TSrcGen, kind: TokType, s: string; sym: PSym = nil) =
|
proc addTok(g: var TSrcGen, kind: TokType, s: string; sym: PSym = nil) =
|
||||||
setLen(g.tokens, g.tokens.len + 1)
|
g.tokens.add TRenderTok(kind: kind, length: int16(s.len), sym: sym)
|
||||||
g.tokens[^1].kind = kind
|
|
||||||
g.tokens[^1].length = int16(s.len)
|
|
||||||
g.tokens[^1].sym = sym
|
|
||||||
g.buf.add(s)
|
g.buf.add(s)
|
||||||
|
if kind != tkSpaces:
|
||||||
|
inc g.col, s.len
|
||||||
|
|
||||||
proc addPendingNL(g: var TSrcGen) =
|
proc addPendingNL(g: var TSrcGen) =
|
||||||
if g.pendingNL >= 0:
|
if g.pendingNL >= 0:
|
||||||
|
|
@ -133,6 +133,7 @@ proc addPendingNL(g: var TSrcGen) =
|
||||||
const newlines = "\n"
|
const newlines = "\n"
|
||||||
addTok(g, tkSpaces, newlines & spaces(g.pendingNL))
|
addTok(g, tkSpaces, newlines & spaces(g.pendingNL))
|
||||||
g.lineLen = g.pendingNL
|
g.lineLen = g.pendingNL
|
||||||
|
g.col = g.pendingNL
|
||||||
g.pendingNL = - 1
|
g.pendingNL = - 1
|
||||||
g.pendingWhitespace = -1
|
g.pendingWhitespace = -1
|
||||||
elif g.pendingWhitespace >= 0:
|
elif g.pendingWhitespace >= 0:
|
||||||
|
|
@ -141,7 +142,10 @@ proc addPendingNL(g: var TSrcGen) =
|
||||||
|
|
||||||
proc putNL(g: var TSrcGen, indent: int) =
|
proc putNL(g: var TSrcGen, indent: int) =
|
||||||
if g.pendingNL >= 0: addPendingNL(g)
|
if g.pendingNL >= 0: addPendingNL(g)
|
||||||
else: addTok(g, tkSpaces, "\n")
|
else:
|
||||||
|
addTok(g, tkSpaces, "\n")
|
||||||
|
g.col = 0
|
||||||
|
|
||||||
g.pendingNL = indent
|
g.pendingNL = indent
|
||||||
g.lineLen = indent
|
g.lineLen = indent
|
||||||
g.pendingWhitespace = -1
|
g.pendingWhitespace = -1
|
||||||
|
|
@ -185,14 +189,15 @@ proc put(g: var TSrcGen, kind: TokType, s: string; sym: PSym = nil) =
|
||||||
addTok(g, kind, s, sym)
|
addTok(g, kind, s, sym)
|
||||||
else:
|
else:
|
||||||
g.pendingWhitespace = s.len
|
g.pendingWhitespace = s.len
|
||||||
|
inc g.col, s.len
|
||||||
inc(g.lineLen, s.len)
|
inc(g.lineLen, s.len)
|
||||||
|
|
||||||
proc putComment(g: var TSrcGen, s: string) =
|
proc putComment(g: var TSrcGen, s: string) =
|
||||||
if s.len == 0: return
|
if s.len == 0: return
|
||||||
var i = 0
|
var i = 0
|
||||||
let hi = s.len - 1
|
let hi = s.len - 1
|
||||||
var isCode = (s.len >= 2) and (s[1] != ' ')
|
let isCode = (s.len >= 2) and (s[1] != ' ')
|
||||||
var ind = g.lineLen
|
let ind = g.col
|
||||||
var com = "## "
|
var com = "## "
|
||||||
while i <= hi:
|
while i <= hi:
|
||||||
case s[i]
|
case s[i]
|
||||||
|
|
@ -218,7 +223,7 @@ proc putComment(g: var TSrcGen, s: string) =
|
||||||
# compute length of the following word:
|
# compute length of the following word:
|
||||||
var j = i
|
var j = i
|
||||||
while j <= hi and s[j] > ' ': inc(j)
|
while j <= hi and s[j] > ' ': inc(j)
|
||||||
if not isCode and (g.lineLen + (j - i) > MaxLineLen):
|
if not isCode and (g.col + (j - i) > MaxLineLen):
|
||||||
put(g, tkComment, com)
|
put(g, tkComment, com)
|
||||||
optNL(g, ind)
|
optNL(g, ind)
|
||||||
com = "## "
|
com = "## "
|
||||||
|
|
@ -300,15 +305,18 @@ proc shouldRenderComment(g: var TSrcGen, n: PNode): bool =
|
||||||
proc gcom(g: var TSrcGen, n: PNode) =
|
proc gcom(g: var TSrcGen, n: PNode) =
|
||||||
assert(n != nil)
|
assert(n != nil)
|
||||||
if shouldRenderComment(g, n):
|
if shouldRenderComment(g, n):
|
||||||
|
var oneSpaceAdded = 0
|
||||||
if (g.pendingNL < 0) and (g.buf.len > 0) and (g.buf[^1] != ' '):
|
if (g.pendingNL < 0) and (g.buf.len > 0) and (g.buf[^1] != ' '):
|
||||||
put(g, tkSpaces, Space)
|
put(g, tkSpaces, Space)
|
||||||
|
oneSpaceAdded = 1
|
||||||
# Before long comments we cannot make sure that a newline is generated,
|
# Before long comments we cannot make sure that a newline is generated,
|
||||||
# because this might be wrong. But it is no problem in practice.
|
# because this might be wrong. But it is no problem in practice.
|
||||||
if (g.pendingNL < 0) and (g.buf.len > 0) and
|
if (g.pendingNL < 0) and (g.buf.len > 0) and
|
||||||
(g.lineLen < LineCommentColumn):
|
(g.col < LineCommentColumn):
|
||||||
var ml = maxLineLength(n.comment)
|
var ml = maxLineLength(n.comment)
|
||||||
if ml + LineCommentColumn <= MaxLineLen:
|
if ml + LineCommentColumn <= MaxLineLen:
|
||||||
put(g, tkSpaces, spaces(LineCommentColumn - g.lineLen))
|
put(g, tkSpaces, spaces(LineCommentColumn - g.col))
|
||||||
|
dec g.col, oneSpaceAdded
|
||||||
putComment(g, n.comment) #assert(g.comStack[high(g.comStack)] = n);
|
putComment(g, n.comment) #assert(g.comStack[high(g.comStack)] = n);
|
||||||
|
|
||||||
proc gcoms(g: var TSrcGen) =
|
proc gcoms(g: var TSrcGen) =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue