MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / extractCodeBlocks

Method extractCodeBlocks

src/context/index.ts:1234–1295  ·  view source on GitHub ↗

* Extract code blocks for key nodes in the subgraph

(
    subgraph: Subgraph,
    maxBlocks: number,
    maxBlockSize: number
  )

Source from the content-addressed store, hash-verified

1232 * Extract code blocks for key nodes in the subgraph
1233 */
1234 private async extractCodeBlocks(
1235 subgraph: Subgraph,
1236 maxBlocks: number,
1237 maxBlockSize: number
1238 ): Promise<CodeBlock[]> {
1239 const blocks: CodeBlock[] = [];
1240
1241 // Prioritize entry points, then functions/methods
1242 const priorityNodes: Node[] = [];
1243
1244 // First: entry points
1245 for (const id of subgraph.roots) {
1246 const node = subgraph.nodes.get(id);
1247 if (node) {
1248 priorityNodes.push(node);
1249 }
1250 }
1251
1252 // Then: functions and methods
1253 for (const node of subgraph.nodes.values()) {
1254 if (!subgraph.roots.includes(node.id)) {
1255 if (node.kind === 'function' || node.kind === 'method') {
1256 priorityNodes.push(node);
1257 }
1258 }
1259 }
1260
1261 // Then: classes
1262 for (const node of subgraph.nodes.values()) {
1263 if (!subgraph.roots.includes(node.id)) {
1264 if (node.kind === 'class') {
1265 priorityNodes.push(node);
1266 }
1267 }
1268 }
1269
1270 // Extract code for priority nodes
1271 for (const node of priorityNodes) {
1272 if (blocks.length >= maxBlocks) break;
1273
1274 const code = await this.extractNodeCode(node);
1275 if (code) {
1276 // Truncate if too long. Language-neutral marker (no `//` — not a
1277 // comment in Python, Ruby, etc.); this renders inside a fenced
1278 // source block whose language varies.
1279 const truncated = code.length > maxBlockSize
1280 ? code.slice(0, maxBlockSize) + '\n... (truncated) ...'
1281 : code;
1282
1283 blocks.push({
1284 content: truncated,
1285 filePath: node.filePath,
1286 startLine: node.startLine,
1287 endLine: node.endLine,
1288 language: node.language,
1289 node,
1290 });
1291 }

Callers 1

buildContextMethod · 0.95

Calls 2

extractNodeCodeMethod · 0.95
getMethod · 0.65

Tested by

no test coverage detected