diff --git a/src/vs/workbench/services/search/node/ripgrepTextSearchEngine.ts b/src/vs/workbench/services/search/node/ripgrepTextSearchEngine.ts index 81e9d7a417a0eb..f70c901e0720b3 100644 --- a/src/vs/workbench/services/search/node/ripgrepTextSearchEngine.ts +++ b/src/vs/workbench/services/search/node/ripgrepTextSearchEngine.ts @@ -237,7 +237,12 @@ export class RipgrepParser extends EventEmitter { private createTextSearchMatch(data: IRgMatch, uri: vscode.Uri): vscode.TextSearchMatch { const lineNumber = data.line_number - 1; - const fullText = bytesOrTextToString(data.lines); + let isBOMStripped = false; + let fullText = bytesOrTextToString(data.lines); + if (lineNumber === 0 && startsWithUTF8BOM(fullText)) { + isBOMStripped = true; + fullText = stripUTF8BOM(fullText); + } const fullTextBytes = Buffer.from(fullText); let prevMatchEnd = 0; @@ -255,6 +260,11 @@ export class RipgrepParser extends EventEmitter { } let matchText = bytesOrTextToString(match.match); + if (lineNumber === 0 && i === 0 && isBOMStripped) { + matchText = stripUTF8BOM(matchText); + match.start = match.start <= 3 ? 0 : match.start - 3; + match.end = match.end <= 3 ? 0 : match.end - 3; + } const inBetweenChars = fullTextBytes.slice(prevMatchEnd, match.start).toString().length; let startCol = prevMatchEndCol + inBetweenChars; @@ -265,12 +275,6 @@ export class RipgrepParser extends EventEmitter { stats.lastLineLength : stats.lastLineLength + startCol; - if (lineNumber === 0 && i === 0 && startsWithUTF8BOM(matchText)) { - matchText = stripUTF8BOM(matchText); - startCol -= 3; - endCol -= 3; - } - prevMatchEnd = match.end; prevMatchEndCol = endCol; prevMatchEndLine = endLineNumber;