Skip to content

Commit 4bfc5a9

Browse files
authored
Rst test check messages (fix #17280) (#17338)
1 parent 72b89ef commit 4bfc5a9

File tree

4 files changed

+156
-90
lines changed

4 files changed

+156
-90
lines changed

compiler/docgen.nim

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ from std/private/globs import nativeToUnixPath
2525
const
2626
exportSection = skField
2727
docCmdSkip = "skip"
28+
DocColOffset = "## ".len # assuming that a space was added after ##
2829

2930
type
3031
TSections = array[TSymKind, Rope]
@@ -322,8 +323,12 @@ proc genComment(d: PDoc, n: PNode): string =
322323
when false:
323324
# RFC: to preseve newlines in comments, this would work:
324325
comment = comment.replace("\n", "\n\n")
325-
renderRstToOut(d[], parseRst(comment, toFullPath(d.conf, n.info), toLinenumber(n.info),
326-
toColumn(n.info), (var dummy: bool; dummy), d.options, d.conf), result)
326+
renderRstToOut(d[],
327+
parseRst(comment, toFullPath(d.conf, n.info),
328+
toLinenumber(n.info),
329+
toColumn(n.info) + DocColOffset,
330+
(var dummy: bool; dummy), d.options, d.conf),
331+
result)
327332

328333
proc genRecCommentAux(d: PDoc, n: PNode): Rope =
329334
if n == nil: return nil

lib/packages/docutils/rst.nim

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@
146146
## .. _Sphinx directives: https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html
147147

148148
import
149-
os, strutils, rstast, algorithm, lists, sequtils
149+
os, strutils, rstast, std/enumutils, algorithm, lists, sequtils,
150+
std/private/miscdollars
150151

151152
type
152153
RstParseOption* = enum ## options for the RST parser
@@ -477,17 +478,18 @@ type
477478
EParseError* = object of ValueError
478479

479480
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)
481+
LineRstInit* = 1 ## Initial line number for standalone RST text
482+
ColRstInit* = 0 ## Initial column number for standalone RST text
483+
## (Nim global reporting adds ColOffset=1)
484+
ColRstOffset* = 1 ## 1: a replica of ColOffset for internal use
483485

484486
template currentTok(p: RstParser): Token = p.tok[p.idx]
485487
template prevTok(p: RstParser): Token = p.tok[p.idx - 1]
486488
template nextTok(p: RstParser): Token = p.tok[p.idx + 1]
487489

488490
proc whichMsgClass*(k: MsgKind): MsgClass =
489491
## returns which message class `k` belongs to.
490-
case ($k)[1]
492+
case k.symbolName[1]
491493
of 'e', 'E': result = mcError
492494
of 'w', 'W': result = mcWarning
493495
of 'h', 'H': result = mcHint
@@ -497,7 +499,9 @@ proc defaultMsgHandler*(filename: string, line, col: int, msgkind: MsgKind,
497499
arg: string) =
498500
let mc = msgkind.whichMsgClass
499501
let a = $msgkind % arg
500-
let message = "$1($2, $3) $4: $5" % [filename, $line, $col, $mc, a]
502+
var message: string
503+
toLocation(message, filename, line, col + ColRstOffset)
504+
message.add " $1: $2" % [$mc, a]
501505
if mc == mcError: raise newException(EParseError, message)
502506
else: writeLine(stdout, message)
503507

@@ -1973,25 +1977,32 @@ proc parseEnumList(p: var RstParser): PRstNode =
19731977
if match(p, p.idx, wildcards[w]): break
19741978
inc w
19751979
assert w < wildcards.len
1980+
19761981
proc checkAfterNewline(p: RstParser, report: bool): bool =
1982+
## If no indentation on the next line then parse as a normal paragraph
1983+
## according to the RST spec. And report a warning with suggestions
19771984
let j = tokenAfterNewline(p, start=p.idx+1)
1985+
let requiredIndent = p.tok[p.idx+wildToken[w]].col
19781986
if p.tok[j].kind notin {tkIndent, tkEof} and
1979-
p.tok[j].col < p.tok[p.idx+wildToken[w]].col and
1987+
p.tok[j].col < requiredIndent and
19801988
(p.tok[j].col > col or
19811989
(p.tok[j].col == col and not match(p, j, wildcards[w]))):
19821990
if report:
19831991
let n = p.line + p.tok[j].line
19841992
let msg = "\n" & """
19851993
not enough indentation on line $2
1986-
(if it's continuation of enumeration list),
1994+
(should be at column $3 if it's a continuation of enum. list),
19871995
or no blank line after line $1 (if it should be the next paragraph),
19881996
or no escaping \ at the beginning of line $1
19891997
(if lines $1..$2 are a normal paragraph, not enum. list)""".
19901998
unindent(8)
1991-
rstMessage(p, mwRstStyle, msg % [$(n-1), $n])
1999+
let c = p.col + requiredIndent + ColRstOffset
2000+
rstMessage(p, mwRstStyle, msg % [$(n-1), $n, $c],
2001+
p.tok[j].line, p.tok[j].col)
19922002
result = false
19932003
else:
19942004
result = true
2005+
19952006
if not checkAfterNewline(p, report = true):
19962007
return nil
19972008
result = newRstNodeA(p, rnEnumList)

lib/packages/docutils/rstgen.nim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,7 +1476,8 @@ $content
14761476
# ---------- forum ---------------------------------------------------------
14771477

14781478
proc rstToHtml*(s: string, options: RstParseOptions,
1479-
config: StringTableRef): string =
1479+
config: StringTableRef,
1480+
msgHandler: MsgHandler = rst.defaultMsgHandler): string =
14801481
## Converts an input rst string into embeddable HTML.
14811482
##
14821483
## This convenience proc parses any input string using rst markup (it doesn't
@@ -1503,11 +1504,10 @@ proc rstToHtml*(s: string, options: RstParseOptions,
15031504

15041505
const filen = "input"
15051506
var d: RstGenerator
1506-
initRstGenerator(d, outHtml, config, filen, options, myFindFile,
1507-
rst.defaultMsgHandler)
1507+
initRstGenerator(d, outHtml, config, filen, options, myFindFile, msgHandler)
15081508
var dummyHasToc = false
15091509
var rst = rstParse(s, filen, line=LineRstInit, column=ColRstInit,
1510-
dummyHasToc, options)
1510+
dummyHasToc, options, myFindFile, msgHandler)
15111511
result = ""
15121512
renderRstToOut(d, rst, result)
15131513

0 commit comments

Comments
 (0)