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

[Feature Request]: add predicates support #23

Closed
1 task done
AntonPieper opened this issue Jun 20, 2023 · 4 comments
Closed
1 task done

[Feature Request]: add predicates support #23

AntonPieper opened this issue Jun 20, 2023 · 4 comments

Comments

@AntonPieper
Copy link

AntonPieper commented Jun 20, 2023

I am trying to port the rust bindings for the Query including predicates) to kotlin. Whilst doing this I noticed that not all functions have bindings.

What is missing:

Implementation

getCaptureQuantifierForId (untested)

TSQuantifier.java

public enum TSQuantifier {
  TSQuantifierZero, // must match the array initialization value
  TSQuantifierZeroOrOne,
  TSQuantifierZeroOrMore,
  TSQuantifierOne,
  TSQuantifierOneOrMore
}

TSQuery.java

public class TSQuery {
  // ...
  public TSQuantifier getCaptureQuantifierForId(int patternId, int captureId) {
    checkAccess();
    int value = Native.getCaptureQuantifierForId(this.pointer, patternId, captureId);
    if (value >= TSQualifier.values().length) throw new IllegalStateException("Illegal TSQuantifier value: " + value);
    return TSQuantifier.values()[value];
  }
  // ...
  private static class Native {
    // ...
    public static native int getCaptureQuantifierForId(long query, int patternId, int captureId);
  }
}

ts_query.cc

extern "C" JNIEXPORT jint JNICALL
Java_com_itsaky_androidide_treesitter_TSQuery_00024Native_getCaptureQuantifierForId(JNIEnv* env, jclass self, jlong query, jint patternId, jint captureId) {
  return (jint)ts_query_capture_quantifier_for_id((TSQuery*)query, patternId, captureId);
}
@itsaky
Copy link
Member

itsaky commented Jun 20, 2023

Sure, I'll add it. Are there any other APIs you want me to implement?

@AntonPieper
Copy link
Author

AntonPieper commented Jun 20, 2023

I already have the constructor for Query down. Later I'll try to implement the missing methods. For now the only missing function is the TSQuantifiers. When this works out, Query could be used to implement syntax highlighting for all tree-sitter grammars using the queries/ directory. As is now, TSQuery is a wrapper around the API and TSQuery does not directly support predicates like #match?

@itsaky
Copy link
Member

itsaky commented Jun 20, 2023

As is now, TSQuery is a wrapper around the API and TSQuery does not directly support predicates like #match?

As written in the tree-sitter docs, predicates are not directly handled by the tree sitter library and as android-tree-sitter only provides Java bindings for the library, I think it will be better if the code editor implementation handles predicates.

You can have a look at sora-editor. It supports tree sitter syntax highlighting and has support for predicates. You can even add your own predicates (example).

@itsaky
Copy link
Member

itsaky commented Jun 22, 2023

Added in 5c61cff. A new release will be published soon.

@itsaky itsaky closed this as completed Jun 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants