-
-
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
docgen: move to shared RST state (fix #16990) #18256
Conversation
just a side-note, @a-mr, please write |
lib/packages/docutils/rst.nim
Outdated
|
||
PSharedState = ref SharedState | ||
RstSharedState* = ref SharedState |
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.
don't violate the convention type PFoo = ref Foo
though, if you do that, then also rename SharedState to RstSharedState, ditto with newSharedState => newRstSharedState (not newSharedStateRst)
(if you want, you can make a separate PR with just those renames maybe that can be reviewed fast; looks like those symbols are all private anyways)
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.
ok, ok, i don't argue. Done.
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.
don't violate the convention type PFoo = ref Foo though
That's an old and outdated convention.
lib/packages/docutils/rst.nim
Outdated
proc newSharedStateRst*(options: RstParseOptions, | ||
filename: string, | ||
findFile: FindFileHandler, | ||
msgHandler: MsgHandler): RstSharedState = | ||
new(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 this or (preferably i guess) separate cleanup PR:
result = RstSharedState(a1: a1, a2: a2, ...)
and these are not needed anymore (relic from the past):
result.subs = @[]
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.
ok, done.
lib/packages/docutils/rst.nim
Outdated
p.col + currentTok(p).col, msgKind, arg) | ||
|
||
proc rstMessage(s: RstSharedState, msgKind: MsgKind, arg: string) = | ||
## Handle message `arg`. Its position can not be localized at the moment. |
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.
doc comment unclear, can you try to explain can not be localized
better in the doc comment?
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.
rephrased it.
var key = addNodes(n) | ||
# the spec says: if no exact match, try one without case distinction: | ||
for i in countup(0, high(p.s.subs)): | ||
if key == p.s.subs[i].key: | ||
for i in countup(0, high(s.subs)): |
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.
it's ok to cleanup lines that you are editing; 0..high(s.subs)
is shorter and better
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.
agree but let us to do it in follow-up
@@ -1034,7 +1034,8 @@ Test1 | |||
""" | |||
var warnings8 = new seq[string] | |||
let output8 = input8.toHtml(warnings=warnings8) | |||
check(warnings8[] == @["input(4, 1) Warning: unknown substitution " & | |||
# TODO: the line 1 is arbitrary because reference lines are not preserved |
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.
is that a regression or was 4
meaningless/wrong? (i know that line numbers were often wrong in nim rst2html)
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.
not a regression: it's wrong in both cases. Before it was always last line of text, after it's always first line. The problem is that line/column info has never been preserved in any references :-(. We need to add it to (P)RstNode
, a good task for one of future PRs.
compiler/docgen.nim
Outdated
case isRst: bool | ||
of true: | ||
rst: PRstNode | ||
of false: |
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.
add a comment indicating what it corresponds to if isRst is false
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.
added.
compiler/docgen.nim
Outdated
Item = object ## Any item in documentation, e.g. type or | ||
## proc entry. Configuration variable ``doc.item`` | ||
## is used for its HTML rendering. | ||
descRst: ItemPre ## Description of the item (may contain runnable |
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.
call a cat a cat (runnableExamples
), more searchable
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.
ok.
if not isVisible(d, nameNode): return | ||
var | ||
name = getName(d, nameNode) | ||
comm = genRecComment(d, n) | ||
r: TSrcGen | ||
initTokRender(r, n, {renderNoBody, renderNoComments, renderDocComments}) | ||
result = %{ "name": %name, "type": %($k), "line": %n.info.line.int, | ||
"col": %n.info.col} | ||
result.json = %{ "name": %name, "type": %($k), "line": %n.info.line.int, |
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 future work, IMO we should use jsonutils here, doing a similar simplification as was done in #18100, which allows more type-safety (via a type that represents the spec of what's serialized, making deserialization + serialization easier)
@@ -1357,6 +1434,7 @@ proc commandRst2TeX*(cache: IdentCache, conf: ConfigRef) = | |||
commandRstAux(cache, conf, conf.projectFull, TexExt) | |||
|
|||
proc commandJson*(cache: IdentCache, conf: ConfigRef) = | |||
## implementation of a deprecated jsondoc0 command |
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.
then should we rename commandJson as commandJson0 ? (can be done in future 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.
I thought about deleting this jsondoc0
altogether. It always crashes for me, including stable 1.4 version. Does it work for you?
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.
crashes with options.nim(637, 3)
not conf.outFile.isEmpty [AssertionDefect]
but that doesn't mean it'd be hard to fix; if this were to be removed, it should be a separate PR though (IIRC the use case is for include files where semcheck don't work; just like nim doc0; it's hacky but ./koch docs relies on at least nim doc0)
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.
may be but i'm not planning on doing any fixes, because i don't understand the use cases of jsondoc/jsondoc0
commands in the 1st place :-/
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.
nim jsondoc
use case is for tooling, so that a program can use the json output generated by nim for further processing (eg to support queries, such as find all procs matching a given signature) without having to link against nim or use compiler API, refs https://github.com/nim-lang/Nim/issues?q=is%3Aissue+is%3Aopen+jsondoc.
But yes, defering to future work is good
compiler/docgen.nim
Outdated
of true: | ||
var resolved = resolveSubs(d.sharedState, f.rst) | ||
var str: string | ||
renderRstToOut(d[], resolved, str) |
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.
can we make this work? (ie, make sure that renderRstToOut doesn't clear its 3rd arg but insteads just appeands, as is good practice)
renderRstToOut(d[], resolved, 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.
yes, well spotted, thanks.
|
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.
LGTM, remaining points can be addressed in followup PR
I tested As i wrote above |
does it also fix any of these ? |
Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
* docgen: move to shared RST state (fix nim-lang#16990) * Update lib/packages/docutils/rst.nim Co-authored-by: Andreas Rumpf <rumpf_a@web.de> * Update lib/packages/docutils/rst.nim Co-authored-by: Andreas Rumpf <rumpf_a@web.de> * Update lib/packages/docutils/rst.nim Co-authored-by: Andreas Rumpf <rumpf_a@web.de> * Update compiler/docgen.nim Co-authored-by: Timothee Cour <timothee.cour2@gmail.com> * Update compiler/docgen.nim Co-authored-by: Timothee Cour <timothee.cour2@gmail.com> * Update compiler/docgen.nim Co-authored-by: Timothee Cour <timothee.cour2@gmail.com> * Update lib/packages/docutils/rst.nim Co-authored-by: Timothee Cour <timothee.cour2@gmail.com> * rename `cmdDoc2` to `cmdDoc` * fix (P)RstSharedState convention * new style of initialization * misc suggestions * 1 more rename * fix a regression Co-authored-by: Andreas Rumpf <rumpf_a@web.de> Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
Previously each doc comment inside a
.nim
file was processed as a separate document.This PR:
A. makes doc comments related through a common (per-file) RST shared state. (Strictly speaking this would be enough for fixing #16990)
B. splits RST processing into 2 phases:
string
doc comment are converted to RST ASTPRstNode
finishGenerateDoc
), which traverses RST AST and performs final resolution (headings + links + footnotes + RST substitutions) convertingPRstNode
to HTML or Latexstring
B. is done by saving
PRstNode
of any doc comment to a data structure after pass 1.So point B. makes
.nim
doc comments feel like a single document. This style becomes possible:This PR aids in implementing smart links nim-lang/RFCs#125, because previously doc comments were converted immediately, that is before the whole
.nim
AST tree were traversed and hence the list of all local symbols (proc
s etc) were not yet known. NowfinishGenerateDoc
will be the right place to resolve all the links.cc @timotheecour