nimpretty: relax line length rules when dealing with (inline) comments (#11657)
This commit is contained in:
parent
95f2f2396f
commit
96a19ea1b4
4 changed files with 36 additions and 10 deletions
|
|
@ -16,7 +16,7 @@ from sequtils import delete
|
||||||
|
|
||||||
const
|
const
|
||||||
MaxLineLen = 80
|
MaxLineLen = 80
|
||||||
MinLineLen = 10
|
MinLineLen = 15
|
||||||
|
|
||||||
type
|
type
|
||||||
SplitKind = enum
|
SplitKind = enum
|
||||||
|
|
@ -102,12 +102,14 @@ proc findNewline(em: Emitter; p, lineLen: var int) =
|
||||||
inc lineLen, em.tokens[p].len
|
inc lineLen, em.tokens[p].len
|
||||||
inc p
|
inc p
|
||||||
|
|
||||||
proc optionalIsGood(em: var Emitter; pos: int): bool =
|
proc optionalIsGood(em: var Emitter; pos, currentLen: int): bool =
|
||||||
let ourIndent = em.tokens[pos].len
|
let ourIndent = em.tokens[pos].len
|
||||||
var p = pos+1
|
var p = pos+1
|
||||||
var lineLen = 0
|
var lineLen = 0
|
||||||
em.findNewline(p, lineLen)
|
em.findNewline(p, lineLen)
|
||||||
if p+1 < em.tokens.len and em.kinds[p+1] == ltSpaces and
|
if em.kinds[p-1] == ltComment and currentLen+lineLen < MaxLineLen+MinLineLen:
|
||||||
|
result = false
|
||||||
|
elif p+1 < em.tokens.len and em.kinds[p+1] == ltSpaces and
|
||||||
em.kinds[p-1] == ltOptionalNewline:
|
em.kinds[p-1] == ltOptionalNewline:
|
||||||
if em.tokens[p+1].len == ourIndent:
|
if em.tokens[p+1].len == ourIndent:
|
||||||
# concatenate lines with the same indententation
|
# concatenate lines with the same indententation
|
||||||
|
|
@ -163,7 +165,7 @@ proc closeEmitter*(em: var Emitter) =
|
||||||
else:
|
else:
|
||||||
# pick the shorter indentation token:
|
# pick the shorter indentation token:
|
||||||
var spaces = maxLhs - lineLen
|
var spaces = maxLhs - lineLen
|
||||||
if spaces < em.tokens[i].len or computeRhs(em, i+1)+maxLhs <= MaxLineLen:
|
if spaces < em.tokens[i].len or computeRhs(em, i+1)+maxLhs <= MaxLineLen+MinLineLen:
|
||||||
if spaces <= 0 and content[^1] notin {' ', '\L'}: spaces = 1
|
if spaces <= 0 and content[^1] notin {' ', '\L'}: spaces = 1
|
||||||
for j in 1..spaces: content.add ' '
|
for j in 1..spaces: content.add ' '
|
||||||
inc lineLen, spaces
|
inc lineLen, spaces
|
||||||
|
|
@ -177,7 +179,7 @@ proc closeEmitter*(em: var Emitter) =
|
||||||
of ltOptionalNewline:
|
of ltOptionalNewline:
|
||||||
let totalLineLen = lineLen + lenOfNextTokens(em, i)
|
let totalLineLen = lineLen + lenOfNextTokens(em, i)
|
||||||
if totalLineLen > MaxLineLen + MinLineLen or
|
if totalLineLen > MaxLineLen + MinLineLen or
|
||||||
totalLineLen > MaxLineLen and optionalIsGood(em, i):
|
totalLineLen > MaxLineLen and optionalIsGood(em, i, lineLen):
|
||||||
if i-1 >= 0 and em.kinds[i-1] == ltSpaces:
|
if i-1 >= 0 and em.kinds[i-1] == ltSpaces:
|
||||||
let spaces = em.tokens[i-1].len
|
let spaces = em.tokens[i-1].len
|
||||||
content.setLen(content.len - spaces)
|
content.setLen(content.len - spaces)
|
||||||
|
|
|
||||||
|
|
@ -688,3 +688,15 @@ proc newRecordGen(ctx: Context; typ: TypRef): PNode =
|
||||||
nkRecList.t(
|
nkRecList.t(
|
||||||
typ.recFields.map(newRecFieldGen))))
|
typ.recFields.map(newRecFieldGen))))
|
||||||
|
|
||||||
|
|
||||||
|
let
|
||||||
|
lla = 42394219 - 42429849 + 1293293 - 13918391 + 424242 # this here is an okayish comment
|
||||||
|
llb = 42394219 - 42429849 + 1293293 - 13918391 + 424242 # this here is a very long comment which should be split
|
||||||
|
llc = 42394219 - 42429849 + 1293293 - 13918391 + 424242 - 3429424 + 4239489 - 42399
|
||||||
|
lld = 42394219 - 42429849 + 1293293 - 13918391 + 424242 - 342949924 + 423948999 - 42399
|
||||||
|
|
||||||
|
type
|
||||||
|
MyLongEnum = enum ## doc comment here
|
||||||
|
first, ## this is a long comment here, but please align it
|
||||||
|
secondWithAVeryLongNameMightBreak, ## this is a short one
|
||||||
|
thirdOne ## it's ok
|
||||||
|
|
|
||||||
|
|
@ -19,8 +19,7 @@ var body = newNimNode(nnkIfExpr).add(
|
||||||
ident("kind"))),
|
ident("kind"))),
|
||||||
condition
|
condition
|
||||||
),
|
),
|
||||||
newNimNode(nnkElse).add(newStmtList(newNimNode(nnkReturnStmt).add(ident(
|
newNimNode(nnkElse).add(newStmtList(newNimNode(nnkReturnStmt).add(ident("false"))))
|
||||||
"false"))))
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# comment
|
# comment
|
||||||
|
|
@ -122,7 +121,7 @@ type
|
||||||
inquote {.pragmaHereWrongCurlyEnd.}: bool
|
inquote {.pragmaHereWrongCurlyEnd.}: bool
|
||||||
col, lastLineNumber, lineSpan, indentLevel: int
|
col, lastLineNumber, lineSpan, indentLevel: int
|
||||||
content: string
|
content: string
|
||||||
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*[T, S](em: var Emitter; config: ConfigRef;
|
proc openEmitter*[T, S](em: var Emitter; config: ConfigRef;
|
||||||
|
|
@ -695,3 +694,17 @@ proc newRecordGen(ctx: Context; typ: TypRef): PNode =
|
||||||
nkRecList.t(
|
nkRecList.t(
|
||||||
typ.recFields.map(newRecFieldGen))))
|
typ.recFields.map(newRecFieldGen))))
|
||||||
|
|
||||||
|
|
||||||
|
let
|
||||||
|
lla = 42394219 - 42429849 + 1293293 - 13918391 + 424242 # this here is an okayish comment
|
||||||
|
llb = 42394219 - 42429849 + 1293293 - 13918391 +
|
||||||
|
424242 # this here is a very long comment which should be split
|
||||||
|
llc = 42394219 - 42429849 + 1293293 - 13918391 + 424242 - 3429424 + 4239489 - 42399
|
||||||
|
lld = 42394219 - 42429849 + 1293293 - 13918391 + 424242 - 342949924 +
|
||||||
|
423948999 - 42399
|
||||||
|
|
||||||
|
type
|
||||||
|
MyLongEnum = enum ## doc comment here
|
||||||
|
first, ## this is a long comment here, but please align it
|
||||||
|
secondWithAVeryLongNameMightBreak, ## this is a short one
|
||||||
|
thirdOne ## it's ok
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,7 @@ proc fun() =
|
||||||
echo "ok1"
|
echo "ok1"
|
||||||
|
|
||||||
proc fun2(a = "fooo" & "bar" & "bar" & "bar" & "bar" & ("bar" & "bar" & "bar") &
|
proc fun2(a = "fooo" & "bar" & "bar" & "bar" & "bar" & ("bar" & "bar" & "bar") &
|
||||||
"bar" & "bar" & "bar" & "bar" & "bar" & "bar" & "bar" & "bar" &
|
"bar" & "bar" & "bar" & "bar" & "bar" & "bar" & "bar" & "bar" & "bar"): auto =
|
||||||
"bar"): auto =
|
|
||||||
discard
|
discard
|
||||||
|
|
||||||
fun2()
|
fun2()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue