Skip to content

show top-level import for top-level runnableExamples in generated docs #352

@timotheecour

Description

@timotheecour

proposal

make docgen automatically add import foo in the generated html for a top-level runnableExamples inside module foo;

in fact this is what is done by the part of docgen that does for running the code, see https://github.com/nim-lang/Nim/pull/17282/files#diff-2471f1aaac2c91ed83e0317b1b0b322fc719a4cb431de811baa8e350dade283fR531, except that:

pros

easier to copy paste and edit, matches what's actually being run

note

to avoid generating too much noise, this proposal only concerns top-level runnableExamples, so that

doAssert toUpperAscii("FooBar!") == "FOOBAR!"

will remain unaffected instead of generating:

import std/strutils
doAssert toUpperAscii("FooBar!") == "FOOBAR!"

which would just obfuscate (a little bit) the code; it should be clear from context that import std/strutils is needed for runnableExamples attached to a symbol.

it's not perfect, because some large but non top-level runnableExamples could benefit from showing such import, but these are rare; eg: https://nim-lang.github.io/Nim/sugar.html#collect.m%2Cuntyped

links

refs nim-lang/Nim#17203 (comment)
refs https://forum.nim-lang.org/t/7622#48410
pr_fix_rfc_17282_runnableExamples_show_import

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions