Skip to content

Commit 538c4b0

Browse files
committed
Various suggestions regarding syntax
1 parent 25c2881 commit 538c4b0

File tree

2 files changed

+212
-37
lines changed

2 files changed

+212
-37
lines changed

src/chapters/api/documentation.typ

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ An item may be a state variable, a counter, a function, a reusable piece of regu
99
For the exact syntax used for documentation comments, refer to @sec:style:docs.
1010
]
1111

12-
For functions a public API should always document all public arguments, their usage and purpose as well as constraints on their values.
12+
For public functions the documentation should always incldue all public parameters, their usage and purpose as well as possible constraints on their values.
1313
Internal arguments may be ommited, if they are not meant to be exposed to a user, but should still be documented for contributors in the source or a contribution document.
1414
Depending on the doc aprser in use, named arguments may need their defaults explicitly stated and should have if the doc parser can't parse them automatically.
1515
If functions require or return contextual values, this should also be communicated clearly.

src/chapters/style/documentation.typ

+211-36
Original file line numberDiff line numberDiff line change
@@ -6,58 +6,233 @@
66
This section documents the syntax of documentation commens, see @sec:api:docs for the contents and purpose of documentation comments.
77
]
88

9-
Documentation for items are placed on comments right before the item, with three forward slashes `///` and a leading space.
10-
The first leading space and comment tokens are stripped of the line and each line is joined to make up the body of the doc comment.
11-
The content of doc comments should be simple Typst markup with some extra documentation specific syntax and assumptions about structure.
12-
Doc comments do not use any other strucutre at this moment, regular comments `//` and `/**/` are ignored by most doc parsers.
9+
Documentation is placed on special comments using three forward slashes `///` and an optional space.
10+
These are called doc comments.
11+
While the leading space is optional, it is encouraged as it makes the documentaiton easier to read.
12+
Doc comments may not be interrupted by empty lines, markup, or regular comments.
1313

1414
#do-dont[
1515
```typst
16-
/// This is a doc comment
17-
#let item = ...
16+
/// A1
17+
/// A2
18+
/// A3
19+
20+
// discuraged, but valid
21+
///B1
22+
///B2
23+
///B3
1824
```
1925
][
2026
```typst
27+
/// A1
28+
//
29+
/// B1
30+
31+
/// A1
32+
33+
/// B1
34+
35+
/// C1
36+
Hello World
37+
/// D1
38+
2139
/**
22-
* This is not a doc comment, it's a regular comment.
40+
* This is a regular comment, not a doc comment.
2341
*/
24-
#let item = ...
42+
#let item = { ... }
2543
26-
// This is not a doc comment, it's a regular comment.
27-
#let item = ...
44+
// This is a regular comment, not a doc comment.
45+
#let item = { ... }
2846
```
2947
]
3048

31-
Doc comments are split into two parts:
32-
- the general description, including examples and package specific sections and the
33-
- optional trailer containing semantic information about the annotated item.
49+
There are two kinds of documentation comments, inner and outer doc comments.
50+
Outer doc comments are placed right above the declaration they are attached to.
3451

35-
Doc comments may include special syntax such as `@@val` or `@@func()`, these are used by doc generators like #mty.package[Tidy] to generate cross references in documentation.
52+
#do-dont[
53+
```typst
54+
/// Documentation for func
55+
#let func() = { ... }
3656
37-
#wbox[
38-
There seems to be no special syntax to refer to things other than functions and values at this moment.
57+
```
58+
][
59+
```typst
60+
/// Stray doc comment, not the doc for func
61+
62+
#let func() = {
63+
/// Stray doc comment, not the doc for func
64+
...
65+
}
66+
67+
/// Stray doc comment, not the doc for func
68+
```
69+
]
70+
71+
Inner doc comments are used to document modules and must not have a declaration, instead they refer to the file they are placed in and may only be declared once and as the first non comment item in a file.
72+
73+
#do-dont[
74+
```typst
75+
// optional leading comments
76+
77+
/// Module doc
78+
79+
/// Function or value doc
80+
#let item = { ... }
81+
```
82+
][
83+
```typst
84+
/// Function or valtion doc
85+
#let item = { ... }
86+
87+
/// Stray doc comment, not the module or func doc
88+
```
89+
]
90+
91+
Outer doc comments may be used on `let` bindings only.
92+
93+
#do-dont[
94+
```typst
95+
/// Function doc
96+
#let func() = { ... }
97+
98+
/// Value doc
99+
#let value = { ... }
100+
```
101+
][
102+
```typst
103+
/// Stray doc comment, markup may not be documented
104+
Hello World
105+
106+
/// Stray doc comment, imports my not be documented
107+
#import "module.typ"
108+
109+
/// Stray doc comment, scopes may not be documented
110+
#[
111+
...
112+
]
113+
```
39114
]
40115

116+
Doc comments contain a description of the documented item itself, as well as an optional semantic trailer.
117+
The content of descriptions should generally be simple Typst markup and should not contain any scripting, (i.e. no loops, conditionals or function calls, except for `#link("...")[...]`), this allows the documentation to be turned into Markdown or plaintext or LSP to send to editors.
118+
41119
== Description
42-
The general description makes no assumptions on syntax other than including the aforementioned cross-reference syntax.
43-
It may be any Typst syntax, but should ideally kept simple to allow LSPs to convert it into Markdown for editor hover actions.
120+
As mentioned before, the description should be simple, containing mostly markup and no scripting.
44121

45122
== Semantic Trailer
46-
The semantic trailer of doc comments is the last section of the doc comments containing optional information about the documented item.
47-
This may include a list of parameter descriptions, a return type annotation, and other annotations, all of which are not treated as regular Typst syntax.
48-
49-
At this moment #mty.package[Tidy] accepts the following syntax:
50-
```ebnf
51-
param-name ::= typst-ident
52-
type-name ::= typst-ident
53-
type ::= ( '..' )? type-name
54-
type-list ::= '(' type ( ',' type )+ ')'
55-
description ::= typst-markup
56-
57-
param-documentation ::= '/// - ' param-name ' ' type-list ':' [description]
58-
return-type ::= '/// -> ' type-name
59-
```
60-
61-
As well as some other more specific ones like doc tests.
62-
We shall only consider this subset for now.
63-
The trailer may contain zero or more lines of `param-documentation` and an optional `return-type` line.
123+
The semantic trailer is fully optional, starts with an empty doc comment line to separate it from the description, and may contain:
124+
- multiple `term` items for parameter type hints and descriptions,
125+
- a return type hint `-> type`,
126+
- and multiple property annotations (`#property("private")` or `#property("deprecated")`).
127+
128+
Types in type lists or return type annotations may be separated by `|` to indicate that more than one type is accepted, the exact types allowed depend on the doc parser, but the built in types are generally supported.
129+
130+
Parameter description and return types (if present) are placed tightly together, property annotations if present are separated using another empty doc comment line.
131+
132+
Parameter documentation is created by writing a term item containing the parameter name, a mandatory type list in parenthesis and optional description.
133+
If the parameter is an argument sink it's name must also containe the spread operator `..`.
134+
Each parameter can only be documented once, but doesn't have to, undocumented parameters are considered private.
135+
136+
#do-dont[
137+
```typst
138+
/// Function description
139+
///
140+
/// / b (any): b descpription
141+
/// / a (int | float): a description
142+
#let func(a, b, c) = { ... }
143+
144+
/// Function description
145+
///
146+
/// / ..args (int): args description
147+
#let func(..args) = { ... }
148+
```
149+
][
150+
```typst
151+
/// Function description
152+
///
153+
/// / c (any): c doesn't exist
154+
/// / a (int | float): a description
155+
#let func(a, b) = { ... }
156+
157+
/// Missing empty line between function description and trailer
158+
/// / b (any): b descpription
159+
/// / a (int | float): a description
160+
#let func(a, b) = { ... }
161+
162+
163+
/// Function description
164+
///
165+
/// / b (any): b descpription
166+
/// / a: missing types
167+
#let func(a, b) = { ... }
168+
169+
/// Function description
170+
///
171+
/// / args (int): missing spread operator for args
172+
#let func(..args) = { ... }
173+
```
174+
]
175+
176+
The return type can only be annotated once, on a single line after all parameters if any exist.
177+
For non function types the return type annotation can be used as a normal type annotation.
178+
179+
#do-dont[
180+
```typst
181+
/// Function doc
182+
///
183+
/// / arg (types): Description for arg
184+
/// -> types
185+
#let func(arg) = { ... }
186+
187+
/// Value doc
188+
///
189+
/// -> type
190+
#let value = { ... }
191+
```
192+
][
193+
```typst
194+
/// Missing empty line between description trailer
195+
///
196+
/// -> type
197+
/// / arg (types): arg descrption after return type
198+
#let func(arg) = { ... }
199+
```
200+
]
201+
202+
Property annotations can be used to document package specific or otherwise important information like deprecation status, visibility or contextuality and may only be used after the return type (if one exists) and an empty doc comment line.
203+
204+
#do-dont[
205+
```typst
206+
/// Function doc
207+
///
208+
/// / arg (types): Description for arg
209+
/// -> types
210+
///
211+
/// #property("deprecated")
212+
/// #property("contextual")
213+
#let func(arg) = { ... }
214+
215+
/// Value doc
216+
///
217+
/// #property("contextual")
218+
#let value = { ... }
219+
```
220+
][
221+
```typst
222+
/// Missing empty line between return type and properties
223+
///
224+
/// / arg (types): Description for arg
225+
/// -> types
226+
/// #property("deprecated")
227+
/// #property("contextual")
228+
#let func(arg) = { ... }
229+
230+
/// Return type after properties and missing empty separation lines
231+
///
232+
/// / arg (types): Description for arg
233+
/// #property("deprecated")
234+
/// #property("contextual")
235+
/// -> types
236+
#let func(arg) = { ... }
237+
```
238+
]

0 commit comments

Comments
 (0)