From 22a5cfbe102056ff3d51b2b44a8666fd8f85fdc0 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 13 Sep 2021 12:39:48 -0700 Subject: [PATCH] Assign ids to query matches only when the matches are returned Refs #1372 --- lib/src/query.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/src/query.c b/lib/src/query.c index 1e6ba848..86a9dfea 100644 --- a/lib/src/query.c +++ b/lib/src/query.c @@ -2561,6 +2561,7 @@ static void ts_query_cursor__add_state( pattern->step_index ); array_insert(&self->states, index, ((QueryState) { + .id = UINT32_MAX, .capture_list_id = NONE, .step_index = pattern->step_index, .pattern_index = pattern->pattern_index, @@ -2725,7 +2726,6 @@ static inline bool ts_query_cursor__advance( if (step->depth == PATTERN_DONE_MARKER) { if (state->start_depth > self->depth || self->halted) { LOG(" finish pattern %u\n", state->pattern_index); - state->id = self->next_state_id++; array_push(&self->finished_states, *state); did_match = true; deleted_count++; @@ -3114,7 +3114,6 @@ static inline bool ts_query_cursor__advance( LOG(" defer finishing pattern %u\n", state->pattern_index); } else { LOG(" finish pattern %u\n", state->pattern_index); - state->id = self->next_state_id++; array_push(&self->finished_states, *state); array_erase(&self->states, state - self->states.contents); did_match = true; @@ -3169,6 +3168,7 @@ bool ts_query_cursor_next_match( } QueryState *state = &self->finished_states.contents[0]; + if (state->id == UINT32_MAX) state->id = self->next_state_id++; match->id = state->id; match->pattern_index = state->pattern_index; const CaptureList *captures = capture_list_pool_get( @@ -3278,6 +3278,7 @@ bool ts_query_cursor_next_capture( } if (state) { + if (state->id == UINT32_MAX) state->id = self->next_state_id++; match->id = state->id; match->pattern_index = state->pattern_index; const CaptureList *captures = capture_list_pool_get(