From d8cd960b64824e5154c0e02d003df400e55e25f2 Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Thu, 23 Feb 2023 10:16:00 +0100 Subject: [PATCH] Offer no suggestion for single constructor --- .../compiler/context/SuggestionBuilder.scala | 12 +- .../test/context/SuggestionBuilderTest.scala | 156 +++++++++++++++++- 2 files changed, 161 insertions(+), 7 deletions(-) diff --git a/engine/runtime/src/main/scala/org/enso/compiler/context/SuggestionBuilder.scala b/engine/runtime/src/main/scala/org/enso/compiler/context/SuggestionBuilder.scala index c03d4cb084b5..333e203d2c65 100644 --- a/engine/runtime/src/main/scala/org/enso/compiler/context/SuggestionBuilder.scala +++ b/engine/runtime/src/main/scala/org/enso/compiler/context/SuggestionBuilder.scala @@ -382,10 +382,14 @@ final class SuggestionBuilder[A: IndexedSource]( resolvedName: BindingsMap.ResolvedName ): TypeArg = resolvedName match { case tp: BindingsMap.ResolvedType => - TypeArg.Sum( - Some(tp.qualifiedName), - tp.getVariants.map(r => TypeArg.Value(r.qualifiedName)) - ) + 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) } diff --git a/engine/runtime/src/test/scala/org/enso/compiler/test/context/SuggestionBuilderTest.scala b/engine/runtime/src/test/scala/org/enso/compiler/test/context/SuggestionBuilderTest.scala index 8766be0f77a9..fd99c9554bee 100644 --- a/engine/runtime/src/test/scala/org/enso/compiler/test/context/SuggestionBuilderTest.scala +++ b/engine/runtime/src/test/scala/org/enso/compiler/test/context/SuggestionBuilderTest.scala @@ -2125,10 +2125,160 @@ class SuggestionBuilderTest extends AnyWordSpecLike with Matchers { ) } - "build module with overloaded functions" in { + "build module with overloaded functions and two constructors" in { val code = """type A | Mk_A + | Mk_A_Plus a + | + | quux : A -> A + | quux self x = x + | + |quux : A -> A + |quux x = x + | + |main = + | quux A + | A.quux A""".stripMargin + val module = code.preprocessModule + + build(code, module) shouldEqual Tree.Root( + Vector( + ModuleNode, + Tree.Node( + Suggestion.Type( + externalId = None, + module = "Unnamed.Test", + name = "A", + params = List(), + returnType = "Unnamed.Test.A", + parentType = Some(SuggestionBuilder.Any), + documentation = None + ), + Vector() + ), + Tree.Node( + Suggestion.Constructor( + externalId = None, + module = "Unnamed.Test", + name = "Mk_A", + arguments = List(), + returnType = "Unnamed.Test.A", + documentation = None + ), + Vector() + ), + Tree.Node( + Suggestion.Constructor( + None, + "Unnamed.Test", + "Mk_A_Plus", + List( + Suggestion.Argument( + "a", + "Standard.Base.Any.Any", + false, + false, + None, + None + ) + ), + "Unnamed.Test.A", + None, + None, + None, + None + ), + Vector() + ), + Tree.Node( + Suggestion.Method( + None, + "Unnamed.Test", + "a", + Vector( + Suggestion + .Argument("self", "Unnamed.Test.A", false, false, None, None) + ), + "Unnamed.Test.A", + "Standard.Base.Any.Any", + false, + None, + None, + None, + None + ), + Vector() + ), + Tree.Node( + Suggestion.Method( + None, + "Unnamed.Test", + "quux", + Vector( + Suggestion + .Argument("self", "Unnamed.Test.A", false, false, None), + Suggestion.Argument( + "x", + "Unnamed.Test.A", + false, + false, + None, + Some(List("Unnamed.Test.Mk_A", "Unnamed.Test.Mk_A_Plus")) + ) + ), + selfType = "Unnamed.Test.A", + returnType = "Unnamed.Test.A", + isStatic = false, + documentation = None + ), + Vector() + ), + Tree.Node( + Suggestion.Method( + externalId = None, + module = "Unnamed.Test", + name = "quux", + arguments = Vector( + Suggestion.Argument("self", "Unnamed.Test", false, false, None), + Suggestion.Argument( + "x", + "Unnamed.Test.A", + false, + false, + None, + Some(List("Unnamed.Test.Mk_A", "Unnamed.Test.Mk_A_Plus")) + ) + ), + selfType = "Unnamed.Test", + returnType = "Unnamed.Test.A", + isStatic = true, + documentation = None + ), + Vector() + ), + Tree.Node( + Suggestion.Method( + externalId = None, + module = "Unnamed.Test", + name = "main", + arguments = List(), + selfType = "Unnamed.Test", + returnType = SuggestionBuilder.Any, + isStatic = true, + documentation = None + ), + Vector() + ) + ) + ) + } + + "single constructor isn't suggested" in { + val code = + """type A + | Mk_A + | | quux : A -> A | quux self x = x | @@ -2180,7 +2330,7 @@ class SuggestionBuilderTest extends AnyWordSpecLike with Matchers { false, false, None, - Some(List("Unnamed.Test.Mk_A")) + None ) ), selfType = "Unnamed.Test.A", @@ -2203,7 +2353,7 @@ class SuggestionBuilderTest extends AnyWordSpecLike with Matchers { false, false, None, - Some(List("Unnamed.Test.Mk_A")) + None ) ), selfType = "Unnamed.Test",