Skip to content

Commit 516836c

Browse files
committed
[SourceKit] Don't handle labels when renaming operators
This allows us to at least rename the base of operators. The labels don’t matter too much since they only occur on the function definition.
1 parent 2e806b1 commit 516836c

File tree

5 files changed

+43
-4
lines changed

5 files changed

+43
-4
lines changed

lib/Refactoring/SyntacticRenameRangeDetails.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,8 +470,13 @@ RegionType RenameRangeDetailCollector::addSyntacticRenameRanges(
470470
isCallSite = true;
471471
break;
472472
case RenameLocUsage::Definition:
473-
// All function definitions have argument labels that should be renamed.
474-
handleLabels = true;
473+
// Don't rename labels of operators. There is a mismatch between the
474+
// indexer reporting all labels as `_` even if they are spelled as e.g.
475+
// `x: Int` in the function declaration. Since the labels only appear
476+
// on the operator declaration and in no calls, just don't rename them for
477+
// now.
478+
// For all other (normal) function declarations, always rename the labels.
479+
handleLabels = !Lexer::isOperator(Old.base());
475480
isCallSite = false;
476481
break;
477482
case RenameLocUsage::Reference:
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file --leading-lines %s %t
3+
4+
//--- a.swift
5+
6+
struct Foo {}
7+
// RUN: %sourcekitd-test -req=related-idents -pos=%(line + 1):6 %t/a.swift -- %t/a.swift | %FileCheck %s
8+
func +(x: Foo, y: Foo) {}
9+
Foo() + Foo()
10+
11+
//--- dummy.swift
12+
13+
// CHECK: START RANGES
14+
// CHECK: 8:6 - 1 - source.syntacticrename.definition
15+
// CHECK: 9:7 - 1 - source.syntacticrename.call
16+
// CHECK: END RANGES
17+
// CHECK: NAME: +(_:_:)

test/refactoring/SyntacticRename/Outputs/functions/infix-operator.swift.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class AStruct {
2828
return {a in a};
2929
}
3030

31-
static func /*infix-operator:def*/<base>+</base> (<arglabel index=0>left</arglabel><param index=0></param>: AStruct, <arglabel index=1>right</arglabel><param index=1></param>: AStruct) -> AStruct {
31+
static func /*infix-operator:def*/<base>+</base> (left: AStruct, right: AStruct) -> AStruct {
3232
return AStruct()
3333
}
3434

test/refactoring/SyntacticRename/Outputs/functions/prefix-operator.swift.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class AStruct {
3232
return AStruct()
3333
}
3434

35-
static prefix func /*prefix-operator:def*/<base>-</base> (<arglabel index=0>struct</arglabel><param index=0></param>: AStruct) -> AStruct {
35+
static prefix func /*prefix-operator:def*/<base>-</base> (struct: AStruct) -> AStruct {
3636
return AStruct()
3737
}
3838
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file %s %t
3+
// RUN: %refactor -find-rename-ranges -source-filename %t/input.swift -pos="test" -old-name "+(x:y:)" -new-name "-(x:y:)" > %t/output.txt
4+
// RUN: diff -u %t/expected.swift %t/output.txt
5+
6+
//--- input.swift
7+
8+
struct Foo {}
9+
func /*test:def*/+(x: Foo, y: Foo) {}
10+
Foo() /*test:ref*/+ Foo()
11+
12+
//--- expected.swift
13+
14+
struct Foo {}
15+
func /*test:def*/<base>+</base>(x: Foo, y: Foo) {}
16+
Foo() /*test:ref*/<base>+</base> Foo()
17+

0 commit comments

Comments
 (0)