[IDE] Mark the parens for string interpolation with a special syntax annotation.

Part of rdar://19655729

Swift SVN r24983
This commit is contained in:
Argyrios Kyrtzidis
2015-02-05 00:53:57 +00:00
parent e855ae8f58
commit f15d56b8aa
4 changed files with 37 additions and 3 deletions

View File

@@ -32,6 +32,8 @@ enum class SyntaxNodeKind : uint8_t {
Integer,
Floating,
String,
/// Marks the parens for a string interpolation.
StringInterpolationAnchor,
Character,
CommentLine,
CommentBlock,

View File

@@ -56,7 +56,8 @@ SyntaxModelContext::SyntaxModelContext(SourceFile &SrcFile)
/*TokenizeInterpolatedString=*/true);
std::vector<SyntaxNode> Nodes;
SourceLoc AttrLoc;
for (auto &Tok : Tokens) {
for (unsigned I = 0, E = Tokens.size(); I != E; ++I) {
auto &Tok = Tokens[I];
SyntaxNodeKind Kind;
SourceLoc Loc;
Optional<unsigned> Length;
@@ -100,6 +101,35 @@ SyntaxModelContext::SyntaxModelContext(SourceFile &SrcFile)
// attribute name.
AttrLoc = Tok.getLoc();
continue;
case tok::l_paren: {
// Check if this is a string interpolation paren.
if (I == 0)
continue;
auto &PrevTok = Tokens[I-1];
if (PrevTok.getKind() != tok::string_literal)
continue;
StringRef StrText = PrevTok.getText();
if (StrText.size() > 1 && StrText.back() == '\"')
continue;
Kind = SyntaxNodeKind::StringInterpolationAnchor;
break;
}
case tok::r_paren: {
// Check if this is a string interpolation paren.
if (I+1 == E)
continue;
auto &NextTok = Tokens[I+1];
if (NextTok.getKind() != tok::string_literal)
continue;
StringRef StrText = NextTok.getText();
if (StrText.size() > 1 && StrText.front() == '\"')
continue;
Kind = SyntaxNodeKind::StringInterpolationAnchor;
break;
}
default:
continue;
}

View File

@@ -173,7 +173,7 @@ func genFn<T : Prot where T.Blarg : Prot2>(_: T) -> Int {}
func f(x: Int) -> Int {
// CHECK: <comment-line>// string interpolation is the best</comment-line>
// string interpolation is the best
// CHECK: <str>"This is string </str>\(genFn({(a:<type>Int</type> -> <type>Int</type>) <kw>in</kw> a}))<str> interpolation"</str>
// 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>
"This is string \(genFn({(a:Int -> Int) in a})) interpolation"
}
@@ -194,7 +194,7 @@ func test() {
typealias MyInt = Int
func test2(x: Int) {
// CHECK: <str>"</str>\(x)<str>"</str>
// CHECK: <str>"</str>\<anchor>(</anchor>x<anchor>)</anchor><str>"</str>
"\(x)"
}

View File

@@ -610,6 +610,7 @@ public:
case SyntaxNodeKind::Integer: Id = "int"; break;
case SyntaxNodeKind::Floating: Id = "float"; break;
case SyntaxNodeKind::String: Id = "str"; break;
case SyntaxNodeKind::StringInterpolationAnchor: Id = "anchor"; break;
case SyntaxNodeKind::Character: Id = "char"; break;
case SyntaxNodeKind::CommentLine: Id = "comment-line"; break;
case SyntaxNodeKind::CommentBlock: Id = "comment-block"; break;
@@ -635,6 +636,7 @@ public:
case SyntaxNodeKind::Integer: Col = llvm::raw_ostream::BLUE; break;
case SyntaxNodeKind::Floating: Col = llvm::raw_ostream::BLUE; break;
case SyntaxNodeKind::String: Col = llvm::raw_ostream::RED; break;
case SyntaxNodeKind::StringInterpolationAnchor: Col = llvm::raw_ostream::CYAN; break;
case SyntaxNodeKind::Character: Col = llvm::raw_ostream::BLUE; break;
case SyntaxNodeKind::CommentLine: Col = llvm::raw_ostream::GREEN; break;
case SyntaxNodeKind::CommentBlock: Col = llvm::raw_ostream::GREEN; break;