Fix highlighting for strings within interpolation (#85)

This patch fixes syntax highlighting for string literals that appear
within string interpolation. This patch only takes single words into
account.
This commit is contained in:
John Sundell 2019-09-05 14:11:58 +02:00 committed by GitHub
parent dddf418cea
commit 8ef48daff9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

@ -154,12 +154,17 @@ private extension SwiftGrammar {
var tokenType: TokenType { return .string }
func matches(_ segment: Segment) -> Bool {
if segment.tokens.current.hasPrefix("\"") &&
segment.tokens.current.hasSuffix("\"") {
return true
}
guard segment.isWithinStringLiteral(withStart: "\"", end: "\"") else {
return false
}
return !segment.isWithinStringInterpolation &&
!segment.isWithinRawStringInterpolation
!segment.isWithinRawStringInterpolation
}
}

View File

@ -130,6 +130,21 @@ final class LiteralTests: SyntaxHighlighterTestCase {
])
}
func testStringLiteralWithInterpolationContainingString() {
let components = highlighter.highlight(#""\(name ?? "name")""#)
XCTAssertEqual(components, [
.token("\"", .string),
.plainText("\\(name"),
.whitespace(" "),
.plainText("??"),
.whitespace(" "),
.token("\"name\"", .string),
.plainText(")"),
.token("\"", .string)
])
}
func testMultiLineStringLiteral() {
let components = highlighter.highlight("""
let string = \"\"\"
@ -300,6 +315,7 @@ extension LiteralTests {
("testStringLiteralWithCustomIterpolation", testStringLiteralWithCustomIterpolation),
("testStringLiteralWithInterpolationSurroundedByBrackets", testStringLiteralWithInterpolationSurroundedByBrackets),
("testStringLiteralWithInterpolationPrefixedByPunctuation", testStringLiteralWithInterpolationPrefixedByPunctuation),
("testStringLiteralWithInterpolationContainingString", testStringLiteralWithInterpolationContainingString),
("testMultiLineStringLiteral", testMultiLineStringLiteral),
("testSingleLineRawStringLiteral", testSingleLineRawStringLiteral),
("testMultiLineRawStringLiteral", testMultiLineRawStringLiteral),