MCPcopy Index your code
hub / github.com/PatrickSys/codebase-context / findIdentifierOccurrences

Function findIdentifierOccurrences

src/utils/tree-sitter.ts:431–518  ·  view source on GitHub ↗
(
  content: string,
  language: string,
  symbol: string
)

Source from the content-addressed store, hash-verified

429 * Returns null when Tree-sitter isn't available/supported, so callers can fall back safely.
430 */
431export async function findIdentifierOccurrences(
432 content: string,
433 language: string,
434 symbol: string
435): Promise<IdentifierOccurrence[] | null> {
436 const normalizedSymbol = symbol.trim();
437 if (!normalizedSymbol) {
438 return [];
439 }
440
441 if (!supportsTreeSitter(language) || !content.trim()) {
442 return null;
443 }
444
445 if (Buffer.byteLength(content, 'utf8') > MAX_TREE_SITTER_PARSE_BYTES) {
446 return null;
447 }
448
449 try {
450 const parser = await getParserForLanguage(language);
451 setParseTimeout(parser);
452
453 let tree: ReturnType<Parser['parse']>;
454 try {
455 tree = parser.parse(content);
456 } catch (error) {
457 evictParser(language, parser);
458 throw error;
459 }
460
461 if (!tree) {
462 evictParser(language, parser);
463 return null;
464 }
465
466 try {
467 const hasErrorValue = tree.rootNode.hasError as unknown;
468 const rootHasError =
469 typeof hasErrorValue === 'function'
470 ? Boolean((hasErrorValue as () => unknown)())
471 : Boolean(hasErrorValue);
472
473 if (rootHasError) {
474 return null;
475 }
476
477 const nodes = tree.rootNode.descendantsOfType([...IDENTIFIER_NODE_TYPES]);
478 const occurrences: IdentifierOccurrence[] = [];
479 const seen = new Set<string>();
480
481 for (const node of nodes) {
482 if (!node || !node.isNamed) continue;
483 if (node.text !== normalizedSymbol) continue;
484 if (isInsideNonCodeContext(node)) continue;
485
486 const occ: IdentifierOccurrence = {
487 line: node.startPosition.row + 1,
488 startIndex: node.startIndex,

Callers 1

findSymbolReferencesFunction · 0.85

Calls 8

supportsTreeSitterFunction · 0.85
getParserForLanguageFunction · 0.85
setParseTimeoutFunction · 0.85
evictParserFunction · 0.85
isInsideNonCodeContextFunction · 0.85
isTreeSitterDebugEnabledFunction · 0.85
hasMethod · 0.80
addMethod · 0.45

Tested by

no test coverage detected