Skip to content

Commit 2cf5ac1

Browse files
author
Jesse Haigh
committed
add more tests, remove docs, code cleanup
1 parent 3ae43b1 commit 2cf5ac1

File tree

5 files changed

+49
-34
lines changed

5 files changed

+49
-34
lines changed

Sources/SwiftDocC/Model/Rendering/Content/RenderBlockContent.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -721,10 +721,7 @@ extension RenderBlockContent: Codable {
721721
}
722722
self = try .aside(.init(style: style, content: container.decode([RenderBlockContent].self, forKey: .content)))
723723
case .codeListing:
724-
var copy = false
725-
if FeatureFlags.current.isExperimentalCodeBlockEnabled {
726-
copy = true
727-
}
724+
let copy = FeatureFlags.current.isExperimentalCodeBlockEnabled
728725
self = try .codeListing(.init(
729726
syntax: container.decodeIfPresent(String.self, forKey: .syntax),
730727
code: container.decode([String].self, forKey: .code),

Sources/SwiftDocC/Semantics/Snippets/Snippet.swift

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,12 @@ extension Snippet: RenderableDirectiveConvertible {
6868
let lines = snippetMixin.lines[lineRange]
6969
let minimumIndentation = lines.map { $0.prefix { $0.isWhitespace }.count }.min() ?? 0
7070
let trimmedLines = lines.map { String($0.dropFirst(minimumIndentation)) }
71-
var copy = false
72-
if FeatureFlags.current.isExperimentalCodeBlockEnabled {
73-
copy = true
74-
}
71+
let copy = FeatureFlags.current.isExperimentalCodeBlockEnabled
7572
return [RenderBlockContent.codeListing(.init(syntax: snippetMixin.language, code: trimmedLines, metadata: nil, copyToClipboard: copy))]
7673
} else {
7774
// Render the whole snippet with its explanation content.
7875
let docCommentContent = snippetEntity.markup.children.flatMap { contentCompiler.visit($0) }
79-
var copy = false
80-
if FeatureFlags.current.isExperimentalCodeBlockEnabled {
81-
copy = true
82-
}
76+
let copy = FeatureFlags.current.isExperimentalCodeBlockEnabled
8377
let code = RenderBlockContent.codeListing(.init(syntax: snippetMixin.language, code: snippetMixin.lines, metadata: nil, copyToClipboard: copy))
8478
return docCommentContent + [code]
8579
}

Sources/SwiftDocCUtilities/ArgumentParsing/Subcommands/Convert.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ extension Docc {
478478

479479
@Flag(
480480
name: .customLong("enable-experimental-code-block"),
481-
help: "Annotate code blocks with additional metadata to support copy-to-clipboard, highlighting, and wrapping on code blocks."
481+
help: "Support copy-to-clipboard for code blocks."
482482
)
483483
var enableExperimentalCodeBlock = false
484484

Sources/docc/DocCDocumentation.docc/formatting-your-documentation-content.md

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -143,27 +143,6 @@ add a new line and terminate the code listing by adding another three backticks:
143143
instead of tabs so that DocC preserves the indentation when compiling your
144144
documentation.
145145

146-
#### Formatting Code Listings
147-
148-
A copy-to-clipboard button is added to code listings by default behind the
149-
feature flag `--enable-experimental-code-block`.
150-
This renders a copy button in the top-right cotner of the code listing in
151-
generated documentation. When clicked, it copies the contents of the code
152-
block to the clipboard.
153-
154-
If you don't want a code block to have a copy-to-clipboard button, you can
155-
include the `nocopy` option after the name of the programming language to
156-
disable it for that code listing:
157-
158-
```swift, nocopy
159-
struct Sightseeing: Activity {
160-
func perform(with sloth: inout Sloth) -> Speed {
161-
sloth.energyLevel -= 10
162-
return .slow
163-
}
164-
}
165-
```
166-
167146
DocC uses the programming language you specify to apply the correct syntax
168147
color formatting. For the example above, DocC generates the following:
169148

Tests/SwiftDocCTests/Rendering/RenderContentCompilerTests.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,49 @@ class RenderContentCompilerTests: XCTestCase {
271271

272272
XCTAssertEqual(codeListing.copyToClipboard, false)
273273
}
274+
275+
func testCopyToClipboardNoFeatureFlag() async throws {
276+
let (bundle, context) = try await testBundleAndContext()
277+
var compiler = RenderContentCompiler(context: context, bundle: bundle, identifier: ResolvedTopicReference(bundleID: bundle.id, path: "/path", fragment: nil, sourceLanguage: .swift))
278+
279+
let source = #"""
280+
```swift
281+
let x = 1
282+
```
283+
"""#
284+
let document = Document(parsing: source)
285+
286+
let result = document.children.flatMap { compiler.visit($0) }
287+
288+
let renderCodeBlock = try XCTUnwrap(result[0] as? RenderBlockContent)
289+
guard case let .codeListing(codeListing) = renderCodeBlock else {
290+
XCTFail("Expected RenderBlockContent.codeListing")
291+
return
292+
}
293+
294+
XCTAssertEqual(codeListing.copyToClipboard, false)
295+
}
296+
297+
func testNoCopyToClipboardNoFeatureFlag() async throws {
298+
let (bundle, context) = try await testBundleAndContext()
299+
var compiler = RenderContentCompiler(context: context, bundle: bundle, identifier: ResolvedTopicReference(bundleID: bundle.id, path: "/path", fragment: nil, sourceLanguage: .swift))
300+
301+
let source = #"""
302+
```swift, nocopy
303+
let x = 1
304+
```
305+
"""#
306+
let document = Document(parsing: source)
307+
308+
let result = document.children.flatMap { compiler.visit($0) }
309+
310+
let renderCodeBlock = try XCTUnwrap(result[0] as? RenderBlockContent)
311+
guard case let .codeListing(codeListing) = renderCodeBlock else {
312+
XCTFail("Expected RenderBlockContent.codeListing")
313+
return
314+
}
315+
316+
XCTAssertEqual(codeListing.syntax, "swift, nocopy")
317+
XCTAssertEqual(codeListing.copyToClipboard, false)
318+
}
274319
}

0 commit comments

Comments
 (0)