### Parse Kotlin Code with KTreeSitter Source: https://github.com/tree-sitter/kotlin-tree-sitter/blob/master/ktreesitter/README.md This example shows the fundamental steps to parse a Kotlin code string using KTreeSitter: initializing a language, creating a parser, parsing the text, and accessing the root node of the syntax tree for inspection. ```Kotlin val language = Language(TreeSitterKotlin.language()) val parser = Parser(language) val tree = parser.parse("fun main() {}") val rootNode = tree.rootNode assert(rootNode.type == "source_file") assert(rootNode.startPoint.column == 0) assert(rootNode.endPoint.column == 13) ``` -------------------------------- ### Install KTreeSitter Plugin in Gradle Source: https://github.com/tree-sitter/kotlin-tree-sitter/blob/master/ktreesitter-plugin/README.md This snippet demonstrates how to add the KTreeSitter plugin to a Gradle project's build script, enabling its functionalities. It includes repository and plugin ID declarations necessary for plugin resolution. ```Groovy buildscript { repositories { gradlePluginPortal() } } plugins { id("io.github.tree-sitter.ktreesitter-plugin") } ``` -------------------------------- ### Install KTreeSitter Dependency in Kotlin Source: https://github.com/tree-sitter/kotlin-tree-sitter/blob/master/ktreesitter/README.md This snippet demonstrates how to add the KTreeSitter library as a dependency in a Kotlin project's `build.gradle.kts` file, specifying the artifact and repository for build systems like Gradle. ```Kotlin dependencies { implementation("io.github.tree-sitter:ktreesitter") version $ktreesitterVersion } repositories { mavenCentral() } ``` -------------------------------- ### Configure KTreeSitter Grammar Options in Gradle Source: https://github.com/tree-sitter/kotlin-tree-sitter/blob/master/ktreesitter-plugin/README.md This configuration block defines the essential settings for a KTreeSitter grammar within a Gradle project. It covers both default and required options such as base directory, interop and library names, grammar and class names, package, and source file paths for the grammar. ```Groovy grammar { /* Default options */ // The base directory of the grammar baseDir = projectDir.parentFile.parentFile // The name of the C interop def file interopName = "grammar" // The name of the JNI library libraryName = "ktreesitter-$grammarName" /* Required options */ // The name of the grammar grammarName = "java" // The name of the class className = "TreeSitterJava" // The name of the package packageName = "io.github.treesitter.ktreesitter.java" // The source files of the grammar files = arrayOf( baseDir.get().resolve("src/parser.c") // baseDir.get().resolve("src/scanner.c") ) } ``` -------------------------------- ### Configure CMake Build for ktreesitter Shared Library Source: https://github.com/tree-sitter/kotlin-tree-sitter/blob/master/ktreesitter/CMakeLists.txt This CMake script configures the build for the `ktreesitter` shared library. It sets the minimum CMake version, reads the project version from `gradle.properties`, defines compiler standards and options (including platform-specific flags for MSVC and GCC/Clang), includes necessary directories for JNI and Tree-sitter, defines `TREE_SITTER_HIDE_SYMBOLS`, and compiles the shared library from specified C source files. ```CMake cmake_minimum_required(VERSION 3.12.0) file(READ ../gradle.properties GRADLE_PROPERTIES) string(REGEX MATCH "project[.]version=([0-9.]+)" _ ${GRADLE_PROPERTIES}) project(ktreesitter VERSION ${CMAKE_MATCH_1} LANGUAGES C) find_package(JNI REQUIRED) set(CMAKE_C_STANDARD 11) if(MSVC) add_compile_options(/W3 /wd4244) else(MSVC) set(CMAKE_C_VISIBILITY_PRESET hidden) add_compile_options(-Wall -Wextra -Wno-unused-parameter -Wno-cast-function-type -Werror=incompatible-pointer-types -Werror=implicit-function-declaration) endif(MSVC) include_directories(${JNI_INCLUDE_DIRS} ../tree-sitter/lib/src ../tree-sitter/lib/include) add_compile_definitions(TREE_SITTER_HIDE_SYMBOLS) add_library(ktreesitter SHARED ./src/jni/language.c ./src/jni/lookahead_iterator.c ./src/jni/node.c ./src/jni/parser.c ./src/jni/query.c ./src/jni/tree.c ./src/jni/tree_cursor.c ./src/jni/module.c ../tree-sitter/lib/src/lib.c) set_target_properties(ktreesitter PROPERTIES DEFINE_SYMBOL "") install(TARGETS ktreesitter ARCHIVE EXCLUDE_FROM_ALL) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.