docstyle.rst 5.1 KB

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