summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-doc-markdown-writer.h
blob: f4ca4de7fd9a3e0ba1b97f4cbff1ab6bfa65f0ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// slang-doc-markdown-writer.h
#ifndef SLANG_DOC_MARKDOWN_WRITER_H
#define SLANG_DOC_MARKDOWN_WRITER_H

#include "slang-ast-print.h"
#include "slang-compiler.h"
#include "slang-doc-ast.h"

namespace Slang
{

class ASTBuilder;

struct DocumentPage : public RefObject
{
    String title;
    String shortName;
    String path;
    String category;
    StringBuilder contentSB;
    Decl* decl = nullptr;
    bool skipWrite = false;
    DocumentPage* parentPage = nullptr;
    DocumentPage* findChildByShortName(const UnownedStringSlice& shortName);
    StringBuilder& get() { return contentSB; }
    List<RefPtr<DocumentPage>> children;

    // List of entries to document on this page.
    OrderedHashSet<ASTMarkup::Entry*> entries;
    ASTMarkup::Entry* getFirstEntry() { return *entries.begin(); }
    void writeToDisk();

    // Write summary on number of documented entries.
    void writeSummary(UnownedStringSlice fileName);
};

struct DocumentationConfig
{
    String preamble;
    String title;
    String libName;
    String rootDir;
    HashSet<String> visibleDeclNames;
    void parse(UnownedStringSlice configStr);
};

enum DocumentationSpanKind
{
    OrdinaryText,
    InlineCode,
};
struct ParsedDocumentationSpan
{
    String text;
    DocumentationSpanKind kind;
};
struct DocMarkdownWriter;

struct ParsedDescription
{
    String ownedText;
    List<ParsedDocumentationSpan> spans;
    void parse(UnownedStringSlice text);
    void write(DocMarkdownWriter* writer, Decl* decl, StringBuilder& out);
};

struct ParamDocumentation
{
    String name;
    String direction;
    ParsedDescription description;
};

enum class DocPageSection
{
    Description,
    Parameter,
    ReturnInfo,
    Remarks,
    Example,
    SeeAlso,
    InternalCallout,
    ExperimentalCallout,
    DeprecatedCallout,
};

struct DeclDocumentation
{
    Dictionary<String, ParamDocumentation> parameters;
    Dictionary<DocPageSection, ParsedDescription> sections;
    String categoryName;
    String categoryText;

    void parse(const UnownedStringSlice& text);
    void writeDescription(StringBuilder& out, DocMarkdownWriter* writer, Decl* decl);
    void writeGenericParameters(StringBuilder& out, DocMarkdownWriter* writer, Decl* decl);
    void writeSection(
        StringBuilder& sb,
        DocMarkdownWriter* writer,
        Decl* decl,
        DocPageSection section);
};

struct DocMarkdownWriter
{
    typedef ASTPrinter::Part Part;
    typedef ASTPrinter::PartPair PartPair;

    struct Signature
    {
        struct GenericParam
        {
            Part name;
            Part type;
        };

        Part returnType;
        List<PartPair> params;
        List<GenericParam> genericParams;
        Part name;
    };

    struct Requirement
    {
        typedef Requirement ThisType;

        bool operator==(const ThisType& rhs) const { return capabilitySet == rhs.capabilitySet; }
        SLANG_FORCE_INLINE bool operator!=(const ThisType& rhs) const
        {
            return !(capabilitySet == rhs.capabilitySet);
        }

        CapabilitySet capabilitySet;
    };

    /// Write out all documentation to the output buffer
    DocumentPage* writeAll(UnownedStringSlice configStr);
    String writeTOC();

    void ensureDeclPageCreated(ASTMarkup::Entry& entry);
    String translateToHTMLWithLinks(Decl* decl, String text);
    String translateToMarkdownWithLinks(String text, bool strictChildLookup = false);
    String escapeMarkdownText(String text);
    void generateSectionIndexPage(DocumentPage* page);
    void writePageRecursive(DocumentPage* page);
    void writePage(DocumentPage* page);

    /// This will write information about *all* of the overridden versions of a function/method
    void writeCallableOverridable(
        DocumentPage* page,
        const ASTMarkup::Entry& entry,
        CallableDecl* callable);

    void writeEnum(const ASTMarkup::Entry& entry, EnumDecl* enumDecl);
    void writeAggType(
        DocumentPage* page,
        const ASTMarkup::Entry& entry,
        AggTypeDeclBase* aggTypeDecl);
    void writeVar(const ASTMarkup::Entry& entry, VarDecl* varDecl);
    void writeProperty(const ASTMarkup::Entry& entry, PropertyDecl* propertyDecl);
    void writeTypeDef(const ASTMarkup::Entry& entry, TypeDefDecl* typeDefDecl);
    void writeAttribute(const ASTMarkup::Entry& entry, AttributeDecl* attributeDecl);

    void createPage(ASTMarkup::Entry& entry, Decl* decl);
    void registerCategory(DocumentPage* page, DeclDocumentation& doc);

    void writePreamble();

    void writeSignature(CallableDecl* callableDecl);
    void writeExtensionConditions(
        StringBuilder& sb,
        ExtensionDecl* decl,
        const char* prefix,
        bool isHtml);

    bool isVisible(const ASTMarkup::Entry& entry);
    bool isVisible(Decl* decl);
    bool isVisible(const Name* name);

    DocumentPage* findPageForToken(
        DocumentPage* currentPage,
        String token,
        String& outSectionName,
        Decl*& outDecl);
    String findLinkForToken(DocumentPage* currentPage, String token);

    /// Get the output string
    Dictionary<String, RefPtr<DocumentPage>>& getOutput() { return m_output; }

    DocumentPage* getPage(Decl* decl);
    StringBuilder* getBuilder(Decl* decl);

    /// Ctor.
    DocMarkdownWriter(ASTMarkup* markup, ASTBuilder* astBuilder, DiagnosticSink* sink)
        : m_markup(markup), m_astBuilder(astBuilder), m_sink(sink)
    {
    }

    struct StringListSet;

    /// Given a list of ASTPrinter::Parts, works out the different parts of the sig
    static void getSignature(const List<Part>& parts, Signature& outSig);

    struct NameAndText
    {
        Decl* decl = nullptr;
        String name;
        String text;
    };

    List<NameAndText> _getUniqueParams(const List<Decl*>& decls, DeclDocumentation* funcDoc);

    String _getName(Decl* decl);
    String _getFullName(Decl* decl);
    String _getDocFilePath(Decl* decl);
    String _getName(InheritanceDecl* decl);
    String _getName(ExtensionDecl* extDecl);

    NameAndText _getNameAndText(ASTMarkup::Entry* entry, Decl* decl);
    NameAndText _getNameAndText(Decl* decl);

    template<typename T>
    List<NameAndText> _getAsNameAndTextList(const FilteredMemberList<T>& in)
    {
        List<NameAndText> out;
        for (auto decl : const_cast<FilteredMemberList<T>&>(in))
        {
            out.add(_getNameAndText(decl));
        }
        return out;
    }
    template<typename T>
    List<String> _getAsStringList(const List<T*>& in)
    {
        List<String> strings;
        for (auto decl : in)
        {
            strings.add(_getName(decl));
        }
        return strings;
    }

    List<NameAndText> _getAsNameAndTextList(const List<Decl*>& in);
    List<String> _getAsStringList(const List<Decl*>& in);

    void _appendAsBullets(const List<NameAndText>& values, bool insertLinkForName, char wrapChar);
    void _appendAsBullets(const List<String>& values, char wrapChar);

    void _appendCommaList(const List<String>& strings, char wrapChar);

    void _appendRequirements(const DocMarkdownWriter::Requirement& requirements);
    void _maybeAppendRequirements(
        const UnownedStringSlice& title,
        const List<DocMarkdownWriter::Requirement>& uniqueRequirements);

    void _appendExpr(StringBuilder& sb, Expr* expr);

    /// Appends prefix and the list of types derived from
    void _appendDerivedFrom(const UnownedStringSlice& prefix, AggTypeDeclBase* aggTypeDecl);
    void _appendEscaped(const UnownedStringSlice& text);

    void _appendAggTypeName(const ASTMarkup::Entry& entry, Decl* aggTypeDecl);

    ASTMarkup* m_markup;
    ASTBuilder* m_astBuilder;
    DiagnosticSink* m_sink;
    StringBuilder* m_builder = nullptr;
    DocumentPage* m_currentPage = nullptr;
    Dictionary<String, RefPtr<DocumentPage>> m_output;
    RefPtr<DocumentPage> m_rootPage;
    RefPtr<DocumentPage> m_typesPage;
    RefPtr<DocumentPage> m_attributesPage;
    RefPtr<DocumentPage> m_interfacesPage;
    RefPtr<DocumentPage> m_globalDeclsPage;

    DocumentationConfig m_config;
    Dictionary<String, String> m_categories;
};

} // namespace Slang

#endif