nimpretty: implement a --maxLineLen command line option
This commit is contained in:
parent
eaf4b42ff9
commit
5bf3734984
2 changed files with 12 additions and 7 deletions
|
|
@ -15,7 +15,6 @@ from os import changeFileExt
|
||||||
from sequtils import delete
|
from sequtils import delete
|
||||||
|
|
||||||
const
|
const
|
||||||
MaxLineLen = 80
|
|
||||||
MinLineLen = 15
|
MinLineLen = 15
|
||||||
|
|
||||||
type
|
type
|
||||||
|
|
@ -50,6 +49,7 @@ type
|
||||||
indentStack: seq[int]
|
indentStack: seq[int]
|
||||||
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
|
||||||
|
maxLineLen*: int
|
||||||
|
|
||||||
proc openEmitter*(em: var Emitter, cache: IdentCache;
|
proc openEmitter*(em: var Emitter, cache: IdentCache;
|
||||||
config: ConfigRef, fileIdx: FileIndex) =
|
config: ConfigRef, fileIdx: FileIndex) =
|
||||||
|
|
@ -77,7 +77,7 @@ proc computeMax(em: Emitter; pos: int): int =
|
||||||
var foundTab = false
|
var foundTab = false
|
||||||
while p < em.tokens.len and em.kinds[p] != ltEndSection:
|
while p < em.tokens.len and em.kinds[p] != ltEndSection:
|
||||||
if em.kinds[p] in {ltCrucialNewline, ltSplittingNewline}:
|
if em.kinds[p] in {ltCrucialNewline, ltSplittingNewline}:
|
||||||
if foundTab and lineLen <= MaxLineLen:
|
if foundTab and lineLen <= em.maxLineLen:
|
||||||
result = max(result, lhs + extraSpace)
|
result = max(result, lhs + extraSpace)
|
||||||
inc p
|
inc p
|
||||||
break
|
break
|
||||||
|
|
@ -124,7 +124,7 @@ proc optionalIsGood(em: var Emitter; pos, currentLen: int): bool =
|
||||||
em.findNewline(p, lineLen)
|
em.findNewline(p, lineLen)
|
||||||
if p == pos+1: # optionalNewline followed by another newline
|
if p == pos+1: # optionalNewline followed by another newline
|
||||||
result = false
|
result = false
|
||||||
elif em.kinds[p-1] == ltComment and currentLen+lineLen < MaxLineLen+MinLineLen:
|
elif em.kinds[p-1] == ltComment and currentLen+lineLen < em.maxLineLen+MinLineLen:
|
||||||
result = false
|
result = false
|
||||||
elif p+1 < em.tokens.len and em.kinds[p+1] == ltSpaces and
|
elif p+1 < em.tokens.len and em.kinds[p+1] == ltSpaces and
|
||||||
em.kinds[p-1] == ltOptionalNewline:
|
em.kinds[p-1] == ltOptionalNewline:
|
||||||
|
|
@ -183,7 +183,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+MinLineLen:
|
if spaces < em.tokens[i].len or computeRhs(em, i+1)+maxLhs <= em.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
|
||||||
|
|
@ -196,7 +196,7 @@ proc closeEmitter*(em: var Emitter) =
|
||||||
lineBegin = i+1
|
lineBegin = i+1
|
||||||
of ltOptionalNewline:
|
of ltOptionalNewline:
|
||||||
let totalLineLen = lineLen + lenOfNextTokens(em, i)
|
let totalLineLen = lineLen + lenOfNextTokens(em, i)
|
||||||
if totalLineLen > MaxLineLen and optionalIsGood(em, i, lineLen):
|
if totalLineLen > em.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)
|
||||||
|
|
@ -298,7 +298,7 @@ const
|
||||||
oprSet = {tkOpr, tkDiv, tkMod, tkShl, tkShr, tkIn, tkNotin, tkIs,
|
oprSet = {tkOpr, tkDiv, tkMod, tkShl, tkShr, tkIn, tkNotin, tkIs,
|
||||||
tkIsnot, tkNot, tkOf, tkAs, tkDotDot, tkAnd, tkOr, tkXor}
|
tkIsnot, tkNot, tkOf, tkAs, tkDotDot, tkAnd, tkOr, tkXor}
|
||||||
|
|
||||||
template goodCol(col): bool = col >= MaxLineLen div 2
|
template goodCol(col): bool = col >= em.maxLineLen div 2
|
||||||
|
|
||||||
template moreIndent(em): int =
|
template moreIndent(em): int =
|
||||||
if em.doIndentMore > 0: em.indWidth*2 else: em.indWidth
|
if em.doIndentMore > 0: em.indWidth*2 else: em.indWidth
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ import ../compiler / [idents, msgs, ast, syntaxes, renderer, options,
|
||||||
import parseopt, strutils, os
|
import parseopt, strutils, os
|
||||||
|
|
||||||
const
|
const
|
||||||
Version = "0.1"
|
Version = "0.2"
|
||||||
Usage = "nimpretty - Nim Pretty Printer Version " & Version & """
|
Usage = "nimpretty - Nim Pretty Printer Version " & Version & """
|
||||||
|
|
||||||
(c) 2017 Andreas Rumpf
|
(c) 2017 Andreas Rumpf
|
||||||
|
|
@ -28,6 +28,7 @@ Options:
|
||||||
--output:file set the output file (default: overwrite the input file)
|
--output:file set the output file (default: overwrite the input file)
|
||||||
--indent:N[=0] set the number of spaces that is used for indentation
|
--indent:N[=0] set the number of spaces that is used for indentation
|
||||||
--indent:0 means autodetection (default behaviour)
|
--indent:0 means autodetection (default behaviour)
|
||||||
|
--maxLineLen:N set the desired maximum line length (default: 80)
|
||||||
--version show the version
|
--version show the version
|
||||||
--help show this help
|
--help show this help
|
||||||
"""
|
"""
|
||||||
|
|
@ -45,6 +46,7 @@ proc writeVersion() =
|
||||||
type
|
type
|
||||||
PrettyOptions = object
|
PrettyOptions = object
|
||||||
indWidth: int
|
indWidth: int
|
||||||
|
maxLineLen: int
|
||||||
|
|
||||||
proc prettyPrint(infile, outfile: string, opt: PrettyOptions) =
|
proc prettyPrint(infile, outfile: string, opt: PrettyOptions) =
|
||||||
var conf = newConfigRef()
|
var conf = newConfigRef()
|
||||||
|
|
@ -55,6 +57,7 @@ proc prettyPrint(infile, outfile: string, opt: PrettyOptions) =
|
||||||
var p: TParsers
|
var p: TParsers
|
||||||
p.parser.em.indWidth = opt.indWidth
|
p.parser.em.indWidth = opt.indWidth
|
||||||
if setupParsers(p, fileIdx, newIdentCache(), conf):
|
if setupParsers(p, fileIdx, newIdentCache(), conf):
|
||||||
|
p.parser.em.maxLineLen = opt.maxLineLen
|
||||||
discard parseAll(p)
|
discard parseAll(p)
|
||||||
closeParsers(p)
|
closeParsers(p)
|
||||||
|
|
||||||
|
|
@ -66,6 +69,7 @@ proc main =
|
||||||
# if input is not actually over-written, when nimpretty is a noop).
|
# if input is not actually over-written, when nimpretty is a noop).
|
||||||
# --backup was un-documented (rely on git instead).
|
# --backup was un-documented (rely on git instead).
|
||||||
var opt: PrettyOptions
|
var opt: PrettyOptions
|
||||||
|
opt.maxLineLen = 80
|
||||||
for kind, key, val in getopt():
|
for kind, key, val in getopt():
|
||||||
case kind
|
case kind
|
||||||
of cmdArgument:
|
of cmdArgument:
|
||||||
|
|
@ -77,6 +81,7 @@ proc main =
|
||||||
of "backup": backup = parseBool(val)
|
of "backup": backup = parseBool(val)
|
||||||
of "output", "o": outfile = val
|
of "output", "o": outfile = val
|
||||||
of "indent": opt.indWidth = parseInt(val)
|
of "indent": opt.indWidth = parseInt(val)
|
||||||
|
of "maxlinelen": opt.maxLineLen = parseInt(val)
|
||||||
else: writeHelp()
|
else: writeHelp()
|
||||||
of cmdEnd: assert(false) # cannot happen
|
of cmdEnd: assert(false) # cannot happen
|
||||||
if infile.len == 0:
|
if infile.len == 0:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue