MCPcopy
hub / github.com/colbymchenry/codegraph / extractCodeBlocks

Method extractCodeBlocks

src/context/index.ts:1205–1266  ·  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

1203 * Extract code blocks for key nodes in the subgraph
1204 */
1205 private async extractCodeBlocks(
1206 subgraph: Subgraph,
1207 maxBlocks: number,
1208 maxBlockSize: number
1209 ): Promise<CodeBlock[]> {
1210 const blocks: CodeBlock[] = [];
1211
1212 // Prioritize entry points, then functions/methods
1213 const priorityNodes: Node[] = [];
1214
1215 // First: entry points
1216 for (const id of subgraph.roots) {
1217 const node = subgraph.nodes.get(id);
1218 if (node) {
1219 priorityNodes.push(node);
1220 }
1221 }
1222
1223 // Then: functions and methods
1224 for (const node of subgraph.nodes.values()) {
1225 if (!subgraph.roots.includes(node.id)) {
1226 if (node.kind === 'function' || node.kind === 'method') {
1227 priorityNodes.push(node);
1228 }
1229 }
1230 }
1231
1232 // Then: classes
1233 for (const node of subgraph.nodes.values()) {
1234 if (!subgraph.roots.includes(node.id)) {
1235 if (node.kind === 'class') {
1236 priorityNodes.push(node);
1237 }
1238 }
1239 }
1240
1241 // Extract code for priority nodes
1242 for (const node of priorityNodes) {
1243 if (blocks.length >= maxBlocks) break;
1244
1245 const code = await this.extractNodeCode(node);
1246 if (code) {
1247 // Truncate if too long. Language-neutral marker (no `//` — not a
1248 // comment in Python, Ruby, etc.); this renders inside a fenced
1249 // source block whose language varies.
1250 const truncated = code.length > maxBlockSize
1251 ? code.slice(0, maxBlockSize) + '\n... (truncated) ...'
1252 : code;
1253
1254 blocks.push({
1255 content: truncated,
1256 filePath: node.filePath,
1257 startLine: node.startLine,
1258 endLine: node.endLine,
1259 language: node.language,
1260 node,
1261 });
1262 }

Callers 1

buildContextMethod · 0.95

Calls 2

extractNodeCodeMethod · 0.95
getMethod · 0.65

Tested by

no test coverage detected