Skip to content

Commit 7ee4855

Browse files
committed
Fix #4995: Exclude constructors when finding refs
When doing a rename on a class, we used to rename the class and its constructor, which yield to incorrect code as shown in #4995. Similarly, finding the references of a class also shows the constructor. To fix this issue, we omit constructors when finding the references.
1 parent a8818cc commit 7ee4855

File tree

3 files changed

+82
-5
lines changed

3 files changed

+82
-5
lines changed

language-server/src/dotty/tools/languageserver/DottyLanguageServer.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,11 @@ class DottyLanguageServer extends LanguageServer
283283
// only need to look for trees in the target directory if the symbol is defined in the
284284
// current project
285285
val trees = driver.allTreesContaining(sym.name.sourceModuleName.toString)
286-
val refs = Interactive.namedTrees(trees, includeReferences = true, (tree: tpd.NameTree) =>
287-
(includeDeclaration || !Interactive.isDefinition(tree))
288-
&& Interactive.matchSymbol(tree, sym, Include.overriding))
286+
val refs = Interactive.namedTrees(trees, includeReferences = true, tree =>
287+
tree.pos.isSourceDerived
288+
&& (includeDeclaration || !Interactive.isDefinition(tree))
289+
&& !tree.symbol.isConstructor
290+
&& (Interactive.matchSymbol(tree, sym, Include.overriding)))
289291

290292
refs.map(ref => location(ref.namePos)).asJava
291293
}
@@ -307,6 +309,7 @@ class DottyLanguageServer extends LanguageServer
307309

308310
val refs = Interactive.namedTrees(trees, includeReferences = true, tree =>
309311
tree.pos.isSourceDerived
312+
&& !tree.symbol.isConstructor
310313
&& (Interactive.matchSymbol(tree, sym, Include.overriding)
311314
|| (linkedSym != NoSymbol && Interactive.matchSymbol(tree, linkedSym, Include.overriding))))
312315

language-server/test/dotty/tools/languageserver/ReferencesTest.scala

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,33 @@ class ReferencesTest {
1919
.references(m1 to m2, List(m1 to m2, m3 to m4, m5 to m6), withDecl = true)
2020
}
2121

22+
@Test def classReference0: Unit = {
23+
code"class ${m1}Foo${m2} { val a = new ${m3}Foo${m4} }".withSource
24+
.references(m1 to m2, List(m1 to m2, m3 to m4), withDecl = true)
25+
.references(m1 to m2, List(m3 to m4), withDecl = false)
26+
.references(m3 to m4, List(m1 to m2, m3 to m4), withDecl = true)
27+
.references(m3 to m4, List(m3 to m4), withDecl = false)
28+
}
29+
30+
@Test def classReference1: Unit = {
31+
code"class ${m1}Foo${m2}(x: Int) { val a = new ${m3}Foo${m4}(1) }".withSource
32+
.references(m1 to m2, List(m1 to m2, m3 to m4), withDecl = true)
33+
.references(m1 to m2, List(m3 to m4), withDecl = false)
34+
.references(m3 to m4, List(m1 to m2, m3 to m4), withDecl = true)
35+
.references(m3 to m4, List(m3 to m4), withDecl = false)
36+
}
37+
38+
@Test def classReferenceCompanion: Unit = {
39+
code"""class ${m1}Foo${m2}(x: Any)
40+
object ${m3}Foo${m4} { val bar = new ${m5}Foo${m6}(${m7}Foo${m8}) }""".withSource
41+
.references(m1 to m2, List(m1 to m2, m5 to m6), withDecl = true)
42+
.references(m1 to m2, List(m5 to m6), withDecl = false)
43+
.references(m3 to m4, List(m3 to m4, m7 to m8), withDecl = true)
44+
.references(m3 to m4, List(m7 to m8), withDecl = false)
45+
.references(m5 to m6, List(m1 to m2, m5 to m6), withDecl = true)
46+
.references(m5 to m6, List(m5 to m6), withDecl = false)
47+
.references(m7 to m8, List(m3 to m4, m7 to m8), withDecl = true)
48+
.references(m7 to m8, List(m7 to m8), withDecl = false)
49+
}
50+
2251
}

language-server/test/dotty/tools/languageserver/RenameTest.scala

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,57 @@ class RenameTest {
1919
def testRenameFrom(m: CodeMarker) =
2020
withSources(
2121
code"class ${m1}Foo$m2 { new ${m3}Foo$m4 }",
22-
code"class Bar { new ${m5}Foo$m6 }"
23-
).rename(m, "Bar", Set(m1 to m2, m3 to m4, m5 to m6))
22+
code"class Bar { new ${m5}Foo$m6 }",
23+
code"class Baz extends ${m7}Foo${m8}"
24+
).rename(m, "Bar", Set(m1 to m2, m3 to m4, m5 to m6, m7 to m8))
2425

2526
testRenameFrom(m1)
2627
testRenameFrom(m3)
2728
testRenameFrom(m5)
2829
}
2930

31+
@Test def renameObject: Unit = {
32+
def testRenameFrom(m: CodeMarker) =
33+
withSources(
34+
code"object ${m1}Foo${m2}",
35+
code"class Bar { val x = ${m3}Foo${m4} }"
36+
).rename(m, "NewName", Set(m1 to m2, m3 to m4))
37+
38+
testRenameFrom(m1)
39+
testRenameFrom(m2)
40+
}
41+
42+
@Test def renameDef: Unit = {
43+
def testRenameFrom(m: CodeMarker) =
44+
withSources(
45+
code"object Foo { def ${m1}bar${m2} = 0 }",
46+
code"object Buzz { Foo.${m3}bar${m4} }"
47+
).rename(m, "newName", Set(m1 to m2, m3 to m4))
48+
49+
testRenameFrom(m1)
50+
testRenameFrom(m3)
51+
}
52+
53+
@Test def renameClass: Unit = {
54+
def testRenameFrom(m: CodeMarker) =
55+
withSources(
56+
code"class ${m1}Foo${m2}(x: Int)",
57+
code"class Bar extends ${m3}Foo${m4}(1)"
58+
).rename(m, "NewName", Set(m1 to m2, m3 to m4))
59+
60+
testRenameFrom(m1)
61+
testRenameFrom(m3)
62+
}
63+
64+
@Test def renameCaseClass: Unit = {
65+
def testRenameFrom(m: CodeMarker) =
66+
withSources(
67+
code"case class ${m1}Foo${m2}(x: Int)",
68+
code"class Bar extends ${m3}Foo${m4}(1)"
69+
).rename(m, "NewName", Set(m1 to m2, m3 to m4))
70+
71+
testRenameFrom(m1)
72+
testRenameFrom(m2)
73+
}
74+
3075
}

0 commit comments

Comments
 (0)