-
Notifications
You must be signed in to change notification settings - Fork 158
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
Merged
Merged
Changes from all commits
Commits
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
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
108 changes: 108 additions & 0 deletions
108
Tests/SwiftDocCTests/Checker/Checkers/InvalidCodeBlockOptionTests.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,108 @@ | ||
/* | ||
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 | ||
*/ | ||
|
||
import XCTest | ||
@testable import SwiftDocC | ||
import Markdown | ||
|
||
class InvalidCodeBlockOptionTests: XCTestCase { | ||
|
||
func testNoOptions() { | ||
let markupSource = """ | ||
``` | ||
let a = 1 | ||
``` | ||
""" | ||
let document = Document(parsing: markupSource, options: []) | ||
var checker = InvalidCodeBlockOption(sourceFile: nil) | ||
checker.visit(document) | ||
XCTAssertTrue(checker.problems.isEmpty) | ||
XCTAssertEqual(RenderBlockContent.CodeListing.knownOptions, ["nocopy"]) | ||
} | ||
|
||
func testOption() { | ||
let markupSource = """ | ||
```nocopy | ||
let a = 1 | ||
``` | ||
""" | ||
let document = Document(parsing: markupSource, options: []) | ||
var checker = InvalidCodeBlockOption(sourceFile: nil) | ||
checker.visit(document) | ||
XCTAssertTrue(checker.problems.isEmpty) | ||
} | ||
|
||
func testMultipleOptionTypos() { | ||
let markupSource = """ | ||
```nocoy | ||
let b = 2 | ||
``` | ||
|
||
```nocoy | ||
let c = 3 | ||
``` | ||
""" | ||
let document = Document(parsing: markupSource, options: []) | ||
var checker = InvalidCodeBlockOption(sourceFile: URL(fileURLWithPath: #file)) | ||
checker.visit(document) | ||
XCTAssertEqual(2, checker.problems.count) | ||
|
||
for problem in checker.problems { | ||
XCTAssertEqual("org.swift.docc.InvalidCodeBlockOption", problem.diagnostic.identifier) | ||
XCTAssertEqual(problem.diagnostic.summary, "Unknown option 'nocoy' in code block.") | ||
XCTAssertEqual(problem.possibleSolutions.map(\.summary), ["Replace 'nocoy' with 'nocopy'."]) | ||
} | ||
} | ||
|
||
func testOptionDifferentTypos() throws { | ||
let markupSource = """ | ||
```swift, nocpy | ||
let d = 4 | ||
``` | ||
|
||
```unknown, nocpoy | ||
let e = 5 | ||
``` | ||
|
||
```nocopy | ||
let f = 6 | ||
``` | ||
|
||
```ncopy | ||
let g = 7 | ||
``` | ||
""" | ||
let document = Document(parsing: markupSource, options: []) | ||
var checker = InvalidCodeBlockOption(sourceFile: URL(fileURLWithPath: #file)) | ||
checker.visit(document) | ||
|
||
XCTAssertEqual(3, checker.problems.count) | ||
|
||
let summaries = checker.problems.map { $0.diagnostic.summary } | ||
XCTAssertEqual(summaries, [ | ||
"Unknown option 'nocpy' in code block.", | ||
"Unknown option 'nocpoy' in code block.", | ||
"Unknown option 'ncopy' in code block.", | ||
]) | ||
|
||
for problem in checker.problems { | ||
XCTAssertEqual( | ||
"org.swift.docc.InvalidCodeBlockOption", | ||
problem.diagnostic.identifier | ||
) | ||
|
||
XCTAssertEqual(problem.possibleSolutions.count, 1) | ||
let solution = try XCTUnwrap(problem.possibleSolutions.first) | ||
XCTAssert(solution.summary.hasSuffix("with 'nocopy'.")) | ||
|
||
} | ||
} | ||
} | ||
|
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.
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.