Skip to content

Commit

Permalink
fix: correctly prefix scope completions when conflicting for Scala 2
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek committed Jul 24, 2024
1 parent 8eeebaf commit df81755
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package scala.meta.internal.pc
import java.net.URI
import java.{util => ju}

import scala.annotation.tailrec
import scala.collection.mutable

import scala.meta.internal.jdk.CollectionConverters._
Expand Down Expand Up @@ -73,10 +74,34 @@ class CompletionProvider(
lazy val importPosition = autoImportPosition(pos, params.text())
lazy val context = doLocateImportContext(pos)

@tailrec
def findNonConflictingPrefix(sym: Symbol, acc: List[String]): String = {
val symName = if (sym.name.isTermName) sym.name.dropLocal else sym.name
context.lookupSymbol(symName, _ => true) match {
case LookupSucceeded(_, symbol)
if sym.effectiveOwner.exists && symbol.effectiveOwner.exists && symbol.effectiveOwner != sym.effectiveOwner =>
findNonConflictingPrefix(
sym.effectiveOwner,
Identifier.backtickWrap(sym.effectiveOwner.name.decoded) :: acc
)
case _ => acc.mkString(".")
}
}

val items = sorted.iterator.zipWithIndex.map { case (member, idx) =>
params.checkCanceled()
val symbolName = member.symNameDropLocal.decoded
val ident = Identifier.backtickWrap(symbolName)
val simpleIdent = Identifier.backtickWrap(symbolName)
val ident =
member match {
case _: WorkspaceMember | _: WorkspaceImplicitMember |
_: NamedArgMember | _: DependecyMember =>
simpleIdent
case _: ScopeMember =>
findNonConflictingPrefix(member.sym, List(simpleIdent))
case _ => simpleIdent
}

val detail = member match {
case o: OverrideDefMember => o.detail
case t: TextEditMember if t.detail.isDefined => t.detail.get
Expand Down
43 changes: 43 additions & 0 deletions tests/cross/src/test/scala/tests/pc/CompletionSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2325,4 +2325,47 @@ class CompletionSuite extends BaseCompletionSuite {
filter = _ == "O(i: Int): Int"
)

check(
"conflict",
"""|package a
|object O {
| val x: Int = 123
| def method = {
| val x: String = "abc"
| x@@
| }
|}
|""".stripMargin,
"""|x: String
|O.x: Int
|""".stripMargin,
compat = Map(
"3" -> """|x: String
|x: Int
|""".stripMargin
)
)

checkEdit(
"conflict-edit",
"""|object O {
| val x: Int = 123
| def method = {
| val x: String = "abc"
| x@@
| }
|}
|""".stripMargin,
"""|object O {
| val x: Int = 123
| def method = {
| val x: String = "abc"
| O.x
| }
|}
|""".stripMargin,
assertSingleItem = false,
itemIndex = 1
)

}

0 comments on commit df81755

Please sign in to comment.