Skip to content

Commit aa74473

Browse files
committed
revert dedent workarounds, address other comments
1 parent 75ab616 commit aa74473

File tree

4 files changed

+63
-58
lines changed

4 files changed

+63
-58
lines changed

compiler/docgen.nim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1347,7 +1347,8 @@ proc commandRstAux(cache: IdentCache, conf: ConfigRef;
13471347
var d = newDocumentor(filen, cache, conf, outExt)
13481348

13491349
d.isPureRst = true
1350-
var rst = parseRst(readFile(filen.string), filen.string, line=1, column=0,
1350+
var rst = parseRst(readFile(filen.string), filen.string,
1351+
line=LineRstInit, column=ColRstInit,
13511352
d.hasToc, {roSupportRawDirective, roSupportMarkdown}, conf)
13521353
var modDesc = newStringOfCap(30_000)
13531354
renderRstToOut(d[], rst, modDesc)

lib/packages/docutils/rst.nim

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -471,13 +471,16 @@ type
471471
## documenation fragment that will be added
472472
## in case of error/warning reporting to
473473
## (relative) line/column of the token.
474-
## For standalone text should be line=1 and
475-
## col=0 (Nim global reporting adds ColOffset=1)
476474
hasToc*: bool
477475
curAnchor*: string # variable to track latest anchor in s.anchors
478476

479477
EParseError* = object of ValueError
480478

479+
const
480+
LineRstInit* = 1 ## Initial line number for standalone RST text
481+
ColRstInit* = 0 ## Initial column number for standalone RST text
482+
## (Nim global reporting adds ColOffset=1)
483+
481484
template currentTok(p: RstParser): Token = p.tok[p.idx]
482485
template prevTok(p: RstParser): Token = p.tok[p.idx - 1]
483486
template nextTok(p: RstParser): Token = p.tok[p.idx + 1]
@@ -547,8 +550,8 @@ proc initParser(p: var RstParser, sharedState: PSharedState) =
547550
p.idx = 0
548551
p.filename = ""
549552
p.hasToc = false
550-
p.col = 0
551-
p.line = 1
553+
p.col = ColRstInit
554+
p.line = LineRstInit
552555
p.s = sharedState
553556

554557
proc addNodesAux(n: PRstNode, result: var string) =
@@ -1951,25 +1954,26 @@ proc parseEnumList(p: var RstParser): PRstNode =
19511954
if match(p, p.idx, wildcards[w]): break
19521955
inc w
19531956
assert w < wildcards.len
1954-
template checkAfterNewline =
1957+
proc checkAfterNewline(p: RstParser, report: bool): bool =
19551958
let j = tokenAfterNewline(p, start=p.idx+1)
19561959
if p.tok[j].kind notin {tkIndent, tkEof} and
19571960
p.tok[j].col < p.tok[p.idx+wildToken[w]].col and
19581961
(p.tok[j].col > col or
19591962
(p.tok[j].col == col and not match(p, j, wildcards[w]))):
1960-
# check `result == nil` to avoid duplication of warning since for
1961-
# subsequent enum.items parseEnumList will be called second time
1962-
if result == nil:
1963+
if report:
19631964
let n = p.line + p.tok[j].line
1964-
let msg = "\n" & dedent """
1965+
let msg = "\n" & """
19651966
not enough indentation on line $2
19661967
(if it's continuation of enumeration list),
19671968
or no blank line after line $1 (if it should be the next paragraph),
19681969
or no escaping \ at the beginning of line $1
1969-
(if lines $1..$2 are a normal paragraph, not enum. list)"""
1970-
rstMessage(p, mwRstStyle, indent(msg, 2) % [$(n-1), $n])
1971-
return
1972-
checkAfterNewline
1970+
(if lines $1..$2 are a normal paragraph, not enum. list)""".
1971+
unindent(8)
1972+
rstMessage(p, mwRstStyle, msg % [$(n-1), $n])
1973+
result = false
1974+
else:
1975+
result = true
1976+
if not checkAfterNewline(p, report = true): return nil
19731977
result = newRstNodeA(p, rnEnumList)
19741978
let autoEnums = if roSupportMarkdown in p.s.options: @["#", "1"] else: @["#"]
19751979
var prevAE = "" # so as not allow mixing auto-enumerators `1` and `#`
@@ -1990,7 +1994,9 @@ proc parseEnumList(p: var RstParser): PRstNode =
19901994
result.add(item)
19911995
if currentTok(p).kind == tkIndent and currentTok(p).ival == col and
19921996
match(p, p.idx+1, wildcards[w]):
1993-
checkAfterNewline
1997+
# don't report to avoid duplication of warning since for
1998+
# subsequent enum. items parseEnumList will be called second time:
1999+
if not checkAfterNewline(p, report = false): return nil
19942000
let enumerator = p.tok[p.idx + 1 + wildIndex[w]].symbol
19952001
# check that it's in sequence: enumerator == next(prevEnum)
19962002
if "n" in wildcards[w]: # arabic numeral

lib/packages/docutils/rstgen.nim

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,7 +1506,8 @@ proc rstToHtml*(s: string, options: RstParseOptions,
15061506
initRstGenerator(d, outHtml, config, filen, options, myFindFile,
15071507
rst.defaultMsgHandler)
15081508
var dummyHasToc = false
1509-
var rst = rstParse(s, filen, line=1, column=0, dummyHasToc, options)
1509+
var rst = rstParse(s, filen, line=LineRstInit, column=ColRstInit,
1510+
dummyHasToc, options)
15101511
result = ""
15111512
renderRstToOut(d, rst, result)
15121513

@@ -1519,5 +1520,6 @@ proc rstToLatex*(rstSource: string; options: RstParseOptions): string {.inline,
15191520
var rstGenera: RstGenerator
15201521
rstGenera.initRstGenerator(outLatex, defaultConfig(), "input", options)
15211522
rstGenera.renderRstToOut(
1522-
rstParse(rstSource, "", line=1, column=0, option, options),
1523+
rstParse(rstSource, "", line=LineRstInit, column=ColRstInit,
1524+
option, options),
15231525
result)

lib/pure/strutils.nim

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,47 +1438,43 @@ func unindent*(s: string, count: Natural = int.high,
14381438
result.add(line[indentCount*padding.len .. ^1])
14391439
i.inc
14401440

1441-
since (1, 3):
1442-
func indentation*(s: string): Natural =
1443-
## Returns the amount of indentation all lines of `s` have in common,
1444-
## ignoring lines that consist only of whitespace.
1445-
result = int.high
1446-
for line in s.splitLines:
1447-
for i, c in line:
1448-
if i >= result: break
1449-
elif c != ' ':
1450-
result = i
1451-
break
1452-
if result == int.high:
1453-
result = 0
1454-
1455-
func dedent*(s: string, count: Natural): string {.rtl, extern: "nsuDedent".} =
1456-
unindent(s, count, " ")
1457-
1458-
proc dedent*(a: string): string {.inline.} =
1459-
## Unindents each line in `s` by `count` amount of `padding`.
1460-
## The only difference between this and the
1461-
## `unindent func<#unindent,string,Natural,string>`_ is that this by default
1462-
## only cuts off the amount of indentation that all lines of `s` share as
1463-
## opposed to all indentation. It only supports spaces as padding.
1464-
##
1465-
## **Note:** This does not preserve the new line characters used in `s`.
1466-
##
1467-
## See also:
1468-
## * `unindent func<#unindent,string,Natural,string>`_
1469-
## * `align func<#align,string,Natural,char>`_
1470-
## * `alignLeft func<#alignLeft,string,Natural,char>`_
1471-
## * `spaces func<#spaces,Natural>`_
1472-
## * `indent func<#indent,string,Natural,string>`_
1473-
# workaround, see https://github.com/timotheecour/Nim/issues/521
1474-
runnableExamples:
1475-
let x = """
1476-
Hello
1477-
There
1478-
""".dedent()
1479-
1480-
doAssert x == "Hello\n There\n"
1481-
dedent(a, indentation(a))
1441+
func indentation*(s: string): Natural {.since: (1, 3).} =
1442+
## Returns the amount of indentation all lines of `s` have in common,
1443+
## ignoring lines that consist only of whitespace.
1444+
result = int.high
1445+
for line in s.splitLines:
1446+
for i, c in line:
1447+
if i >= result: break
1448+
elif c != ' ':
1449+
result = i
1450+
break
1451+
if result == int.high:
1452+
result = 0
1453+
1454+
func dedent*(s: string, count: Natural = indentation(s)): string {.rtl,
1455+
extern: "nsuDedent", since: (1, 3).} =
1456+
## Unindents each line in `s` by `count` amount of `padding`.
1457+
## The only difference between this and the
1458+
## `unindent func<#unindent,string,Natural,string>`_ is that this by default
1459+
## only cuts off the amount of indentation that all lines of `s` share as
1460+
## opposed to all indentation. It only supports spaces as padding.
1461+
##
1462+
## **Note:** This does not preserve the new line characters used in `s`.
1463+
##
1464+
## See also:
1465+
## * `unindent func<#unindent,string,Natural,string>`_
1466+
## * `align func<#align,string,Natural,char>`_
1467+
## * `alignLeft func<#alignLeft,string,Natural,char>`_
1468+
## * `spaces func<#spaces,Natural>`_
1469+
## * `indent func<#indent,string,Natural,string>`_
1470+
runnableExamples:
1471+
let x = """
1472+
Hello
1473+
There
1474+
""".dedent()
1475+
1476+
doAssert x == "Hello\n There\n"
1477+
unindent(s, count, " ")
14821478

14831479
func delete*(s: var string, first, last: int) {.rtl, extern: "nsuDelete".} =
14841480
## Deletes in `s` (must be declared as `var`) the characters at positions

0 commit comments

Comments
 (0)