Skip to content

Commit d6091d5

Browse files
committed
CursorInfo: Teach SemaLocResolver to recognize the start of an expression, if the location cannot be further refined to other kind, e.g. a reference. rdar://32749670
1 parent da87ab8 commit d6091d5

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

include/swift/IDE/Utils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ enum class SemaTokenKind {
148148
Invalid,
149149
ValueRef,
150150
ModuleRef,
151+
ExprStart,
151152
StmtStart,
152153
};
153154

@@ -164,6 +165,7 @@ struct SemaToken {
164165
DeclContext *DC = nullptr;
165166
Type ContainerType;
166167
Stmt *TrailingStmt = nullptr;
168+
Expr *TrailingExpr = nullptr;
167169

168170
SemaToken() = default;
169171
SemaToken(ValueDecl *ValueD, TypeDecl *CtorTyRef, ExtensionDecl *ExtTyRef,
@@ -175,6 +177,8 @@ struct SemaToken {
175177
Mod(Mod), Loc(Loc) { }
176178
SemaToken(Stmt *TrailingStmt) : Kind(SemaTokenKind::StmtStart),
177179
TrailingStmt(TrailingStmt) {}
180+
SemaToken(Expr* TrailingExpr) : Kind(SemaTokenKind::ExprStart),
181+
TrailingExpr(TrailingExpr) {}
178182
bool isValid() const { return !isInvalid(); }
179183
bool isInvalid() const { return Kind == SemaTokenKind::Invalid; }
180184
};
@@ -191,6 +195,7 @@ class SemaLocResolver : public SourceEntityWalker {
191195
SourceManager &getSourceMgr() const;
192196
private:
193197
bool walkToExprPre(Expr *E) override;
198+
bool walkToExprPost(Expr *E) override;
194199
bool walkToDeclPre(Decl *D, CharSourceRange Range) override;
195200
bool walkToDeclPost(Decl *D) override;
196201
bool walkToStmtPre(Stmt *S) override;
@@ -211,6 +216,7 @@ class SemaLocResolver : public SourceEntityWalker {
211216
SourceLoc Loc, bool IsRef, Type Ty = Type());
212217
bool tryResolve(ModuleEntity Mod, SourceLoc Loc);
213218
bool tryResolve(Stmt *St);
219+
bool tryResolve(Expr *Exp);
214220
bool visitSubscriptReference(ValueDecl *D, CharSourceRange Range,
215221
bool IsOpenBracket) override;
216222
};

lib/IDE/SwiftSourceDocInfo.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ bool SemaLocResolver::tryResolve(Stmt *St) {
108108
return false;
109109
}
110110

111+
bool SemaLocResolver::tryResolve(Expr *Exp) {
112+
if (Exp->getStartLoc() == LocToResolve) {
113+
SemaTok = { Exp };
114+
return true;
115+
}
116+
return false;
117+
}
118+
111119
bool SemaLocResolver::visitSubscriptReference(ValueDecl *D, CharSourceRange Range,
112120
bool IsOpenBracket) {
113121
// We should treat both open and close brackets equally
@@ -189,6 +197,12 @@ bool SemaLocResolver::walkToExprPre(Expr *E) {
189197
return true;
190198
}
191199

200+
bool SemaLocResolver::walkToExprPost(Expr *E) {
201+
if (isDone())
202+
return false;
203+
return !tryResolve(E);
204+
}
205+
192206
bool SemaLocResolver::visitCallArgName(Identifier Name, CharSourceRange Range,
193207
ValueDecl *D) {
194208
if (isDone())

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,7 @@ static void resolveCursor(SwiftLangSupport &Lang,
11811181
}
11821182
return;
11831183
}
1184+
case SemaTokenKind::ExprStart:
11841185
case SemaTokenKind::StmtStart: {
11851186
Receiver({});
11861187
return;

0 commit comments

Comments
 (0)