Merge pull request #47 from JohnSundell/multi-line-comments

Improve support for multi-line comments
This commit is contained in:
John Sundell 2019-03-11 11:35:33 +01:00 committed by GitHub
commit f50a610849
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 9 deletions

View File

@ -80,11 +80,12 @@ private extension SwiftGrammar {
return true
}
if segment.tokens.current.isAny(of: "/*", "*/") {
if segment.tokens.current.isAny(of: "/*", "/**", "*/") {
return true
}
return !segment.tokens.containsBalancedOccurrences(of: "/*", and: "*/")
let multiLineStartCount = segment.tokens.count(of: "/*") + segment.tokens.count(of: "/**")
return multiLineStartCount != segment.tokens.count(of: "*/")
}
}

View File

@ -45,10 +45,4 @@ public extension Segment.Tokens {
func count(of token: String) -> Int {
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)
}
}

View File

@ -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() {
XCTAssertTrue(TestCaseVerifier.verifyLinuxTests((type(of: self)).allTests))
}
@ -68,7 +121,9 @@ extension CommentTests {
static var allTests: [(String, TestClosure<CommentTests>)] {
return [
("testSingleLineComment", testSingleLineComment),
("testMultiLineComment", testMultiLineComment)
("testMultiLineComment", testMultiLineComment),
("testMultiLineCommentWithDoubleAsterisks", testMultiLineCommentWithDoubleAsterisks),
("testMutliLineDocumentationComment", testMutliLineDocumentationComment)
]
}
}