nimpretty: add #!nimpretty on/off directives
This commit is contained in:
parent
ed38079b9b
commit
f3c0703b7d
5 changed files with 43 additions and 9 deletions
|
|
@ -31,7 +31,7 @@ type
|
||||||
inquote: bool
|
inquote: bool
|
||||||
semicolons: SemicolonKind
|
semicolons: SemicolonKind
|
||||||
col, lastLineNumber, lineSpan, indentLevel, indWidth: int
|
col, lastLineNumber, lineSpan, indentLevel, indWidth: int
|
||||||
inParamList*: int
|
keepIndents*: int
|
||||||
doIndentMore*: int
|
doIndentMore*: int
|
||||||
content: string
|
content: string
|
||||||
indentStack: seq[int]
|
indentStack: seq[int]
|
||||||
|
|
@ -134,6 +134,22 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) =
|
||||||
for i in 1 .. LineCommentColumn - em.col: wr(" ")
|
for i in 1 .. LineCommentColumn - em.col: wr(" ")
|
||||||
wr lit
|
wr lit
|
||||||
|
|
||||||
|
if tok.tokType == tkComment and tok.literal.startsWith("#!nimpretty"):
|
||||||
|
case tok.literal
|
||||||
|
of "#!nimpretty off":
|
||||||
|
inc em.keepIndents
|
||||||
|
wr("\L")
|
||||||
|
em.lastLineNumber = tok.line + 1
|
||||||
|
of "#!nimpretty on":
|
||||||
|
dec em.keepIndents
|
||||||
|
em.lastLineNumber = tok.line
|
||||||
|
wr("\L")
|
||||||
|
#for i in 1 .. tok.indent: wr " "
|
||||||
|
wr tok.literal
|
||||||
|
em.col = 0
|
||||||
|
em.lineSpan = 0
|
||||||
|
return
|
||||||
|
|
||||||
var preventComment = false
|
var preventComment = false
|
||||||
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:
|
||||||
|
|
@ -142,7 +158,7 @@ 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:
|
||||||
if em.lastTok in (splitters + oprSet) or em.inParamList > 0:
|
if em.lastTok in (splitters + oprSet) or em.keepIndents > 0:
|
||||||
em.indentLevel = tok.indent
|
em.indentLevel = tok.indent
|
||||||
else:
|
else:
|
||||||
if tok.indent > em.indentStack[^1]:
|
if tok.indent > em.indentStack[^1]:
|
||||||
|
|
|
||||||
|
|
@ -1114,7 +1114,9 @@ proc skip(L: var TLexer, tok: var TToken) =
|
||||||
buf = L.buf
|
buf = L.buf
|
||||||
else:
|
else:
|
||||||
tokenBegin(tok, pos)
|
tokenBegin(tok, pos)
|
||||||
while buf[pos] notin {CR, LF, nimlexbase.EndOfFile}: inc(pos)
|
while buf[pos] notin {CR, LF, nimlexbase.EndOfFile}:
|
||||||
|
when defined(nimpretty): tok.literal.add buf[pos]
|
||||||
|
inc(pos)
|
||||||
tokenEndIgnore(tok, pos+1)
|
tokenEndIgnore(tok, pos+1)
|
||||||
when defined(nimpretty):
|
when defined(nimpretty):
|
||||||
tok.commentOffsetB = L.offsetBase + pos + 1
|
tok.commentOffsetB = L.offsetBase + pos + 1
|
||||||
|
|
|
||||||
|
|
@ -1037,7 +1037,7 @@ proc parseParamList(p: var TParser, retColon = true): PNode =
|
||||||
addSon(result, p.emptyNode) # return type
|
addSon(result, p.emptyNode) # return type
|
||||||
when defined(nimpretty2):
|
when defined(nimpretty2):
|
||||||
inc p.em.doIndentMore
|
inc p.em.doIndentMore
|
||||||
inc p.em.inParamList
|
inc p.em.keepIndents
|
||||||
let hasParLe = p.tok.tokType == tkParLe and p.tok.indent < 0
|
let hasParLe = p.tok.tokType == tkParLe and p.tok.indent < 0
|
||||||
if hasParLe:
|
if hasParLe:
|
||||||
getTok(p)
|
getTok(p)
|
||||||
|
|
@ -1074,7 +1074,7 @@ proc parseParamList(p: var TParser, retColon = true): PNode =
|
||||||
result = p.emptyNode
|
result = p.emptyNode
|
||||||
when defined(nimpretty2):
|
when defined(nimpretty2):
|
||||||
dec p.em.doIndentMore
|
dec p.em.doIndentMore
|
||||||
dec p.em.inParamList
|
dec p.em.keepIndents
|
||||||
|
|
||||||
proc optPragmas(p: var TParser): PNode =
|
proc optPragmas(p: var TParser): PNode =
|
||||||
if p.tok.tokType == tkCurlyDotLe and (p.tok.indent < 0 or realInd(p)):
|
if p.tok.tokType == tkCurlyDotLe and (p.tok.indent < 0 or realInd(p)):
|
||||||
|
|
|
||||||
|
|
@ -315,9 +315,17 @@ proc f() =
|
||||||
# escape char
|
# escape char
|
||||||
str &= c
|
str &= c
|
||||||
|
|
||||||
const test = r"C:\Users\-\Desktop\test.txt"
|
|
||||||
|
|
||||||
proc getKeyAndData(cursor: int, op: int):
|
proc getKeyAndData(cursor: int, op: int):
|
||||||
tuple[key, data: string, success: bool] {.noInit.} =
|
tuple[key, data: string, success: bool] {.noInit.} =
|
||||||
var keyVal: string
|
var keyVal: string
|
||||||
var dataVal: string
|
var dataVal: string
|
||||||
|
|
||||||
|
#!nimpretty off
|
||||||
|
when stuff:
|
||||||
|
echo "so nice"
|
||||||
|
echo "more"
|
||||||
|
else:
|
||||||
|
echo "misaligned"
|
||||||
|
#!nimpretty on
|
||||||
|
|
||||||
|
const test = r"C:\Users\-\Desktop\test.txt"
|
||||||
|
|
|
||||||
|
|
@ -324,9 +324,17 @@ proc f() =
|
||||||
# escape char
|
# escape char
|
||||||
str &= c
|
str &= c
|
||||||
|
|
||||||
const test = r"C:\Users\-\Desktop\test.txt"
|
|
||||||
|
|
||||||
proc getKeyAndData(cursor: int; op: int):
|
proc getKeyAndData(cursor: int; op: int):
|
||||||
tuple[key, data: string; success: bool] {.noInit.} =
|
tuple[key, data: string; success: bool] {.noInit.} =
|
||||||
var keyVal: string
|
var keyVal: string
|
||||||
var dataVal: string
|
var dataVal: string
|
||||||
|
|
||||||
|
#!nimpretty off
|
||||||
|
when stuff:
|
||||||
|
echo "so nice"
|
||||||
|
echo "more"
|
||||||
|
else:
|
||||||
|
echo "misaligned"
|
||||||
|
#!nimpretty on
|
||||||
|
|
||||||
|
const test = r"C:\Users\-\Desktop\test.txt"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue