Merge pull request #47 from JohnSundell/multi-line-comments
Improve support for multi-line comments
This commit is contained in:
commit
f50a610849
@ -80,11 +80,12 @@ private extension SwiftGrammar {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
if segment.tokens.current.isAny(of: "/*", "*/") {
|
if segment.tokens.current.isAny(of: "/*", "/**", "*/") {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
return !segment.tokens.containsBalancedOccurrences(of: "/*", and: "*/")
|
let multiLineStartCount = segment.tokens.count(of: "/*") + segment.tokens.count(of: "/**")
|
||||||
|
return multiLineStartCount != segment.tokens.count(of: "*/")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,10 +45,4 @@ public extension Segment.Tokens {
|
|||||||
func count(of token: String) -> Int {
|
func count(of token: String) -> Int {
|
||||||
return counts[token] ?? 0
|
return counts[token] ?? 0
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return whether an equal number of occurrences have been found of two tokens.
|
|
||||||
/// For example, this can be used to check if a token is encapsulated by parenthesis.
|
|
||||||
func containsBalancedOccurrences(of tokenA: String, and tokenB: String) -> Bool {
|
|
||||||
return count(of: tokenA) == count(of: tokenB)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -59,6 +59,59 @@ final class CommentTests: SyntaxHighlighterTestCase {
|
|||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testMultiLineCommentWithDoubleAsterisks() {
|
||||||
|
let components = highlighter.highlight("""
|
||||||
|
struct Foo {}
|
||||||
|
/** Comment
|
||||||
|
Hello!
|
||||||
|
*/ call()
|
||||||
|
""")
|
||||||
|
|
||||||
|
XCTAssertEqual(components, [
|
||||||
|
.token("struct", .keyword),
|
||||||
|
.whitespace(" "),
|
||||||
|
.plainText("Foo"),
|
||||||
|
.whitespace(" "),
|
||||||
|
.plainText("{}"),
|
||||||
|
.whitespace("\n"),
|
||||||
|
.token("/**", .comment),
|
||||||
|
.whitespace(" "),
|
||||||
|
.token("Comment", .comment),
|
||||||
|
.whitespace("\n "),
|
||||||
|
.token("Hello!", .comment),
|
||||||
|
.whitespace("\n"),
|
||||||
|
.token("*/", .comment),
|
||||||
|
.whitespace(" "),
|
||||||
|
.token("call", .call),
|
||||||
|
.plainText("()")
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
|
func testMutliLineDocumentationComment() {
|
||||||
|
let components = highlighter.highlight("""
|
||||||
|
/**
|
||||||
|
* Documentation
|
||||||
|
*/
|
||||||
|
class MyClass {}
|
||||||
|
""")
|
||||||
|
|
||||||
|
XCTAssertEqual(components, [
|
||||||
|
.token("/**", .comment),
|
||||||
|
.whitespace("\n "),
|
||||||
|
.token("*", .comment),
|
||||||
|
.whitespace(" "),
|
||||||
|
.token("Documentation", .comment),
|
||||||
|
.whitespace("\n "),
|
||||||
|
.token("*/", .comment),
|
||||||
|
.whitespace("\n"),
|
||||||
|
.token("class", .keyword),
|
||||||
|
.whitespace(" "),
|
||||||
|
.plainText("MyClass"),
|
||||||
|
.whitespace(" "),
|
||||||
|
.plainText("{}")
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
func testAllTestsRunOnLinux() {
|
func testAllTestsRunOnLinux() {
|
||||||
XCTAssertTrue(TestCaseVerifier.verifyLinuxTests((type(of: self)).allTests))
|
XCTAssertTrue(TestCaseVerifier.verifyLinuxTests((type(of: self)).allTests))
|
||||||
}
|
}
|
||||||
@ -68,7 +121,9 @@ extension CommentTests {
|
|||||||
static var allTests: [(String, TestClosure<CommentTests>)] {
|
static var allTests: [(String, TestClosure<CommentTests>)] {
|
||||||
return [
|
return [
|
||||||
("testSingleLineComment", testSingleLineComment),
|
("testSingleLineComment", testSingleLineComment),
|
||||||
("testMultiLineComment", testMultiLineComment)
|
("testMultiLineComment", testMultiLineComment),
|
||||||
|
("testMultiLineCommentWithDoubleAsterisks", testMultiLineCommentWithDoubleAsterisks),
|
||||||
|
("testMutliLineDocumentationComment", testMutliLineDocumentationComment)
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user