Approach for including external source content in DocC (snippets beyond Swift) — #1436

Adding to what Joe already said;
DocC knows very little about the snippets; it doesn't inspect what language the snippets are in or where their original source files are located. Instead all of that information comes from another tool that packages up all the information that DocC needs as "symbol" elements in a symbol graph file (separate from the symbol graph file that describes the framework's API).

For Swift snippets that happens in a tool called snippet-extract that's built together with the Swift-DocC Plugin. That's also where the Swift snippet parsing and the transformation into symbol graph "symbol" elements happens.

If someone created one of these snippet symbol graph files representing code in C or C++ and passed that to docc convert, DocC should be able to resolve the snippet paths, match the names slices and extract only those lines for display on the page.

The primary problem then becomes making the tool that orchestrates the build call the tool that creates those snippet symbol graph files and pass those files as input to the docc convert call that the build orchestration tool makes. For Swift snippets, that documentation build orchestration is done by the Swift-DocC Plugin. It is the tool responsible for calling snippet-extract, calling other tools to extract symbol graph files representing a certain target's API, and gathering those files and other inputs to determine what flags to pass to docc.


If the workflow we're talking about is swift package generate-documentation, then the Swift-DocC plugin is the tool that orchestrates that documentation build (pending any changes from [Pitch] Documentation generation for any SwiftPM package). If you want that workflow to call a tool that extracts snippets for more languages then you would either need to:

  • make a new tool and have the DocC Plugin check that it's available and then call it
  • update the existing snippet-extract tool to support parsing the snippet slices for more languages (potentially with different code comment syntax)

Currently, the Swift-DocC Plugin is also what's responsible for finding the snippet files in the "/Snippets" directory. If we wanted to allow referencing certain files in for example "/Sources" as well, then the configuration about which source files to extract snippets for would need to be some configuration that's aimed at the plugin (for example a command line option). The DocC Plugin is also what would be responsible for validating that the referenced files exist and constrain which file paths are allowed to be referenced.

Both of these pieces—what source languages of snippets can be extracted and new configuration to support extracting snippets from a different location—can be implemented rather independently but it might still be valuable having a high-level design discussion about which tools are responsible for which behaviors—especially with regards to future extensibility.

2 Likes