Code Documentation
Tinymist will read the documentation from the source code and display it in the editor. For example, you can hover over a identifier to see its documentation, usually the content of the comments above the identifier's definition. The format of the documentation follows this guideline.
Note: the feature is not yet officially supported.
- ✓ Syntax of Docstring's Content: We have reached consensus on the syntax of content. It MUST be written in Typst.
-
? Annotations in Docstring's Content: We check the annotations in docstring by tidy style (< v0.4.0). It's not an official standard.
- Related Issue: Tidy #53: Long-term compatibility with
tinymist
tinymist
.
- Related Issue: Tidy #53: Long-term compatibility with
- ✗ Syntax of Docstring: We haven't reached consensus on the syntax of docstring. It's not clear whether we should distinguish the docstring from regular comments.
A docstring is an object in source code associating with some typst definition, whose content is the documentation information of the definition. Documentation is placed on consecutive special comments using three forward slashes ///
///
and an optional space. These are called doc comments.
While the DocCommentMatcher
DocCommentMatcher
matches doc comments in a looser way, we recommend using the strict syntax mentioned in the following sections.
The content MUST follow typst syntax instead of markdown syntax.
Explanation: The documentation of foo
foo
is "You can use typst markup in docstring."
The comments SHOULD be line comments starting with three forward slashes ///
///
and an optional space.
Explanation: There SHOULD be no documentation for foo
foo
in the three cases. The first comment is not a line comment, the second and the third one don't start with exact three forward slashes. However, the language server will regard them as doc comments loosely.
The comments SHOULD be consecutively and exactly placed aside the associating definition.
Explanation: The documentation of foo
foo
is "1\\n2"
"1\\n2"
.
Explanation: The documentation of bar
bar
is "2"
"2"
, because there is a space between /// 1
/// 1
and /// 2
/// 2
.
Explanation: There SHOULD be no documentation for baz
baz
, because the comments is not exactly placed before the let statement of the baz
baz
.
A module-level appears at the beginning of the module (file).
Given a file foo.typ
foo.typ
containing code:
Explanation: The documentation of the module foo
foo
(foo.typ
foo.typ
) is "1"
"1"
. It is not "1\n2"
"1\n2"
, because there is a space between /// 1
/// 1
and /// 2
/// 2
.
Given a file foo.typ
foo.typ
containing code:
Explanation: The documentation of the module foo
foo
(foo.typ
foo.typ
) is "1"
"1"
. It is not "License: Apache 2.0\n1"
"License: Apache 2.0\n1"
, because // License: Apache 2.0
// License: Apache 2.0
is not a strict doc comment.
A variable appears exactly before some let statement (the ast starting with #let
#let
or let
let
). BNF Syntax:
You can use an arrow ->
->
following a type annotation to mark the type of the initializer expression of the let statement. The initializer expression is the expression at the right side of the equal marker in the let statement. BNF Syntax:
Explanation: The docstring tells that the type of { /* code */ }
{ /* code */ }
is int
int
. Thus, the resultant type of the function f
f
is also annotated as int
int
.
Explanation: The docstring tells that the type of { /* code */ }
{ /* code */ }
is float
float
. Thus, the type of the variable G
G
is also annotated as float
float
.
You can use a list item - name (type): description
- name (type): description
to document the related variable at the left side of the let statement. BNF Syntax:
Explanation: The docstring tells that the type of x
x
is int
int
and the documentation of x
x
is "The input of the function f
f
."
Explanation: The docstring tells that the type of x
x
at the left side is any
any
and its documentation is "The swapped value from y
y
." The variables at the right side of the let statement are not documented by the docstring.
You can use #example
#example
function to provide examples in docstrings.
The docstring tells that there is an associated example in the docstring. It will be rendered as a code block following the rendered result when possible:
A type annotation is a comma separated list containing types. BNF Syntax:
Currently, only built-in types and the generic array type are supported in docstrings.
The list of built-in types:
any
any
content
content
none
none
auto
auto
bool
bool
orboolean
boolean
false
false
true
true
int
int
orinteger
integer
float
float
length
length
angle
angle
ratio
ratio
relative
relative
fraction
fraction
str
str
orstring
string
color
color
gradient
gradient
pattern
pattern
symbol
symbol
version
version
bytes
bytes
label
label
datetime
datetime
duration
duration
styles
styles
array
array
dictionary
dictionary
function
function
arguments
arguments
type
type
module
module
plugin
plugin