Skip to content

Commit 0288354

Browse files
committed
Updated docs about tags
1 parent 3758801 commit 0288354

File tree

2 files changed

+188
-15
lines changed

2 files changed

+188
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
---
2+
title: Scaladoc docstrings - specific Tags and Features
3+
---
4+
5+
# {{page.title}}
6+
7+
This chapater describes how to write correctly docstrings and how to use all available features of scaladoc.
8+
Since many things are the same as in the old scaladoc, some parts are reused from this [article](https://docs.scala-lang.org/overviews/scaladoc/for-library-authors.html)
9+
10+
11+
Scaladoc extends Markdown with additional features, such as linking
12+
to API definitions. This can be used from within static documentation and blog
13+
posts to provide blend-in content.
14+
15+
16+
## Where to put scaladoc
17+
18+
Scaladoc comments go before the items they pertain to in a special comment block that starts with a /** and ends with a */, like this:
19+
20+
```scala
21+
/** Start the comment here
22+
* and use the left star followed by a
23+
* white space on every line.
24+
*
25+
* Even on empty paragraph-break lines.
26+
*
27+
* Note that the * on each line is aligned
28+
* with the second * in /** so that the
29+
* left margin is on the same column on the
30+
* first line and on subsequent ones.
31+
*
32+
* The closing Scaladoc tag goes on its own,
33+
* separate line. E.g.
34+
*
35+
* Calculate the square of the given number
36+
*
37+
* @param d the Double to square
38+
* @return the result of squaring d
39+
*/
40+
def square(d: Double): Double = d * d
41+
```
42+
43+
In the example above, this Scaladoc comment is associated with the method square since it is right before it in the source code.
44+
45+
Scaladoc comments can go before fields, methods, classes, traits, objects and even (especially) package objects. Scaladoc comments for package objects make a great place to put an overview of a specific package or API.
46+
47+
For class primary constructors which in Scala coincide with the definition of the class itself, a @constructor tag is used to target a comment to be put on the primary constructor documentation rather than the class overview.
48+
49+
## Tags
50+
51+
Scaladoc uses `@<tagname>` tags to provide specific detail fields in the comments. These include:
52+
53+
### Class specific tags
54+
55+
- `@constructor` placed in the class comment will describe the primary constructor.
56+
57+
### Method specific tags
58+
59+
- `@return` detail the return value from a method (one per method).
60+
61+
### Method, Constructor and/or Class tags
62+
63+
- `@throws` what exceptions (if any) the method or constructor may throw.
64+
- `@param` detail a value parameter for a method or constructor, provide one per parameter to the method/constructor.
65+
- `@tparam` detail a type parameter for a method, constructor or class. Provide one per type parameter.
66+
67+
### Usage tags
68+
69+
- `@see` reference other sources of information like external document links or related entities in the documentation.
70+
- `@note` add a note for pre or post conditions, or any other notable restrictions or expectations.
71+
- `@example` for providing example code or related example documentation.
72+
- `@usecase` provide a simplified method definition for when the full method definition is too complex or noisy. An example is (in the collections API), providing documentation for methods that omit the implicit canBuildFrom.
73+
74+
### Member grouping tags
75+
76+
These tags are well-suited to larger types or packages, with many members. They allow you to organize the Scaladoc page into distinct sections, with each one shown separately, in the order that you choose.
77+
78+
These tags are not enabled by default! You must pass the -groups flag to Scaladoc in order to turn them on. Typically the sbt for this will look something like:
79+
80+
```scala
81+
scalacOptions in (Compile, doc) ++= Seq(
82+
"-groups"
83+
)
84+
```
85+
Each section should have a single-word identifier that is used in all of these tags, shown as <group> below. By default, that identifier is shown as the title of that documentation section, but you can use @groupname to provide a longer title.
86+
87+
Typically, you should put @groupprio (and optionally @groupname and @groupdesc) in the Scaladoc for the package/trait/class/object itself, describing what all the groups are, and their order. Then put @group in the Scaladoc for each member, saying which group it is in.
88+
89+
Members that do not have a `@group` tag will be listed as “Ungrouped” in the resulting documentation.
90+
91+
- `@group <group>` - mark the entity as a member of the `<group>` group.
92+
- `@groupname <group> <name>` - provide an optional name for the group. `<name>` is displayed as the group header before the group description.
93+
- `@groupdesc <group> <description>` - add optional descriptive text to display under the group name. Supports multiline formatted text.
94+
- `@groupprio <group> <priority>` - control the order of the group on the page. Defaults to 0. Ungrouped elements have an implicit priority of 1000. Use a value between 0 and 999 to set a relative position to other groups. Low values will appear before high values.
95+
96+
### Other tags
97+
98+
- `@author` provide author information for the following entity
99+
- `@version` the version of the system or API that this entity is a part of.
100+
- `@since` like `@version` but defines the system or API that this entity was first defined in.
101+
- `@deprecated` marks the entity as deprecated, providing both the replacement implementation that should be used and the version/date at which this entity was deprecated.
102+
- `@syntax <name>` let you change the parser for docstring. The default syntax is markdown, however you can change it using this directive. Currently available syntaxes are `markdown` or `wiki`, e. g. `@syntax wiki`
103+
104+
### Macros
105+
106+
- `@define <name> <definition>` allows use of $name in other Scaladoc comments within the same source file which will be expanded to the contents of `<definition>`.
107+
108+
If a comment is not provided for an entity at the current inheritance level, but is supplied for the overridden entity at a higher level in the inheritance hierarchy, the comment from the super-class will be used.
109+
110+
Likewise if `@param`, `@tparam`, `@return` and other entity tags are omitted but available from a superclass, those comments will be used.
111+
Explicit
112+
113+
For explicit comment inheritance, use the @inheritdoc tag.
114+
Markup
115+
116+
It is still possible to embed HTML tags in Scaladoc (like with Javadoc), but not necessary most of the time as markup may be used instead.
117+
118+
### Markup
119+
120+
Scaladoc provides two syntax parsers: `markdown` (default) or `wikidoc`.
121+
122+
#### Markdown
123+
124+
Markdown uses commonmark flavour to parse docstring, however it can understand `wikidoc` links.
125+
For more info about wiki links look at this [chapter](#linking-to-api)
126+
127+
#### Wikidoc
128+
129+
Wikidoc is syntax used for scala2 scaladoc. It is supported because of many existing source code, however it is unrecommended to use it in new projects.
130+
Wikisyntax can be toggled on with flag `-comment-syntax wiki` globally, or with `@syntax wiki` directive in docstring.
131+
132+
Some of the standard markup available:
133+
134+
```
135+
`monospace`
136+
''italic text''
137+
'''bold text'''
138+
__underline__
139+
^superscript^
140+
,,subscript,,
141+
[[entity link]], e.g. [[scala.collection.Seq]]
142+
[[https://external.link External Link]], e.g. [[https://scala-lang.org Scala Language Site]]
143+
```
144+
145+
Other formatting notes
146+
147+
- Paragraphs are started with one (or more) blank lines. `*` in the margin for the comment is valid (and should be included) but the line should be blank otherwise.
148+
- Headings are defined with surrounding `=` characters, with more `=` denoting subheadings. E.g. `=Heading=`, `==Sub-Heading==`, etc.
149+
- List blocks are a sequence of list items with the same style and level, with no interruptions from other block styles. Unordered lists can be bulleted using `-`, numbered lists can be denoted using `1.`, `i.`, `I.`, or `a.` for the various numbering styles. In both cases, you must have extra space in front, and more space makes a sub-level.
150+
151+
The markup for list blocks looks like:
152+
153+
```
154+
/** Here is an unordered list:
155+
*
156+
* - First item
157+
* - Second item
158+
* - Sub-item to the second
159+
* - Another sub-item
160+
* - Third item
161+
*
162+
* Here is an ordered list:
163+
*
164+
* 1. First numbered item
165+
* 1. Second numbered item
166+
* i. Sub-item to the second
167+
* i. Another sub-item
168+
* 1. Third item
169+
*/
170+
```
171+
172+
### General Notes for Writing Scaladoc Comments
173+
174+
Concise is nice! Get to the point quickly, people have limited time to spend on your documentation, use it wisely.
175+
Omit unnecessary words. Prefer returns X rather than this method returns X, and does X,Y & Z rather than this method does X, Y and Z.
176+
DRY - don’t repeat yourself. Resist duplicating the method description in the @return tag and other forms of repetitive commenting.
177+
178+
More details on writing Scaladoc
179+
180+
Further information on the formatting and style recommendations can be found in Scala-lang scaladoc style guide.
181+
182+
183+
184+
## Linking to API
185+
186+
Scaladoc allows linking to API documentation with Wiki-style links. Linking to
187+
`scala.collection.immutable.List` is as simple as
188+
`[[scala.collection.immutable.List]]`. For more information on the exact syntax, see [doc comment documentation](./docComments.html#definition-links).

docs/docs/usage/scaladoc/specificTags.md

-15
This file was deleted.

0 commit comments

Comments
 (0)