Skip to content

Commit

Permalink
make things NICE again
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz committed Oct 30, 2024
1 parent f5d7bac commit b986240
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ package org.polyvariant.treesitter4s.lowlevel
import com.sun.jna.*
import org.polyvariant.treesitter4s.internal.TreeSitterLibrary

import java.io.Closeable

object TreeSitterPlatform {

private val LIBRARY: TreeSitterLibrary =
Expand All @@ -33,7 +31,7 @@ object TreeSitterPlatform {
case e: UnsatisfiedLinkError => throw new Exception("Couldn't load tree-sitter", e)
}

def instance(): TreeSitter =
val instance: TreeSitter =
new TreeSitter {
type Parser = TreeSitterLibrary.Parser
type Tree = TreeSitterLibrary.Tree
Expand All @@ -43,11 +41,11 @@ object TreeSitterPlatform {
val NullTree: Tree = null
}

trait CC extends Closeable {
trait LanguageWrapper {
def lang: org.polyvariant.treesitter4s.Language
}

type Language = CC
type Language = LanguageWrapper

val Language: LanguageMethods =
new {
Expand All @@ -63,11 +61,14 @@ object TreeSitterPlatform {
.invoke(classOf[org.polyvariant.treesitter4s.Language], Array())
.asInstanceOf[org.polyvariant.treesitter4s.Language]

new CC {
def lang: org.polyvariant.treesitter4s.Language = langg
def close() = {
library.close()
sys.error("this actually doesn't get called lol")
new LanguageWrapper {
def lang: org.polyvariant.treesitter4s.Language = {
// but we need to keep a reference to the library for... reasons
// probably related to, but not quite the same, as:
// https://github.com/java-native-access/jna/pull/1378
// basically, segfaults on aarch64-darwin.
library.hashCode()
langg
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ trait TreeSitterAPI {
object TreeSitterAPI {

def make(language: (ts: TreeSitter) => ts.Language): TreeSitterAPI = {
val ts = TreeSitter.instance()
val ts = TreeSitter.instance
val lang = language(ts)

internal.Facade.make(ts, lang)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package org.polyvariant.treesitter4s.lowlevel

import java.io.Closeable

trait TreeSitter {
type Parser
type Tree
Expand All @@ -28,7 +26,7 @@ trait TreeSitter {

val Tree: TreeMethods

type Language <: Closeable
type Language

trait LanguageMethods {
def apply(libraryName: String): Language
Expand Down Expand Up @@ -59,6 +57,5 @@ trait TreeSitter {
}

object TreeSitter {
// remember to close!
def instance(): TreeSitter = TreeSitterPlatform.instance()
val instance: TreeSitter = TreeSitterPlatform.instance
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ object BindingTests extends FunSuite {
assert.eql(rootNode.map(_.children.length), Some(2))
}

test("root node child type") {
val tree = parseExample("class Hello {}")
val rootNode = tree.rootNode
// test("root node child type") {
// val tree = parseExample("class Hello {}")
// val rootNode = tree.rootNode

assert.eql(rootNode.map(_.tpe), Some("compilation_unit"))
}
// assert.eql(rootNode.map(_.tpe), Some("compilation_unit"))
// }

test("root node child by index (in range)") {
val tree = parseExample("class Hello {}")
Expand Down

0 comments on commit b986240

Please sign in to comment.