Skip to content

Commit 743d3a5

Browse files
author
Nathan Hawes
authored
Merge pull request #14980 from nathawes/rdar37905156-rename-init-gives-empty-categorised-edit-ranges
[refactoring] Avoid producing empty string/comment categorized edit ranges when renaming init. Also disallow renaming inits with no arguments.
2 parents 7f1ba5d + 91ec998 commit 743d3a5

File tree

7 files changed

+148
-1
lines changed

7 files changed

+148
-1
lines changed

lib/IDE/Refactoring.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,14 @@ class Renamer {
313313
bool IsSubscript = Old.base() == "subscript" && Config.IsFunctionLike;
314314
bool IsInit = Old.base() == "init" && Config.IsFunctionLike;
315315
bool IsKeywordBase = IsInit || IsSubscript;
316+
317+
// Filter out non-semantic keyword basename locations with no labels.
318+
// We've already filtered out those in active code, so these are
319+
// any appearance of just 'init' or 'subscript' in strings, comments, and
320+
// inactive code.
321+
if (IsKeywordBase && (Config.Usage == NameUsage::Unknown &&
322+
Resolved.LabelType == LabelRangeType::None))
323+
return RegionType::Unmatched;
316324

317325
if (!Config.IsFunctionLike || !IsKeywordBase) {
318326
if (renameBase(Resolved.Range, RefactoringRangeKind::BaseName))
@@ -352,7 +360,8 @@ class Renamer {
352360
Resolved.LabelType == LabelRangeType::CallArg;
353361

354362
if (renameLabels(Resolved.LabelRanges, Resolved.LabelType, isCallSite))
355-
return RegionType::Mismatch;
363+
return Config.Usage == NameUsage::Unknown ?
364+
RegionType::Unmatched : RegionType::Mismatch;
356365
}
357366

358367
return RegionKind;
@@ -2895,6 +2904,8 @@ swift::ide::FindRenameRangesAnnotatingConsumer::~FindRenameRangesAnnotatingConsu
28952904
void swift::ide::FindRenameRangesAnnotatingConsumer::
28962905
accept(SourceManager &SM, RegionType RegionType,
28972906
ArrayRef<RenameRangeDetail> Ranges) {
2907+
if (RegionType == RegionType::Mismatch || RegionType == RegionType::Unmatched)
2908+
return;
28982909
for (const auto &Range : Ranges) {
28992910
Impl.accept(SM, Range);
29002911
}
@@ -2923,6 +2934,12 @@ swift::ide::collectRenameAvailabilityInfo(const ValueDecl *VD,
29232934
// Disallow renaming deinit.
29242935
if (isa<DestructorDecl>(VD))
29252936
return Scratch;
2937+
2938+
// Disallow renaming init with no arguments.
2939+
if (auto CD = dyn_cast<ConstructorDecl>(VD)) {
2940+
if (!CD->getParameters()->size())
2941+
return Scratch;
2942+
}
29262943
}
29272944

29282945
// Always return local rename for parameters.

lib/IDE/Utils.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,10 @@ swift::ide::SourceEditOutputConsumer::~SourceEditOutputConsumer() { delete &Impl
940940
void swift::ide::SourceEditOutputConsumer::
941941
accept(SourceManager &SM, RegionType RegionType,
942942
ArrayRef<Replacement> Replacements) {
943+
// ignore mismatched or
944+
if (RegionType == RegionType::Unmatched || RegionType == RegionType::Mismatch)
945+
return;
946+
943947
for (const auto &Replacement : Replacements) {
944948
Impl.accept(SM, Replacement.Range, Replacement.Text);
945949
}

test/SourceKit/CursorInfo/cursor_rename.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
class Foo {
22
func foo() -> Int { return foo() }
3+
init() {}
4+
init(_ a: Int) {}
35
}
6+
_ = Foo.init()
7+
_ = Foo.init(2)
48

59
// RUN: %sourcekitd-test -req=cursor -pos=1:9 -cursor-action %s -- %s | %FileCheck -check-prefix=CHECK1 %s
610
// RUN: %sourcekitd-test -req=cursor -pos=2:9 %s -- %s | %FileCheck -check-prefix=CHECK2 %s
711
// RUN: %sourcekitd-test -req=cursor -pos=2:32 -cursor-action %s -- %s | %FileCheck -check-prefix=CHECK1 %s
812
// RUN: %sourcekitd-test -req=cursor -pos=2:18 %s -- %s | %FileCheck -check-prefix=CHECK2 %s
13+
// RUN: %sourcekitd-test -req=cursor -pos=3:3 -cursor-action %s -- %s | %FileCheck -check-prefix=CHECK2 %s
14+
// RUN: %sourcekitd-test -req=cursor -pos=4:3 -cursor-action %s -- %s | %FileCheck -check-prefix=CHECK1 %s
15+
// RUN: %sourcekitd-test -req=cursor -pos=6:9 -cursor-action %s -- %s | %FileCheck -check-prefix=CHECK2 %s
16+
// RUN: %sourcekitd-test -req=cursor -pos=7:9 -cursor-action %s -- %s | %FileCheck -check-prefix=CHECK1 %s
917

1018
// CHECK1: Rename
1119
// CHECK2-NOT: Rename
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
source.edit.kind.active:
2+
95:17-95:18 source.refactoring.range.kind.call-argument-label arg-index=0
3+
95:18-95:20 source.refactoring.range.kind.call-argument-colon arg-index=0
4+
source.edit.kind.active:
5+
96:22-96:23 source.refactoring.range.kind.call-argument-label arg-index=0
6+
96:23-96:25 source.refactoring.range.kind.call-argument-colon arg-index=0
7+
source.edit.kind.unknown:
8+
source.edit.kind.active:
9+
97:37-97:38 source.refactoring.range.kind.call-argument-label arg-index=0
10+
97:38-97:40 source.refactoring.range.kind.call-argument-colon arg-index=0
11+
source.edit.kind.active:
12+
97:59-97:60 source.refactoring.range.kind.call-argument-label arg-index=0
13+
97:60-97:62 source.refactoring.range.kind.call-argument-colon arg-index=0
14+
source.edit.kind.unknown:
15+
source.edit.kind.unknown:
16+
source.edit.kind.unknown:
17+
source.edit.kind.unknown:
18+
source.edit.kind.inactive:
19+
103:22-103:23 source.refactoring.range.kind.call-argument-label arg-index=0
20+
103:23-103:25 source.refactoring.range.kind.call-argument-colon arg-index=0
21+
source.edit.kind.unknown:

test/SourceKit/Refactoring/syntactic-rename.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@ func genfoo<T: P, U, V where U: P>(x: T, y: U, z: V, a: P) -> P where V: P {
9292
fatalError()
9393
}
9494

95+
_ = Memberwise1(x: 2)
96+
_ = Memberwise1.init(x: 2)
97+
_ = Memberwise2.init(m: Memberwise1(x: 2), n: Memberwise1(x: 34))
98+
_ = " this init is init "
99+
// this init is init too
100+
101+
#if NOTDEFINED
102+
_ = Memberwise1(x: 2)
103+
_ = Memberwise1.init(x: 2)
104+
_ = Memberwise2.init(m: 2, n: Memberwise1(x: 34))
105+
#endif
106+
95107
// RUN: rm -rf %t.result && mkdir -p %t.result
96108
// RUN: %sourcekitd-test -req=syntactic-rename -rename-spec %S/syntactic-rename/x.in.json %s >> %t.result/x.expected
97109
// RUN: diff -u %S/syntactic-rename/x.expected %t.result/x.expected
@@ -116,6 +128,8 @@ func genfoo<T: P, U, V where U: P>(x: T, y: U, z: V, a: P) -> P where V: P {
116128
// RUN: diff -u %S/syntactic-rename/rename-layer.expected %t.result/rename-layer.expected
117129
// RUN: %sourcekitd-test -req=syntactic-rename -rename-spec %S/syntactic-rename/rename-P.in.json %s -- -swift-version 3 >> %t.result/rename-P.expected
118130
// RUN: diff -u %S/syntactic-rename/rename-P.expected %t.result/rename-P.expected
131+
// RUN: %sourcekitd-test -req=syntactic-rename -rename-spec %S/syntactic-rename/keywordbase.in.json %s -- -swift-version 3 >> %t.result/keywordbase.expected
132+
// RUN: diff -u %S/syntactic-rename/keywordbase.expected %t.result/keywordbase.expected
119133

120134
// RUN: rm -rf %t.ranges && mkdir -p %t.ranges
121135
// RUN: %sourcekitd-test -req=find-rename-ranges -rename-spec %S/syntactic-rename/x.in.json %s >> %t.ranges/x.expected
@@ -138,3 +152,5 @@ func genfoo<T: P, U, V where U: P>(x: T, y: U, z: V, a: P) -> P where V: P {
138152
// RUN: diff -u %S/find-rename-ranges/rename-layer.expected %t.ranges/rename-layer.expected
139153
// RUN: %sourcekitd-test -req=find-rename-ranges -rename-spec %S/syntactic-rename/rename-P.in.json %s -- -swift-version 3 >> %t.ranges/rename-P.expected
140154
// RUN: diff -u %S/find-rename-ranges/rename-P.expected %t.ranges/rename-P.expected
155+
// RUN: %sourcekitd-test -req=find-rename-ranges -rename-spec %S/syntactic-rename/keywordbase.in.json %s -- -swift-version 3 >> %t.ranges/keywordbase.expected
156+
// RUN: diff -u %S/find-rename-ranges/keywordbase.expected %t.ranges/keywordbase.expected
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
source.edit.kind.active:
2+
95:17-95:18 "y"
3+
source.edit.kind.active:
4+
96:22-96:23 "y"
5+
source.edit.kind.unknown:
6+
source.edit.kind.active:
7+
97:37-97:38 "y"
8+
source.edit.kind.active:
9+
97:59-97:60 "y"
10+
source.edit.kind.unknown:
11+
source.edit.kind.unknown:
12+
source.edit.kind.unknown:
13+
source.edit.kind.unknown:
14+
source.edit.kind.inactive:
15+
103:22-103:23 "y"
16+
source.edit.kind.unknown:
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
[
2+
{
3+
"key.name": "init(x:)",
4+
"key.newname": "init(y:)",
5+
"key.is_function_like": 1,
6+
"key.is_non_protocol_type": 0,
7+
"key.locations": [
8+
{
9+
"key.line": 95,
10+
"key.column": 5,
11+
"key.nametype": source.syntacticrename.call
12+
},
13+
{
14+
"key.line": 96,
15+
"key.column": 17,
16+
"key.nametype": source.syntacticrename.call
17+
},
18+
{
19+
"key.line": 97,
20+
"key.column": 17,
21+
"key.nametype": source.syntacticrename.unknown
22+
},
23+
{
24+
"key.line": 97,
25+
"key.column": 25,
26+
"key.nametype": source.syntacticrename.call
27+
},
28+
{
29+
"key.line": 97,
30+
"key.column": 47,
31+
"key.nametype": source.syntacticrename.call
32+
},
33+
{
34+
"key.line": 98,
35+
"key.column": 13,
36+
"key.nametype": source.syntacticrename.unknown
37+
},
38+
{
39+
"key.line": 98,
40+
"key.column": 21,
41+
"key.nametype": source.syntacticrename.unknown
42+
},
43+
{
44+
"key.line": 99,
45+
"key.column": 9,
46+
"key.nametype": source.syntacticrename.unknown
47+
},
48+
{
49+
"key.line": 99,
50+
"key.column": 17,
51+
"key.nametype": source.syntacticrename.unknown
52+
},
53+
{
54+
"key.line": 103,
55+
"key.column": 17,
56+
"key.nametype": source.syntacticrename.unknown
57+
},
58+
{
59+
"key.line": 104,
60+
"key.column": 17,
61+
"key.nametype": source.syntacticrename.unknown
62+
}
63+
]
64+
}
65+
]

0 commit comments

Comments
 (0)