Skip to content

Commit 8c2cef9

Browse files
Victorique Koroblourens
authored andcommitted
Search results for "UTF-8 with BOM" files shifted on first line by a character (microsoft#66189)
* Fix microsoft#66188 * Strip utf-8 bom from matchText * Index can't be negative
1 parent d4beb23 commit 8c2cef9

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

src/vs/workbench/services/search/node/ripgrepTextSearchEngine.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,12 @@ export class RipgrepParser extends EventEmitter {
237237

238238
private createTextSearchMatch(data: IRgMatch, uri: vscode.Uri): vscode.TextSearchMatch {
239239
const lineNumber = data.line_number - 1;
240-
const fullText = bytesOrTextToString(data.lines);
240+
let isBOMStripped = false;
241+
let fullText = bytesOrTextToString(data.lines);
242+
if (lineNumber === 0 && startsWithUTF8BOM(fullText)) {
243+
isBOMStripped = true;
244+
fullText = stripUTF8BOM(fullText);
245+
}
241246
const fullTextBytes = Buffer.from(fullText);
242247

243248
let prevMatchEnd = 0;
@@ -255,6 +260,11 @@ export class RipgrepParser extends EventEmitter {
255260
}
256261

257262
let matchText = bytesOrTextToString(match.match);
263+
if (lineNumber === 0 && i === 0 && isBOMStripped) {
264+
matchText = stripUTF8BOM(matchText);
265+
match.start = match.start <= 3 ? 0 : match.start - 3;
266+
match.end = match.end <= 3 ? 0 : match.end - 3;
267+
}
258268
const inBetweenChars = fullTextBytes.slice(prevMatchEnd, match.start).toString().length;
259269
let startCol = prevMatchEndCol + inBetweenChars;
260270

@@ -265,12 +275,6 @@ export class RipgrepParser extends EventEmitter {
265275
stats.lastLineLength :
266276
stats.lastLineLength + startCol;
267277

268-
if (lineNumber === 0 && i === 0 && startsWithUTF8BOM(matchText)) {
269-
matchText = stripUTF8BOM(matchText);
270-
startCol -= 3;
271-
endCol -= 3;
272-
}
273-
274278
prevMatchEnd = match.end;
275279
prevMatchEndCol = endCol;
276280
prevMatchEndLine = endLineNumber;

0 commit comments

Comments
 (0)