Skip to content

Commit 55aacd6

Browse files
authored
Merge pull request github#12137 from github/redsun82/swift-doc-for-schema-doc
Swift: add documentation for generated documentation
2 parents d55e9d5 + 542c921 commit 55aacd6

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

swift/schema.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* no flow control
66
* no aliases
77
* only class definitions with annotations and `include` calls
8+
9+
For how documentation of generated QL code works, please read schema_documentation.md.
810
"""
911

1012
from swift.codegen.lib.schema.defs import *

swift/schema_documentation.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Documenting `schema.py` entities
2+
3+
## Classes
4+
5+
Classes can be documented with plain python docstrings, for example
6+
7+
```
8+
class ErrorElement(Locatable):
9+
'''The superclass of all elements indicating some kind of error.'''
10+
pass
11+
```
12+
13+
This gets copied verbatim as QL doc comments for the class (with some internal handling for preservation of indentation,
14+
as explained in https://peps.python.org/pep-0257/#handling-docstring-indentation).
15+
16+
## Properties
17+
18+
Properties by default get a generated doc comment created from the name of the property and the enclosing class. So for
19+
example property `name` in class `File` will get documented as
20+
21+
```
22+
/**
23+
* Gets the name of this file.
24+
*/
25+
```
26+
27+
This documentation generation will expand common abbreviations. The list of expanded abbreviations can be found
28+
in [`codegen/generators/qlgen.py`](./codegen/generators/qlgen.py) as a dictionary under the `abbreviations` variable.
29+
30+
The `name of this file` part in the example above can be customized by appending `| doc("<replacement>")` to the
31+
property specification, for example
32+
33+
```
34+
class Locatable(Element):
35+
location: optional[Location] | doc("location associated with this element in the code")
36+
```
37+
38+
When keeping the default documentation header, the name used for the class (for example `file` above) can be customized
39+
at the class level by applying to the class the `@ql.default_doc_name("<replacement>")` decorator, for example
40+
41+
```
42+
@ql.default_doc_name("function type")
43+
class AnyFunctionType(Type):
44+
...
45+
```
46+
47+
Additionally, a description can be given which will be added after the documentation header
48+
using `| desc("<description>")`. For example
49+
50+
```
51+
class PoundDiagnosticDecl(Decl):
52+
kind: int | desc("This is 1 for `#error` and 2 for `#warning`.")
53+
```
54+
55+
will result in
56+
57+
```
58+
/**
59+
* Gets the kind of this pound diagnostic declaration.
60+
*
61+
* This is 1 for `#error` and 2 for `#warning`.
62+
*/
63+
```
64+
65+
### Plural/singular
66+
67+
Notice that for repeated properties both the plural and the singular forms will be present in documentation. What term
68+
is taken to be pluralized/singularized depends on customization:
69+
70+
* for auto-generated documentation headers, the _last_ word of the property name will be taken;
71+
* for headers overridden with `doc("<override>")`, the _first_ word of the override will be taken.
72+
73+
So for example:
74+
75+
```
76+
generic_type_params: list[GenericTypeParamDecl]
77+
-> generic type parameter/generic type parameters of this generic context
78+
arguments: list[Argument] | doc("arguments passed to the applied function")
79+
-> argument/arguments passed to the applied function
80+
```
81+
82+
If this behaviour is not wanted, this can be overridden by enclosing the term to be pluralized/singularized in `{ }`
83+
within `doc`. So for example
84+
85+
```
86+
class Foo:
87+
names_of_the_things: list[string] | doc("{names} of the things in this foo")
88+
silly_cats_or_dogs: list[Animal] | doc("silly {cats} or {dogs} in this foo")
89+
```
90+
91+
## Predicates
92+
93+
Similarly as properties, predicates get by default an automatically generated doc comment from the class name and the
94+
predicate name. For example
95+
96+
```
97+
class ImportDecl(Decl):
98+
is_exported: predicate
99+
```
100+
101+
will generate the doc
102+
103+
```
104+
/**
105+
* Holds if this import declaration is exported.
106+
*/
107+
```
108+
109+
And similarly to properties, one can:
110+
111+
* customize everything that comes strictly after `if` with `| doc("<replacement>")`;
112+
* customize the default name for used for the class (`import declaration` in the example above) with
113+
the `@ql.default_doc_name("<replacement>")` class decorator;
114+
* add a more in-depth description with `| desc("<description>")`.

0 commit comments

Comments
 (0)