Skip to content

Commit

Permalink
Merge pull request #262 from alephium/suggest_global_enum_fields
Browse files Browse the repository at this point in the history
Suggest global `enum` fields
  • Loading branch information
simerplaha authored Aug 6, 2024
2 parents 4cd05c7 + 25b22af commit cc8a304
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package org.alephium.ralph.lsp.pc.search.completion

import org.alephium.ralph
import org.alephium.ralph.lsp.pc.workspace.{WorkspaceState, WorkspaceSearcher}
import org.alephium.ralph.lsp.pc.sourcecode.SourceLocation
import org.alephium.ralph.lsp.pc.sourcecode.{SourceLocation, SourceCodeSearcher}
import org.alephium.ralph.Ast
import org.alephium.ralph.lsp.access.compiler.ast.node.Node

Expand All @@ -35,10 +35,20 @@ object EnumFieldCompleter {
def suggest(
enumId: Ast.TypeId,
sourceCode: SourceLocation.Code,
workspace: WorkspaceState.IsSourceAware): Iterator[Suggestion.EnumFields] =
WorkspaceSearcher
.collectInheritedParents(sourceCode, workspace)
.parentTrees
workspace: WorkspaceState.IsSourceAware): Iterator[Suggestion.EnumFields] = {
val trees =
WorkspaceSearcher.collectInheritedParents(
sourceCode = sourceCode,
workspace = workspace
)

val globalEnumTrees =
SourceCodeSearcher.collectGlobalEnumsCode(trees.allTrees.iterator)

val allTrees =
trees.parentTrees ++ globalEnumTrees

allTrees
.iterator
.flatMap {
sourceCode =>
Expand All @@ -47,5 +57,6 @@ object EnumFieldCompleter {
Suggestion.EnumFields(SourceLocation.Node(enumDef, sourceCode))
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,50 @@ class EnumFieldCompleterSpec extends AnyWordSpec with Matchers {

actual.sortBy(_.label) shouldBe expected.sortBy(_.label)
}

"global enum" should {
"list enum fields" in {
val suggestions =
suggest {
"""
|enum MyEnum {
| ONE = 1
| TWO = 2
|}
|
|Contract Test() {
|
| fn main() -> () {
| let field = MyEnum.F@@
| }
|}
|""".stripMargin
}

val actual =
suggestions
.collect {
case fields: Suggestion.EnumFields => fields
}
.flatMap(_.toCompletion())

val expected =
List(
Completion.EnumMember(
label = "ONE",
insert = "ONE",
detail = ""
),
Completion.EnumMember(
label = "TWO",
insert = "TWO",
detail = ""
)
)

actual.sortBy(_.label) shouldBe expected.sortBy(_.label)
}
}
}

}

0 comments on commit cc8a304

Please sign in to comment.