Skip to content

Commit 51dd734

Browse files
committed
fix: use JSON parsing for ripgrep results in workspace removal
The processSearchResults function was still using legacy vimgrep format parsing while the actual ripgrep command uses --json output. This caused the "Remove from Workspace" feature to fail completely. - Replace vimgrep parsing with parseRipgrepResults from core/tlog-search - Update test cases to use JSON format matching ripgrep --json output - Remove unused RIPGREP_LINE_INDEX_OFFSET constant
1 parent 6965bb6 commit 51dd734

2 files changed

Lines changed: 28 additions & 31 deletions

File tree

src/src/remover.test.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import {
66

77
describe("Remover Functions", () => {
88
describe("processSearchResults", () => {
9-
test("processes ripgrep results correctly", () => {
9+
test("processes ripgrep JSON results correctly", () => {
1010
const searchResults = [
11-
'/path/to/file1.ts:10:5:console.log("[TLOG] test1");',
12-
'/path/to/file2.ts:20:3:console.log("[TLOG] test2");',
13-
'/path/to/file1.ts:30:8:console.log("[TLOG] test3");',
11+
'{"type":"match","data":{"path":{"text":"/path/to/file1.ts"},"lines":{"text":"console.log(\\"[TLOG] test1\\");"},"line_number":10,"submatches":[{"start":5}]}}',
12+
'{"type":"match","data":{"path":{"text":"/path/to/file2.ts"},"lines":{"text":"console.log(\\"[TLOG] test2\\");"},"line_number":20,"submatches":[{"start":3}]}}',
13+
'{"type":"match","data":{"path":{"text":"/path/to/file1.ts"},"lines":{"text":"console.log(\\"[TLOG] test3\\");"},"line_number":30,"submatches":[{"start":8}]}}',
1414
];
1515

1616
const result = processSearchResults(searchResults);
@@ -30,12 +30,12 @@ describe("Remover Functions", () => {
3030
});
3131
});
3232

33-
test("ignores malformed lines", () => {
33+
test("ignores malformed JSON lines", () => {
3434
const searchResults = [
35-
'/path/to/file1.ts:10:5:console.log("[TLOG] test");',
36-
"malformed-line-without-colon",
37-
'/path/to/file2.ts:not-a-number:3:console.log("[TLOG] test");',
38-
'/path/to/file3.ts:15:2:console.log("[TLOG] test");',
35+
'{"type":"match","data":{"path":{"text":"/path/to/file1.ts"},"lines":{"text":"console.log(\\"[TLOG] test\\");"},"line_number":10,"submatches":[{"start":5}]}}',
36+
"malformed-json-line",
37+
'{"type":"begin"}',
38+
'{"type":"match","data":{"path":{"text":"/path/to/file3.ts"},"lines":{"text":"console.log(\\"[TLOG] test\\");"},"line_number":15,"submatches":[{"start":2}]}}',
3939
];
4040

4141
const result = processSearchResults(searchResults);
@@ -45,14 +45,17 @@ describe("Remover Functions", () => {
4545
expect(result[1].filePath).toBe("/path/to/file3.ts");
4646
});
4747

48-
test("ignores lines with extra colons in path", () => {
48+
test("ignores non-match JSON types", () => {
4949
const searchResults = [
50-
'/path/with:colons/file.ts:10:5:console.log("[TLOG] test");',
50+
'{"type":"begin"}',
51+
'{"type":"match","data":{"path":{"text":"/path/to/file1.ts"},"lines":{"text":"console.log(\\"[TLOG] test\\");"},"line_number":10,"submatches":[{"start":5}]}}',
52+
'{"type":"end"}',
5153
];
5254

5355
const result = processSearchResults(searchResults);
5456

55-
expect(result).toHaveLength(0);
57+
expect(result).toHaveLength(1);
58+
expect(result[0].filePath).toBe("/path/to/file1.ts");
5659
});
5760
});
5861

src/src/remover.ts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
CONFIRMATION_YES,
77
CONFIRMATION_NO,
88
} from "./core/tlog-patterns";
9-
const RIPGREP_LINE_INDEX_OFFSET = 1;
109

1110
type RemovalScope = "current" | "workspace";
1211

@@ -189,23 +188,13 @@ const deleteLinesFromSearchResults = async (
189188
export const processSearchResults = (
190189
searchResults: string[]
191190
): Array<{ filePath: string; lineNumber: number }> => {
192-
const processedResults: Array<{ filePath: string; lineNumber: number }> = [];
193-
194-
searchResults.forEach((line) => {
195-
const parts = line.split(":");
196-
if (parts.length < 2) return;
197-
198-
const filePath = parts[0];
199-
const lineNumberStr = parts[1];
200-
const lineNumber = parseInt(lineNumberStr, 10);
201-
202-
if (isNaN(lineNumber)) return;
203-
204-
const zeroBasedLineNumber = lineNumber - RIPGREP_LINE_INDEX_OFFSET;
205-
processedResults.push({ filePath, lineNumber: zeroBasedLineNumber });
206-
});
207-
208-
return processedResults;
191+
const stdout = searchResults.join('\n');
192+
const parsedResults = parseRipgrepResults(stdout);
193+
194+
return parsedResults.map(result => ({
195+
filePath: result.filePath,
196+
lineNumber: result.line
197+
}));
209198
};
210199

211200
export const createFileLineMap = (
@@ -228,7 +217,12 @@ export const createFileLineMap = (
228217
};
229218

230219
const parseSearchResults = (results: string[]): Map<string, number[]> => {
231-
const processedResults = processSearchResults(results);
220+
const stdout = results.join('\n');
221+
const parsedResults = parseRipgrepResults(stdout);
222+
const processedResults = parsedResults.map(result => ({
223+
filePath: result.filePath,
224+
lineNumber: result.line
225+
}));
232226
return createFileLineMap(processedResults);
233227
};
234228

0 commit comments

Comments
 (0)