Skip to content

[5.0][sourcekitd] Fix cursor resolving when pointing inside string interpolations of return statements #14109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions include/swift/IDE/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,8 @@ class CursorInfoResolver : public SourceEntityWalker {
bool visitDeclarationArgumentName(Identifier Name, SourceLoc StartLoc,
ValueDecl *D) override;
bool visitModuleReference(ModuleEntity Mod, CharSourceRange Range) override;
bool rangeContainsLoc(SourceRange Range) const {
return getSourceMgr().rangeContainsTokenLoc(Range, LocToResolve);
}
bool rangeContainsLoc(SourceRange Range) const;
bool rangeContainsLoc(CharSourceRange Range) const;
bool isDone() const { return CursorInfo.isValid(); }
bool tryResolve(ValueDecl *D, TypeDecl *CtorTyRef, ExtensionDecl *ExtTyRef,
SourceLoc Loc, bool IsRef, Type Ty = Type());
Expand Down
18 changes: 17 additions & 1 deletion lib/IDE/SwiftSourceDocInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,17 @@ bool CursorInfoResolver::walkToDeclPost(Decl *D) {
}

bool CursorInfoResolver::walkToStmtPre(Stmt *S) {
// Getting the character range for the statement, to account for interpolation
// strings. The token range for the interpolation string is the whole string,
// with begin/end locations pointing at the beginning of the string, so if
// there is a token location inside the string, it will seem as if it is out
// of the source range, unless we convert to character range.

// FIXME: Even implicit Stmts should have proper ranges that include any
// non-implicit Stmts (fix Stmts created for lazy vars).
if (!S->isImplicit() && !rangeContainsLoc(S->getSourceRange()))
if (!S->isImplicit() &&
!rangeContainsLoc(Lexer::getCharSourceRangeFromSourceRange(
getSourceMgr(), S->getSourceRange())))
return false;
return !tryResolve(S);
}
Expand Down Expand Up @@ -249,6 +257,14 @@ SourceManager &NameMatcher::getSourceMgr() const {
return SrcFile.getASTContext().SourceMgr;
}

bool CursorInfoResolver::rangeContainsLoc(SourceRange Range) const {
return getSourceMgr().rangeContainsTokenLoc(Range, LocToResolve);
}

bool CursorInfoResolver::rangeContainsLoc(CharSourceRange Range) const {
return Range.contains(LocToResolve);
}

std::vector<ResolvedLoc> NameMatcher::resolve(ArrayRef<UnresolvedLoc> Locs, ArrayRef<Token> Tokens) {

// Note the original indices and sort them in reverse source order
Expand Down
13 changes: 12 additions & 1 deletion test/SourceKit/CursorInfo/cursor_info_expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@ protocol P1 {}

struct S1 : P1 {}

func test(_ o: P1?) {
func test(_ o: P1?) -> String {
switch o {
case let s as S1:
test(s)
default:
test(o)
}

_ = "\(o)"
return "\(o)"
}

// RUN: %sourcekitd-test -req=cursor -pos=7:17 %s -- %s | %FileCheck -check-prefix=CHECK1 %s
// CHECK1: source.lang.swift.ref.struct (3:8-3:10)
// CHECK1: S1

// RUN: %sourcekitd-test -req=cursor -pos=13:10 %s -- %s | %FileCheck -check-prefix=CHECK2 %s
// CHECK2: source.lang.swift.ref.var.local (5:13-5:14)
// CHECK2: P1?

// RUN: %sourcekitd-test -req=cursor -pos=14:13 %s -- %s | %FileCheck -check-prefix=CHECK3 %s
// CHECK3: source.lang.swift.ref.var.local (5:13-5:14)
// CHECK3: P1?