Skip to content

Commit 995e9af

Browse files
authored
Merge pull request #14109 from akyrtzi/fix-cursor-interpolation-in-stmts-5.0
[5.0][sourcekitd] Fix cursor resolving when pointing inside string interpolations of return statements
2 parents 594b12a + 9442541 commit 995e9af

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

include/swift/IDE/Utils.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,8 @@ class CursorInfoResolver : public SourceEntityWalker {
233233
bool visitDeclarationArgumentName(Identifier Name, SourceLoc StartLoc,
234234
ValueDecl *D) override;
235235
bool visitModuleReference(ModuleEntity Mod, CharSourceRange Range) override;
236-
bool rangeContainsLoc(SourceRange Range) const {
237-
return getSourceMgr().rangeContainsTokenLoc(Range, LocToResolve);
238-
}
236+
bool rangeContainsLoc(SourceRange Range) const;
237+
bool rangeContainsLoc(CharSourceRange Range) const;
239238
bool isDone() const { return CursorInfo.isValid(); }
240239
bool tryResolve(ValueDecl *D, TypeDecl *CtorTyRef, ExtensionDecl *ExtTyRef,
241240
SourceLoc Loc, bool IsRef, Type Ty = Type());

lib/IDE/SwiftSourceDocInfo.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,17 @@ bool CursorInfoResolver::walkToDeclPost(Decl *D) {
149149
}
150150

151151
bool CursorInfoResolver::walkToStmtPre(Stmt *S) {
152+
// Getting the character range for the statement, to account for interpolation
153+
// strings. The token range for the interpolation string is the whole string,
154+
// with begin/end locations pointing at the beginning of the string, so if
155+
// there is a token location inside the string, it will seem as if it is out
156+
// of the source range, unless we convert to character range.
157+
152158
// FIXME: Even implicit Stmts should have proper ranges that include any
153159
// non-implicit Stmts (fix Stmts created for lazy vars).
154-
if (!S->isImplicit() && !rangeContainsLoc(S->getSourceRange()))
160+
if (!S->isImplicit() &&
161+
!rangeContainsLoc(Lexer::getCharSourceRangeFromSourceRange(
162+
getSourceMgr(), S->getSourceRange())))
155163
return false;
156164
return !tryResolve(S);
157165
}
@@ -249,6 +257,14 @@ SourceManager &NameMatcher::getSourceMgr() const {
249257
return SrcFile.getASTContext().SourceMgr;
250258
}
251259

260+
bool CursorInfoResolver::rangeContainsLoc(SourceRange Range) const {
261+
return getSourceMgr().rangeContainsTokenLoc(Range, LocToResolve);
262+
}
263+
264+
bool CursorInfoResolver::rangeContainsLoc(CharSourceRange Range) const {
265+
return Range.contains(LocToResolve);
266+
}
267+
252268
std::vector<ResolvedLoc> NameMatcher::resolve(ArrayRef<UnresolvedLoc> Locs, ArrayRef<Token> Tokens) {
253269

254270
// Note the original indices and sort them in reverse source order

test/SourceKit/CursorInfo/cursor_info_expressions.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,26 @@ protocol P1 {}
22

33
struct S1 : P1 {}
44

5-
func test(_ o: P1?) {
5+
func test(_ o: P1?) -> String {
66
switch o {
77
case let s as S1:
88
test(s)
99
default:
1010
test(o)
1111
}
12+
13+
_ = "\(o)"
14+
return "\(o)"
1215
}
1316

1417
// RUN: %sourcekitd-test -req=cursor -pos=7:17 %s -- %s | %FileCheck -check-prefix=CHECK1 %s
1518
// CHECK1: source.lang.swift.ref.struct (3:8-3:10)
1619
// CHECK1: S1
20+
21+
// RUN: %sourcekitd-test -req=cursor -pos=13:10 %s -- %s | %FileCheck -check-prefix=CHECK2 %s
22+
// CHECK2: source.lang.swift.ref.var.local (5:13-5:14)
23+
// CHECK2: P1?
24+
25+
// RUN: %sourcekitd-test -req=cursor -pos=14:13 %s -- %s | %FileCheck -check-prefix=CHECK3 %s
26+
// CHECK3: source.lang.swift.ref.var.local (5:13-5:14)
27+
// CHECK3: P1?

0 commit comments

Comments
 (0)