Skip to content

Commit c073174

Browse files
author
Jesse Haigh
committed
WIP add copy button to CodeListing
1 parent 1a01b54 commit c073174

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,14 @@ public enum RenderBlockContent: Equatable {
124124
public var code: [String]
125125
/// Additional metadata for this code block.
126126
public var metadata: RenderContentMetadata?
127+
public var copyToClipboard: Bool = false
127128

128129
/// Make a new `CodeListing` with the given data.
129-
public init(syntax: String?, code: [String], metadata: RenderContentMetadata?) {
130+
public init(syntax: String?, code: [String], metadata: RenderContentMetadata?, copyToClipboard: Bool) {
130131
self.syntax = syntax
131132
self.code = code
132133
self.metadata = metadata
134+
self.copyToClipboard = copyToClipboard
133135
}
134136
}
135137

@@ -697,7 +699,7 @@ extension RenderBlockContent.Table: Codable {
697699
extension RenderBlockContent: Codable {
698700
private enum CodingKeys: CodingKey {
699701
case type
700-
case inlineContent, content, caption, style, name, syntax, code, level, text, items, media, runtimePreview, anchor, summary, example, metadata, start
702+
case inlineContent, content, caption, style, name, syntax, code, level, text, items, media, runtimePreview, anchor, summary, example, metadata, start, copyToClipboard
701703
case request, response
702704
case header, rows
703705
case numberOfColumns, columns
@@ -722,7 +724,8 @@ extension RenderBlockContent: Codable {
722724
self = try .codeListing(.init(
723725
syntax: container.decodeIfPresent(String.self, forKey: .syntax),
724726
code: container.decode([String].self, forKey: .code),
725-
metadata: container.decodeIfPresent(RenderContentMetadata.self, forKey: .metadata)
727+
metadata: container.decodeIfPresent(RenderContentMetadata.self, forKey: .metadata),
728+
copyToClipboard: container.decode(Bool.self, forKey: .copyToClipboard)
726729
))
727730
case .heading:
728731
self = try .heading(.init(level: container.decode(Int.self, forKey: .level), text: container.decode(String.self, forKey: .text), anchor: container.decodeIfPresent(String.self, forKey: .anchor)))

Sources/SwiftDocC/Model/Rendering/RenderContentCompiler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct RenderContentCompiler: MarkupVisitor {
4747

4848
mutating func visitCodeBlock(_ codeBlock: CodeBlock) -> [any RenderContent] {
4949
// Default to the bundle's code listing syntax if one is not explicitly declared in the code block.
50-
return [RenderBlockContent.codeListing(.init(syntax: codeBlock.language ?? bundle.info.defaultCodeListingLanguage, code: codeBlock.code.splitByNewlines, metadata: nil))]
50+
return [RenderBlockContent.codeListing(.init(syntax: codeBlock.language ?? bundle.info.defaultCodeListingLanguage, code: codeBlock.code.splitByNewlines, metadata: nil, copyToClipboard: false))]
5151
}
5252

5353
mutating func visitHeading(_ heading: Heading) -> [any RenderContent] {

Sources/SwiftDocC/Semantics/Snippets/Snippet.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +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-
return [RenderBlockContent.codeListing(.init(syntax: snippetMixin.language, code: trimmedLines, metadata: nil))]
71+
let copy = true
72+
return [RenderBlockContent.codeListing(.init(syntax: snippetMixin.language, code: trimmedLines, metadata: nil, copyToClipboard: copy))]
7273
} else {
7374
// Render the whole snippet with its explanation content.
7475
let docCommentContent = snippetEntity.markup.children.flatMap { contentCompiler.visit($0) }
75-
let code = RenderBlockContent.codeListing(.init(syntax: snippetMixin.language, code: snippetMixin.lines, metadata: nil))
76+
let code = RenderBlockContent.codeListing(.init(syntax: snippetMixin.language, code: snippetMixin.lines, metadata: nil, copyToClipboard: false))
7677
return docCommentContent + [code]
7778
}
7879
}

0 commit comments

Comments
 (0)