Skip to content

Commit 4f87317

Browse files
committed
[SourceKit] Only report textual results inside comments and strings from syntactic rename ranges
1 parent 420859c commit 4f87317

File tree

9 files changed

+36
-73
lines changed

9 files changed

+36
-73
lines changed

include/swift/IDE/Utils.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,12 +437,26 @@ class DeclNameViewer {
437437
};
438438

439439
enum class RegionType {
440+
/// We could not match the rename location to a symbol to be renamed and the
441+
/// symbol was originally a text match result (has `RenameLocUsage::Unknown`).
440442
Unmatched,
443+
/// We could not match the rename location to a symbol to be renamed and the
444+
/// symbol came from the index (does not have `RenameLocUsage::Unknown`).
441445
Mismatch,
446+
/// We were able to match the result to a location in source code that's
447+
/// active with respect to the current compiler arguments.
442448
ActiveCode,
449+
/// We were able to match the result to a location in source code that's
450+
/// inactive with respect to the current compiler arguments.
451+
///
452+
/// Currently, we don't evaluate #if so all occurrences inside #if blocks
453+
/// are considered inactive.
443454
InactiveCode,
455+
/// The location is inside a string literal.
444456
String,
457+
/// The location is inside a `#selector`.
445458
Selector,
459+
/// The location is inside a comment.
446460
Comment,
447461
};
448462

lib/Refactoring/SyntacticRenameRangeDetails.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -406,19 +406,18 @@ RegionType RenameRangeDetailCollector::addSyntacticRenameRanges(
406406
// Unknown name usage occurs if we don't have an entry in the index that
407407
// tells us whether the location is a call, reference or a definition. The
408408
// most common reasons why this happens is if the editor is adding syntactic
409-
// results (eg. from comments or string literals).
409+
// results to cover comments or string literals.
410410
//
411-
// Determine whether we should include them.
412-
if (regionKind == RegionType::ActiveCode) {
413-
// If the reference is in active code, we should have had a name usage
414-
// from the index. Since we don't, they are likely unrelated symbols that
415-
// happen to have the same name. Don't return them as matching ranges.
411+
// We only want to include these textual matches inside comments and string
412+
// literals. All other matches inside are likely bugus results.
413+
if (regionKind != RegionType::Comment && regionKind != RegionType::String) {
416414
return RegionType::Unmatched;
417415
}
416+
418417
if (specialBaseName != SpecialBaseName::None &&
419418
resolved.labelType == LabelRangeType::None) {
420419
// Filter out non-semantic special basename locations with no labels.
421-
// We've already filtered out those in active code, so these are
420+
// We've already filtered out those in code, so these are
422421
// any appearance of just 'init', 'subscript', or 'callAsFunction' in
423422
// strings, comments, and inactive code.
424423
return RegionType::Unmatched;

test/SourceKit/Refactoring/find-rename-ranges/foo_arity1.expected

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,9 @@ source.edit.kind.active:
1010
18:17-18:20 source.refactoring.range.kind.basename
1111
source.edit.kind.string:
1212
22:12-22:15 source.refactoring.range.kind.basename
13-
source.edit.kind.selector:
14-
23:19-23:22 source.refactoring.range.kind.basename
15-
23:23-23:24 source.refactoring.range.kind.selector-argument-label arg-index=0
16-
source.edit.kind.selector:
17-
24:19-24:22 source.refactoring.range.kind.basename
18-
source.edit.kind.selector:
19-
25:19-25:22 source.refactoring.range.kind.basename
13+
source.edit.kind.unknown:
14+
source.edit.kind.unknown:
15+
source.edit.kind.unknown:
2016
source.edit.kind.string:
2117
26:17-26:20 source.refactoring.range.kind.basename
2218
source.edit.kind.active:

test/SourceKit/Refactoring/find-rename-ranges/foo_remove.expected

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,9 @@ source.edit.kind.active:
1010
18:17-18:20 source.refactoring.range.kind.basename
1111
source.edit.kind.string:
1212
22:12-22:15 source.refactoring.range.kind.basename
13-
source.edit.kind.selector:
14-
23:19-23:22 source.refactoring.range.kind.basename
15-
23:23-23:24 source.refactoring.range.kind.selector-argument-label arg-index=0
16-
source.edit.kind.selector:
17-
24:19-24:22 source.refactoring.range.kind.basename
18-
source.edit.kind.selector:
19-
25:19-25:22 source.refactoring.range.kind.basename
13+
source.edit.kind.unknown:
14+
source.edit.kind.unknown:
15+
source.edit.kind.unknown:
2016
source.edit.kind.string:
2117
26:17-26:20 source.refactoring.range.kind.basename
2218
source.edit.kind.active:

test/SourceKit/Refactoring/find-rename-ranges/keywordbase.expected

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,5 @@ source.edit.kind.unknown:
1616
source.edit.kind.unknown:
1717
source.edit.kind.unknown:
1818
source.edit.kind.unknown:
19-
source.edit.kind.inactive:
20-
103:17-103:21 source.refactoring.range.kind.keyword-basename
21-
103:22-103:23 source.refactoring.range.kind.call-argument-label arg-index=0
22-
103:23-103:25 source.refactoring.range.kind.call-argument-colon arg-index=0
2319
source.edit.kind.unknown:
24-
104:17-104:21 source.refactoring.range.kind.keyword-basename
20+
source.edit.kind.unknown:

test/SourceKit/Refactoring/find-rename-ranges/z.expected

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ source.edit.kind.active:
22
8:9-8:10 source.refactoring.range.kind.basename
33
source.edit.kind.inactive:
44
10:16-10:17 source.refactoring.range.kind.basename
5-
source.edit.kind.inactive:
6-
12:16-12:17 source.refactoring.range.kind.basename
5+
source.edit.kind.unknown:

test/refactoring/SyntacticRename/Outputs/textual/MyClass.swift.expected

Lines changed: 0 additions & 38 deletions
This file was deleted.

test/refactoring/SyntacticRename/Outputs/textual/foo.swift.expected

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ func /*foo:def*/<base>foo</base>() {
77
// /*foo:unknown*/<base>foo</base>'s return
88
#selector(Struct . /*foo:unknown*/foo(_:aboveSubview:))
99
#selector(/*foo:unknown*/foo(_:))
10-
#selector(#selector(/*foo:unknown*/<base>foo</base>))
10+
#selector(#selector(/*foo:unknown*/foo))
1111

1212
#if true
1313
/*foo*/<base>foo</base> = 2
1414
/*foo*/<base>foo</base>()
1515
/*foo:call*/<base>foo</base>()
16-
/*foo:unknown*/<base>foo</base> = 3
17-
/*foo:unknown*/<base>foo</base>()
16+
/*foo:unknown*/foo = 3
17+
/*foo:unknown*/foo()
1818
#if false
19-
/*foo:unknown*/<base>foo</base> += 2
20-
/*foo:unknown*/<base>foo</base>()
19+
/*foo:unknown*/foo += 2
20+
/*foo:unknown*/foo()
2121
#endif
2222
#else
23-
/*foo:unknown*/<base>foo</base> = 4
23+
/*foo:unknown*/foo = 4
2424
#endif
2525

2626
return 1

test/refactoring/SyntacticRename/textual.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ _ = /*MyClass:unknown*/MyClass()
4141
// RUN: %refactor -find-rename-ranges -source-filename %s -pos="foo" -is-function-like -old-name "foo()" >> %t.ranges/textual_foo.swift
4242
// RUN: diff -u %S/Outputs/textual/foo.swift.expected %t.ranges/textual_foo.swift
4343
// RUN: %refactor -find-rename-ranges -source-filename %s -pos="MyClass" -is-non-protocol-type -old-name "MyClass" -new-name "YourClass" >> %t.ranges/textual_MyClass.swift
44-
// RUN: diff -u %S/Outputs/textual/MyClass.swift.expected %t.ranges/textual_MyClass.swift
44+
// All occcurrences of MyClass are outside of comments and string literals, so there's nothing to rename. swift-refactor indicates this by outputing empty results.
45+
// RUN: diff -u %S/Outputs/textual/MyClass.swift.expected -

0 commit comments

Comments
 (0)