Skip to content

Commit d587ce8

Browse files
committed
Support Tree-sitter version 0.26 and later
* src/treesit.c (init_treesit_functions) [TREE_SITTER_LANGUAGE_VERSION >= 15]: Define prototype for, and load 'ts_language_abi_version' instead of the deprecated (and removed in tree-sitter 0.26) 'ts_language_version'. (ts_language_abi_version) [TREE_SITTER_LANGUAGE_VERSION >= 15]: Define on WINDOWSNT, instead of 'ts_language_version'. (treesit_language_abi_version): New compatibility function. (treesit_load_language, Ftreesit_language_abi_version): Use 'treesit_language_abi_version' instead of 'ts_language_version'. (Bug#79627)
1 parent bd0a141 commit d587ce8

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

src/treesit.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
3535
# include "w32common.h"
3636

3737
/* In alphabetical order. */
38+
#if TREE_SITTER_LANGUAGE_VERSION >= 15
39+
#undef ts_language_abi_version
40+
#else
3841
#undef ts_language_version
42+
#endif
3943
#undef ts_node_child
4044
#undef ts_node_child_by_field_name
4145
#undef ts_node_child_count
@@ -90,7 +94,11 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
9094
#undef ts_tree_get_changed_ranges
9195
#undef ts_tree_root_node
9296

97+
#if TREE_SITTER_LANGUAGE_VERSION >= 15
98+
DEF_DLL_FN (uint32_t, ts_language_abi_version, (const TSLanguage *));
99+
#else
93100
DEF_DLL_FN (uint32_t, ts_language_version, (const TSLanguage *));
101+
#endif
94102
DEF_DLL_FN (TSNode, ts_node_child, (TSNode, uint32_t));
95103
DEF_DLL_FN (TSNode, ts_node_child_by_field_name,
96104
(TSNode, const char *, uint32_t));
@@ -167,7 +175,11 @@ init_treesit_functions (void)
167175
if (!library)
168176
return false;
169177

178+
#if TREE_SITTER_LANGUAGE_VERSION >= 15
179+
LOAD_DLL_FN (library, ts_language_abi_version);
180+
#else
170181
LOAD_DLL_FN (library, ts_language_version);
182+
#endif
171183
LOAD_DLL_FN (library, ts_node_child);
172184
LOAD_DLL_FN (library, ts_node_child_by_field_name);
173185
LOAD_DLL_FN (library, ts_node_child_count);
@@ -225,7 +237,11 @@ init_treesit_functions (void)
225237
return true;
226238
}
227239

240+
#if TREE_SITTER_LANGUAGE_VERSION >= 15
241+
#define ts_language_abi_version fn_ts_language_abi_version
242+
#else
228243
#define ts_language_version fn_ts_language_version
244+
#endif
229245
#define ts_node_child fn_ts_node_child
230246
#define ts_node_child_by_field_name fn_ts_node_child_by_field_name
231247
#define ts_node_child_count fn_ts_node_child_count
@@ -711,6 +727,22 @@ treesit_load_language_push_for_each_suffix (Lisp_Object lib_base_name,
711727
}
712728
}
713729

730+
/* This function is a compatibility shim. Tree-sitter 0.25 introduced
731+
ts_language_abi_version as a replacement for ts_language_version, and
732+
tree-sitter 0.26 removed ts_language_version. Here we use the fact
733+
that 0.25 bumped TREE_SITTER_LANGUAGE_VERSION to 15, to use the new
734+
function instead of the old one, when Emacs is compiled against
735+
tree-sitter version 0.25 or newer. */
736+
static uint32_t
737+
treesit_language_abi_version (const TSLanguage *ts_lang)
738+
{
739+
#if TREE_SITTER_LANGUAGE_VERSION >= 15
740+
return ts_language_abi_version (ts_lang);
741+
#else
742+
return ts_language_version (ts_lang);
743+
#endif
744+
}
745+
714746
/* Load the dynamic library of LANGUAGE_SYMBOL and return the pointer
715747
to the language definition.
716748
@@ -832,7 +864,7 @@ treesit_load_language (Lisp_Object language_symbol,
832864
build_string ("%s's ABI version is %d, but supported versions are %d-%d");
833865
Lisp_Object formatted_msg =
834866
CALLN (Fformat_message, fmt, loaded_lib,
835-
make_fixnum (ts_language_version (lang)),
867+
make_fixnum (treesit_language_abi_version (lang)),
836868
make_fixnum (TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION),
837869
make_fixnum (TREE_SITTER_LANGUAGE_VERSION));
838870
*signal_symbol = Qtreesit_load_language_error;
@@ -914,7 +946,7 @@ Return nil if a grammar library for LANGUAGE is not available. */)
914946
TSLanguage *ts_language = lang.lang;
915947
if (ts_language == NULL)
916948
return Qnil;
917-
uint32_t version = ts_language_version (ts_language);
949+
uint32_t version = treesit_language_abi_version (ts_language);
918950
return make_fixnum((ptrdiff_t) version);
919951
}
920952
}

0 commit comments

Comments
 (0)