Skip to content

Commit bd3a18f

Browse files
committed
Support multiple renames for the same symbol
1 parent a073f97 commit bd3a18f

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,18 +335,22 @@ object Completion {
335335
* in the REPL and the IDE.
336336
*/
337337
private class RenameAwareScope extends Scopes.MutableScope {
338-
private[this] val renames: mutable.Map[Symbol, Name] = mutable.Map.empty
338+
private[this] val renames: mutable.Map[Symbol, List[Name]] = mutable.Map.empty
339339

340340
/** Enter the symbol `sym` in this scope, recording a potential renaming. */
341341
def enter[T <: Symbol](sym: T, name: Name)(implicit ctx: Context): T = {
342-
if (name != sym.name) renames += sym -> name
342+
renames += sym -> (name :: renames.getOrElse(sym, Nil))
343343
newScopeEntry(name, sym)
344344
sym
345345
}
346346

347347
/** Lists the symbols in this scope along with the name associated with them. */
348-
def toListWithNames(implicit ctx: Context): List[(Symbol, Name)] =
349-
toList.map(sym => (sym, renames.get(sym).getOrElse(sym.name)))
348+
def toListWithNames(implicit ctx: Context): List[(Symbol, Name)] = {
349+
for {
350+
sym <- toList
351+
name <- renames.getOrElse(sym, List(sym.name))
352+
} yield (sym, name)
353+
}
350354
}
351355

352356
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,26 @@ class CompletionTest {
214214
}""".withSource
215215
.completion(m1, Set(("MyImportedSymbol", Class, "java.io.FileDescriptor")))
216216
}
217+
218+
@Test def completionRenamedAndOriginalNames: Unit = {
219+
code"""import java.util.HashMap
220+
|trait Foo {
221+
| import java.util.{HashMap => HashMap2}
222+
| val x: Hash$m1
223+
|}""".withSource
224+
.completion(m1, Set(("HashMap", Class, "java.util.HashMap"),
225+
("HashMap2", Class, "java.util.HashMap")))
226+
}
227+
228+
@Test def completionRenamedThrice: Unit = {
229+
code"""import java.util.{HashMap => MyHashMap}
230+
|import java.util.{HashMap => MyHashMap2}
231+
|trait Foo {
232+
| import java.util.{HashMap => MyHashMap3}
233+
| val x: MyHash$m1
234+
|}""".withSource
235+
.completion(m1, Set(("MyHashMap", Class, "java.util.HashMap"),
236+
("MyHashMap2", Class, "java.util.HashMap"),
237+
("MyHashMap3", Class, "java.util.HashMap")))
238+
}
217239
}

0 commit comments

Comments
 (0)