Skip to content

docs: extend Javadoc #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# Java Tree-sitter

[![CI][ci]](https://github.com/tree-sitter/java-tree-sitter/actions/workflows/ci.yml)
[![central][central]](https://central.sonatype.com/artifact/io.github.tree-sitter/jtreesitter)
[![docs][docs]](https://tree-sitter.github.io/java-tree-sitter/)
[![CI][ci-shield]](https://github.com/tree-sitter/java-tree-sitter/actions/workflows/ci.yml)
[![central][central-shield]](https://central.sonatype.com/artifact/io.github.tree-sitter/jtreesitter)
[![docs][docs-shield]][docs]

Java bindings to the [tree-sitter] parsing library.

## Usage
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems superfluous. Just click the badge.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My idea here was to make it a bit easier for users to discover.

Otherwise new users might not know how to get started with this project, and might not consider looking at the Javadoc (in case they do notice the badge). They might not expect such an extensive usage example in the package Javadoc because not all projects have that (respectively have it in separate documentation or in the README).


See the [documentation][docs].

## Building

- Install JDK 22 and set `JAVA_HOME` to it
Expand All @@ -29,8 +33,9 @@ These alternatives support older JDK versions or Android:
- [AndroidIDEOfficial/android-tree-sitter](https://github.com/AndroidIDEOfficial/android-tree-sitter) (Android SDK 21+)

[tree-sitter]: https://tree-sitter.github.io/tree-sitter/
[ci]: https://img.shields.io/github/actions/workflow/status/tree-sitter/java-tree-sitter/ci.yml?logo=github&label=CI
[central]: https://img.shields.io/maven-central/v/io.github.tree-sitter/jtreesitter?logo=sonatype&label=Maven%20Central
[docs]: https://img.shields.io/github/deployments/tree-sitter/java-tree-sitter/github-pages?logo=githubpages&label=API%20Docs
[ci-shield]: https://img.shields.io/github/actions/workflow/status/tree-sitter/java-tree-sitter/ci.yml?logo=github&label=CI
[central-shield]: https://img.shields.io/maven-central/v/io.github.tree-sitter/jtreesitter?logo=sonatype&label=Maven%20Central
[docs-shield]: https://img.shields.io/github/deployments/tree-sitter/java-tree-sitter/github-pages?logo=githubpages&label=API%20Docs
[docs]: https://tree-sitter.github.io/java-tree-sitter/
[FFM]: https://docs.oracle.com/en/java/javase/22/core/foreign-function-and-memory-api.html
[jextract]: https://jdk.java.net/jextract/
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Charset charset() {
* @param charset one of {@link StandardCharsets#UTF_8}, {@link StandardCharsets#UTF_16BE},
* {@link StandardCharsets#UTF_16LE}, or {@link StandardCharsets#UTF_16} (native byte order).
* @throws IllegalArgumentException If the character set is invalid.
* @since 0.25.1
Copy link
Contributor Author

@Marcono1234 Marcono1234 Mar 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #85; for external users this method effectively did not exist before. This assumes the next version will be 0.25.1

*/
@SuppressWarnings("SameParameterValue")
public static @NonNull InputEncoding valueOf(@NonNull Charset charset) throws IllegalArgumentException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ private static UnsatisfiedLinkError unresolved(String name) {
* <strong>The {@linkplain Arena} used to load the language
* must not be closed while the language is being used.</strong>
*
* @throws UnsatisfiedLinkError If the language symbol could not be found.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thrown by the unresolved(language) call

* @throws RuntimeException If the language could not be loaded.
* @since 0.23.1
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* <p>Lookahead iterators can be useful to generate suggestions and improve syntax error diagnostics.<br>
* To get symbols valid in an {@index ERROR} node, use the lookahead iterator on its first leaf node state.<br>
* For {@index MISSING} nodes, a lookahead iterator created on the previous non-extra leaf node may be appropriate.
*
* @see Language#lookaheadIterator(short)
*/
@NullMarked
public final class LookaheadIterator implements AutoCloseable, Iterator<LookaheadIterator.Symbol> {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/io/github/treesitter/jtreesitter/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public final class Parser implements AutoCloseable {
* Creates a new instance with a {@code null} language.
*
* @apiNote Parsing cannot be performed while the language is {@code null}.
* @see #setLanguage(Language)
*/
public Parser() {
arena = Arena.ofShared();
Expand Down Expand Up @@ -400,6 +401,10 @@ public String toString() {
public static final class Options {
private final Predicate<State> progressCallback;

/**
* @param progressCallback Called when parsing progress was made. Return {@code true} to cancel parsing,
* {@code false} to continue parsing.
Comment on lines +405 to +406
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope I understood the logic in https://github.com/tree-sitter/tree-sitter/blob/37a9ecd5b7d52786d0673611f0b8fc54292b1ac1/lib/src/parser.c#L1553 correctly, and true does actually cancel parsing.

That is probably worth documenting, otherwise users might be unsure how this progressCallback works, or assume that true means "continue parsing".

Copy link
Contributor Author

@Marcono1234 Marcono1234 Mar 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that true means cancelling is maybe a bit confusing (even if that matches the tree-sitter behavior). Maybe it would be better to rename it to cancellationCallback (see also tree-sitter/tree-sitter#4312).

It seems even ParserTest might have gotten this wrong?

var options = new Parser.Options((state) -> state.getCurrentByteOffset() <= 1000);

Maybe that should be >= instead of <= to cancel as soon as 1000 or more bytes were consumed? Or is the intention here to additionally verify that the parser consumes the bytes in pieces and not all the 1024 bytes at once?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is probably worth documenting, otherwise users might be unsure how this progressCallback works, or assume that true means "continue parsing".

That is what I assumed. 💀

*/
public Options(Predicate<State> progressCallback) {
this.progressCallback = progressCallback;
}
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/io/github/treesitter/jtreesitter/QueryCursor.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public QueryCursor setMaxStartDepth(@Unsigned int maxStartDepth) {
* than the specified range, but part of that node intersects with the range,
* the entire match will be returned.
*
* @throws IllegalArgumentException If `endByte > startByte`.
* @throws IllegalArgumentException If {@code endByte > startByte}.
*/
public QueryCursor setByteRange(@Unsigned int startByte, @Unsigned int endByte) throws IllegalArgumentException {
if (!ts_query_cursor_set_byte_range(self, startByte, endByte)) {
Expand All @@ -123,7 +123,7 @@ public QueryCursor setByteRange(@Unsigned int startByte, @Unsigned int endByte)
* than the specified range, but part of that node intersects with the range,
* the entire match will be returned.
*
* @throws IllegalArgumentException If `endPoint > startPoint`.
* @throws IllegalArgumentException If {@code endPoint > startPoint}.
*/
public QueryCursor setPointRange(Point startPoint, Point endPoint) throws IllegalArgumentException {
try (var alloc = Arena.ofConfined()) {
Expand Down Expand Up @@ -293,7 +293,8 @@ public static class Options {
private final @Nullable BiPredicate<QueryPredicate, QueryMatch> predicateCallback;

/**
* @param progressCallback Progress handler.
* @param progressCallback Progress handler. Return {@code true} to cancel query execution,
* {@code false} to continue query execution.
Comment on lines +296 to +297
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the comment in Parser; I hope I understood the logic in https://github.com/tree-sitter/tree-sitter/blob/37a9ecd5b7d52786d0673611f0b8fc54292b1ac1/lib/src/query.c#L3631 correctly, and true does actually cancel query execution.

* @param predicateCallback Custom predicate handler.
*/
private Options(
Expand All @@ -304,7 +305,8 @@ private Options(
}

/**
* @param progressCallback Progress handler.
* @param progressCallback Progress handler. Return {@code true} to cancel query execution,
* {@code false} to continue query execution.
*/
public Options(Predicate<State> progressCallback) {
this.progressCallback = progressCallback;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.List;
import org.jspecify.annotations.NullMarked;

/** A match that corresponds to a certain pattern in the query. */
/** A match that corresponds to a certain pattern in a {@link Query}. */
@NullMarked
public record QueryMatch(@Unsigned int patternIndex, List<QueryCapture> captures) {
/** Creates an instance of a QueryMatch record class. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
*
* @apiNote The node the cursor was constructed with is considered the
* root of the cursor, and the cursor cannot walk outside this node.
*
* @see Tree#walk()
* @see Node#walk()
*/
@NullMarked
public final class TreeCursor implements AutoCloseable, Cloneable {
Expand Down