Skip to content

Commit afcb022

Browse files
committed
Add option for aggressive line number stripping
1 parent 5fa555e commit afcb022

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

src/integrations/misc/__tests__/extract-text.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,46 @@ describe("stripLineNumbers", () => {
137137
const expected = "line one\nline two\nline three"
138138
expect(stripLineNumbers(input)).toBe(expected)
139139
})
140+
141+
describe("aggressive mode", () => {
142+
it("should strip content with just a pipe character", () => {
143+
const input = "| line one\n| line two\n| line three"
144+
const expected = "line one\nline two\nline three"
145+
expect(stripLineNumbers(input, true)).toBe(expected)
146+
})
147+
148+
it("should strip content with mixed formats in aggressive mode", () => {
149+
const input = "1 | line one\n| line two\n123 | line three"
150+
const expected = "line one\nline two\nline three"
151+
expect(stripLineNumbers(input, true)).toBe(expected)
152+
})
153+
154+
it("should not strip content with pipe characters not at start in aggressive mode", () => {
155+
const input = "text | more text\nx | y"
156+
expect(stripLineNumbers(input, true)).toBe(input)
157+
})
158+
159+
it("should handle empty content in aggressive mode", () => {
160+
expect(stripLineNumbers("", true)).toBe("")
161+
})
162+
163+
it("should preserve padding after pipe in aggressive mode", () => {
164+
const input = "| line with extra spaces\n1 | indented content"
165+
const expected = " line with extra spaces\n indented content"
166+
expect(stripLineNumbers(input, true)).toBe(expected)
167+
})
168+
169+
it("should preserve windows-style line endings in aggressive mode", () => {
170+
const input = "| line one\r\n| line two\r\n| line three"
171+
const expected = "line one\r\nline two\r\nline three"
172+
expect(stripLineNumbers(input, true)).toBe(expected)
173+
})
174+
175+
it("should not affect regular content when using aggressive mode", () => {
176+
const input = "regular line\nanother line\nno pipes here"
177+
expect(stripLineNumbers(input, true)).toBe(input)
178+
})
179+
})
140180
})
141181

142182
describe("truncateOutput", () => {

src/integrations/misc/extract-text.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,23 @@ export function everyLineHasLineNumbers(content: string): boolean {
8686
return lines.length > 0 && lines.every((line) => /^\s*\d+\s+\|(?!\|)/.test(line))
8787
}
8888

89-
// Strips line numbers from content while preserving the actual content
90-
// Handles formats like "1 | content", " 12 | content", "123 | content"
91-
// Preserves content that naturally starts with pipe characters
92-
export function stripLineNumbers(content: string): string {
89+
/**
90+
* Strips line numbers from content while preserving the actual content.
91+
*
92+
* @param content The content to process
93+
* @param aggressive When false (default): Only strips lines with clear number patterns like "123 | content"
94+
* When true: Uses a more lenient pattern that also matches lines with just a pipe character,
95+
* which can be useful when LLMs don't perfectly format the line numbers in diffs
96+
* @returns The content with line numbers removed
97+
*/
98+
export function stripLineNumbers(content: string, aggressive: boolean = false): string {
9399
// Split into lines to handle each line individually
94100
const lines = content.split(/\r?\n/)
95101

96102
// Process each line
97103
const processedLines = lines.map((line) => {
98104
// Match line number pattern and capture everything after the pipe
99-
const match = line.match(/^\s*\d+\s+\|(?!\|)\s?(.*)$/)
105+
const match = aggressive ? line.match(/^\s*(?:\d+\s)?\|\s(.*)$/) : line.match(/^\s*\d+\s+\|(?!\|)\s?(.*)$/)
100106
return match ? match[1] : line
101107
})
102108

0 commit comments

Comments
 (0)