MCPcopy
hub / github.com/continuedev/continue / constructFunctionDefinitionChunk

Function constructFunctionDefinitionChunk

core/indexing/chunk/code.ts:125–171  ·  view source on GitHub ↗
(
  node: SyntaxNode,
  code: string,
  maxChunkSize: number,
)

Source from the content-addressed store, hash-verified

123}
124
125async function constructFunctionDefinitionChunk(
126 node: SyntaxNode,
127 code: string,
128 maxChunkSize: number,
129): Promise<string> {
130 const bodyNode = node.children[node.children.length - 1];
131 const collapsedBody = collapsedReplacement(bodyNode);
132 const signature = code.slice(node.startIndex, bodyNode.startIndex);
133 const funcText = signature + collapsedBody;
134
135 const isInClass =
136 node.parent &&
137 ["block", "declaration_list"].includes(node.parent.type) &&
138 node.parent.parent &&
139 ["class_definition", "impl_item"].includes(node.parent.parent.type);
140
141 if (isInClass) {
142 const classNode = node.parent!.parent!;
143 const classBlock = node.parent!;
144 const classHeader = code.slice(classNode.startIndex, classBlock.startIndex);
145 const indent = " ".repeat(node.startPosition.column);
146 const combined = `${classHeader}...\n\n${indent}${funcText}`;
147
148 if ((await countTokensAsync(combined)) <= maxChunkSize) {
149 return combined;
150 }
151 if ((await countTokensAsync(funcText)) <= maxChunkSize) {
152 return funcText;
153 }
154 const firstLine = signature.split("\n")[0] ?? "";
155 const minimal = `${firstLine} ${collapsedBody}`;
156 if ((await countTokensAsync(minimal)) <= maxChunkSize) {
157 return minimal;
158 }
159 return collapsedBody;
160 }
161
162 if ((await countTokensAsync(funcText)) <= maxChunkSize) {
163 return funcText;
164 }
165 const firstLine = signature.split("\n")[0] ?? "";
166 const minimal = `${firstLine} ${collapsedBody}`;
167 if ((await countTokensAsync(minimal)) <= maxChunkSize) {
168 return minimal;
169 }
170 return collapsedBody;
171}
172
173const collapsedNodeConstructors: {
174 [key: string]: (

Callers

nothing calls this directly

Calls 2

collapsedReplacementFunction · 0.85
countTokensAsyncFunction · 0.85

Tested by

no test coverage detected