export function formatGrepSearchResults(results: string): string { const keepLines: string[] = []; function countLeadingSpaces(line: string) { return line?.match(/^ */)?.[0].length ?? 0; } const processResult = (lines: string[]) => { // Skip results in which only the file path was kept const resultPath = lines[0]; const resultContent = lines.slice(1); if (resultContent.length === 0) { return; } // Add path keepLines.push(resultPath); // Find the minimum indentation of content lines let minIndent = Infinity; for (const line of resultContent) { const indent = countLeadingSpaces(line); if (indent < minIndent) { minIndent = indent; } } // Make all lines line up to 2-space indent const changeIndentBy = 2 - minIndent; if (changeIndentBy === 0) { keepLines.push(...resultContent); } else if (changeIndentBy < 0) { keepLines.push( ...resultContent.map((line) => line.substring(-changeIndentBy)), ); } else { keepLines.push( ...resultContent.map((line) => " ".repeat(changeIndentBy) + line), ); } }; let resultLines: string[] = []; for (const line of results.split("\n").filter((l) => !!l)) { if (line.startsWith("./") || line === "--") { processResult(resultLines); // process previous result resultLines = [line]; continue; } // Exclude leading zero- or single-char lines if (resultLines.length === 1 && line.trim().length <= 1) { continue; } resultLines.push(line); } processResult(resultLines); return keepLines.join("\n"); }