Skip to content

Commit 81d7060

Browse files
author
Nathan Hawes
authored
Merge pull request #22948 from nathawes/unterminated-raw-string-syntax-highlighting-5.1
[5.1][IDE][syntax-coloring] Treat unterminated raw strings as strings for syntax coloring purposes
2 parents 40f766c + c0cc878 commit 81d7060

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

lib/IDE/SyntaxModel.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,9 @@ SyntaxModelContext::SyntaxModelContext(SourceFile &SrcFile)
178178
}
179179

180180
case tok::unknown: {
181-
if (Tok.getRawText().startswith("\"")) {
182-
// This is an invalid string literal
181+
if (Tok.getRawText().ltrim('#').startswith("\"")) {
182+
// This is likely an invalid single-line ("), multi-line ("""),
183+
// or raw (#", ##", #""", etc.) string literal.
183184
Kind = SyntaxNodeKind::String;
184185
break;
185186
}

test/IDE/coloring.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,25 @@ class SubCls : MyCls, Prot {}
197197
func genFn<T : Prot where T.Blarg : Prot2>(_: T) -> Int {}
198198

199199
func f(x: Int) -> Int {
200+
201+
// CHECK: <str>#"This is a raw string"#</str>
202+
#"This is a raw string"#
203+
204+
// CHECK: <str>##"This is also a raw string"##</str>
205+
##"This is also a raw string"##
206+
207+
// CHECK: <str>###"This is an unterminated raw string"</str>
208+
###"This is an unterminated raw string"
209+
210+
// CHECK: <str>#"""This is a multiline raw string"""#</str>
211+
#"""This is a multiline raw string"""#
212+
213+
// CHECK: <str>#"This is an </str>\#<anchor>(</anchor>interpolated<anchor>)</anchor><str> raw string"#</str>
214+
#"This is an \#(interpolated) raw string"#
215+
216+
// CHECK: <str>#"This is a raw string with an invalid \##() interpolation"#</str>
217+
#"This is a raw string with an invalid \##() interpolation"#
218+
200219
// CHECK: <str>"This is string </str>\<anchor>(</anchor>genFn({(a:<type>Int</type> -> <type>Int</type>) <kw>in</kw> a})<anchor>)</anchor><str> interpolation"</str>
201220
"This is string \(genFn({(a:Int -> Int) in a})) interpolation"
202221

0 commit comments

Comments
 (0)