Skip to content

Commit aaf2b97

Browse files
author
Jesse Haigh
committed
parse strikeout option, solution for language not as the first option on language line, tests
1 parent 53a5aca commit aaf2b97

File tree

4 files changed

+125
-1
lines changed

4 files changed

+125
-1
lines changed

Sources/SwiftDocC/Checker/Checkers/InvalidCodeBlockOption.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ public struct InvalidCodeBlockOption: Checker {
4747
)
4848
}
4949
problems.append(Problem(diagnostic: diagnostic, possibleSolutions: possibleSolutions))
50+
} else if lang == nil {
51+
let diagnostic = Diagnostic(source: sourceFile, severity: .warning, range: codeBlock.range, identifier: "org.swift.docc.InvalidCodeBlockOption", summary: "Unknown option \(value.singleQuoted) in code block.")
52+
let possibleSolutions =
53+
Solution(
54+
summary: "If \(value.singleQuoted) is the language for this code block, then write \(value.singleQuoted) as the first option.",
55+
replacements: []
56+
)
57+
problems.append(Problem(diagnostic: diagnostic, possibleSolutions: [possibleSolutions]))
5058
}
5159
}
5260

Sources/SwiftDocC/Utility/ParseLanguageString.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public func tokenizeLanguageString(_ input: String?) -> (lang: String?, tokens:
3535
tokens.append((.wrap, value))
3636
} else if key == "highlight" {
3737
tokens.append((.highlight, value))
38+
} else if key == "strikeout" {
39+
tokens.append((.strikeout, value))
3840
} else {
3941
tokens.append((.unknown, key))
4042
}
@@ -46,6 +48,8 @@ public func tokenizeLanguageString(_ input: String?) -> (lang: String?, tokens:
4648
tokens.append((.wrap, nil as String?))
4749
} else if key == "highlight" {
4850
tokens.append((.highlight, nil as String?))
51+
} else if key == "strikeout" {
52+
tokens.append((.strikeout, nil as String?))
4953
} else if index == 0 && !key.contains("[") && !key.contains("]") {
5054
lang = key
5155
} else {

Tests/SwiftDocCTests/Checker/Checkers/InvalidCodeBlockOptionTests.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ let a = 1
2424
var checker = InvalidCodeBlockOption(sourceFile: nil)
2525
checker.visit(document)
2626
XCTAssertTrue(checker.problems.isEmpty)
27-
XCTAssertEqual(RenderBlockContent.CodeListing.knownOptions, ["highlight", "nocopy", "unknown", "wrap"])
27+
XCTAssertEqual(RenderBlockContent.CodeListing.knownOptions, ["highlight", "nocopy", "strikeout", "unknown", "wrap"])
2828
}
2929

3030
func testOption() {
@@ -104,5 +104,23 @@ let g = 7
104104

105105
}
106106
}
107+
108+
func testLanguageNotFirst() {
109+
let markupSource = """
110+
```nocopy, swift, highlight=[1]
111+
let b = 2
112+
```
113+
"""
114+
let document = Document(parsing: markupSource, options: [])
115+
var checker = InvalidCodeBlockOption(sourceFile: URL(fileURLWithPath: #file))
116+
checker.visit(document)
117+
XCTAssertEqual(1, checker.problems.count)
118+
119+
for problem in checker.problems {
120+
XCTAssertEqual("org.swift.docc.InvalidCodeBlockOption", problem.diagnostic.identifier)
121+
XCTAssertEqual(problem.diagnostic.summary, "Unknown option 'swift' in code block.")
122+
XCTAssertEqual(problem.possibleSolutions.map(\.summary), ["If 'swift' is the language for this code block, then write 'swift' as the first option."])
123+
}
124+
}
107125
}
108126

Tests/SwiftDocCTests/Rendering/RenderContentCompilerTests.swift

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,4 +459,98 @@ class RenderContentCompilerTests: XCTestCase {
459459
XCTAssertEqual(codeListing.syntax, "swift")
460460
XCTAssertEqual(codeListing.highlight, [1, 2, 3])
461461
}
462+
463+
func testMultipleHighlightMultipleStrikeout() async throws {
464+
enableFeatureFlag(\.isExperimentalCodeBlockEnabled)
465+
466+
let (bundle, context) = try await testBundleAndContext()
467+
var compiler = RenderContentCompiler(context: context, bundle: bundle, identifier: ResolvedTopicReference(bundleID: bundle.id, path: "/path", fragment: nil, sourceLanguage: .swift))
468+
469+
let source = #"""
470+
```swift, strikeout=[3,5], highlight=[1, 2, 3]
471+
let a = 1
472+
let b = 2
473+
let c = 3
474+
let d = 4
475+
let e = 5
476+
```
477+
"""#
478+
479+
let document = Document(parsing: source)
480+
481+
let result = document.children.flatMap { compiler.visit($0) }
482+
483+
let renderCodeBlock = try XCTUnwrap(result[0] as? RenderBlockContent)
484+
guard case let .codeListing(codeListing) = renderCodeBlock else {
485+
XCTFail("Expected RenderBlockContent.codeListing")
486+
return
487+
}
488+
489+
XCTAssertEqual(codeListing.syntax, "swift")
490+
XCTAssertEqual(codeListing.highlight, [1, 2, 3])
491+
XCTAssertEqual(codeListing.strikeout, [3, 5])
492+
}
493+
494+
func testLanguageNotFirstOption() async throws {
495+
enableFeatureFlag(\.isExperimentalCodeBlockEnabled)
496+
497+
let (bundle, context) = try await testBundleAndContext()
498+
var compiler = RenderContentCompiler(context: context, bundle: bundle, identifier: ResolvedTopicReference(bundleID: bundle.id, path: "/path", fragment: nil, sourceLanguage: .swift))
499+
500+
let source = #"""
501+
```highlight=[1, 2, 3], swift, wrap=20, strikeout=[3]
502+
let a = 1
503+
let b = 2
504+
let c = 3
505+
let d = 4
506+
let e = 5
507+
```
508+
"""#
509+
510+
let document = Document(parsing: source)
511+
512+
let result = document.children.flatMap { compiler.visit($0) }
513+
514+
let renderCodeBlock = try XCTUnwrap(result[0] as? RenderBlockContent)
515+
guard case let .codeListing(codeListing) = renderCodeBlock else {
516+
XCTFail("Expected RenderBlockContent.codeListing")
517+
return
518+
}
519+
520+
XCTAssertEqual(codeListing.highlight, [1, 2, 3])
521+
// we expect the language to be the first option in the language line, otherwise it remains nil.
522+
XCTAssertEqual(codeListing.syntax, nil)
523+
XCTAssertEqual(codeListing.wrap, 20)
524+
XCTAssertEqual(codeListing.strikeout, [3])
525+
}
526+
527+
func testUnorderedArrayOptions() async throws {
528+
enableFeatureFlag(\.isExperimentalCodeBlockEnabled)
529+
530+
let (bundle, context) = try await testBundleAndContext()
531+
var compiler = RenderContentCompiler(context: context, bundle: bundle, identifier: ResolvedTopicReference(bundleID: bundle.id, path: "/path", fragment: nil, sourceLanguage: .swift))
532+
533+
let source = #"""
534+
```highlight=[5,3,4], strikeout=[3,1]
535+
let a = 1
536+
let b = 2
537+
let c = 3
538+
let d = 4
539+
let e = 5
540+
```
541+
"""#
542+
543+
let document = Document(parsing: source)
544+
545+
let result = document.children.flatMap { compiler.visit($0) }
546+
547+
let renderCodeBlock = try XCTUnwrap(result[0] as? RenderBlockContent)
548+
guard case let .codeListing(codeListing) = renderCodeBlock else {
549+
XCTFail("Expected RenderBlockContent.codeListing")
550+
return
551+
}
552+
553+
XCTAssertEqual(codeListing.highlight, [5, 3, 4])
554+
XCTAssertEqual(codeListing.strikeout, [3, 1])
555+
}
462556
}

0 commit comments

Comments
 (0)