JetBrains / jsitter

tree-sitter API for JVM
Other
65 stars 10 forks source link

Support for tree-sitter version 0.16.0+ and more languages #3

Open Mizzlr opened 4 years ago

Mizzlr commented 4 years ago

As of this date, tree-sitter and tree-sitter- projects have moved to 0.16.0.

One of the changes we will need to do is

diff --git a/native/src/java_api.c b/native/src/java_api.c
--- a/native/src/java_api.c
+++ b/native/src/java_api.c
@@ -40,25 +40,25 @@
     (JNIEnv *env, jclass class, jlong language_ptr, jstring name) {
         jboolean copy;
         const char *chars = (*env)->GetStringUTFChars(env, name, &copy);
-        TSSymbol symbol = ts_language_symbol_for_name((TSLanguage *)language_ptr, chars);
+        TSSymbol symbol = ts_language_symbol_for_name((TSLanguage *)language_ptr, chars, 0, false);
         if (copy) {
             (*env)->ReleaseStringUTFChars(env, name, chars);
         }
         return symbol;

This just made it compile and work. But not sure if it is fully correct.

Also probably expand to other languages as well with CMakeLists.txt

cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
project(JSitter)

set(CMAKE_BUILD_TYPE Release)

find_package(JNI REQUIRED)

SET(CMAKE_C_COMPILER "/usr/bin/clang")
SET(CMAKE_C_FLAGS "-Wall -Ofast -g")

include_directories(src/)
include_directories(tree-sitter/lib/src tree-sitter/lib/include tree-sitter/lib/utf8proc)
include_directories(${JNI_INCLUDE_DIRS})

add_library(jsitter SHARED src/java_api.c tree-sitter/lib/src/lib.c)

add_library(tsjavascript SHARED grammars/tree-sitter-javascript/src/parser.c grammars/tree-sitter-javascript/src/scanner.c)
add_library(tstypescript SHARED grammars/tree-sitter-typescript/typescript/src/parser.c grammars/tree-sitter-typescript/typescript/src/scanner.c)
add_library(tstsx SHARED grammars/tree-sitter-typescript/tsx/src/parser.c grammars/tree-sitter-typescript/tsx/src/scanner.c)

add_library(tspython SHARED grammars/tree-sitter-python/src/parser.c grammars/tree-sitter-python/src/scanner.cc)
add_library(tsruby SHARED grammars/tree-sitter-ruby/src/parser.c grammars/tree-sitter-ruby/src/scanner.cc)
add_library(tsphp SHARED grammars/tree-sitter-php/src/parser.c grammars/tree-sitter-php/src/scanner.cc)

add_library(tshtml SHARED grammars/tree-sitter-html/src/parser.c grammars/tree-sitter-html/src/scanner.cc)
add_library(tscss SHARED grammars/tree-sitter-css/src/parser.c grammars/tree-sitter-css/src/scanner.c)
add_library(tsyaml SHARED grammars/tree-sitter-yaml/src/parser.c grammars/tree-sitter-yaml/src/scanner.cc)
add_library(tsjson SHARED grammars/tree-sitter-json/src/parser.c)
add_library(tsbash SHARED grammars/tree-sitter-bash/src/parser.c grammars/tree-sitter-bash/src/scanner.cc)

add_library(tsc SHARED grammars/tree-sitter-c/src/parser.c)
add_library(tscpp SHARED grammars/tree-sitter-cpp/src/parser.c grammars/tree-sitter-cpp/src/scanner.cc)
add_library(tsgo SHARED grammars/tree-sitter-go/src/parser.c)
add_library(tsrust SHARED grammars/tree-sitter-rust/src/parser.c grammars/tree-sitter-rust/src/scanner.c)

target_link_libraries(jsitter tscss tscpp tsc tsgo tsjavascript tstypescript tsrust tsbash tsjson tsyaml tshtml tsphp tsruby tspython)

I have a working copy of this with ts version 0.16.0 with all the languages in my own fork.

zajac commented 4 years ago

Hi, Thank you for this! I'll merge the change and see if it works with new tree-sitter. The critical thing to check is the layout of Subtree struct. I'll drop a note once I'll do this. I would refrain from adding grammars to CMakeLists. The go grammar is added only for testing purposes. Grammars to be used in production should be compiled or downloaded separately and loaded dynamically from shared libs.

bzz commented 4 years ago

I was wondering if there has been work done on this already elsewhere 🤔

XVilka commented 2 years ago

Note, that latest available Tree-Sitter version is 0.20 now, with some breaking changes: https://github.com/tree-sitter/tree-sitter/releases/tag/v0.20.0