extractCodeSnippet function
Extracts up to a 3-line (or capped multi-line) snippet surrounding
1-based startLine..endLine.
Implementation
String extractCodeSnippet(List<String> rawLines, int startLine, int endLine) {
if (rawLines.isEmpty) return '';
final firstIdx = math.max(0, startLine - 2);
var lastIdx = math.min(rawLines.length - 1, endLine);
if (lastIdx - firstIdx + 1 < 3) {
if (firstIdx == 0) {
lastIdx = math.min(rawLines.length - 1, 2);
} else if (lastIdx == rawLines.length - 1) {
final adjustedFirst = math.max(0, rawLines.length - 3);
return rawLines
.sublist(adjustedFirst, lastIdx + 1)
.map((line) => line.replaceAll('\r', ''))
.join('\n');
}
}
final cappedLast = math.min(lastIdx, firstIdx + 14);
return rawLines
.sublist(firstIdx, cappedLast + 1)
.map((line) => line.replaceAll('\r', ''))
.join('\n');
}