Skip to content

Commit

Permalink
bugfix: don't show scope completions for multi-imports
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek committed Jan 4, 2024
1 parent e3d8eac commit 24ee66b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import dotty.tools.dotc.core.StdNames.*
import dotty.tools.dotc.util.SourcePosition
import dotty.tools.dotc.util.Spans
import org.eclipse.{lsp4j as l}
import scala.annotation.tailrec

enum CompletionKind:
case Empty, Scope, Members
Expand Down Expand Up @@ -58,6 +59,7 @@ object CompletionPos:
val kind =
if query.isEmpty && !prevIsDot then CompletionKind.Empty
else if prevIsDot then CompletionKind.Members
else if isImportSelect(cursorPos, treePath) then CompletionKind.Members
else CompletionKind.Scope

CompletionPos(
Expand Down Expand Up @@ -92,6 +94,19 @@ object CompletionPos:
(i, tabIndented)
end inferIndent

private def isImportSelect(pos: SourcePosition, path: List[Tree])(using
Context
): Boolean =
@tailrec
def loop(enclosing: List[Tree]): Boolean =
enclosing match
case head :: tl if !head.sourcePos.contains(pos) => loop(tl)
case Import(_, sel) :: _ =>
sel.exists(_.imported.sourcePos.contains(pos))
case _ => false

loop(path)

/**
* Returns the start offset of the identifier starting as the given offset position.
*/
Expand Down
26 changes: 25 additions & 1 deletion tests/cross/src/test/scala/tests/pc/CompletionSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,6 @@ class CompletionSuite extends BaseCompletionSuite {
compat = Map(
"3" ->
"""|*
|*: scala
|""".stripMargin
)
)
Expand Down Expand Up @@ -2152,4 +2151,29 @@ class CompletionSuite extends BaseCompletionSuite {
assertSingleItem = false
)

check(
"completions-in-multi-imports".tag(IgnoreForScala3CompilerPC),
"""|import scala.collection.{AbstractMap, Set@@}
|""".stripMargin,
"""|Set scala.collection
|SetLike scala.collection
|SetProxy scala.collection
|SetProxyLike scala.collection
|AbstractSet scala.collection
|""".stripMargin,
topLines = Some(5),
compat = Map(
"3" ->
"""|Set scala.collection
|SetOps scala.collection
|""".stripMargin,
"2.13" -> """|Set scala.collection
|SetOps scala.collection
|AbstractSet scala.collection
|BitSet scala.collection
|BitSetOps scala.collection
|""".stripMargin
)
)

}

0 comments on commit 24ee66b

Please sign in to comment.