Skip to content

Commit e6a70f4

Browse files
committed
Improve renaming with overridden symbols
When renaming a symbol that overrides other symbols, the overridden symbols and all their overrides will be renamed too.
1 parent 4890eac commit e6a70f4

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,9 @@ class DottyLanguageServer extends LanguageServer
369369
val uriTrees = driver.openedTrees(uri)
370370
val pos = sourcePosition(driver, uri, params.getPosition)
371371
val path = Interactive.pathTo(uriTrees, pos)
372-
val syms = Interactive.enclosingSourceSymbols(path, pos)
372+
val syms = Interactive.enclosingSourceSymbols(path, pos).flatMap { sym =>
373+
sym :: sym.allOverriddenSymbols.toList
374+
}
373375
val newName = params.getNewName
374376

375377
def findRenamedReferences(trees: List[SourceTree], syms: List[Symbol], withName: Name): List[SourceTree] = {
@@ -390,10 +392,13 @@ class DottyLanguageServer extends LanguageServer
390392
findRenamedReferences(uriTrees, syms, nameTree.name)
391393

392394
case _ =>
393-
val includes = Include.all.except(Include.overridden)
395+
val names = syms.map(_.name.sourceModuleName).toSet
396+
val trees = names.flatMap(name => driver.allTreesContaining(name.toString)).toList
394397
syms.flatMap { sym =>
395-
val trees = driver.allTreesContaining(sym.name.sourceModuleName.toString)
396-
Interactive.findTreesMatching(trees, includes, sym, t => Interactive.sameName(t.name, sym.name))
398+
Interactive.findTreesMatching(trees,
399+
Include.all,
400+
sym,
401+
t => names.exists(Interactive.sameName(t.name, _)))
397402
}
398403
}
399404

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,23 @@ class RenameTest {
215215
testRename(m8)
216216
}
217217

218+
@Test def renameOverridden: Unit = {
219+
def testRename(m: CodeMarker) =
220+
withSources(
221+
code"""class A { def ${m1}foo${m2}: Int = 0 }
222+
class B extends A { override def ${m3}foo${m4}: Int = 1 }
223+
class C extends A { override def ${m5}foo${m6}: Int = 2 }"""
224+
).rename(m, "NewName", Set(m1 to m2, m3 to m4, m5 to m6))
225+
226+
testRename(m1)
227+
testRename(m2)
228+
testRename(m3)
229+
testRename(m4)
230+
testRename(m5)
231+
testRename(m6)
232+
233+
}
234+
235+
236+
218237
}

0 commit comments

Comments
 (0)