Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete when importing same symbol multiple times #13972

Merged
merged 1 commit into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions compiler/src/dotty/tools/dotc/interactive/Completion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,22 @@ object Completion {

mappings.foreach { (name, denotss) =>
val first = denotss.head

// import a.c
def isSingleImport = denotss.length < 2
// import a.C
// locally { import b.C }
def isImportedInDifferentScope = first.ctx.scope ne denotss(1).ctx.scope
// import a.C
// import a.C
def isSameSymbolImportedDouble = denotss.forall(_.denots == first.denots)

denotss.find(!_.ctx.isImportContext) match {
// most deeply nested member or local definition if not shadowed by an import
case Some(local) if local.ctx.scope == first.ctx.scope =>
resultMappings += name -> local.denots

// most deeply nested import if not shadowed by another import
case None if denotss.length < 2 || (denotss(1).ctx.scope ne first.ctx.scope) =>
case None if isSingleImport || isImportedInDifferentScope || isSameSymbolImportedDouble =>
resultMappings += name -> first.denots

case _ =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,19 @@ class CompletionTest {
.completion(m1, Set())
}

@Test def completeFromSameImportsForEqualNestingLevels: Unit = {
code"""object Foo {
| def xxxx(i: Int): Int = i
|}
|object Test {
| import Foo.xxxx
| import Foo.xxxx
| import Foo.xxxx
| val x = xx$m1
|}""".withSource
.completion(m1, Set(("xxxx", Method, "(i: Int): Int")))
}

@Test def preferLocalDefinitionToImportForEqualNestingLevels: Unit = {
code"""object Foo {
| val xxxx = 1
Expand Down