Skip to content

Commit

Permalink
Stop storing query's source text in the query
Browse files Browse the repository at this point in the history
We can just pull it from the object table
  • Loading branch information
euclidianAce committed May 18, 2023
1 parent 55e4a60 commit 2fee195
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
25 changes: 19 additions & 6 deletions csrc/query.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,15 @@ void ltreesitter_push_query(
set_child(L, child_idx); // query

ltreesitter_SourceText *source = ltreesitter_source_text_push(L, src_len, src); // query, source text
if (!source) {
ALLOC_FAIL(L);
return;
}
lua_pushvalue(L, -2); // query, source text, query
set_child(L, -2); // query, source text
lua_pop(L, 1); // query

*lq = (ltreesitter_Query){
.source = source,
.query = q,
.lang = lang,
};
Expand All @@ -123,8 +126,8 @@ static void push_query_copy(lua_State *L, int query_idx) {
TSQueryError err_type;
TSQuery *q = ts_query_new(
orig->lang,
orig->source->text,
orig->source->length,
source_text->text,
source_text->length,
&err_offset,
&err_type);

Expand All @@ -141,7 +144,6 @@ static void push_query_copy(lua_State *L, int query_idx) {
*lq = (ltreesitter_Query){
.lang = orig->lang,
.query = q,
.source = source_text,
};
}

Expand Down Expand Up @@ -677,8 +679,19 @@ static int query_exec(lua_State *L) {
Gets the source that the query was initialized with
]]*/
static int query_source(lua_State *L) {
ltreesitter_Query *q = ltreesitter_check_query(L, 1);
lua_pushlstring(L, q->source->text, q->source->length);
ltreesitter_Query *q = ltreesitter_check_query(L, 1); // query
if (!q) {
int t = lua_type(L, 1);
luaL_error(L, "Expected an ltreesitter.Query, got %s", lua_typename(L, t));
return 0;
}
push_child(L, -1); // query, source
ltreesitter_SourceText const *source_text = ltreesitter_check_source_text(L, -1);
if (!source_text) {
luaL_error(L, "Internal error: Query child was not a SourceText");
return 0;
}
lua_pushlstring(L, source_text->text, source_text->length); // query, source, string
return 1;
}

Expand Down
2 changes: 0 additions & 2 deletions include/ltreesitter/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ struct ltreesitter_Query {
TSQuery *query;

const TSLanguage *lang;
// TODO: this doesn't need to be stored here
ltreesitter_SourceText const *source;
};
#define LTREESITTER_QUERY_METATABLE_NAME "ltreesitter.Query"

Expand Down

0 comments on commit 2fee195

Please sign in to comment.