Skip to content

Commit 3b99e90

Browse files
authored
Merge pull request #38920 from apple/show-cursor-refactorings
[Refactorings] Add cursor refactorings for the start of the range
2 parents afaa9e1 + c7620fa commit 3b99e90

File tree

5 files changed

+51
-68
lines changed

5 files changed

+51
-68
lines changed

lib/IDE/Refactoring.cpp

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3788,43 +3788,18 @@ bool RefactoringActionTrailingClosure::performChange() {
37883788
return false;
37893789
}
37903790

3791-
static bool rangeStartMayNeedRename(const ResolvedRangeInfo &Info) {
3792-
switch(Info.Kind) {
3793-
case RangeKind::SingleExpression: {
3794-
Expr *E = Info.ContainedNodes[0].get<Expr*>();
3795-
// We should show rename for the selection of "foo()"
3796-
if (auto *CE = dyn_cast<CallExpr>(E)) {
3797-
if (CE->getFn()->getKind() == ExprKind::DeclRef)
3798-
return true;
3799-
3800-
// When callling an instance method inside another instance method,
3801-
// we have a dot syntax call whose dot and base are both implicit. We
3802-
// need to explicitly allow the specific case here.
3803-
if (auto *DSC = dyn_cast<DotSyntaxCallExpr>(CE->getFn())) {
3804-
if (DSC->getBase()->isImplicit() &&
3805-
DSC->getFn()->getStartLoc() == Info.TokensInRange.front().getLoc())
3806-
return true;
3807-
}
3808-
}
3809-
return false;
3810-
}
3811-
case RangeKind::PartOfExpression: {
3812-
if (auto *CE = dyn_cast<CallExpr>(Info.CommonExprParent)) {
3813-
if (auto *DSC = dyn_cast<DotSyntaxCallExpr>(CE->getFn())) {
3814-
if (DSC->getFn()->getStartLoc() == Info.TokensInRange.front().getLoc())
3815-
return true;
3816-
}
3817-
}
3818-
return false;
3819-
}
3820-
case RangeKind::SingleDecl:
3821-
case RangeKind::MultiTypeMemberDecl:
3822-
case RangeKind::SingleStatement:
3823-
case RangeKind::MultiStatement:
3824-
case RangeKind::Invalid:
3825-
return false;
3791+
static bool collectRangeStartRefactorings(const ResolvedRangeInfo &Info) {
3792+
switch (Info.Kind) {
3793+
case RangeKind::SingleExpression:
3794+
case RangeKind::SingleStatement:
3795+
case RangeKind::SingleDecl:
3796+
case RangeKind::PartOfExpression:
3797+
return true;
3798+
case RangeKind::MultiStatement:
3799+
case RangeKind::MultiTypeMemberDecl:
3800+
case RangeKind::Invalid:
3801+
return false;
38263802
}
3827-
llvm_unreachable("unhandled kind");
38283803
}
38293804

38303805
bool RefactoringActionConvertToComputedProperty::
@@ -8293,7 +8268,7 @@ void swift::ide::collectAvailableRefactorings(
82938268
}
82948269

82958270
void swift::ide::collectAvailableRefactorings(
8296-
SourceFile *SF, RangeConfig Range, bool &RangeStartMayNeedRename,
8271+
SourceFile *SF, RangeConfig Range, bool &CollectRangeStartRefactorings,
82978272
SmallVectorImpl<RefactoringKind> &Kinds,
82988273
ArrayRef<DiagnosticConsumer *> DiagConsumers) {
82998274
if (Range.Length == 0) {
@@ -8322,7 +8297,7 @@ void swift::ide::collectAvailableRefactorings(
83228297
RANGE_REFACTORING(KIND, NAME, ID)
83238298
#include "swift/IDE/RefactoringKinds.def"
83248299

8325-
RangeStartMayNeedRename = rangeStartMayNeedRename(Result);
8300+
CollectRangeStartRefactorings = collectRangeStartRefactorings(Result);
83268301
}
83278302

83288303
bool swift::ide::

test/SourceKit/Refactoring/basic.swift

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,12 @@ HasInitWithDefaultArgs(y: 45, z: 89)
116116
func `hasBackticks`(`x`: Int) {}
117117
`hasBackticks`(`x`:2)
118118

119-
func hasAsyncAlternative(completion: @escaping (String?, Error?) -> Void) { }
120-
func hasCallToAsyncAlternative() {
121-
hasAsyncAlternative { str, err in print(str!) }
119+
struct ConvertAsync {
120+
func hasAsyncAlternative(completion: @escaping (String?, Error?) -> Void) { }
121+
}
122+
func hasCallToAsyncAlternative(c: ConvertAsync) {
123+
((((c)).hasAsyncAlternative)) { str, err in print(str!) }
124+
c.hasAsyncAlternative() { str, err in print(str!) }
122125
}
123126

124127
// RUN: %sourcekitd-test -req=cursor -pos=3:1 -end-pos=5:13 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK1
@@ -167,12 +170,9 @@ func hasCallToAsyncAlternative() {
167170
// RUN: %sourcekitd-test -req=cursor -pos=117:16 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-GLOBAL
168171
// RUN: %sourcekitd-test -req=cursor -pos=117:17 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-GLOBAL
169172

170-
// RUN: %sourcekitd-test -req=cursor -pos=119:6 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-ASYNC
171-
// RUN: %sourcekitd-test -req=cursor -pos=121:3 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-CALLASYNC
172-
173-
// RUN: %sourcekitd-test -req=cursor -pos=35:10 -end-pos=35:16 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-RENAME-EXTRACT
174173
// RUN: %sourcekitd-test -req=cursor -pos=35:10 -end-pos=35:16 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-RENAME-EXTRACT
175174

175+
// RUN: %sourcekitd-test -req=cursor -pos=54:10 -end-pos=54:22 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-LOCAL
176176
// RUN: %sourcekitd-test -req=cursor -pos=54:12 -end-pos=54:22 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-SELF-RENAME1
177177
// RUN: %sourcekitd-test -req=cursor -pos=54:23 -end-pos=54:33 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-SELF-RENAME2
178178
// RUN: %sourcekitd-test -req=cursor -pos=54:34 -end-pos=54:44 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-SELF-RENAME3
@@ -184,6 +184,17 @@ func hasCallToAsyncAlternative() {
184184
// RUN: %sourcekitd-test -req=cursor -pos=72:5 -end-pos=72:11 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-RENAME-EXTRACT
185185
// RUN: %sourcekitd-test -req=cursor -pos=78:3 -end-pos=78:9 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-RENAME-EXTRACT
186186

187+
// RUN: %sourcekitd-test -req=cursor -pos=120:8 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-ASYNC
188+
// RUN: %sourcekitd-test -req=cursor -pos=123:11 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-CALLASYNC
189+
// RUN: %sourcekitd-test -req=cursor -pos=123:11 -end-pos=123:30 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-CALLASYNC
190+
// RUN: %sourcekitd-test -req=cursor -pos=123:3 -end-pos=123:30 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-CALLASYNC
191+
// RUN: %sourcekitd-test -req=cursor -pos=123:3 -end-pos=123:60 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-CALLASYNC
192+
// RUN: %sourcekitd-test -req=cursor -pos=123:3 -end-pos=123:46 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-CALLASYNC
193+
// RUN: %sourcekitd-test -req=cursor -pos=123:3 -end-pos=123:58 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-CALLASYNC
194+
// RUN: %sourcekitd-test -req=cursor -pos=124:3 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-CALLASYNC
195+
// RUN: %sourcekitd-test -req=cursor -pos=124:3 -end-pos=124:26 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-CALLASYNC
196+
// RUN: %sourcekitd-test -req=cursor -pos=124:3 -end-pos=124:54 -cursor-action %s -- %s | %FileCheck %s -check-prefix=CHECK-CALLASYNC
197+
187198
// CHECK-NORENAME-NOT: Global Rename
188199
// CHECK-NORENAME-NOT: Local Rename
189200

test/SourceKit/Refactoring/rename-objc.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@ func foo1() {
88
// RUN: %sourcekitd-test -req=cursor -pos=4:30 -cursor-action %s -- -F %S/Inputs/mock-sdk -I %t.tmp %s | %FileCheck %s -check-prefix=CHECK1
99
// RUN: %sourcekitd-test -req=cursor -pos=4:30 -length=3 -cursor-action %s -- -F %S/Inputs/mock-sdk -I %t.tmp %s | %FileCheck %s -check-prefix=CHECK1
1010
// RUN: %sourcekitd-test -req=cursor -pos=4:20 -length=15 -cursor-action %s -- -F %S/Inputs/mock-sdk -I %t.tmp %s | %FileCheck %s -check-prefix=CHECK1
11-
// RUN: %sourcekitd-test -req=cursor -pos=4:20 -length=16 -cursor-action %s -- -F %S/Inputs/mock-sdk -I %t.tmp %s | %FileCheck %s -check-prefix=CHECK2
11+
// RUN: %sourcekitd-test -req=cursor -pos=4:20 -length=16 -cursor-action %s -- -F %S/Inputs/mock-sdk -I %t.tmp %s | %FileCheck %s -check-prefix=CHECK1
1212

1313
// CHECK1: ACTIONS BEGIN
1414
// CHECK1-NEXT: source.refactoring.kind.rename.global
1515
// CHECK1-NEXT: Global Rename
1616
// CHECK1-NEXT: cannot rename a Clang symbol from its Swift reference
1717

18-
// CHECK2: ACTIONS BEGIN
19-
// CHECK2-NEXT: ACTIONS END
20-
2118
// REQUIRES: OS=macosx || OS=linux-gnu

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,24 +1451,24 @@ static void resolveCursor(
14511451
Range.Line = Pair.first;
14521452
Range.Column = Pair.second;
14531453
Range.Length = Length;
1454-
bool RangeStartMayNeedRename = false;
1454+
bool CollectRangeStartRefactorings = false;
14551455
collectAvailableRefactorings(&AstUnit->getPrimarySourceFile(), Range,
1456-
RangeStartMayNeedRename, Kinds, {});
1456+
CollectRangeStartRefactorings, Kinds, {});
14571457
for (RefactoringKind Kind : Kinds) {
14581458
Actions.emplace_back(SwiftLangSupport::getUIDForRefactoringKind(Kind),
14591459
getDescriptiveRefactoringKindName(Kind),
14601460
/*UnavailableReason*/ StringRef());
14611461
}
1462-
if (!RangeStartMayNeedRename) {
1463-
// If Length is given, then the cursor-info request should only about
1464-
// collecting available refactorings for the range.
1462+
if (!CollectRangeStartRefactorings) {
1463+
// If Length is given then this request is only for refactorings,
1464+
// return straight away unless we need cursor based refactorings as
1465+
// well.
14651466
CursorInfoData Data;
14661467
Data.AvailableActions = llvm::makeArrayRef(Actions);
14671468
Receiver(RequestResult<CursorInfoData>::fromResult(Data));
14681469
return;
14691470
}
1470-
// If the range start may need rename, we fall back to a regular cursor
1471-
// info request to get the available rename kinds.
1471+
// Fall through to collect cursor based refactorings
14721472
}
14731473

14741474
auto *File = &AstUnit->getPrimarySourceFile();
@@ -1477,12 +1477,6 @@ static void resolveCursor(
14771477
CursorInfoRequest{CursorInfoOwner(File, Loc)},
14781478
ResolvedCursorInfo());
14791479

1480-
if (CursorInfo.isInvalid()) {
1481-
CursorInfoData Info;
1482-
Info.InternalDiagnostic = "Unable to resolve cursor info.";
1483-
Receiver(RequestResult<CursorInfoData>::fromResult(Info));
1484-
return;
1485-
}
14861480
CompilerInvocation CompInvok;
14871481
ASTInvok->applyTo(CompInvok);
14881482

@@ -1529,9 +1523,15 @@ static void resolveCursor(
15291523
Receiver(RequestResult<CursorInfoData>::fromResult(Info));
15301524
return;
15311525
}
1532-
case CursorInfoKind::Invalid: {
1533-
llvm_unreachable("bad sema token kind");
1534-
}
1526+
case CursorInfoKind::Invalid:
1527+
CursorInfoData Data;
1528+
if (Actionables) {
1529+
Data.AvailableActions = llvm::makeArrayRef(Actions);
1530+
} else {
1531+
Data.InternalDiagnostic = "Unable to resolve cursor info.";
1532+
}
1533+
Receiver(RequestResult<CursorInfoData>::fromResult(Data));
1534+
return;
15351535
}
15361536
}
15371537

tools/swift-refactor/swift-refactor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,9 @@ int main(int argc, char *argv[]) {
404404

405405
if (options::Action == RefactoringKind::None) {
406406
llvm::SmallVector<RefactoringKind, 32> Kinds;
407-
bool RangeStartMayNeedRename = false;
408-
collectAvailableRefactorings(SF, Range, RangeStartMayNeedRename, Kinds,
409-
{&PrintDiags});
407+
bool CollectRangeStartRefactorings = false;
408+
collectAvailableRefactorings(SF, Range, CollectRangeStartRefactorings,
409+
Kinds, {&PrintDiags});
410410
llvm::outs() << "Action begins\n";
411411
for (auto Kind : Kinds) {
412412
llvm::outs() << getDescriptiveRefactoringKindName(Kind) << "\n";

0 commit comments

Comments
 (0)