|
1 | | -import type { TreeSitterNode } from '../types.js'; |
| 1 | +import type { SubDeclaration, TreeSitterNode } from '../types.js'; |
2 | 2 |
|
3 | 3 | /** |
4 | 4 | * Maximum recursion depth for tree-sitter AST walkers. |
@@ -70,6 +70,82 @@ export function rustVisibility(node: TreeSitterNode): 'public' | 'private' { |
70 | 70 | return 'private'; |
71 | 71 | } |
72 | 72 |
|
| 73 | +// ── Parser abstraction helpers ───────────────────────────────────────────── |
| 74 | + |
| 75 | +/** |
| 76 | + * Walk up the parent chain to find an enclosing node whose type is in `typeNames`. |
| 77 | + * Returns the text of `nameField` (default `'name'`) on the matching ancestor, or null. |
| 78 | + * |
| 79 | + * Replaces per-language `findParentClass` / `findParentType` / `findCurrentImpl` helpers. |
| 80 | + */ |
| 81 | +export function findParentNode( |
| 82 | + node: TreeSitterNode, |
| 83 | + typeNames: readonly string[], |
| 84 | + nameField: string = 'name', |
| 85 | +): string | null { |
| 86 | + let current = node.parent; |
| 87 | + while (current) { |
| 88 | + if (typeNames.includes(current.type)) { |
| 89 | + const nameNode = current.childForFieldName(nameField); |
| 90 | + return nameNode ? nameNode.text : null; |
| 91 | + } |
| 92 | + current = current.parent; |
| 93 | + } |
| 94 | + return null; |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Extract child declarations from a container node's body. |
| 99 | + * Finds the body via `bodyFields` (tries childForFieldName then findChild for each), |
| 100 | + * iterates its children, filters by `memberType`, extracts `nameField`, and returns SubDeclarations. |
| 101 | + * |
| 102 | + * Replaces per-language extractStructFields / extractEnumVariants / extractEnumConstants helpers |
| 103 | + * for the common case where each member has a direct name field. |
| 104 | + */ |
| 105 | +export function extractBodyMembers( |
| 106 | + containerNode: TreeSitterNode, |
| 107 | + bodyFields: readonly string[], |
| 108 | + memberType: string, |
| 109 | + kind: SubDeclaration['kind'], |
| 110 | + nameField: string = 'name', |
| 111 | + visibility?: (member: TreeSitterNode) => SubDeclaration['visibility'], |
| 112 | +): SubDeclaration[] { |
| 113 | + const members: SubDeclaration[] = []; |
| 114 | + let body: TreeSitterNode | null = null; |
| 115 | + for (const field of bodyFields) { |
| 116 | + body = containerNode.childForFieldName(field) || findChild(containerNode, field); |
| 117 | + if (body) break; |
| 118 | + } |
| 119 | + if (!body) return members; |
| 120 | + for (let i = 0; i < body.childCount; i++) { |
| 121 | + const member = body.child(i); |
| 122 | + if (!member || member.type !== memberType) continue; |
| 123 | + const nn = member.childForFieldName(nameField); |
| 124 | + if (nn) { |
| 125 | + const entry: SubDeclaration = { name: nn.text, kind, line: member.startPosition.row + 1 }; |
| 126 | + if (visibility) entry.visibility = visibility(member); |
| 127 | + members.push(entry); |
| 128 | + } |
| 129 | + } |
| 130 | + return members; |
| 131 | +} |
| 132 | + |
| 133 | +/** |
| 134 | + * Strip leading/trailing quotes (single, double, or backtick) from a string. |
| 135 | + * Strips only the leading/trailing delimiter; interior quotes are untouched. |
| 136 | + */ |
| 137 | +export function stripQuotes(text: string): string { |
| 138 | + return text.replace(/^['"`]|['"`]$/g, ''); |
| 139 | +} |
| 140 | + |
| 141 | +/** |
| 142 | + * Extract the last segment of a delimited path. |
| 143 | + * e.g. `lastPathSegment('java.util.List', '.')` → `'List'` |
| 144 | + */ |
| 145 | +export function lastPathSegment(path: string, separator: string = '/'): string { |
| 146 | + return path.split(separator).pop() ?? path; |
| 147 | +} |
| 148 | + |
73 | 149 | export function extractModifierVisibility( |
74 | 150 | node: TreeSitterNode, |
75 | 151 | modifierTypes: Set<string> = DEFAULT_MODIFIER_TYPES, |
|
0 commit comments