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:
parent
dddf418cea
commit
8ef48daff9
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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),
|
||||
|
|
Loading…
Reference in New Issue