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

bugfix: completions for implicit methods #6156

Merged
merged 5 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ class CompletionProvider(
d.label
case o: TextEditMember =>
o.label.getOrElse(labelWithSig)
case _: WorkspaceImplicitMember =>
s"$labelWithSig (implicit)"
case o: WorkspaceMember =>
s"$ident - ${o.sym.owner.fullName}"
case _ => labelWithSig
Expand Down Expand Up @@ -152,6 +154,31 @@ class CompletionProvider(
item.setAdditionalTextEdits(i.autoImports.asJava)
case d: DependecyMember =>
item.setTextEdit(d.edit)
case m: WorkspaceImplicitMember =>
val impPos = importPosition.getOrElse(AutoImportPosition(0, 0, false))
val suffix =
if (
clientSupportsSnippets && m.sym.paramss.headOption.exists(
_.nonEmpty
)
) "($0)"
else ""
val (short, edits) = ShortenedNames.synthesize(
TypeRef(
ThisType(m.sym.owner),
m.sym,
Nil
),
pos,
context,
impPos
)
val edit: l.TextEdit = textEdit(
short + suffix,
editRange
)
item.setTextEdit(edit)
item.setAdditionalTextEdits(edits.asJava)
case w: WorkspaceMember =>
def createTextEdit(identifier: String) =
textEdit(w.wrap(identifier), w.editRange.getOrElse(editRange))
Expand Down Expand Up @@ -360,16 +387,49 @@ class CompletionProvider(
text,
isAmmoniteScript
)

val searchResults =
if (kind == CompletionListKind.Scope) {
workspaceSymbolListMembers(query, pos, visit)
} else {
SymbolSearch.Result.COMPLETE
typedTreeAt(pos) match {
case Select(qualifier, _)
if qualifier.tpe != null && !qualifier.tpe.isError =>
workspaceExtensionMethods(query, pos, visit, qualifier.tpe)
case _ => SymbolSearch.Result.COMPLETE
}
}

InterestingMembers(buf.result(), searchResults)
}

private def workspaceExtensionMethods(
query: String,
pos: Position,
visit: Member => Boolean,
selectType: Type
): SymbolSearch.Result = {
val context = doLocateContext(pos)
val visitor = new CompilerSearchVisitor(
context,
sym =>
if (sym.safeOwner.isImplicit) {
val ownerConstructor = sym.owner.info.member(nme.CONSTRUCTOR)
def typeParams = sym.owner.info.typeParams
ownerConstructor.info.paramss match {
case List(List(param))
if selectType fuzzy_<:< TypeWithParams(
param.info,
typeParams
) =>
visit(new WorkspaceImplicitMember(sym))
case _ => false
}
} else false
)
search.searchMethods(query, buildTargetIdentifier, visitor)
}

private def isFunction(symbol: Symbol): Boolean = {
compiler.definitions.isFunctionSymbol(
symbol.info.finalResultType.typeSymbol
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class MetalsGlobal(
with completions.MillIvyCompletions
with completions.SbtLibCompletions
with completions.MultilineCommentCompletions
with completions.FuzzyUpperBound
with Signatures
with Compat
with GlobalProxy
Expand Down Expand Up @@ -361,6 +362,13 @@ class MetalsGlobal(
args.map(arg => loop(arg, None))
)
}
} else if (sym.isMethod && sym.safeOwner.isImplicit) {
history.tryShortenName(ShortName(sym.safeOwner))
TypeRef(
NoPrefix,
shortSymbol,
args.map(arg => loop(arg, None))
)
} else {
TypeRef(
loop(pre, Some(ShortName(sym))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ trait Signatures { compiler: MetalsGlobal =>
sym.isStaticMember || // Java static
sym.owner.ownerChain.forall { s =>
// ensure the symbol can be referenced in a static manner, without any instance
s.isPackageClass || s.isPackageObjectClass || s.isModule
s.isPackageClass || s.isPackageObjectClass || s.isModule || s.isModuleClass
}
) {
history(name) = short
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ trait Completions { this: MetalsGlobal =>
def editRange: Option[l.Range] = None
}

class WorkspaceImplicitMember(sym: Symbol)
extends ScopeMember(sym, sym.tpe, true, EmptyTree)

class WorkspaceInterpolationMember(
sym: Symbol,
override val additionalTextEdits: List[l.TextEdit],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package scala.meta.internal.pc.completions

import scala.meta.internal.pc.MetalsGlobal

trait FuzzyUpperBound { this: MetalsGlobal =>
implicit class XtensionType(tpe: Type) {
def fuzzy_<:<(tpeWithParams: TypeWithParams) = {
def adjustedType =
performSubstitutions(tpeWithParams.tpe, tpeWithParams.typeParams)

tpe <:< tpeWithParams.tpe || tpe <:< adjustedType
}

private def performSubstitutions(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some explanations here?

tpe: Type,
typeParams: List[Symbol]
): Type = {
typeParams.find(_ == tpe.typeSymbol) match {
case Some(tpeDef) =>
tpeDef.info match {
case bounds: TypeBounds => BoundedWildcardType(bounds)
case tpe => tpe
}
case None =>
tpe match {
case TypeRef(pre, sym, args) =>
TypeRef(pre, sym, args.map(performSubstitutions(_, typeParams)))
case t => t
}
}
}
}

case class TypeWithParams(tpe: Type, typeParams: List[Symbol])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this case class is helpful, but its just a nit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point actually. I changed this and it should much cleaner now.

}
1 change: 1 addition & 0 deletions tests/cross/src/main/scala/tests/BaseCompletionSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ abstract class BaseCompletionSuite extends BasePCSuite {
private def resolvedCompletions(
params: CompilerOffsetParams
): CompletionList = {
presentationCompiler.restart()
val result = presentationCompiler.complete(params).get()
val newItems = result.getItems.asScala.map { item =>
item.data
Expand Down
Loading
Loading