-
Notifications
You must be signed in to change notification settings - Fork 157
Add copy-to-clipboard support to code blocks #1273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DebugSteven
wants to merge
16
commits into
swiftlang:main
Choose a base branch
from
DebugSteven:codeblock-copy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
5eac2fa
WIP add copy button to CodeListing
af05461
update OpenAPI spec with copyToClipboard on CodeListing
2318b84
Add option to copy a code block parsed from the language string, deli…
5721030
fix unit tests
2d6131d
add unit test for copyToClipboard
cb321ea
fixup missing async on test
056b0dc
update docs
d26471c
copy by default
e2cde6e
add feature flag 'enable-experimental-code-block' for copy-to-clipboa…
7bc5467
add more tests, remove docs, code cleanup
7374b5a
WIP diagnostics
15dbba5
WIP diagnostics: tests
2987eac
add code block options onto RenderBlockContent.CodeListing
155ef45
rename feature flag
4740cae
remaining PR feedback
48ed2d8
copyToClipboard should be false when nocopy is present
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
Sources/SwiftDocC/Checker/Checkers/InvalidCodeBlockOption.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
This source file is part of the Swift.org open source project | ||
|
||
Copyright (c) 2025 Apple Inc. and the Swift project authors | ||
Licensed under Apache License v2.0 with Runtime Library Exception | ||
|
||
See https://swift.org/LICENSE.txt for license information | ||
See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
*/ | ||
|
||
internal import Foundation | ||
internal import Markdown | ||
|
||
/** | ||
Code blocks can have a `nocopy` option after the \`\`\`, in the language line. | ||
`nocopy` can be immediately after the \`\`\` or after a specified language and a comma (`,`). | ||
*/ | ||
internal struct InvalidCodeBlockOption: Checker { | ||
var problems = [Problem]() | ||
|
||
/// Parsing options for code blocks | ||
private let knownOptions = RenderBlockContent.CodeListing.knownOptions | ||
|
||
private var sourceFile: URL? | ||
|
||
/// Creates a new checker that detects documents with multiple titles. | ||
/// | ||
/// - Parameter sourceFile: The URL to the documentation file that the checker checks. | ||
init(sourceFile: URL?) { | ||
self.sourceFile = sourceFile | ||
} | ||
|
||
mutating func visitCodeBlock(_ codeBlock: CodeBlock) { | ||
let info = codeBlock.language?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "" | ||
guard !info.isEmpty else { return } | ||
|
||
let tokens = info | ||
.split(separator: ",") | ||
.map { $0.trimmingCharacters(in: .whitespaces) } | ||
.filter { !$0.isEmpty } | ||
|
||
guard !tokens.isEmpty else { return } | ||
|
||
for token in tokens { | ||
// if the token is an exact match, we don't need to do anything | ||
guard !knownOptions.contains(token) else { continue } | ||
|
||
let matches = NearMiss.bestMatches(for: knownOptions, against: token) | ||
|
||
if !matches.isEmpty { | ||
let diagnostic = Diagnostic(source: sourceFile, severity: .warning, range: codeBlock.range, identifier: "org.swift.docc.InvalidCodeBlockOption", summary: "Unknown option \(token.singleQuoted) in code block.") | ||
let possibleSolutions = matches.map { candidate in | ||
Solution( | ||
summary: "Replace \(token.singleQuoted) with \(candidate.singleQuoted).", | ||
replacements: [] | ||
) | ||
} | ||
problems.append(Problem(diagnostic: diagnostic, possibleSolutions: possibleSolutions)) | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,41 @@ struct RenderContentCompiler: MarkupVisitor { | |
|
||
mutating func visitCodeBlock(_ codeBlock: CodeBlock) -> [any RenderContent] { | ||
// Default to the bundle's code listing syntax if one is not explicitly declared in the code block. | ||
return [RenderBlockContent.codeListing(.init(syntax: codeBlock.language ?? bundle.info.defaultCodeListingLanguage, code: codeBlock.code.splitByNewlines, metadata: nil))] | ||
|
||
if FeatureFlags.current.isExperimentalCodeBlockAnnotationsEnabled { | ||
|
||
func parseLanguageString(_ input: String?) -> (lang: String? , tokens: [RenderBlockContent.CodeListing.OptionName]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. non-blocking: In the long term it might be worth extracting this so that the warning and the rendering don't have behavioral differences. Since there's already follow up PRs that parse more things, it's probably better to extract this in those PRs rather than here. |
||
guard let input else { return (lang: nil, tokens: []) } | ||
let parts = input | ||
.split(separator: ",") | ||
.map { $0.trimmingCharacters(in: .whitespaces) } | ||
var lang: String? = nil | ||
var options: [RenderBlockContent.CodeListing.OptionName] = [] | ||
|
||
for part in parts { | ||
if let opt = RenderBlockContent.CodeListing.OptionName(caseInsensitive: part) { | ||
options.append(opt) | ||
} else if lang == nil { | ||
lang = String(part) | ||
} | ||
} | ||
return (lang, options) | ||
} | ||
|
||
let options = parseLanguageString(codeBlock.language) | ||
|
||
let listing = RenderBlockContent.CodeListing( | ||
syntax: options.lang ?? bundle.info.defaultCodeListingLanguage, | ||
code: codeBlock.code.splitByNewlines, | ||
metadata: nil, | ||
copyToClipboard: !options.tokens.contains(.nocopy) | ||
) | ||
|
||
return [RenderBlockContent.codeListing(listing)] | ||
|
||
} else { | ||
return [RenderBlockContent.codeListing(.init(syntax: codeBlock.language ?? bundle.info.defaultCodeListingLanguage, code: codeBlock.code.splitByNewlines, metadata: nil, copyToClipboard: false))] | ||
} | ||
} | ||
|
||
mutating func visitHeading(_ heading: Heading) -> [any RenderContent] { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: should this default value be based on
FeatureFlags.current.isExperimentalCodeBlockAnnotationsEnabled
?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure. I had intentionally written it to be on by default because I thought having a copy-to-clipboard button by default was wanted. But without the feature flag, users have no way of turning it off.
https://forums.swift.org/t/extending-docc-with-additional-metadata-to-provide-alternate-displays-for-code-blocks/81056/5
If it's best to turn this on only when the feature flag is present, I can make that change. Please just let me know.