Skip to content

Commit d0ac023

Browse files
committed
Apply requested changes
1 parent 18606ad commit d0ac023

File tree

6 files changed

+100
-16
lines changed

6 files changed

+100
-16
lines changed

lib/IDE/Refactoring.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3214,23 +3214,19 @@ class AddEquatableContext {
32143214
return false;
32153215
}
32163216
auto Req = dyn_cast<FuncDecl>(Reqs[0]);
3217-
auto Params = Req->getParameters();
3218-
if (!Req || Params->size() != 2) {
3219-
return false;
3220-
}
3221-
return true;
3217+
return Req && Req->getParameters()->size() == 2;
32223218
}
32233219

32243220
bool isPropertiesListValid() {
3225-
return !getPublicProperties().empty();
3221+
return !getUserAccessibleProperties().empty();
32263222
}
32273223

32283224
void printFunctionBody(ASTPrinter &Printer, StringRef ExtraIndent,
32293225
ParameterList *Params);
32303226

32313227
std::vector<ValueDecl *> getProtocolRequirements();
32323228

3233-
std::vector<VarDecl *> getPublicProperties();
3229+
std::vector<VarDecl *> getUserAccessibleProperties();
32343230

32353231
public:
32363232

@@ -3252,11 +3248,13 @@ class AddEquatableContext {
32523248

32533249
static AddEquatableContext getDeclarationContextFromInfo(ResolvedCursorInfo Info);
32543250

3255-
std::string getDeclForProtocol();
3251+
std::string getInsertionTextForProtocol();
32563252

3257-
std::string getDeclForFunction(SourceManager &SM);
3253+
std::string getInsertionTextForFunction(SourceManager &SM);
32583254

32593255
bool isValid() {
3256+
// FIXME: Allow to generate explicit == method for declarations which already have
3257+
// compiler-generated == method
32603258
return StartLoc.isValid() && ProtInsertStartLoc.isValid() &&
32613259
!conformsToEquatableProtocol() && isPropertiesListValid() &&
32623260
isRequirementValid();
@@ -3289,7 +3287,7 @@ getInsertStartLoc() {
32893287
}
32903288

32913289
std::string AddEquatableContext::
3292-
getDeclForProtocol() {
3290+
getInsertionTextForProtocol() {
32933291
StringRef ProtocolName = getProtocolName(KnownProtocolKind::Equatable);
32943292
std::string Buffer;
32953293
llvm::raw_string_ostream OS(Buffer);
@@ -3302,7 +3300,7 @@ getDeclForProtocol() {
33023300
}
33033301

33043302
std::string AddEquatableContext::
3305-
getDeclForFunction(SourceManager &SM) {
3303+
getInsertionTextForFunction(SourceManager &SM) {
33063304
auto Reqs = getProtocolRequirements();
33073305
auto Req = dyn_cast<FuncDecl>(Reqs[0]);
33083306
auto Params = Req->getParameters();
@@ -3337,10 +3335,10 @@ getDeclForFunction(SourceManager &SM) {
33373335
}
33383336

33393337
std::vector<VarDecl *> AddEquatableContext::
3340-
getPublicProperties() {
3338+
getUserAccessibleProperties() {
33413339
std::vector<VarDecl *> PublicProperties;
33423340
for (VarDecl *Decl : StoredProperties) {
3343-
if (!Decl->hasPrivateAccessor()) {
3341+
if (Decl->Decl::isUserAccessible()) {
33443342
PublicProperties.push_back(Decl);
33453343
}
33463344
}
@@ -3386,7 +3384,7 @@ printFunctionBody(ASTPrinter &Printer, StringRef ExtraIndent, ParameterList *Par
33863384
StringRef Point = ".";
33873385
StringRef Join = " == ";
33883386
StringRef And = " &&";
3389-
auto Props = getPublicProperties();
3387+
auto Props = getUserAccessibleProperties();
33903388
auto FParam = Params->get(0)->getName();
33913389
auto SParam = Params->get(1)->getName();
33923390
auto Prop = Props[0]->getName();
@@ -3412,9 +3410,9 @@ bool RefactoringActionAddEquatableConformance::
34123410
performChange() {
34133411
auto Context = AddEquatableContext::getDeclarationContextFromInfo(CursorInfo);
34143412
EditConsumer.insertAfter(SM, Context.getStartLocForProtocolDecl(),
3415-
Context.getDeclForProtocol());
3413+
Context.getInsertionTextForProtocol());
34163414
EditConsumer.insertAfter(SM, Context.getInsertStartLoc(),
3417-
Context.getDeclForFunction(SM));
3415+
Context.getInsertionTextForFunction(SM));
34183416
return false;
34193417
}
34203418

test/refactoring/AddEquatableConformance/Outputs/basic/first.swift.expected

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class TestAddEquatable: Equatable {
55

66
static func == (lhs: TestAddEquatable, rhs: TestAddEquatable) -> Bool {
77
return lhs.property == rhs.property &&
8+
lhs.prop == rhs.prop &&
89
lhs.pr == rhs.pr
910
}
1011
}
@@ -15,5 +16,9 @@ extension TestAddEquatable {
1516
}
1617
}
1718

19+
extension TestAddEquatable {
20+
}
21+
22+
1823

1924

test/refactoring/AddEquatableConformance/Outputs/basic/second.swift.expected

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@ extension TestAddEquatable: Equatable {
1111

1212
static func == (lhs: TestAddEquatable, rhs: TestAddEquatable) -> Bool {
1313
return lhs.property == rhs.property &&
14+
lhs.prop == rhs.prop &&
1415
lhs.pr == rhs.pr
1516
}
1617
}
1718

19+
extension TestAddEquatable {
20+
}
21+
22+
1823

1924

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class TestAddEquatable {
2+
var property = "test"
3+
private var prop = "test2"
4+
let pr = "test3"
5+
}
6+
7+
extension TestAddEquatable {
8+
func test() -> Bool {
9+
return true
10+
}
11+
}
12+
13+
extension TestAddEquatable: Equatable {
14+
static func == (lhs: TestAddEquatable, rhs: TestAddEquatable) -> Bool {
15+
return lhs.property == rhs.property &&
16+
lhs.prop == rhs.prop &&
17+
lhs.pr == rhs.pr
18+
}
19+
}
20+
21+
22+
23+

test/refactoring/AddEquatableConformance/basic.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@ extension TestAddEquatable {
1010
}
1111
}
1212

13+
extension TestAddEquatable {
14+
}
15+
1316
// RUN: rm -rf %t.result && mkdir -p %t.result
1417

1518
// RUN: %refactor -add-equatable-conformance -source-filename %s -pos=1:16 > %t.result/first.swift
1619
// RUN: diff -u %S/Outputs/basic/first.swift.expected %t.result/first.swift
1720

1821
// RUN: %refactor -add-equatable-conformance -source-filename %s -pos=7:13 > %t.result/second.swift
1922
// RUN: diff -u %S/Outputs/basic/second.swift.expected %t.result/second.swift
23+
24+
// RUN: %refactor -add-equatable-conformance -source-filename %s -pos=13:13 > %t.result/third.swift
25+
// RUN: diff -u %S/Outputs/basic/third.swift.expected %t.result/third.swift

test/refactoring/RefactoringKind/basic.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,43 @@ class TestAddEquatable {
302302
let pr = "test3"
303303
}
304304

305+
struct TestAddEquatableStruct {
306+
var property = "test"
307+
private var prop = "test2"
308+
let pr = "test3"
309+
}
310+
311+
enum AddEquatableEnum {
312+
case first
313+
case second
314+
}
315+
316+
class TestAddEquatableConforming: Equatable {
317+
var property = "test"
318+
319+
public static func ==(lhs: TestAddEquatableConforming,
320+
rhs: TestAddEquatableConforming) -> Bool {
321+
return lhs.property == rhs.property
322+
}
323+
}
324+
325+
struct TestAddEquatableStructConforming: Equatable {
326+
var property = "test"
327+
}
328+
329+
extension TestAddEquatable {
330+
func test() -> Bool {
331+
return false
332+
}
333+
}
334+
335+
extension TestAddEquatableStructConforming: Equatable {
336+
public static func ==(lhs: TestAddEquatableConforming,
337+
rhs: TestAddEquatableConforming) -> Bool {
338+
return lhs.property == rhs.property
339+
}
340+
}
341+
305342
// RUN: %refactor -source-filename %s -pos=2:1 -end-pos=5:13 | %FileCheck %s -check-prefix=CHECK1
306343
// RUN: %refactor -source-filename %s -pos=3:1 -end-pos=5:13 | %FileCheck %s -check-prefix=CHECK1
307344
// RUN: %refactor -source-filename %s -pos=4:1 -end-pos=5:13 | %FileCheck %s -check-prefix=CHECK1
@@ -404,6 +441,12 @@ class TestAddEquatable {
404441
// RUN: %refactor -source-filename %s -pos=292:3 -end-pos=296:4 | %FileCheck %s -check-prefix=CHECK-IS-NOT-CONVERT-TO-COMPUTED-PROPERTY
405442

406443
// RUN: %refactor -source-filename %s -pos=299:16 | %FileCheck %s -check-prefix=CHECK-ADD-EQUATABLE-CONFORMANCE
444+
// RUN: %refactor -source-filename %s -pos=305:12 | %FileCheck %s -check-prefix=CHECK-ADD-EQUATABLE-CONFORMANCE
445+
// RUN: %refactor -source-filename %s -pos=311:9 | %FileCheck %s -check-prefix=CHECK-ADD-EQUATABLE-CONFORMANCE-NOT-INCLUDED
446+
// RUN: %refactor -source-filename %s -pos=316:11 | %FileCheck %s -check-prefix=CHECK-ADD-EQUATABLE-CONFORMANCE-NOT-INCLUDED
447+
// RUN: %refactor -source-filename %s -pos=325:12 | %FileCheck %s -check-prefix=CHECK-ADD-EQUATABLE-CONFORMANCE-NOT-INCLUDED
448+
// RUN: %refactor -source-filename %s -pos=329:15 | %FileCheck %s -check-prefix=CHECK-ADD-EQUATABLE-CONFORMANCE
449+
// RUN: %refactor -source-filename %s -pos=335:15 | %FileCheck %s -check-prefix=CHECK-ADD-EQUATABLE-CONFORMANCE-NOT-INCLUDED
407450

408451
// CHECK1: Action begins
409452
// CHECK1-NEXT: Extract Method
@@ -463,3 +506,7 @@ class TestAddEquatable {
463506
// CHECK-IS-NOT-CONVERT-TO-COMPUTED-PROPERTY: Action ends
464507

465508
// CHECK-ADD-EQUATABLE-CONFORMANCE: Add Equatable Conformance
509+
510+
// CHECK-ADD-EQUATABLE-CONFORMANCE-NOT-INCLUDED: Action begins
511+
// CHECK-ADD-EQUATABLE-CONFORMANCE-NOT-INCLUDED-NOT: Add Equatable Conformance
512+
// CHECK-ADD-EQUATABLE-CONFORMANCE-NOT-INCLUDED: Action ends

0 commit comments

Comments
 (0)