MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / ts_walk_nested

Function ts_walk_nested

internal/cbm/doclink_ts.c:786–839  ·  view source on GitHub ↗

V records: every function or class declared inside a function body, with * its line and the body's lines (the scope it is visible in). The graph gives * such a declaration a file-level qualified name. One cursor walk over the * tree; the stack holds the function bodies around the current node. */

Source from the content-addressed store, hash-verified

784 * such a declaration a file-level qualified name. One cursor walk over the
785 * tree; the stack holds the function bodies around the current node. */
786static void ts_walk_nested(ts_walk_t *w, TSNode root) {
787 int cap = CBM_SZ_64;
788 int ns = 0;
789 ts_scope_t *scopes = (ts_scope_t *)cbm_arena_alloc(w->sb->a, (size_t)cap * sizeof(ts_scope_t));
790 if (!scopes) {
791 w->sb->oom = true;
792 return;
793 }
794 TSTreeCursor cur = ts_tree_cursor_new(root);
795 int depth = 0;
796 for (bool more = true; more && !w->sb->oom;) {
797 TSNode n = ts_tree_cursor_current_node(&cur);
798 while (ns > 0 && scopes[ns - SKIP_ONE].depth >= depth) {
799 ns--; /* left that function */
800 }
801 if (ts_node_is_named(n)) {
802 if (ns > 0 &&
803 (ts_kind(n, "function_declaration") ||
804 ts_kind(n, "generator_function_declaration") || ts_kind(n, "class_declaration"))) {
805 ts_put_nested(w, n, &scopes[ns - SKIP_ONE]);
806 }
807 TSNode body = ts_is_function_like(n) ? ts_node_child_by_field_name(n, TS_FIELD("body"))
808 : (TSNode){0};
809 if (!ts_node_is_null(body)) {
810 if (ns == cap) {
811 int ncap = cap * PAIR_LEN;
812 ts_scope_t *g =
813 (ts_scope_t *)cbm_arena_alloc(w->sb->a, (size_t)ncap * sizeof(ts_scope_t));
814 if (!g) {
815 w->sb->oom = true;
816 break;
817 }
818 memcpy(g, scopes, (size_t)ns * sizeof(ts_scope_t));
819 scopes = g;
820 cap = ncap;
821 }
822 scopes[ns++] = (ts_scope_t){ts_node_start_point(body).row + SKIP_ONE,
823 ts_node_end_point(body).row + SKIP_ONE, depth};
824 }
825 }
826 if (ts_tree_cursor_goto_first_child(&cur)) {
827 depth++;
828 continue;
829 }
830 while (!ts_tree_cursor_goto_next_sibling(&cur)) {
831 if (!ts_tree_cursor_goto_parent(&cur)) {
832 more = false;
833 break;
834 }
835 depth--;
836 }
837 }
838 ts_tree_cursor_delete(&cur);
839}
840
841const char *cbm_doclink_ts_scan_scope(CBMExtractCtx *ctx) {
842 if (!ctx || ts_node_is_null(ctx->root)) {

Callers 1

Calls 4

ts_kindFunction · 0.85
ts_put_nestedFunction · 0.85
ts_is_function_likeFunction · 0.85
cbm_arena_allocFunction · 0.70

Tested by

no test coverage detected