docstyle.rst 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. Documentation Style
  2. ===================
  3. General Guidelines
  4. ------------------
  5. * See also `nep1<https://nim-lang.github.io/Nim/nep1.html>`_ which should probably be merged here.
  6. * Authors should document anything that is exported; documentation for private
  7. procs can be useful too (visible via ``nim doc --docInternal foo.nim``).
  8. * Within documentation, a period (`.`) should follow each sentence (or sentence fragment) in a comment block.
  9. The documentation may be limited to one sentence fragment, but if multiple sentences are within the documentation,
  10. each sentence after the first should be complete and in present tense.
  11. * Documentation is parsed as a custom ReStructuredText (RST) with partial markdown support.
  12. * In nim sources, prefer single backticks to double backticks since it's simpler
  13. and `nim doc` supports it (even in rst files with `nim rst2html`).
  14. * In nim sources, for links, prefer `[link text](link.html)` to ``` `link text<link.html>`_ ```
  15. since the syntax is simpler and markdown is more common (likewise, `nim rst2html` also supports it in rst files).
  16. .. code-block:: nim
  17. proc someproc*(s: string, foo: int) =
  18. ## Use single backticks for inline code, eg: `s` or `someExpr(true)`.
  19. ## Use a backlash to follow with alphanumeric char: `int8`\s are great.
  20. Module-level documentation
  21. --------------------------
  22. Documentation of a module is placed at the top of the module itself. Each line of documentation begins with double hashes (``##``).
  23. Sometimes ``##[ multiline docs containing code ]##`` is preferable, see ``lib/pure/times.nim``.
  24. Code samples are encouraged, and should follow the general RST syntax:
  25. .. code-block:: Nim
  26. ## The `universe` module computes the answer to life, the universe, and everything.
  27. ##
  28. ## .. code-block::
  29. ## doAssert computeAnswerString() == 42
  30. Within this top-level comment, you can indicate the authorship and copyright of the code, which will be featured in the produced documentation.
  31. .. code-block:: Nim
  32. ## This is the best module ever. It provides answers to everything!
  33. ##
  34. ## :Author: Steve McQueen
  35. ## :Copyright: 1965
  36. ##
  37. Leave a space between the last line of top-level documentation and the beginning of Nim code (the imports, etc.).
  38. Procs, Templates, Macros, Converters, and Iterators
  39. ---------------------------------------------------
  40. The documentation of a procedure should begin with a capital letter and should be in present tense. Variables referenced in the documentation should be surrounded by single tick marks:
  41. .. code-block:: Nim
  42. proc example1*(x: int) =
  43. ## Prints the value of `x`.
  44. echo x
  45. Whenever an example of usage would be helpful to the user, you should include one within the documentation in RST format as below.
  46. .. code-block:: Nim
  47. proc addThree*(x, y, z: int8): int =
  48. ## Adds three `int8` values, treating them as unsigned and
  49. ## truncating the result.
  50. ##
  51. ## .. code-block::
  52. ## # things that aren't suitable for a `runnableExamples` go in code-block:
  53. ## echo execCmdEx("git pull")
  54. ## drawOnScreen()
  55. runnableExamples:
  56. # `runnableExamples` is usually preferred to `code-block`, when possible.
  57. doAssert addThree(3, 125, 6) == -122
  58. result = x +% y +% z
  59. The commands ``nim doc`` and ``nim doc2`` will then correctly syntax highlight the Nim code within the documentation.
  60. Types
  61. -----
  62. Exported types should also be documented. This documentation can also contain code samples, but those are better placed with the functions to which they refer.
  63. .. code-block:: Nim
  64. type
  65. NamedQueue*[T] = object ## Provides a linked data structure with names
  66. ## throughout. It is named for convenience. I'm making
  67. ## this comment long to show how you can, too.
  68. name*: string ## The name of the item
  69. val*: T ## Its value
  70. next*: ref NamedQueue[T] ## The next item in the queue
  71. You have some flexibility when placing the documentation:
  72. .. code-block:: Nim
  73. type
  74. NamedQueue*[T] = object
  75. ## Provides a linked data structure with names
  76. ## throughout. It is named for convenience. I'm making
  77. ## this comment long to show how you can, too.
  78. name*: string ## The name of the item
  79. val*: T ## Its value
  80. next*: ref NamedQueue[T] ## The next item in the queue
  81. Make sure to place the documentation beside or within the object.
  82. .. code-block:: Nim
  83. type
  84. ## Bad: this documentation disappears because it annotates the ``type`` keyword
  85. ## above, not ``NamedQueue``.
  86. NamedQueue*[T] = object
  87. name*: string ## This becomes the main documentation for the object, which
  88. ## is not what we want.
  89. val*: T ## Its value
  90. next*: ref NamedQueue[T] ## The next item in the queue
  91. Var, Let, and Const
  92. -------------------
  93. When declaring module-wide constants and values, documentation is encouraged. The placement of doc comments is similar to the ``type`` sections.
  94. .. code-block:: Nim
  95. const
  96. X* = 42 ## An awesome number.
  97. SpreadArray* = [
  98. [1,2,3],
  99. [2,3,1],
  100. [3,1,2],
  101. ] ## Doc comment for ``SpreadArray``.
  102. Placement of comments in other areas is usually allowed, but will not become part of the documentation output and should therefore be prefaced by a single hash (``#``).
  103. .. code-block:: Nim
  104. const
  105. BadMathVals* = [
  106. 3.14, # pi
  107. 2.72, # e
  108. 0.58, # gamma
  109. ] ## A bunch of badly rounded values.
  110. Nim supports Unicode in comments, so the above can be replaced with the following:
  111. .. code-block:: Nim
  112. const
  113. BadMathVals* = [
  114. 3.14, # π
  115. 2.72, # e
  116. 0.58, # γ
  117. ] ## A bunch of badly rounded values (including π!).