Fix comments with punctuation just prior or after (#93)

This change makes Splash correctly handle connected multiline
comment delimiters; `/*` and `*/`, when those are either connected,
or directly placed next to punctuation.
This commit is contained in:
Tobias Due Munk 2020-01-29 10:19:38 +01:00 committed by GitHub
parent 97541c8835
commit b7e9141e1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 2 deletions

View File

@ -47,10 +47,14 @@ public struct SwiftGrammar: Grammar {
return false
case (")", _):
return false
case ("/", "/"), ("/", "*"):
case ("/", "/"), ("/", "*"), ("*", "/"):
return true
case ("/", _):
return false
case ("(", _) where delimiterB != ".":
return false
case (".", "/"):
return false
default:
return true
}
@ -106,6 +110,12 @@ private extension SwiftGrammar {
var tokenType: TokenType { return .comment }
func matches(_ segment: Segment) -> Bool {
if segment.tokens.current.hasPrefix("/*") {
if segment.tokens.current.hasSuffix("*/") {
return true
}
}
if segment.tokens.current.hasPrefix("//") {
return true
}

View File

@ -136,6 +136,33 @@ final class CommentTests: SyntaxHighlighterTestCase {
])
}
func testCommentWithNoWhiteSpaceToPunctuation() {
let components = highlighter.highlight("""
(/* Hello */)
.// World
(/**/)
""")
XCTAssertEqual(components, [
.plainText("("),
.token("/*", .comment),
.whitespace(" "),
.token("Hello", .comment),
.whitespace(" "),
.token("*/", .comment),
.plainText(")"),
.whitespace("\n"),
.plainText("."),
.token("//", .comment),
.whitespace(" "),
.token("World", .comment),
.whitespace("\n"),
.plainText("("),
.token("/**/", .comment),
.plainText(")"),
])
}
func testAllTestsRunOnLinux() {
XCTAssertTrue(TestCaseVerifier.verifyLinuxTests((type(of: self)).allTests))
}
@ -149,7 +176,8 @@ extension CommentTests {
("testMultiLineCommentWithDoubleAsterisks", testMultiLineCommentWithDoubleAsterisks),
("testMutliLineDocumentationComment", testMutliLineDocumentationComment),
("testCommentStartingWithPunctuation", testCommentStartingWithPunctuation),
("testCommentEndingWithComma", testCommentEndingWithComma)
("testCommentEndingWithComma", testCommentEndingWithComma),
("testCommentWithNoWhiteSpaceToPunctuation", testCommentWithNoWhiteSpaceToPunctuation)
]
}
}