docstyle.rst 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. Documentation Style
  2. ===================
  3. General Guidelines
  4. ------------------
  5. * Authors should document anything that is exported.
  6. * 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.
  7. * Documentation is parsed as ReStructuredText (RST).
  8. * Inline code should be surrounded by double tick marks ("``````"). If you would like a character to immediately follow inline code (e.g., "``int8``s are great!"), escape the following character with a backslash (``\``). The preceding is typed as ``` ``int8``\s are great!```.
  9. Module-level documentation
  10. --------------------------
  11. Documentation of a module is placed at the top of the module itself. Each line of documentation begins with double hashes (``##``).
  12. Code samples are encouraged, and should follow the general RST syntax:
  13. .. code-block:: Nim
  14. ## The ``universe`` module computes the answer to life, the universe, and everything.
  15. ##
  16. ## .. code-block:: Nim
  17. ## echo computeAnswerString() # "42"
  18. Within this top-level comment, you can indicate the authorship and copyright of the code, which will be featured in the produced documentation.
  19. .. code-block:: Nim
  20. ## This is the best module ever. It provides answers to everything!
  21. ##
  22. ## :Author: Steve McQueen
  23. ## :Copyright: 1965
  24. ##
  25. Leave a space between the last line of top-level documentation and the beginning of Nim code (the imports, etc.).
  26. Procs, Templates, Macros, Converters, and Iterators
  27. ---------------------------------------------------
  28. 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 double tick marks (``````).
  29. .. code-block:: Nim
  30. proc example1*(x: int) =
  31. ## Prints the value of ``x``.
  32. echo x
  33. Whenever an example of usage would be helpful to the user, you should include one within the documentation in RST format as below.
  34. .. code-block:: Nim
  35. proc addThree*(x, y, z: int8): int =
  36. ## Adds three ``int8`` values, treating them as unsigned and
  37. ## truncating the result.
  38. ##
  39. ## .. code-block:: nim
  40. ## echo addThree(3, 125, 6) # -122
  41. result = x +% y +% z
  42. The commands ``nim doc`` and ``nim doc2`` will then correctly syntax highlight the Nim code within the documentation.
  43. Types
  44. -----
  45. 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.
  46. .. code-block:: Nim
  47. type
  48. NamedQueue*[T] = object ## Provides a linked data structure with names
  49. ## throughout. It is named for convenience. I'm making
  50. ## this comment long to show how you can, too.
  51. name*: string ## The name of the item
  52. val*: T ## Its value
  53. next*: ref NamedQueue[T] ## The next item in the queue
  54. You have some flexibility when placing the documentation:
  55. .. code-block:: Nim
  56. type
  57. NamedQueue*[T] = object
  58. ## 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. Make sure to place the documentation beside or within the object.
  65. .. code-block:: Nim
  66. type
  67. ## This documentation disappears because it annotates the ``type`` keyword
  68. ## above, not ``NamedQueue``.
  69. NamedQueue*[T] = object
  70. name*: string ## This becomes the main documentation for the object, which
  71. ## is not what we want.
  72. val*: T ## Its value
  73. next*: ref NamedQueue[T] ## The next item in the queue
  74. Var, Let, and Const
  75. -------------------
  76. When declaring module-wide constants and values, documentation is encouraged. The placement of doc comments is similar to the ``type`` sections.
  77. .. code-block:: Nim
  78. const
  79. X* = 42 ## An awesome number.
  80. SpreadArray* = [
  81. [1,2,3],
  82. [2,3,1],
  83. [3,1,2],
  84. ] ## Doc comment for ``SpreadArray``.
  85. 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 (``#``).
  86. .. code-block:: Nim
  87. const
  88. BadMathVals* = [
  89. 3.14, # pi
  90. 2.72, # e
  91. 0.58, # gamma
  92. ] ## A bunch of badly rounded values.
  93. Nim supports Unicode in comments, so the above can be replaced with the following:
  94. .. code-block:: Nim
  95. const
  96. BadMathVals* = [
  97. 3.14, # π
  98. 2.72, # e
  99. 0.58, # γ
  100. ] ## A bunch of badly rounded values (including π!).