Skip to content

Commit

Permalink
improvement: Support completions for implicit classes
Browse files Browse the repository at this point in the history
Previously, we would only automatically suggest extension methods, not implicit classes.

Now, we also properly suggest implicit classes.

We could follow up with support for Scala 2 also.
  • Loading branch information
tgodzik committed Dec 1, 2023
1 parent fdb0a99 commit b1058ae
Show file tree
Hide file tree
Showing 10 changed files with 414 additions and 41 deletions.
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 @@ -21,7 +22,12 @@ class CompilerSearchVisitor(
val logger: Logger = Logger.getLogger(classOf[CompilerSearchVisitor].getName)

private def isAccessible(sym: Symbol): Boolean = try
sym != NoSymbol && sym.isPublic && sym.isStatic
sym != NoSymbol && sym.isPublic && sym.isStatic || {
val owner = sym.maybeOwner
owner != NoSymbol && owner.isClass &&
owner.is(Flags.Implicit) &&
owner.isStatic && owner.isPublic
}
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,20 @@ 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
val searched = typeName(value)
owner.info.allMembers
.find(_.name == searched)
.map(_.symbol)
.toList
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 +104,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 @@ -273,7 +273,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 @@ -128,6 +128,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,35 @@ 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.allMembers
.find(_.symbol.isAllOf(Flags.PrivateParamAccessor))
.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

0 comments on commit b1058ae

Please sign in to comment.