Handle string literals that only have a new line (#97)

Since such new lines are grouped with their trailing marker, and start
with an escaping backslash, they would previously be ignored (since they
were incorrectly treated as string interpolation or escaped markers).
This commit is contained in:
John Sundell 2020-01-29 11:09:14 +01:00 committed by GitHub
parent 812811ff69
commit cac40caf68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 3 deletions

View File

@ -513,9 +513,11 @@ private extension Segment {
var previousToken: String?
for token in tokens.onSameLine {
guard previousToken != "\\" else {
previousToken = token
continue
if token.hasPrefix("(") || token.hasPrefix("#(") || token.hasPrefix("\"") {
guard previousToken != "\\" else {
previousToken = token
continue
}
}
if token == start {

View File

@ -234,6 +234,19 @@ final class LiteralTests: SyntaxHighlighterTestCase {
])
}
func testStringLiteralContainingOnlyNewLine() {
let components = highlighter.highlight(#"text.split(separator: "\n")"#)
XCTAssertEqual(components, [
.plainText("text."),
.token("split", .call),
.plainText("(separator:"),
.whitespace(" "),
.token(#""\n""#, .string),
.plainText(")")
])
}
func testDoubleLiteral() {
let components = highlighter.highlight("let double = 1.13")
@ -320,6 +333,7 @@ extension LiteralTests {
("testSingleLineRawStringLiteral", testSingleLineRawStringLiteral),
("testMultiLineRawStringLiteral", testMultiLineRawStringLiteral),
("testRawStringWithInterpolation", testRawStringWithInterpolation),
("testStringLiteralContainingOnlyNewLine", testStringLiteralContainingOnlyNewLine),
("testDoubleLiteral", testDoubleLiteral),
("testIntegerLiteralWithSeparators", testIntegerLiteralWithSeparators),
("testKeyPathLiteral", testKeyPathLiteral),