mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-08 14:03:23 +00:00
nimpretty: add #!nimpretty on/off directives
(cherry picked from commit f3c0703b7d)
This commit is contained in:
@@ -31,7 +31,7 @@ type
|
||||
inquote: bool
|
||||
semicolons: SemicolonKind
|
||||
col, lastLineNumber, lineSpan, indentLevel, indWidth: int
|
||||
inParamList*: int
|
||||
keepIndents*: int
|
||||
doIndentMore*: int
|
||||
content: string
|
||||
indentStack: seq[int]
|
||||
@@ -134,6 +134,22 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) =
|
||||
for i in 1 .. LineCommentColumn - em.col: wr(" ")
|
||||
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
|
||||
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:
|
||||
@@ -142,7 +158,7 @@ proc emitTok*(em: var Emitter; L: TLexer; tok: TToken) =
|
||||
em.fixedUntil = em.content.high
|
||||
|
||||
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
|
||||
else:
|
||||
if tok.indent > em.indentStack[^1]:
|
||||
|
||||
@@ -1114,7 +1114,9 @@ proc skip(L: var TLexer, tok: var TToken) =
|
||||
buf = L.buf
|
||||
else:
|
||||
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)
|
||||
when defined(nimpretty):
|
||||
tok.commentOffsetB = L.offsetBase + pos + 1
|
||||
|
||||
@@ -1037,7 +1037,7 @@ proc parseParamList(p: var TParser, retColon = true): PNode =
|
||||
addSon(result, p.emptyNode) # return type
|
||||
when defined(nimpretty2):
|
||||
inc p.em.doIndentMore
|
||||
inc p.em.inParamList
|
||||
inc p.em.keepIndents
|
||||
let hasParLe = p.tok.tokType == tkParLe and p.tok.indent < 0
|
||||
if hasParLe:
|
||||
getTok(p)
|
||||
@@ -1074,7 +1074,7 @@ proc parseParamList(p: var TParser, retColon = true): PNode =
|
||||
result = p.emptyNode
|
||||
when defined(nimpretty2):
|
||||
dec p.em.doIndentMore
|
||||
dec p.em.inParamList
|
||||
dec p.em.keepIndents
|
||||
|
||||
proc optPragmas(p: var TParser): PNode =
|
||||
if p.tok.tokType == tkCurlyDotLe and (p.tok.indent < 0 or realInd(p)):
|
||||
|
||||
@@ -315,9 +315,17 @@ proc f() =
|
||||
# escape char
|
||||
str &= c
|
||||
|
||||
const test = r"C:\Users\-\Desktop\test.txt"
|
||||
|
||||
proc getKeyAndData(cursor: int, op: int):
|
||||
tuple[key, data: string, success: bool] {.noInit.} =
|
||||
var keyVal: 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
|
||||
str &= c
|
||||
|
||||
const test = r"C:\Users\-\Desktop\test.txt"
|
||||
|
||||
proc getKeyAndData(cursor: int; op: int):
|
||||
tuple[key, data: string; success: bool] {.noInit.} =
|
||||
var keyVal: 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"
|
||||
|
||||
Reference in New Issue
Block a user