Skip to content

Commit 3f55690

Browse files
author
Nathan Hawes
authored
Merge pull request swiftlang#9577 from nathawes/multiline-string-syntax-highlighting
[parser] Account for the length of multine string quotes when tokenizing interpolated string components
2 parents b6d7530 + 52374a9 commit 3f55690

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

lib/Parse/Parser.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ static void getStringPartTokens(const Token &Tok, const LangOptions &LangOpts,
172172
const SourceManager &SM,
173173
int BufID, std::vector<Token> &Toks) {
174174
assert(Tok.is(tok::string_literal));
175+
bool IsMultiline = Tok.IsMultilineString();
176+
unsigned QuoteLen = IsMultiline ? 3 : 1;
175177
SmallVector<Lexer::StringSegment, 4> Segments;
176178
Lexer::getStringLiteralSegments(Tok, Segments, /*Diags=*/nullptr);
177179
for (unsigned i = 0, e = Segments.size(); i != e; ++i) {
@@ -183,17 +185,17 @@ static void getStringPartTokens(const Token &Tok, const LangOptions &LangOpts,
183185
unsigned Len = Seg.Length;
184186
if (isFirst) {
185187
// Include the quote.
186-
Loc = Loc.getAdvancedLoc(-1);
187-
++Len;
188+
Loc = Loc.getAdvancedLoc(-QuoteLen);
189+
Len += QuoteLen;
188190
}
189191
if (isLast) {
190192
// Include the quote.
191-
++Len;
193+
Len += QuoteLen;
192194
}
193195

194196
StringRef Text = SM.extractText({ Loc, Len });
195197
Token NewTok;
196-
NewTok.setToken(tok::string_literal, Text);
198+
NewTok.setToken(tok::string_literal, Text, IsMultiline);
197199
Toks.push_back(NewTok);
198200

199201
} else {

test/IDE/coloring.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,20 @@ func f(x: Int) -> Int {
226226
// string interpolation is the best
227227
// 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>
228228
"This is string \(genFn({(a:Int -> Int) in a})) interpolation"
229+
230+
// CHECK: <str>"""
231+
// CHECK-NEXT: This is a multiline string.
232+
// CHECK-NEXT: """</str>
233+
"""
234+
This is a multiline string.
235+
"""
236+
237+
// CHECK: <str>"""
238+
// CHECK-NEXT: This is a multiline</str>\<anchor>(</anchor> <str>"interpolated"</str> <anchor>)</anchor><str>string
239+
// CHECK-NEXT: """</str>
240+
"""
241+
This is a multiline\( "interpolated" )string
242+
"""
229243
}
230244

231245
// CHECK: <kw>func</kw> bar(x: <type>Int</type>) -> (<type>Int</type>, <type>Float</type>) {

0 commit comments

Comments
 (0)