MCPcopy Index your code
hub / github.com/promptfoo/promptfoo / clampToValidLine

Function clampToValidLine

src/codeScan/util/diffLineRanges.ts:181–224  ·  view source on GitHub ↗
(
  filepath: string,
  line: number,
  ranges: FileLineRanges,
)

Source from the content-addressed store, hash-verified

179 * @returns The clamped line number, or null if file not in diff
180 */
181export function clampToValidLine(
182 filepath: string,
183 line: number,
184 ranges: FileLineRanges,
185): number | null {
186 const fileRanges = ranges.get(filepath);
187 if (!fileRanges || fileRanges.length === 0) {
188 return null;
189 }
190
191 // If already valid, return as-is
192 if (isLineInDiff(filepath, line, ranges)) {
193 return line;
194 }
195
196 // Sort ranges by start line
197 const sortedRanges = [...fileRanges].sort((a, b) => a.start - b.start);
198
199 // If line is before all ranges, return start of first range
200 if (line < sortedRanges[0].start) {
201 return sortedRanges[0].start;
202 }
203
204 // If line is after all ranges, return end of last range
205 const lastRange = sortedRanges[sortedRanges.length - 1];
206 if (line > lastRange.end) {
207 return lastRange.end;
208 }
209
210 // Line is in a gap - find the closest range
211 for (let i = 0; i < sortedRanges.length - 1; i++) {
212 const currentRange = sortedRanges[i];
213 const nextRange = sortedRanges[i + 1];
214
215 // Check if line is in the gap between current and next range
216 if (line > currentRange.end && line < nextRange.start) {
217 // Return end of current range (comment on last visible line before gap)
218 return currentRange.end;
219 }
220 }
221
222 // Shouldn't reach here, but return end of last range as fallback
223 return lastRange.end;
224}
225
226/**
227 * Clamp a comment's line range to valid diff lines.

Callers 1

clampCommentLinesFunction · 0.85

Calls 2

isLineInDiffFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…