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

Function parse_csharp_imports

internal/cbm/extract_imports.c:1849–1892  ·  view source on GitHub ↗

--- C# imports --- tree-sitter-c-sharp models `using System.Text;` as a `using_directive`. The namespace path is a `qualified_name`/`identifier` child. For an alias form `using F = System.IO.File;` the directive has a `name` field holding the alias identifier `F` and a separate qualified_name on the right of `=` — the generic path-field extractor wrongly returns the alias `F`, so we instead take

Source from the content-addressed store, hash-verified

1847// take the LAST qualified_name/identifier (the real namespace/type), and use
1848// the `name` field as local_name when present.
1849static void parse_csharp_imports(CBMExtractCtx *ctx) {
1850 CBMArena *a = ctx->arena;
1851 TSTreeCursor cursor = ts_tree_cursor_new(ctx->root);
1852 if (!ts_tree_cursor_goto_first_child(&cursor)) {
1853 ts_tree_cursor_delete(&cursor);
1854 return;
1855 }
1856 do {
1857 TSNode node = ts_tree_cursor_current_node(&cursor);
1858 if (strcmp(ts_node_type(node), "using_directive") != 0) {
1859 continue;
1860 }
1861 // Find the right-most qualified_name / identifier (the namespace/type),
1862 // which is the import target even in the alias form `using F = X;`.
1863 TSNode path_node = node;
1864 bool found = false;
1865 uint32_t nc = ts_node_named_child_count(node);
1866 for (int i = (int)nc - 1; i >= 0; i--) {
1867 TSNode c = ts_node_named_child(node, (uint32_t)i);
1868 const char *ck = ts_node_type(c);
1869 if (strcmp(ck, "qualified_name") == 0 || strcmp(ck, "identifier") == 0 ||
1870 strcmp(ck, "member_access_expression") == 0 || strcmp(ck, "name") == 0) {
1871 path_node = c;
1872 found = true;
1873 break;
1874 }
1875 }
1876 char *path = found ? cbm_node_text(a, path_node, ctx->source) : NULL;
1877 if (!path || !path[0]) {
1878 // Fallback to text stripping (handles `using static X;`).
1879 if (!try_generic_path_fields(ctx, node)) {
1880 generic_import_from_text(ctx, node);
1881 }
1882 continue;
1883 }
1884 // Alias name, if any.
1885 TSNode alias = ts_node_child_by_field_name(node, TS_FIELD("alias"));
1886 const char *local =
1887 !ts_node_is_null(alias) ? cbm_node_text(a, alias, ctx->source) : path_last(a, path);
1888 CBMImport imp = {.local_name = local, .module_path = path};
1889 cbm_imports_push(&ctx->result->imports, a, imp);
1890 } while (ts_tree_cursor_goto_next_sibling(&cursor));
1891 ts_tree_cursor_delete(&cursor);
1892}
1893
1894// --- Generic spec-driven imports ---
1895// For grammar-only languages that have no dedicated parser above, consume the

Callers 1

cbm_extract_importsFunction · 0.85

Calls 5

cbm_node_textFunction · 0.85
try_generic_path_fieldsFunction · 0.85
generic_import_from_textFunction · 0.85
path_lastFunction · 0.85
cbm_imports_pushFunction · 0.85

Tested by

no test coverage detected