Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.

Commit 343eeb8

Browse files
committed
feat: add tree-sitter-kotlin
1 parent 19025f1 commit 343eeb8

File tree

15 files changed

+229
-0
lines changed

15 files changed

+229
-0
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313
[submodule "tree-sitter-json/src/main/cpp/grammar"]
1414
path = tree-sitter-json/src/main/cpp/grammar
1515
url = https://github.com/tree-sitter/tree-sitter-json.git
16+
[submodule "tree-sitter-kotlin/src/main/cpp/grammar"]
17+
path = tree-sitter-kotlin/src/main/cpp/grammar
18+
url = https://github.com/fwcd/tree-sitter-kotlin.git

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

android-tree-sitter/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ android {
5454
dependencies {
5555
testImplementation(project(path = ":tree-sitter-java"))
5656
testImplementation(project(path = ":tree-sitter-json"))
57+
testImplementation(project(path = ":tree-sitter-kotlin"))
5758
testImplementation(project(path = ":tree-sitter-xml"))
5859
testImplementation(project(path = ":tree-sitter-python"))
5960
testImplementation("com.google.truth:truth:1.1.3")

android-tree-sitter/src/test/java/com/itsaky/androidide/treesitter/ParserTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import com.itsaky.androidide.treesitter.java.TSLanguageJava;
2424
import com.itsaky.androidide.treesitter.json.TSLanguageJson;
25+
import com.itsaky.androidide.treesitter.kotlin.TSLanguageKotlin;
2526
import com.itsaky.androidide.treesitter.python.TSLanguagePython;
2627

2728
import org.junit.Test;
@@ -140,4 +141,24 @@ public void testJsonGrammar() {
140141
}
141142
}
142143
}
144+
145+
@Test
146+
public void testKotlinGrammar() {
147+
try (final var parser = new TSParser()) {
148+
parser.setLanguage(TSLanguageKotlin.newInstance());
149+
150+
final var source = "class Main {\n" +
151+
" fun main() {\n" +
152+
" println(\"Hello World\")\n" +
153+
" }\n" +
154+
"}";
155+
156+
try (final var tree = parser.parseString(source)) {
157+
final var rootNode = tree.getRootNode();
158+
assertThat(rootNode).isNotNull();
159+
assertThat(rootNode.getChildCount()).isGreaterThan(0);
160+
assertThat(rootNode.getNodeString()).isEqualTo("(source_file (class_declaration (type_identifier) (class_body (function_declaration (simple_identifier) (function_body (statements (call_expression (simple_identifier) (call_suffix (value_arguments (value_argument (line_string_literal)))))))))))");
161+
}
162+
}
163+
}
143164
}

android-tree-sitter/src/test/java/com/itsaky/androidide/treesitter/TreeSitterTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class TreeSitterTest {
2727
System.load(hostDir + "/libandroid-tree-sitter.so");
2828
System.load(hostDir + "/libtree-sitter-java.so");
2929
System.load(hostDir + "/libtree-sitter-json.so");
30+
System.load(hostDir + "/libtree-sitter-kotlin.so");
3031
System.load(hostDir + "/libtree-sitter-xml.so");
3132
System.load(hostDir + "/libtree-sitter-python.so");
3233
}

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,6 @@ include ':android-tree-sitter'
5555
// =========== Grammars ============
5656
include ':tree-sitter-java'
5757
include ':tree-sitter-json'
58+
include ':tree-sitter-kotlin'
5859
include ':tree-sitter-python'
5960
include ':tree-sitter-xml'

tree-sitter-kotlin/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* This file is part of android-tree-sitter.
3+
*
4+
* android-tree-sitter library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* android-tree-sitter library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with android-tree-sitter. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
import com.itsaky.androidide.treesitter.TreeSitterPlugin
19+
import com.itsaky.androidide.treesitter.TsGrammarPlugin
20+
21+
plugins {
22+
id("com.android.library")
23+
id("com.vanniktech.maven.publish.base")
24+
}
25+
26+
apply {
27+
plugin(TreeSitterPlugin::class.java)
28+
plugin(TsGrammarPlugin::class.java)
29+
}
30+
31+
val rootProjDir: String = rootProject.projectDir.absolutePath
32+
val tsDir = "${rootProjDir}/tree-sitter-lib"
33+
34+
android {
35+
namespace = "com.itsaky.androidide.treesitter.kotlin"
36+
ndkVersion = "24.0.8215888"
37+
38+
defaultConfig {
39+
externalNativeBuild { cmake { arguments("-DPROJECT_DIR=${rootProjDir}", "-DTS_DIR=${tsDir}") } }
40+
}
41+
42+
buildTypes {
43+
release {
44+
isMinifyEnabled = false
45+
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
46+
}
47+
}
48+
49+
externalNativeBuild {
50+
cmake {
51+
path = file("src/main/cpp/CMakeLists.txt")
52+
version = "3.22.1"
53+
}
54+
}
55+
}
56+
57+
dependencies { implementation(project(":android-tree-sitter")) }

tree-sitter-kotlin/consumer-rules.pro

Whitespace-only changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile

0 commit comments

Comments
 (0)