Is it possible to generate a docs link based on NimSym? Or, even better, based on an argument's type?
Here comes an example:
# a.nim
type Mock = object
# b.nim
import a
macro add2docs(sym: typed, docs: string): typed =
newCommentStmtNode(docs.format(XYZ(sym))) # what XYZ should be?
proc initMock(m: var Mock) =
"$1 constructor".add2docs(Mock)
...
# generates `Mock <http://nim-lang.org/docs/a.html#Mock>`_
Alternatively, I could get symbol's parent module (is it possible?) and generate the link manually based on that name. It wouldn't be as nice though, as it would make the link fixed to http://nim-lang.org/docs/ (what if I want to have a local repo)?
It is actually possible if you don't mind using undocumented behavior of getImpl (the docs say it works only for procedures and constants, but that doesn't seem to be true):
# a.nim
type Mock* = object
# b.nim
import macros, strutils
import a as al
type
# Just to prove that module and symbol aliases are transparent.
MockAlias = al.Mock
proc XYZ(sym: NimNode): string =
let
ts = sym.getType[1]
fn = ts.symbol.getImpl.lineInfoObj.filename
result = "`$1 <http://nim-lang.org/docs/$2.html#$1>`_".format(
ts.symbol, fn.substr(0, fn.find('.') - 1)
)
macro add2docs(docs, sym: typed): typed =
newCommentStmtNode(docs.strVal.format(XYZ(sym)))
proc initMock*(m: var MockAlias) =
"$1 constructor".add2docs(MockAlias)
echo "initializing.."
@Lando Thank you for pointing it out. :) Too bad it's not documented, I tend to avoid using undocumented stuff. ^^" Actually, I think I even used lineInfoObj once like a year ago so I probably forgot about it.
Not I only need to think out how to make links blend in nicely in the docs. :)