-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rst test check messages (fix #17280) #17338
Changes from all commits
3a0941c
b251a48
67a436c
b29d7f0
50a7a4f
2174592
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -146,7 +146,8 @@ | |
## .. _Sphinx directives: https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html | ||
|
||
import | ||
os, strutils, rstast, algorithm, lists, sequtils | ||
os, strutils, rstast, std/enumutils, algorithm, lists, sequtils, | ||
std/private/miscdollars | ||
|
||
type | ||
RstParseOption* = enum ## options for the RST parser | ||
|
@@ -477,17 +478,18 @@ type | |
EParseError* = object of ValueError | ||
|
||
const | ||
LineRstInit* = 1 ## Initial line number for standalone RST text | ||
ColRstInit* = 0 ## Initial column number for standalone RST text | ||
## (Nim global reporting adds ColOffset=1) | ||
LineRstInit* = 1 ## Initial line number for standalone RST text | ||
ColRstInit* = 0 ## Initial column number for standalone RST text | ||
## (Nim global reporting adds ColOffset=1) | ||
ColRstOffset* = 1 ## 1: a replica of ColOffset for internal use | ||
|
||
template currentTok(p: RstParser): Token = p.tok[p.idx] | ||
template prevTok(p: RstParser): Token = p.tok[p.idx - 1] | ||
template nextTok(p: RstParser): Token = p.tok[p.idx + 1] | ||
|
||
proc whichMsgClass*(k: MsgKind): MsgClass = | ||
## returns which message class `k` belongs to. | ||
case ($k)[1] | ||
case k.symbolName[1] | ||
of 'e', 'E': result = mcError | ||
of 'w', 'W': result = mcWarning | ||
of 'h', 'H': result = mcHint | ||
|
@@ -497,7 +499,9 @@ proc defaultMsgHandler*(filename: string, line, col: int, msgkind: MsgKind, | |
arg: string) = | ||
let mc = msgkind.whichMsgClass | ||
let a = $msgkind % arg | ||
let message = "$1($2, $3) $4: $5" % [filename, $line, $col, $mc, a] | ||
var message: string | ||
toLocation(message, filename, line, col + ColRstOffset) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in future PR we can add an outplace version of toLocation to make life simpler in here and elsewhere, useful wherever performance is not critical (like here). Or you could also use sugar.dup |
||
message.add " $1: $2" % [$mc, a] | ||
if mc == mcError: raise newException(EParseError, message) | ||
else: writeLine(stdout, message) | ||
|
||
|
@@ -1954,25 +1958,32 @@ proc parseEnumList(p: var RstParser): PRstNode = | |
if match(p, p.idx, wildcards[w]): break | ||
inc w | ||
assert w < wildcards.len | ||
|
||
proc checkAfterNewline(p: RstParser, report: bool): bool = | ||
timotheecour marked this conversation as resolved.
Show resolved
Hide resolved
|
||
## If no indentation on the next line then parse as a normal paragraph | ||
## according to the RST spec. And report a warning with suggestions | ||
let j = tokenAfterNewline(p, start=p.idx+1) | ||
let requiredIndent = p.tok[p.idx+wildToken[w]].col | ||
if p.tok[j].kind notin {tkIndent, tkEof} and | ||
p.tok[j].col < p.tok[p.idx+wildToken[w]].col and | ||
p.tok[j].col < requiredIndent and | ||
(p.tok[j].col > col or | ||
(p.tok[j].col == col and not match(p, j, wildcards[w]))): | ||
if report: | ||
let n = p.line + p.tok[j].line | ||
let msg = "\n" & """ | ||
not enough indentation on line $2 | ||
(if it's continuation of enumeration list), | ||
(should be at column $3 if it's a continuation of enum. list), | ||
or no blank line after line $1 (if it should be the next paragraph), | ||
or no escaping \ at the beginning of line $1 | ||
(if lines $1..$2 are a normal paragraph, not enum. list)""". | ||
unindent(8) | ||
rstMessage(p, mwRstStyle, msg % [$(n-1), $n]) | ||
let c = p.col + requiredIndent + ColRstOffset | ||
rstMessage(p, mwRstStyle, msg % [$(n-1), $n, $c], | ||
p.tok[j].line, p.tok[j].col) | ||
result = false | ||
else: | ||
result = true | ||
|
||
if not checkAfterNewline(p, report = true): | ||
return nil | ||
result = newRstNodeA(p, rnEnumList) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1476,7 +1476,8 @@ $content | |
# ---------- forum --------------------------------------------------------- | ||
|
||
proc rstToHtml*(s: string, options: RstParseOptions, | ||
config: StringTableRef): string = | ||
config: StringTableRef, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of specifying line, col in all callers (and doing so incorrectly, see below with (1,1) instead of (LineRstInit,ColRstInit) which is (1,0), how about add an overload of proc rstParse*(text, filename: string, hasToc: var bool, options: RstParseOptions, findFile: FindFileHandler = nil, msgHandler: MsgHandler = nil): PRstNode =
rstParse(line=LineRstInit,column=ColRstInit,...) will simplify and make these correct:
can be done in cleanup PR though There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, will be overloaded in the future. |
||
msgHandler: MsgHandler = rst.defaultMsgHandler): string = | ||
## Converts an input rst string into embeddable HTML. | ||
## | ||
## This convenience proc parses any input string using rst markup (it doesn't | ||
|
@@ -1503,11 +1504,10 @@ proc rstToHtml*(s: string, options: RstParseOptions, | |
|
||
const filen = "input" | ||
var d: RstGenerator | ||
initRstGenerator(d, outHtml, config, filen, options, myFindFile, | ||
rst.defaultMsgHandler) | ||
initRstGenerator(d, outHtml, config, filen, options, myFindFile, msgHandler) | ||
var dummyHasToc = false | ||
var rst = rstParse(s, filen, line=LineRstInit, column=ColRstInit, | ||
timotheecour marked this conversation as resolved.
Show resolved
Hide resolved
|
||
dummyHasToc, options) | ||
dummyHasToc, options, myFindFile, msgHandler) | ||
result = "" | ||
renderRstToOut(d, rst, result) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in reply to
(i prefer code comments to maintain threading)
take a look at https://gist.github.com/timotheecour/e53f06241914fa1e20a40f631edfae10
run
nim doc -d:case1 main
(and -d:case2, -d:case3)before this PR, line and column number is bad
after this PR, line and column number is also bad (maybe a bit worse?)
IMO we should report line and column numbers wrt source file, and not offset it by
"## ".len
; unless I'm missing something it should be both simpler and more robust.But I suggest merging this PR to make progress and addressing this inconsistent line/col numbering in followup PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the case1 is just incorrectly adjusted line in
parseHeadline
. We can fix it with an appropriate test now.The multiline case2 and case3 are more difficult, some investigation will be needed.