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

improvement: Support completions for implicit classes #5904

Merged
merged 3 commits into from
Dec 19, 2023
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.
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 @@ -10,6 +10,7 @@ import scala.meta.internal.metals.ReportContext
import scala.meta.pc.*

import dotty.tools.dotc.core.Contexts.*
import dotty.tools.dotc.core.Flags
import dotty.tools.dotc.core.Names.*
import dotty.tools.dotc.core.Symbols.*

Expand All @@ -20,8 +21,15 @@ class CompilerSearchVisitor(

val logger: Logger = Logger.getLogger(classOf[CompilerSearchVisitor].getName)

private def isAccessibleImplicitClass(sym: Symbol) =
val owner = sym.maybeOwner
owner != NoSymbol && owner.isClass &&
owner.is(Flags.Implicit) &&
owner.isStatic && owner.isPublic

private def isAccessible(sym: Symbol): Boolean = try
sym != NoSymbol && sym.isPublic && sym.isStatic
sym != NoSymbol && sym.isPublic && sym.isStatic ||
isAccessibleImplicitClass(sym)
catch
case err: AssertionError =>
logger.log(Level.WARNING, err.getMessage())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,16 @@ object SemanticdbSymbols:
// however in scalac this method is defined only in `module Files`
if typeSym.is(JavaDefined) then
typeSym :: owner.info.decl(termName(value)).symbol :: Nil
/**
* Looks like decl doesn't work for:
* package a:
* implicit class <<A>> (i: Int):
* def inc = i + 1
*/
else if typeSym == NoSymbol then
owner.info.member(typeName(value)).symbol :: Nil
else typeSym :: Nil
end if
case Descriptor.Term(value) =>
val outSymbol = owner.info.decl(termName(value)).symbol
if outSymbol.exists
Expand Down Expand Up @@ -91,6 +100,8 @@ object SemanticdbSymbols:
.map(_.symbol)
.filter(sym => symbolName(sym) == s)
.toList
end match
end tryMember

parentSymbol.flatMap(tryMember)
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class CompletionProvider(

def mkItemWithImports(
v: CompletionValue.Workspace | CompletionValue.Extension |
CompletionValue.Interpolator
CompletionValue.Interpolator | CompletionValue.ImplicitClass
) =
val sym = v.symbol
path match
Expand Down Expand Up @@ -277,7 +277,8 @@ class CompletionProvider(
end mkItemWithImports

completion match
case v: (CompletionValue.Workspace | CompletionValue.Extension) =>
case v: (CompletionValue.Workspace | CompletionValue.Extension |
CompletionValue.ImplicitClass) =>
mkItemWithImports(v)
case v: CompletionValue.Interpolator if v.isWorkspace || v.isExtension =>
mkItemWithImports(v)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ object CompletionValue:
override def description(printer: MetalsPrinter)(using Context): String =
s"${printer.completionSymbol(symbol)} (extension)"

/**
* CompletionValue for old implicit classes methods via SymbolSearch
*/
case class ImplicitClass(
label: String,
symbol: Symbol,
override val snippetSuffix: CompletionSuffix,
override val importSymbol: Symbol,
) extends Symbolic:
override def completionItemKind(using Context): CompletionItemKind =
CompletionItemKind.Method
override def description(printer: MetalsPrinter)(using Context): String =
s"${printer.completionSymbol(symbol)} (implicit)"

/**
* @param shortenedNames shortened type names by `Printer`. This field should be used for autoImports
* @param start Starting position of the completion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,14 +595,40 @@ class Completions(
Some(search.search(query, buildTargetIdentifier, visitor))
case CompletionKind.Members =>
val visitor = new CompilerSearchVisitor(sym =>
if sym.is(ExtensionMethod) &&
def isExtensionMethod = sym.is(ExtensionMethod) &&
qualType.widenDealias <:< sym.extensionParam.info.widenDealias
then

def isImplicitClass(owner: Symbol) =

val constructorParam =
owner.info
.membersBasedOnFlags(
Flags.ParamAccessor,
Flags.EmptyFlags,
)
.headOption
.map(_.info)
owner.isClass && owner.is(Flags.Implicit) &&
constructorParam.exists(p =>
qualType.widenDealias <:< p.widenDealias
)
end isImplicitClass

def isImplicitClassMethod = sym.is(Flags.Method) &&
isImplicitClass(sym.maybeOwner)

if isExtensionMethod then
completionsWithSuffix(
sym,
sym.decodedName,
CompletionValue.Extension(_, _, _),
).map(visit).forall(_ == true)
else if isImplicitClassMethod then
completionsWithSuffix(
sym,
sym.decodedName,
CompletionValue.ImplicitClass(_, _, _, sym.maybeOwner),
).map(visit).forall(_ == true)
else false,
)
Some(search.searchMethods(query, buildTargetIdentifier, visitor))
Expand Down
Loading