Skip to content

Commit 01124d7

Browse files
committed
Be less precise about renaming and imports
This commit makes the renaming less precise for the (probably rare) case of an imports being imported multiple times with the same renaming. When doing a renaming of a symbol that is import-renamed, we would exclude subscope where the same symbol was imported again with the same name. This commit changes the logic so that we no longer exclude those subscope, and keep renaming. See RenameTest#renameRenamingImportNested for an example where the langauge server will now behave differently.
1 parent a6397fb commit 01124d7

File tree

2 files changed

+11
-37
lines changed

2 files changed

+11
-37
lines changed

compiler/src/dotty/tools/dotc/interactive/Interactive.scala

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -681,28 +681,6 @@ object Interactive {
681681
source: SourceFile
682682
)(implicit ctx: Context): List[SourceNamedTree] = {
683683

684-
/**
685-
* Remove the blocks that immediately enclose a renaming to `toName` in `inTree`.
686-
*
687-
* @param toName The target name of renamings.
688-
* @param inTree The tree in which to remove the blocks that have such a renaming.
689-
* @return A tree that has no children containing a renaming to `toName`.
690-
*/
691-
def removeBlockWithRenaming(toName: Name, inTree: Tree): Tree = {
692-
new TreeMap {
693-
override def transform(tree: Tree)(implicit ctx: Context): Tree = tree match {
694-
case pkg: PackageDef if immediatelyEnclosesRenaming(toName, pkg) =>
695-
EmptyTree
696-
case template: Template if immediatelyEnclosesRenaming(toName, template) =>
697-
cpy.Template(template)(constr = DefDef(template.constr.symbol.asTerm), body = Nil)
698-
case block @ Block(stats, expr) if immediatelyEnclosesRenaming(toName, block) =>
699-
EmptyTree
700-
case other =>
701-
super.transform(other)
702-
}
703-
}.transform(inTree)
704-
}
705-
706684
val trees = {
707685
val enclosedTrees = enclosing match {
708686
case Some(pkg: PackageDef) =>
@@ -716,13 +694,9 @@ object Interactive {
716694
allTrees
717695
}
718696

719-
// These trees may contain a new renaming of the same symbol to the same name, so we may
720-
// have to cut some branches
721-
val trimmedTrees = enclosedTrees.map(removeBlockWithRenaming(toName, _))
722-
723697
// Some of these trees may not be `NameTrees`. Those that are not are wrapped in a
724698
// synthetic val def, so that everything can go inside `SourceNamedTree`s.
725-
trimmedTrees.map {
699+
enclosedTrees.map {
726700
case tree: NameTree =>
727701
SourceNamedTree(tree, source)
728702
case tree =>

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,22 +174,22 @@ class RenameTest {
174174
def testRename(m: CodeMarker, expectations: Set[CodeRange]) =
175175
sources.rename(m, "NewName", expectations)
176176

177-
testRename(m1, Set(m1 to m2))
178-
testRename(m2, Set(m1 to m2))
179-
testRename(m3, Set(m3 to m4, m5 to m6, m9 to m10, m15 to m16))
180-
testRename(m4, Set(m3 to m4, m5 to m6, m9 to m10, m15 to m16))
181-
testRename(m5, Set(m3 to m4, m5 to m6, m9 to m10, m15 to m16))
182-
testRename(m6, Set(m3 to m4, m5 to m6, m9 to m10, m15 to m16))
177+
testRename(m1, Set(m1 to m2, m3 to m4, m5 to m6, m7 to m8, m9 to m10, m11 to m12, m13 to m14, m15 to m16))
178+
testRename(m2, Set(m1 to m2, m3 to m4, m5 to m6, m7 to m8, m9 to m10, m11 to m12, m13 to m14, m15 to m16))
179+
testRename(m3, Set(m3 to m4, m5 to m6, m7 to m8, m9 to m10, m11 to m12, m13 to m14, m15 to m16))
180+
testRename(m4, Set(m3 to m4, m5 to m6, m7 to m8, m9 to m10, m11 to m12, m13 to m14, m15 to m16))
181+
testRename(m5, Set(m3 to m4, m5 to m6, m7 to m8, m9 to m10, m11 to m12, m13 to m14, m15 to m16))
182+
testRename(m6, Set(m3 to m4, m5 to m6, m7 to m8, m9 to m10, m11 to m12, m13 to m14, m15 to m16))
183183
testRename(m7, Set(m7 to m8))
184184
testRename(m8, Set(m7 to m8))
185-
testRename(m9, Set(m3 to m4, m5 to m6, m9 to m10, m15 to m16))
186-
testRename(m10, Set(m3 to m4, m5 to m6, m9 to m10, m15 to m16))
185+
testRename(m9, Set(m3 to m4, m5 to m6, m7 to m8, m9 to m10, m11 to m12, m13 to m14, m15 to m16))
186+
testRename(m10, Set(m3 to m4, m5 to m6, m7 to m8, m9 to m10, m11 to m12, m13 to m14, m15 to m16))
187187
testRename(m11, Set(m11 to m12, m13 to m14))
188188
testRename(m12, Set(m11 to m12, m13 to m14))
189189
testRename(m13, Set(m11 to m12, m13 to m14))
190190
testRename(m14, Set(m11 to m12, m13 to m14))
191-
testRename(m15, Set(m3 to m4, m5 to m6, m9 to m10, m15 to m16))
192-
testRename(m16, Set(m3 to m4, m5 to m6, m9 to m10, m15 to m16))
191+
testRename(m15, Set(m3 to m4, m5 to m6, m7 to m8, m9 to m10, m11 to m12, m13 to m14, m15 to m16))
192+
testRename(m16, Set(m3 to m4, m5 to m6, m7 to m8, m9 to m10, m11 to m12, m13 to m14, m15 to m16))
193193
}
194194

195195
@Test def renameImportWithRenaming: Unit = {

0 commit comments

Comments
 (0)