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

Function buildCodeBlock

ui/src/lib/symbol-model.ts:145–190  ·  view source on GitHub ↗
(
  startLine: number,
  lines: readonly string[],
  callLines: readonly number[]
)

Source from the content-addressed store, hash-verified

143 * @param callLines every line in the body that makes an outgoing edge
144 */
145export function buildCodeBlock(
146 startLine: number,
147 lines: readonly string[],
148 callLines: readonly number[]
149): CodeBlock {
150 const endLine = startLine + lines.length - 1;
151 const slice = (from: number, to: number): SourceWindow => ({
152 start: from,
153 lines: lines.slice(from - startLine, to - startLine + 1),
154 });
155
156 if (lines.length <= FULL_BODY_LINES) {
157 return {
158 windows: lines.length > 0 ? [slice(startLine, endLine)] : [],
159 gapsAfter: [],
160 tailGap: 0,
161 whole: true,
162 };
163 }
164
165 const headEnd = Math.min(endLine, startLine + HEAD_LINES - 1);
166 const ranges: Array<[number, number]> = [[startLine, headEnd]];
167 const sites = [...new Set(callLines)]
168 .filter((line) => line > headEnd && line <= endLine)
169 .sort((a, b) => a - b);
170 for (const line of sites) {
171 ranges.push([
172 Math.max(startLine, line - WINDOW_CONTEXT),
173 Math.min(endLine, line + WINDOW_CONTEXT),
174 ]);
175 }
176
177 const merged: Array<[number, number]> = [];
178 for (const range of ranges) {
179 const last = merged[merged.length - 1];
180 if (last && range[0] <= last[1] + WINDOW_MERGE_GAP) last[1] = Math.max(last[1], range[1]);
181 else merged.push([...range] as [number, number]);
182 }
183
184 const kept = merged.slice(0, MAX_WINDOWS);
185 const windows = kept.map(([from, to]) => slice(from, to));
186 const gapsAfter = kept.slice(0, -1).map((range, i) => (kept[i + 1] as [number, number])[0] - range[1] - 1);
187 const lastEnd = kept[kept.length - 1]?.[1] ?? endLine;
188
189 return { windows, gapsAfter, tailGap: Math.max(0, endLine - lastEnd), whole: false };
190}
191
192/* ------------------------------------------------------------------ refs -- */
193

Callers 1

Calls 3

sliceFunction · 0.85
pushMethod · 0.80
mapMethod · 0.80

Tested by

no test coverage detected