Skip to content

Commit

Permalink
Automatic type based dropdown does not include singleton in a union t…
Browse files Browse the repository at this point in the history
…ype (#6629)

close #6532

Set tag values to display `Auto` type in dropdowns correctly.

# Important Notes
![2023-05-10-141458_969x566_scrot](https://github.com/enso-org/enso/assets/357683/9a048b3c-d192-4382-bf76-9cbe6c9556d1)
![2023-05-10-141513_991x232_scrot](https://github.com/enso-org/enso/assets/357683/c50e1e73-23a6-4b32-90bf-f849a127c85d)
  • Loading branch information
4e6 authored May 10, 2023
1 parent 4e7191a commit 3dd05c2
Show file tree
Hide file tree
Showing 16 changed files with 202 additions and 61 deletions.
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))
.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

0 comments on commit 3dd05c2

Please sign in to comment.