mirror of
https://github.com/colbymchenry/codegraph.git
synced 2026-11-01 08:12:59 +08:00
Splits the monolithic tree-sitter.ts (3,358 lines) into modular files: - 14 language config files under src/extraction/languages/ - 3 standalone extractors (Liquid, Svelte, DFM) - Shared helpers and types modules to avoid circular imports Also fixes a bug where Java's extractImport hook incorrectly set handledRefs: true, preventing unresolved reference creation and degrading codegraph_explore results for Java codebases. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
84 lines
3.2 KiB
TypeScript
84 lines
3.2 KiB
TypeScript
import type { Node as SyntaxNode } from 'web-tree-sitter';
|
|
import { getNodeText } from '../tree-sitter-helpers';
|
|
import type { LanguageExtractor } from '../tree-sitter-types';
|
|
|
|
export const cExtractor: LanguageExtractor = {
|
|
functionTypes: ['function_definition'],
|
|
classTypes: [],
|
|
methodTypes: [],
|
|
interfaceTypes: [],
|
|
structTypes: ['struct_specifier'],
|
|
enumTypes: ['enum_specifier'],
|
|
typeAliasTypes: ['type_definition'], // typedef
|
|
importTypes: ['preproc_include'],
|
|
callTypes: ['call_expression'],
|
|
variableTypes: ['declaration'],
|
|
nameField: 'declarator',
|
|
bodyField: 'body',
|
|
paramsField: 'parameters',
|
|
extractImport: (node, source) => {
|
|
const importText = source.substring(node.startIndex, node.endIndex).trim();
|
|
// C includes: #include <stdio.h>, #include "myheader.h"
|
|
const systemLib = node.namedChildren.find((c: SyntaxNode) => c.type === 'system_lib_string');
|
|
if (systemLib) {
|
|
return { moduleName: getNodeText(systemLib, source).replace(/^<|>$/g, ''), signature: importText };
|
|
}
|
|
const stringLiteral = node.namedChildren.find((c: SyntaxNode) => c.type === 'string_literal');
|
|
if (stringLiteral) {
|
|
const stringContent = stringLiteral.namedChildren.find((c: SyntaxNode) => c.type === 'string_content');
|
|
if (stringContent) {
|
|
return { moduleName: getNodeText(stringContent, source), signature: importText };
|
|
}
|
|
}
|
|
return null;
|
|
},
|
|
};
|
|
|
|
export const cppExtractor: LanguageExtractor = {
|
|
functionTypes: ['function_definition'],
|
|
classTypes: ['class_specifier'],
|
|
methodTypes: ['function_definition'],
|
|
interfaceTypes: [],
|
|
structTypes: ['struct_specifier'],
|
|
enumTypes: ['enum_specifier'],
|
|
typeAliasTypes: ['type_definition', 'alias_declaration'], // typedef and using
|
|
importTypes: ['preproc_include'],
|
|
callTypes: ['call_expression'],
|
|
variableTypes: ['declaration'],
|
|
nameField: 'declarator',
|
|
bodyField: 'body',
|
|
paramsField: 'parameters',
|
|
getVisibility: (node) => {
|
|
// Check for access specifier in parent
|
|
const parent = node.parent;
|
|
if (parent) {
|
|
for (let i = 0; i < parent.childCount; i++) {
|
|
const child = parent.child(i);
|
|
if (child?.type === 'access_specifier') {
|
|
const text = child.text;
|
|
if (text.includes('public')) return 'public';
|
|
if (text.includes('private')) return 'private';
|
|
if (text.includes('protected')) return 'protected';
|
|
}
|
|
}
|
|
}
|
|
return undefined;
|
|
},
|
|
extractImport: (node, source) => {
|
|
const importText = source.substring(node.startIndex, node.endIndex).trim();
|
|
// C++ includes: #include <iostream>, #include "myheader.h"
|
|
const systemLib = node.namedChildren.find((c: SyntaxNode) => c.type === 'system_lib_string');
|
|
if (systemLib) {
|
|
return { moduleName: getNodeText(systemLib, source).replace(/^<|>$/g, ''), signature: importText };
|
|
}
|
|
const stringLiteral = node.namedChildren.find((c: SyntaxNode) => c.type === 'string_literal');
|
|
if (stringLiteral) {
|
|
const stringContent = stringLiteral.namedChildren.find((c: SyntaxNode) => c.type === 'string_content');
|
|
if (stringContent) {
|
|
return { moduleName: getNodeText(stringContent, source), signature: importText };
|
|
}
|
|
}
|
|
return null;
|
|
},
|
|
};
|