Skip to content

Commit 070a5e2

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 a3fa1ec commit 070a5e2

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
@@ -351,7 +351,9 @@ class DottyLanguageServer extends LanguageServer
351351
val uriTrees = driver.openedTrees(uri)
352352
val pos = sourcePosition(driver, uri, params.getPosition)
353353
val path = Interactive.pathTo(uriTrees, pos)
354-
val syms = Interactive.enclosingSourceSymbols(path, pos)
354+
val syms = Interactive.enclosingSourceSymbols(path, pos).flatMap { sym =>
355+
sym :: sym.allOverriddenSymbols.toList
356+
}
355357
val newName = params.getNewName
356358

357359
def findRenamedReferences(trees: List[SourceTree], syms: List[Symbol], withName: Name): List[SourceTree] = {
@@ -372,10 +374,13 @@ class DottyLanguageServer extends LanguageServer
372374
findRenamedReferences(uriTrees, syms, nameTree.name)
373375

374376
case _ =>
375-
val includes = Include.all.except(Include.overridden)
377+
val names = syms.map(_.name.sourceModuleName).toSet
378+
val trees = names.flatMap(name => driver.allTreesContaining(name.toString)).toList
376379
syms.flatMap { sym =>
377-
val trees = driver.allTreesContaining(sym.name.sourceModuleName.toString)
378-
Interactive.findTreesMatching(trees, includes, sym, t => Interactive.sameName(t.name, sym.name))
380+
Interactive.findTreesMatching(trees,
381+
Include.all,
382+
sym,
383+
t => names.exists(Interactive.sameName(t.name, _)))
379384
}
380385
}
381386

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)