AndroidIDEOfficial / android-tree-sitter

Tree Sitter for Android
GNU Lesser General Public License v2.1
51 stars 17 forks source link

[Feature Request]: add predicates support #23

Closed AntonPieper closed 1 year ago

AntonPieper commented 1 year ago

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 commented 1 year ago

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

AntonPieper commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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