Skip to content

Commit

Permalink
Merge pull request #132 from seart-group/feature/fields
Browse files Browse the repository at this point in the history
  • Loading branch information
dabico authored Feb 13, 2024
2 parents 84f70ad + d9ebc33 commit 0a5bce1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
8 changes: 8 additions & 0 deletions lib/ch_usi_si_seart_treesitter_Language.cc
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,14 @@ JNIEXPORT jint JNICALL Java_ch_usi_si_seart_treesitter_Language_fields(
return (jint)ts_language_field_count(language);
}

JNIEXPORT jstring JNICALL Java_ch_usi_si_seart_treesitter_Language_field(
JNIEnv* env, jclass self, jlong languageId, jint fieldId) {
TSLanguage* language = (TSLanguage*)languageId;
TSFieldId field = (TSFieldId)fieldId;
const char* name = ts_language_field_name_for_id(language, field);
return env->NewStringUTF(name);
}

JNIEXPORT jint JNICALL Java_ch_usi_si_seart_treesitter_Language_states(
JNIEnv* env, jclass self, jlong id) {
TSLanguage* language = (TSLanguage*)id;
Expand Down
8 changes: 8 additions & 0 deletions lib/ch_usi_si_seart_treesitter_Language.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 21 additions & 6 deletions src/main/java/ch/usi/si/seart/treesitter/Language.java
Original file line number Diff line number Diff line change
Expand Up @@ -540,13 +540,14 @@ public static void validate(@NotNull Language language) {
private static native int symbols(long id);
private static native Symbol symbol(long languageId, int symbolId);
private static native int fields(long id);
private static native String field(long languageId, int fieldId);
private static native int states(long id);

long id;
int version;
int totalFields;
int totalStates;
Collection<Symbol> symbols;
List<Symbol> symbols;
List<String> fields;
List<String> extensions;

private static final long INVALID = 0L;
Expand Down Expand Up @@ -574,17 +575,19 @@ public static void validate(@NotNull Language language) {
));

Language(long id, String... extensions) {
this(id, version(id), fields(id), states(id), symbols(id), List.of(extensions));
this(id, version(id), states(id), symbols(id), fields(id), List.of(extensions));
}

Language(long id, int version, int totalFields, int totalStates, int totalSymbols, List<String> extensions) {
Language(long id, int version, int totalStates, int totalSymbols, int totalFields, List<String> extensions) {
this.id = id;
this.version = version;
this.totalFields = totalFields;
this.totalStates = totalStates;
this.symbols = IntStream.range(0, totalSymbols)
.mapToObj(symbolId -> symbol(id, symbolId))
.collect(Collectors.toUnmodifiableList());
this.fields = IntStream.range(1, totalFields + 1)
.mapToObj(fieldId -> field(id, fieldId))
.collect(Collectors.toUnmodifiableList());
this.extensions = extensions;
}

Expand Down Expand Up @@ -628,12 +631,24 @@ public int nextState(@NotNull Node node) {

private static native int nextState(long id, int state, int symbol);

/**
* @deprecated Just calculate the {@link List#size() size} of the list returned by {@link #getSymbols()}.
*/
@Generated
@SuppressWarnings("unused")
@Deprecated(forRemoval = true, since = "1.12.0")
public int getTotalSymbols() {
return symbols.size();
}

/**
* @deprecated Just calculate the {@link List#size() size} of the list returned by {@link #getFields()}.
*/
@Generated
@Deprecated(forRemoval = true, since = "1.12.0")
public int getTotalFields() {
return fields.size();
}

@Override
public String toString() {
switch (this) {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/ch/usi/si/seart/treesitter/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public abstract class BaseTest {

Mockito.when(invalid.getId()).thenReturn(0L);
Mockito.when(invalid.getVersion()).thenReturn(0);
Mockito.when(invalid.getTotalFields()).thenReturn(0);
Mockito.when(invalid.getTotalStates()).thenReturn(0);
Mockito.when(invalid.getTotalSymbols()).thenReturn(0);
Mockito.when(invalid.getSymbols()).thenReturn(List.of());
Mockito.when(invalid.getFields()).thenReturn(List.of());
Mockito.when(invalid.getExtensions()).thenReturn(List.of());

Mockito.when(invalid.nextState(Mockito.any())).thenCallRealMethod();
}
Expand Down

0 comments on commit 0a5bce1

Please sign in to comment.