docstyle.rst 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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`:cmd:).
  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`:cmd: supports it. Likewise with ``rst`` files: `nim rst2html`:cmd: will render those as monospace, and
  14. adding ``.. default-role:: code`` to an ``rst`` file will also make those render as monospace when rendered directly
  15. in tools such as github.
  16. * (debatable) In nim sources, for links, prefer ``[link text](link.html)`` to `\`link text<link.html>\`_`:code:
  17. since the syntax is simpler and markdown is more common (likewise, `nim rst2html`:cmd: also supports it in ``rst`` files).
  18. .. code-block:: nim
  19. proc someproc*(s: string, foo: int) =
  20. ## Use single backticks for inline code, e.g.: `s` or `someExpr(true)`.
  21. ## Use a backlash to follow with alphanumeric char: `int8`\s are great.
  22. Module-level documentation
  23. --------------------------
  24. Documentation of a module is placed at the top of the module itself. Each line of documentation begins with double hashes (`##`).
  25. Sometimes `##[ multiline docs containing code ]##` is preferable, see ``lib/pure/times.nim``.
  26. Code samples are encouraged, and should follow the general RST syntax:
  27. .. code-block:: Nim
  28. ## The `universe` module computes the answer to life, the universe, and everything.
  29. ##
  30. ## .. code-block::
  31. ## doAssert computeAnswerString() == 42
  32. Within this top-level comment, you can indicate the authorship and copyright of the code, which will be featured in the produced documentation.
  33. .. code-block:: Nim
  34. ## This is the best module ever. It provides answers to everything!
  35. ##
  36. ## :Author: Steve McQueen
  37. ## :Copyright: 1965
  38. ##
  39. Leave a space between the last line of top-level documentation and the beginning of Nim code (the imports, etc.).
  40. Procs, Templates, Macros, Converters, and Iterators
  41. ---------------------------------------------------
  42. 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:
  43. .. code-block:: Nim
  44. proc example1*(x: int) =
  45. ## Prints the value of `x`.
  46. echo x
  47. Whenever an example of usage would be helpful to the user, you should include one within the documentation in RST format as below.
  48. .. code-block:: Nim
  49. proc addThree*(x, y, z: int8): int =
  50. ## Adds three `int8` values, treating them as unsigned and
  51. ## truncating the result.
  52. ##
  53. ## .. code-block::
  54. ## # things that aren't suitable for a `runnableExamples` go in code-block:
  55. ## echo execCmdEx("git pull")
  56. ## drawOnScreen()
  57. runnableExamples:
  58. # `runnableExamples` is usually preferred to ``code-block``, when possible.
  59. doAssert addThree(3, 125, 6) == -122
  60. result = x +% y +% z
  61. The command `nim doc`:cmd: will then correctly syntax highlight the Nim code within the documentation.
  62. Types
  63. -----
  64. 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.
  65. .. code-block:: Nim
  66. type
  67. NamedQueue*[T] = object ## Provides a linked data structure with names
  68. ## throughout. It is named for convenience. I'm making
  69. ## this comment long to show how you can, too.
  70. name*: string ## The name of the item
  71. val*: T ## Its value
  72. next*: ref NamedQueue[T] ## The next item in the queue
  73. You have some flexibility when placing the documentation:
  74. .. code-block:: Nim
  75. type
  76. NamedQueue*[T] = object
  77. ## Provides a linked data structure with names
  78. ## throughout. It is named for convenience. I'm making
  79. ## this comment long to show how you can, too.
  80. name*: string ## The name of the item
  81. val*: T ## Its value
  82. next*: ref NamedQueue[T] ## The next item in the queue
  83. Make sure to place the documentation beside or within the object.
  84. .. code-block:: Nim
  85. type
  86. ## Bad: this documentation disappears because it annotates the `type` keyword
  87. ## above, not `NamedQueue`.
  88. NamedQueue*[T] = object
  89. name*: string ## This becomes the main documentation for the object, which
  90. ## is not what we want.
  91. val*: T ## Its value
  92. next*: ref NamedQueue[T] ## The next item in the queue
  93. Var, Let, and Const
  94. -------------------
  95. When declaring module-wide constants and values, documentation is encouraged. The placement of doc comments is similar to the `type` sections.
  96. .. code-block:: Nim
  97. const
  98. X* = 42 ## An awesome number.
  99. SpreadArray* = [
  100. [1,2,3],
  101. [2,3,1],
  102. [3,1,2],
  103. ] ## Doc comment for `SpreadArray`.
  104. 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 (`#`).
  105. .. code-block:: Nim
  106. const
  107. BadMathVals* = [
  108. 3.14, # pi
  109. 2.72, # e
  110. 0.58, # gamma
  111. ] ## A bunch of badly rounded values.
  112. Nim supports Unicode in comments, so the above can be replaced with the following:
  113. .. code-block:: Nim
  114. const
  115. BadMathVals* = [
  116. 3.14, # π
  117. 2.72, # e
  118. 0.58, # γ
  119. ] ## A bunch of badly rounded values (including π!).