Skip to content

Commit 94b70dc

Browse files
docs: add proper documentation
1 parent b8bac54 commit 94b70dc

File tree

15 files changed

+237
-41
lines changed

15 files changed

+237
-41
lines changed

.github/CONTRIBUTING.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Contributing
2+
3+
## Commits
4+
5+
Commits must follow the [Conventional Commits] specification.
6+
If you're developing on IntelliJ IDEA or Android Studio,
7+
you can use the [Conventional Commit plugin].
8+
9+
[Conventional Commits]: https://www.conventionalcommits.org/en/v1.0.0/
10+
[Conventional Commit plugin]: https://plugins.jetbrains.com/plugin/13389-conventional-commit
11+
12+
## Prerequisites
13+
14+
- JDK 17+
15+
- Android SDK & NDK
16+
- CMake
17+
- C compiler
18+
19+
## Building
20+
21+
### Clone the repository
22+
23+
```shell
24+
git clone https://github.com/tree-sitter/kotlin-tree-sitter
25+
cd kotlin-tree-sitter
26+
```
27+
28+
### Build the JNI libraries
29+
30+
*This step is only necessary for JVM or Android development.*
31+
32+
Generate the grammar files:
33+
34+
```shell
35+
./gradlew generateGrammarFiles
36+
```
37+
38+
Set the `CMAKE_INSTALL_LIBDIR` environment variable.
39+
The value depends on your system:
40+
41+
- `lib/linux/x64`
42+
- `lib/linux/aarch64`
43+
- `lib/macos/x64`
44+
- `lib/macos/aarch64`
45+
- `lib/windows/x64`
46+
47+
Build the libraries (change `.sh` to `.ps1` on Windows):
48+
49+
```shell
50+
./.github/scripts/build-jni.sh
51+
```
52+
53+
### Run the tests
54+
55+
#### JVM
56+
57+
```shell
58+
./gradlew :ktreesitter:jvmTest
59+
```
60+
61+
#### Android
62+
63+
*Requires a connected device.*
64+
65+
```shell
66+
./gradlew :ktreesitter:connectedDebugAndroidTest
67+
```
68+
69+
#### Native
70+
71+
Linux:
72+
73+
```shell
74+
./gradlew :ktreesitter:linuxX64Test
75+
```
76+
77+
macOS:
78+
79+
```shell
80+
# x64
81+
./gradlew :ktreesitter:macosX64Test
82+
# arm64
83+
./gradlew :ktreesitter:macosArm64Test
84+
```
85+
86+
Windows:
87+
88+
```shell
89+
./gradlew :ktreesitter:mingwX64Test
90+
```
91+
92+
## Linting
93+
94+
Code linting is performed using [Ktlint] and [detekt].
95+
Configuration for IntelliJ IDEA and Android Studio is included
96+
(requires the [Ktlint][Ktlint plugin] and [detekt][detekt plugin] plugins).
97+
98+
[Ktlint]: https://pinterest.github.io/ktlint/latest/
99+
[detekt]: https://detekt.dev/
100+
[Ktlint plugin]: https://plugins.jetbrains.com/plugin/15057-ktlint
101+
[detekt plugin]: https://plugins.jetbrains.com/plugin/10761-detekt

README.md

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
1-
# KTreeSitter
2-
3-
Kotlin bindings to the Tree-sitter parsing library
4-
5-
## TODO
6-
7-
- [x] CI
8-
- [ ] Implementation
9-
- [x] Native
10-
- [x] JVM
11-
- [x] Android
12-
- [ ] WASI?
13-
- [x] Documentation
14-
- [ ] Bundled languages
15-
- [ ] Kotlin
16-
- [x] Java
17-
- [ ] Groovy
18-
- [ ] Properties
19-
- [ ] XML
20-
- [ ] C
21-
- [ ] C++
22-
- [ ] Objective-C
23-
- [ ] Swift
24-
- [ ] Markdown
25-
- [ ] CMake
26-
- [x] Gradle plugin
27-
- [ ] Package
28-
- [ ] DSL?
1+
# Kotlin Tree-sitter
2+
3+
[![CI][ci]](https://github.com/tree-sitter/kotlin-tree-sitter/actions/workflows/ci.yml)
4+
[![central][central]](https://central.sonatype.com/artifact/io.github.tree-sitter/ktreesitter)
5+
[![docs][docs]](https://tree-sitter.github.io/kotlin-tree-sitter/)
6+
7+
Kotlin bindings to the [tree-sitter] parsing library.
8+
9+
## Modules
10+
11+
### ktreesitter
12+
13+
The KTreeSitter library module.
14+
15+
### ktreesitter-plugin
16+
17+
The plugin responsible for generating source files for languages.
18+
19+
### languages
20+
21+
Some bundled languages that are relevant to Kotlin development.
22+
23+
[tree-sitter]: https://tree-sitter.github.io/tree-sitter/
24+
[ci]: https://img.shields.io/github/actions/workflow/status/tree-sitter/kotlin-tree-sitter/ci.yml?logo=github&label=CI
25+
[central]: https://img.shields.io/maven-central/v/io.github.tree-sitter/ktreesitter?logo=sonatype&label=Maven%20Central
26+
[docs]: https://img.shields.io/github/deployments/tree-sitter/kotlin-tree-sitter/github-pages?logo=kotlin&label=Docs

ktreesitter-plugin/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# KTreeSitter plugin
2+
3+
A plugin that generates code for KTreeSitter grammar packages.
4+
5+
## Installation
6+
7+
```groovy
8+
buildscript {
9+
repositories {
10+
gradlePluginPortal()
11+
}
12+
}
13+
14+
plugins {
15+
id("io.github.tree-sitter.ktreesitter-plugin")
16+
}
17+
```
18+
19+
## Configuration
20+
21+
```groovy
22+
grammar {
23+
/* Default options */
24+
// The base directory of the grammar
25+
baseDir = projectDir.parentFile.parentFile
26+
// The name of the C interop def file
27+
interopName = "grammar"
28+
// The name of the JNI library
29+
libraryName = "ktreesitter-$grammarName"
30+
31+
/* Required options */
32+
// The name of the grammar
33+
grammarName = "java"
34+
// The name of the class
35+
className = "TreeSitterJava"
36+
// The name of the package
37+
packageName = "io.github.treesitter.ktreesitter.java"
38+
// The source files of the grammar
39+
files = arrayOf(
40+
baseDir.get().resolve("src/parser.c"),
41+
// baseDir.get().resolve("src/scanner.c")
42+
)
43+
}
44+
```

ktreesitter-plugin/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ java {
2323
@Suppress("UnstableApiUsage")
2424
gradlePlugin {
2525
vcsUrl = "https://github.com/tree-sitter/kotlin-tree-sitter"
26-
website = "https://github.com/tree-sitter/kotlin-tree-sitter/tree/master/plugin"
26+
website = "https://github.com/tree-sitter/kotlin-tree-sitter/tree/master/ktreesitter-plugin"
2727
plugins.create("ktreesitter") {
2828
id = "$group.${project.name}"
2929
displayName = "KTreeSitter grammar plugin"

ktreesitter/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# KTreeSitter
2+
3+
Kotlin bindings to the [tree-sitter] parsing library.
4+
5+
## Supported platforms
6+
7+
- [x] JVM
8+
- [x] Android
9+
- [x] Native
10+
- [ ] WASI
11+
12+
*JS and WASM JS will not be supported.*
13+
14+
## Installation
15+
16+
```groovy
17+
dependencies {
18+
implementation("io.github.tree-sitter:ktreesitter") version $ktreesitterVersion
19+
}
20+
21+
repositories {
22+
mavenCentral()
23+
}
24+
```
25+
26+
## Basic usage
27+
28+
```kotlin
29+
import io.github.treesitter.ktreesitter.*
30+
import io.github.treesitter.ktreesitter.java.TreeSitterJava
31+
32+
val language = Language(TreeSitterJava.language())
33+
val parser = Parser(language)
34+
val tree = parser.parse("class Foo {}")
35+
36+
assert(tree.rootNode.type == "program")
37+
```
38+
39+
[tree-sitter]: https://tree-sitter.github.io/tree-sitter/

ktreesitter/build.gradle.kts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,10 @@ signing {
209209

210210
tasks.dokkaHtml {
211211
val tmpDir = layout.buildDirectory.get().dir("tmp")
212-
val readme = rootDir.resolve("README.md")
213212
val ref = System.getenv("GITHUB_SHA")?.subSequence(0, 7) ?: "master"
214213
val url = "https://github.com/tree-sitter/kotlin-tree-sitter/blob/$ref/ktreesitter"
215214

216-
inputs.file(readme)
215+
inputs.file(file("README.md"))
217216

218217
moduleName.set("KTreeSitter")
219218

@@ -226,8 +225,7 @@ tasks.dokkaHtml {
226225
dokkaSourceSets.configureEach {
227226
jdkVersion.set(17)
228227
noStdlibLink.set(true)
229-
// TODO: uncomment when ready
230-
// includes.from(tmpDir.file("README.md"))
228+
includes.from(tmpDir.file("README.md"))
231229
externalDocumentationLink("https://kotlinlang.org/api/core/")
232230
sourceLink {
233231
localDirectory.set(projectDir)
@@ -237,9 +235,13 @@ tasks.dokkaHtml {
237235

238236
doFirst {
239237
copy {
240-
from(readme)
238+
from(file("README.md"))
241239
into(tmpDir)
242-
filter { it.replaceFirst("# KTreeSitter", "# Module KTreeSitter") }
240+
filter { file ->
241+
file.replaceFirst("# KTreeSitter", "# Module KTreeSitter")
242+
.replaceFirst("\$ktreesitterVersion", "\"$version\"")
243+
.replace("[x]", "☑").replace("[ ]", "☐")
244+
}
243245
}
244246
}
245247

ktreesitter/src/androidMain/kotlin/io/github/treesitter/ktreesitter/Language.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ actual class Language @Throws(IllegalArgumentException::class) actual constructo
103103
/**
104104
* Create a new [Query] from a string containing one or more S-expression
105105
* [patterns](https://tree-sitter.github.io/tree-sitter/using-parsers#query-syntax).
106+
*
107+
* @throws [QueryError] If any error occurred while creating the query.
106108
*/
109+
@Throws(QueryError::class)
107110
actual fun query(source: String) = Query(this, source)
108111

109112
actual override fun equals(other: Any?) =

ktreesitter/src/androidMain/kotlin/io/github/treesitter/ktreesitter/Parser.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ actual class Parser actual constructor() : AutoCloseable {
3434
* The ranges of text that the parser will include when parsing.
3535
*
3636
* By default, the parser will always include entire documents.
37-
* Setting this property allows you to parse only a *portion* of a
37+
* Setting this property allows you to parse only a _portion_ of a
3838
* document but still return a syntax tree whose ranges match up with
3939
* the document as a whole. You can also pass multiple disjoint ranges.
4040
*

ktreesitter/src/commonMain/kotlin/io/github/treesitter/ktreesitter/Language.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ expect class Language @Throws(IllegalArgumentException::class) constructor(langu
6969
/**
7070
* Create a new [Query] from a string containing one or more S-expression
7171
* [patterns](https://tree-sitter.github.io/tree-sitter/using-parsers#query-syntax).
72+
*
73+
* @throws [QueryError] If any error occurred while creating the query.
7274
*/
75+
@Throws(QueryError::class)
7376
fun query(source: String): Query
7477

7578
override fun equals(other: Any?): Boolean

ktreesitter/src/commonMain/kotlin/io/github/treesitter/ktreesitter/Parser.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ expect class Parser() {
3333
* The ranges of text that the parser will include when parsing.
3434
*
3535
* By default, the parser will always include entire documents.
36-
* Setting this property allows you to parse only a *portion* of a
36+
* Setting this property allows you to parse only a _portion_ of a
3737
* document but still return a syntax tree whose ranges match up with
3838
* the document as a whole. You can also pass multiple disjoint ranges.
3939
*

0 commit comments

Comments
 (0)