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

Automatic type based dropdown does not include singleton in a union type #6629

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,7 +10,7 @@ public Class<? extends Builtin> getSuperType() {
}

@Override
protected boolean containsValues() {
public boolean containsValues() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public final void initialize(EnsoLanguage language, ModuleScope scope, Map<Class
postInitialize();
}

protected boolean containsValues() {
public boolean containsValues() {
return getDeclaredConstructors().size() > 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ protected Class<? extends Builtin> getSuperType() {
}

@Override
protected boolean containsValues() {
public boolean containsValues() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@BuiltinType(name = "Standard.Base.Nothing.Nothing")
public class Nothing extends Builtin {
@Override
protected boolean containsValues() {
public boolean containsValues() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@BuiltinType(name = "Standard.Base.Panic.Panic")
public class Panic extends Builtin {
@Override
protected boolean containsValues() {
public boolean containsValues() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@BuiltinType(name = "Standard.Base.Function.Function")
public class Function extends Builtin {
@Override
protected boolean containsValues() {
public boolean containsValues() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ protected Class<? extends Builtin> getSuperType() {
}

@Override
protected boolean containsValues() {
public boolean containsValues() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ protected Class<? extends Builtin> getSuperType() {
}

@Override
protected boolean containsValues() {
public boolean containsValues() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@BuiltinType(name = "Standard.Base.Data.Numbers.Number")
public class Number extends Builtin {
@Override
protected boolean containsValues() {
public boolean containsValues() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@BuiltinType(name = "Standard.Base.Data.Text.Text")
public class Text extends Builtin {
@Override
protected boolean containsValues() {
public boolean containsValues() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ final class SerializationManager(
compiler.packageRepository
.getModulesForLibrary(libraryName)
.flatMap { module =>
SuggestionBuilder(module)
SuggestionBuilder(module, compiler)
.build(module.getName, module.getIr)
.toVector
.filter(Suggestion.isGlobal)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.enso.compiler.context

import org.enso.compiler.Compiler
import org.enso.compiler.core.IR
import org.enso.compiler.data.BindingsMap
import org.enso.compiler.pass.resolve.{
Expand All @@ -26,7 +27,8 @@ import scala.collection.mutable
*/
final class SuggestionBuilder[A: IndexedSource](
val source: A,
val typeGraph: TypeGraph
val typeGraph: TypeGraph,
val compiler: Compiler
) {

import SuggestionBuilder._
Expand Down Expand Up @@ -379,21 +381,43 @@ final class SuggestionBuilder[A: IndexedSource](
)
}

private def buildResolvedUnionTypeName(
/** Build a [[TypeArg]] from the resolved type name.
*
* @param resolvedName the resolved type name
* @return the corresponding type argument
*/
private def buildResolvedTypeName(
resolvedName: BindingsMap.ResolvedName
): TypeArg = resolvedName match {
case tp: BindingsMap.ResolvedType =>
if (tp.getVariants.size > 1) {
TypeArg.Sum(
Some(tp.qualifiedName),
tp.getVariants.map(r => TypeArg.Value(r.qualifiedName))
)
} else {
TypeArg.Sum(Some(tp.qualifiedName), Seq.empty)
}
case _: BindingsMap.ResolvedName =>
TypeArg.Value(resolvedName.qualifiedName)
}
): TypeArg =
resolvedName match {
case tp: BindingsMap.ResolvedType =>
tp.getVariants.size match {
case 0 =>
val isBuiltinTypeWithValues =
tp.tp.builtinType &&
Option(compiler.builtins.getBuiltinType(tp.tp.name))
Copy link
Member

Choose a reason for hiding this comment

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

I'd add compiler.builtins.isBuiltinTypeWithValues(tp.tp.name).

.exists(_.containsValues())
if (isBuiltinTypeWithValues) {
TypeArg.Sum(Some(tp.qualifiedName), Seq())
} else {
TypeArg.Sum(
Some(tp.qualifiedName),
Seq(TypeArg.Value(tp.qualifiedName))
)
}

case 1 =>
TypeArg.Sum(Some(tp.qualifiedName), Seq())

case _ =>
TypeArg.Sum(
Some(tp.qualifiedName),
tp.getVariants.map(r => TypeArg.Value(r.qualifiedName))
)
}
case _: BindingsMap.ResolvedName =>
TypeArg.Value(resolvedName.qualifiedName)
}

/** Build type signature from the ir metadata.
*
Expand Down Expand Up @@ -437,7 +461,7 @@ final class SuggestionBuilder[A: IndexedSource](
case tname: IR.Name =>
tname
.getMetadata(TypeNames)
.map(t => buildResolvedUnionTypeName(t.target))
.map(t => buildResolvedTypeName(t.target))
.getOrElse(TypeArg.Value(QualifiedName.simpleName(tname.name)))

case _ =>
Expand Down Expand Up @@ -551,24 +575,29 @@ final class SuggestionBuilder[A: IndexedSource](
isSuspended = varg.suspended,
hasDefault = varg.defaultValue.isDefined,
defaultValue = varg.defaultValue.flatMap(buildDefaultValue),
tagValues = targ match {
case s: TypeArg.Sum => {
val tagValues = pluckVariants(s)
if (tagValues.nonEmpty) {
Some(tagValues)
} else {
None
}
}
case _ => None
}
tagValues = buildTagValues(targ)
)

private def pluckVariants(arg: TypeArg): Seq[String] = arg match {
case TypeArg.Sum(_, List()) => Seq()
case TypeArg.Sum(_, variants) => variants.flatMap(pluckVariants)
case TypeArg.Value(n) => Seq(n.toString)
case _ => Seq()
/** Build tag values of type argument.
*
* @param targ the type argument
* @return the list of tag values
*/
private def buildTagValues(targ: TypeArg): Option[Seq[String]] = {
def go(arg: TypeArg): Seq[String] = arg match {
case TypeArg.Sum(_, List()) => Seq()
case TypeArg.Sum(_, variants) => variants.flatMap(go)
case TypeArg.Value(n) => Seq(n.toString)
case _ => Seq()
}

targ match {
case s: TypeArg.Sum =>
val tagValues = go(s)
Option.unless(tagValues.isEmpty)(tagValues)
case _ => None

}
}

/** Build the name of type argument.
Expand Down Expand Up @@ -661,8 +690,11 @@ object SuggestionBuilder {
* @param module the module to index
* @return the suggestions builder for the module
*/
def apply(module: Module): SuggestionBuilder[CharSequence] =
SuggestionBuilder(module.getSource.getCharacters)
def apply(
module: Module,
compiler: Compiler
): SuggestionBuilder[CharSequence] =
SuggestionBuilder(module.getSource.getCharacters, compiler)

/** Create the suggestion builder.
*
Expand All @@ -672,17 +704,21 @@ object SuggestionBuilder {
*/
def apply[A: IndexedSource](
source: A,
typeGraph: TypeGraph
typeGraph: TypeGraph,
compiler: Compiler
): SuggestionBuilder[A] =
new SuggestionBuilder[A](source, typeGraph)
new SuggestionBuilder[A](source, typeGraph, compiler)

/** Create the suggestion builder.
*
* @param source the text source
* @tparam A the type of the text source
*/
def apply[A: IndexedSource](source: A): SuggestionBuilder[A] =
new SuggestionBuilder[A](source, Types.getTypeHierarchy)
def apply[A: IndexedSource](
source: A,
compiler: Compiler
): SuggestionBuilder[A] =
new SuggestionBuilder[A](source, Types.getTypeHierarchy, compiler)

/** A single level of an `IR`.
*
Expand Down Expand Up @@ -722,7 +758,7 @@ object SuggestionBuilder {

/** Function type, like `A -> A`.
*
* @param signature the list of types defining the function
* @param arguments the list of types defining the function
*/
case class Function(arguments: Vector[TypeArg], result: TypeArg)
extends TypeArg
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ final class AnalyzeModuleInScopeJob(
ctx.executionService.getLogger
.log(Level.FINEST, s"Analyzing module in scope ${module.getName}")
val moduleName = module.getName
val newSuggestions = SuggestionBuilder(module.getSource.getCharacters)
.build(moduleName, module.getIr)
.filter(Suggestion.isGlobal)
val newSuggestions =
SuggestionBuilder(module, ctx.executionService.getContext.getCompiler)
.build(moduleName, module.getIr)
.filter(Suggestion.isGlobal)
val prevExports = ModuleExports(moduleName.toString, Set())
val newExports = exportsBuilder.build(module.getName, module.getIr)
val notification = Api.SuggestionsDatabaseModuleUpdateNotification(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ object AnalyzeModuleJob {
changeset: Changeset[Rope]
)(implicit ctx: RuntimeContext): Unit = {
val moduleName = module.getName
val compiler = ctx.executionService.getContext.getCompiler
if (module.isIndexed) {
ctx.executionService.getLogger
.log(Level.FINEST, s"Analyzing indexed module $moduleName")
val prevSuggestions = SuggestionBuilder(changeset.source)
.build(moduleName, changeset.ir)
val prevSuggestions =
SuggestionBuilder(changeset.source, compiler)
.build(moduleName, changeset.ir)
val newSuggestions =
SuggestionBuilder(module.getSource.getCharacters)
SuggestionBuilder(module, compiler)
.build(moduleName, module.getIr)
val diff = SuggestionDiff
.compute(prevSuggestions, newSuggestions)
Expand All @@ -75,7 +77,7 @@ object AnalyzeModuleJob {
ctx.executionService.getLogger
.log(Level.FINEST, s"Analyzing not-indexed module ${module.getName}")
val newSuggestions =
SuggestionBuilder(module.getSource.getCharacters)
SuggestionBuilder(module, compiler)
.build(moduleName, module.getIr)
val prevExports = ModuleExports(moduleName.toString, Set())
val newExports = exportsBuilder.build(moduleName, module.getIr)
Expand Down
Loading