Skip to content

Commit 9963693

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 cf3c247 commit 9963693

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
@@ -353,7 +353,9 @@ class DottyLanguageServer extends LanguageServer
353353
val uriTrees = driver.openedTrees(uri)
354354
val pos = sourcePosition(driver, uri, params.getPosition)
355355
val path = Interactive.pathTo(uriTrees, pos)
356-
val syms = Interactive.enclosingSourceSymbols(path, pos)
356+
val syms = Interactive.enclosingSourceSymbols(path, pos).flatMap { sym =>
357+
sym :: sym.allOverriddenSymbols.toList
358+
}
357359
val newName = params.getNewName
358360

359361
def findRenamedReferences(trees: List[SourceTree], syms: List[Symbol], withName: Name): List[SourceTree] = {
@@ -374,10 +376,13 @@ class DottyLanguageServer extends LanguageServer
374376
findRenamedReferences(uriTrees, syms, nameTree.name)
375377

376378
case _ =>
377-
val includes = Include.all.except(Include.overridden)
379+
val names = syms.map(_.name.sourceModuleName).toSet
380+
val trees = names.flatMap(name => driver.allTreesContaining(name.toString)).toList
378381
syms.flatMap { sym =>
379-
val trees = driver.allTreesContaining(sym.name.sourceModuleName.toString)
380-
Interactive.findTreesMatching(trees, includes, sym, t => Interactive.sameName(t.name, sym.name))
382+
Interactive.findTreesMatching(trees,
383+
Include.all,
384+
sym,
385+
t => names.exists(Interactive.sameName(t.name, _)))
381386
}
382387
}
383388

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)