Skip to content

Commit 6d54a00

Browse files
committed
applying updates based on David's feedback
1 parent 371ddad commit 6d54a00

File tree

3 files changed

+17
-21
lines changed

3 files changed

+17
-21
lines changed

Sources/SwiftDocC/Semantics/Snippets/Snippet.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public import Markdown
1313
import SymbolKit
1414

1515
public final class Snippet: Semantic, AutomaticDirectiveConvertible {
16-
public static let introducedVersion = "5.6"
16+
public static let introducedVersion = "5.7"
1717
public let originalMarkup: BlockDirective
1818

1919
/// The path components of a symbol link that would be used to resolve a reference to a snippet,

Sources/docc/DocCDocumentation.docc/Reference Syntax/API Reference Syntax/Snippet.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ Embeds a code example from the project's code snippets.
66
@Snippet(path: "my-package/Snippets/example-snippet", slice: "setup")
77
```
88

9-
- Parameters:
10-
- path: A reference to the location of a code example.
11-
- slice: The name of a section within the code example that you annotate with comments in the snippet. **(optional)**
12-
139
Place the `Snippet` directive to embed a code example from the project's snippet directory. The path to the snippet is identified with three parts:
1410

1511
1. The package name as defined in `Package.swift`

Sources/docc/DocCDocumentation.docc/adding-code-snippets-to-your-content.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22

33
@Metadata {
44
@Available("Swift", introduced: "5.7")
5+
@TitleHeading("Article")
56
}
67

78
Create and include code snippets to illustrate and provide examples of how to use your API.
89

910
## Overview
1011

12+
1113
DocC supports code listings in your code, as described in <doc:formatting-your-documentation-content>.
12-
However, the content in a code listing isn't compiled or verified to work.
14+
In addition to code listings written directly in the markup, Swift Package Manager and DocC supports compiler verified code examples called "snippets".
1315

1416
Swift Package Manager looks for, and builds, any code included in the `Snippets` directory for your package.
1517
DocC supports referencing all, or parts, of those files to present as code listings.
18+
In addition to snippets presenting your code examples, you can run snippets directly on the command line.
1619
This allows you to verify that code examples, referenced in your documentation, continue to compile as you evolve you app or library.
1720

1821
### Add the Swift DocC plugin
@@ -50,13 +53,12 @@ Your Swift package directory structure should resemble this:
5053

5154
```
5255
YourProject
53-
├─ Package.swift
54-
├─ Snippets/
55-
│ ├─ example-snippet.swift
56-
├─ Sources/
57-
│ ├─ YourProject.docc/
58-
│ │ ├─ YourProject.md
59-
│ ├─ main.swift
56+
├── Package.swift
57+
├── Snippets
58+
│   └── example-snippet.swift
59+
├── Sources
60+
│   └── YourProject
61+
│   └── YourProject.swift
6062
etc...
6163
```
6264

@@ -71,25 +73,24 @@ import Foundation
7173
print("Hello")
7274
```
7375

74-
Within your snippet, you can import your local module, as well as any module that your package depends on.
76+
Your snippets can import targets defined in your local package, as well as products from its direct dependencies.
77+
Each snippet is its own unit and can't access code from other snippet files.
7578

7679
Every time you build your project, the Swift Package Manager compiles any code snippets, and then fails if the build if they are unable to compile.
7780

7881
### Run the snippet
7982

80-
Each code example file you create becomes it's own module.
81-
The name of the code example file you create is the name of the module that Swift creates.
82-
Use the `swift run` command in a terminal to compile and run the module to verify it compiles does what you expect.
83+
You and consumers of your library can run your snippets from the command line using `swift run snippet-name` where "snippet-name" corresponds to a file name in your Snippets directory without the ".swift" file extension.
8384

8485
Run the earlier code example file named `example-snippet.swift` using the following command:
8586

8687
```bash
8788
swift run example-snippet
8889
```
8990

90-
### Reference the snippet
91+
### Embed the snippet
9192

92-
To reference your snippet in an article or within the symbol reference pages, use the `@Snippet` directive.
93+
To embed your snippet in an article or within the symbol reference pages, use the `@Snippet` directive.
9394
```markdown
9495
@Snippet(path: "my-package/Snippets/example-snippet")
9596
```
@@ -120,8 +121,7 @@ This paragraph appears before the snippet.
120121
This paragraph appears after the snippet.
121122
```
122123

123-
Without any additional annotations in your snippet, Docc includes the entirety of your code example as the snippet.
124-
To prevent parts of your snippet file from being rendered in documentation, add comments in your code in the format `// snippet.hide` and `// snippet.show` on new lines, surrounding the content you want to hide.
124+
If your snippet code requires setup — like imports or variable definitions — that distract from the snippet's main focus, you can add `// snippet.hide` and `// snippet.show` lines in the snippet code to exclude the lines in between from displaying in your documentation.
125125
These comments act as a toggle to hide or show content from the snippet.
126126

127127
```swift

0 commit comments

Comments
 (0)