Skip to content

Commit 36ff5cf

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

File tree

6 files changed

+28
-28
lines changed

6 files changed

+28
-28
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:

0 commit comments

Comments
 (0)