Skip to content

Commit 32e366a

Browse files
committed
[SourceKit] Migrate renameAvailabilityInfo to use the SwiftSyntax NameMatcher
The `NameMatcher` implemented in swift-syntax as slightly different semantics because it consideres `(callable.|callAsFunction)(x: 78)` as a reference to `callAsFunction` instead of a call with argument labels `(x: 78)`, which means that one test needs to be updated.
1 parent 35eb5cb commit 32e366a

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

lib/Refactoring/LocalRename.cpp

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "swift/AST/USRGeneration.h"
1717
#include "swift/Basic/StringExtras.h"
1818
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
19+
#include "swift/IDE/IDEBridging.h"
1920
#include "swift/Index/Index.h"
2021

2122
using namespace swift::refactoring;
@@ -44,6 +45,31 @@ struct RenameRefInfo {
4445
bool IsArgLabel; ///< Whether Loc is on an arg label, rather than base name.
4546
};
4647

48+
#if SWIFT_BUILD_SWIFT_SYNTAX
49+
/// Returns `true` if the `RefInfo` points to a location that doesn't have any
50+
/// arguments. For example, returns `true` for `Foo.init` but `false` for
51+
/// `Foo.init()` or `Foo.init(a: 1)`.
52+
static bool
53+
isReferenceWithoutArguments(const llvm::Optional<RenameRefInfo> &refInfo) {
54+
if (!refInfo) {
55+
return false;
56+
}
57+
if (refInfo->IsArgLabel) {
58+
return false;
59+
}
60+
BridgedSourceLoc Loc(refInfo->Loc);
61+
BridgedResolvedLocVector bridgedResolvedLocs =
62+
swift_SwiftIDEUtilsBridging_runNameMatcher(
63+
refInfo->SF->getExportedSourceFile(), &Loc, 1);
64+
std::vector<ResolvedLoc> resolvedLocs = bridgedResolvedLocs.takeUnbridged();
65+
if (!resolvedLocs.empty()) {
66+
ResolvedLoc resolvedLoc = resolvedLocs.front();
67+
return resolvedLoc.labelRanges.empty();
68+
}
69+
return false;
70+
}
71+
#endif // SWIFT_BUILD_SWIFT_SYNTAX
72+
4773
static llvm::Optional<RefactorAvailabilityInfo>
4874
renameAvailabilityInfo(const ValueDecl *VD,
4975
llvm::Optional<RenameRefInfo> RefInfo) {
@@ -88,12 +114,11 @@ renameAvailabilityInfo(const ValueDecl *VD,
88114
if (!CD->getParameters()->size())
89115
return llvm::None;
90116

91-
if (RefInfo && !RefInfo->IsArgLabel) {
92-
NameMatcher Matcher(*(RefInfo->SF));
93-
auto Resolved = Matcher.resolve({RefInfo->Loc});
94-
if (Resolved.labelRanges.empty())
95-
return llvm::None;
117+
#if SWIFT_BUILD_SWIFT_SYNTAX
118+
if (isReferenceWithoutArguments(RefInfo)) {
119+
return llvm::None;
96120
}
121+
#endif
97122
}
98123

99124
// Disallow renaming 'callAsFunction' method with no arguments.
@@ -104,12 +129,11 @@ renameAvailabilityInfo(const ValueDecl *VD,
104129
if (!FD->getParameters()->size())
105130
return llvm::None;
106131

107-
if (RefInfo && !RefInfo->IsArgLabel) {
108-
NameMatcher Matcher(*(RefInfo->SF));
109-
auto Resolved = Matcher.resolve({RefInfo->Loc});
110-
if (Resolved.labelRanges.empty())
111-
return llvm::None;
132+
#if SWIFT_BUILD_SWIFT_SYNTAX
133+
if (isReferenceWithoutArguments(RefInfo)) {
134+
return llvm::None;
112135
}
136+
#endif
113137
}
114138
}
115139
}

lib/Refactoring/SyntacticRename.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ swift::ide::resolveRenameLocations(ArrayRef<RenameLoc> RenameLocs,
8484
swift_SwiftIDEUtilsBridging_runNameMatcher(SF.getExportedSourceFile(),
8585
BridgedUnresolvedLocs.data(),
8686
BridgedUnresolvedLocs.size());
87-
const std::vector<ResolvedLoc> &resolvedLocsInSourceOrder =
87+
std::vector<ResolvedLoc> resolvedLocsInSourceOrder =
8888
bridgedResolvedLocs.takeUnbridged();
8989

9090
// Callers need to corrolate the `ResolvedLoc` with the `RenameLoc` that they

test/SourceKit/Refactoring/basic.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func hasCallToAsyncAlternative(c: ConvertAsync) {
152152
// RUN: %sourcekitd-test -req=cursor -pos=95:10 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-GLOBAL
153153
// RUN: %sourcekitd-test -req=cursor -pos=96:10 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-GLOBAL
154154
// RUN: %sourcekitd-test -req=cursor -pos=96:25 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-GLOBAL
155-
// RUN: %sourcekitd-test -req=cursor -pos=97:11 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-GLOBAL
155+
// RUN: %sourcekitd-test -req=cursor -pos=97:11 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-NORENAME
156156
// RUN: %sourcekitd-test -req=cursor -pos=97:27 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-GLOBAL
157157
// RUN: %sourcekitd-test -req=cursor -pos=98:11 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-NORENAME
158158

0 commit comments

Comments
 (0)