Skip to content

Commit 4cf7754

Browse files
committed
Strip BOM when applying diffs
1 parent 9fcf69c commit 4cf7754

File tree

5 files changed

+41
-9
lines changed

5 files changed

+41
-9
lines changed

jest.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ module.exports = {
3030
"^strip-ansi$": "<rootDir>/src/__mocks__/strip-ansi.js",
3131
"^default-shell$": "<rootDir>/src/__mocks__/default-shell.js",
3232
"^os-name$": "<rootDir>/src/__mocks__/os-name.js",
33+
"^strip-bom$": "<rootDir>/src/__mocks__/strip-bom.js",
3334
},
3435
transformIgnorePatterns: [
35-
"node_modules/(?!(@modelcontextprotocol|delay|p-wait-for|globby|serialize-error|strip-ansi|default-shell|os-name)/)",
36+
"node_modules/(?!(@modelcontextprotocol|delay|p-wait-for|globby|serialize-error|strip-ansi|default-shell|os-name|strip-bom)/)",
3637
],
3738
roots: ["<rootDir>/src", "<rootDir>/webview-ui/src"],
3839
modulePathIgnorePatterns: [".vscode-test"],

package-lock.json

Lines changed: 17 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@
265265
"@anthropic-ai/sdk": "^0.37.0",
266266
"@anthropic-ai/vertex-sdk": "^0.7.0",
267267
"@aws-sdk/client-bedrock-runtime": "^3.706.0",
268-
"@google/generative-ai": "^0.18.0",
269268
"@google-cloud/vertexai": "^1.9.3",
269+
"@google/generative-ai": "^0.18.0",
270270
"@mistralai/mistralai": "^1.3.6",
271271
"@modelcontextprotocol/sdk": "^1.0.1",
272272
"@types/clone-deep": "^4.0.4",
@@ -304,6 +304,7 @@
304304
"sound-play": "^1.1.0",
305305
"string-similarity": "^4.0.4",
306306
"strip-ansi": "^7.1.0",
307+
"strip-bom": "^5.0.0",
307308
"tmp": "^0.2.3",
308309
"tree-sitter-wasms": "^0.1.11",
309310
"turndown": "^7.2.0",

src/__mocks__/strip-bom.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Mock implementation of strip-bom
2+
module.exports = function stripBom(string) {
3+
if (typeof string !== "string") {
4+
throw new TypeError("Expected a string")
5+
}
6+
7+
// Removes UTF-8 BOM
8+
if (string.charCodeAt(0) === 0xfeff) {
9+
return string.slice(1)
10+
}
11+
12+
return string
13+
}

src/integrations/editor/DiffViewProvider.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { formatResponse } from "../../core/prompts/responses"
77
import { DecorationController } from "./DecorationController"
88
import * as diff from "diff"
99
import { diagnosticsToProblemsString, getNewDiagnostics } from "../diagnostics"
10+
import stripBom from "strip-bom"
1011

1112
export const DIFF_VIEW_URI_SCHEME = "cline-diff"
1213

@@ -104,7 +105,7 @@ export class DiffViewProvider {
104105
const edit = new vscode.WorkspaceEdit()
105106
const rangeToReplace = new vscode.Range(0, 0, endLine + 1, 0)
106107
const contentToReplace = accumulatedLines.slice(0, endLine + 1).join("\n") + "\n"
107-
edit.replace(document.uri, rangeToReplace, contentToReplace)
108+
edit.replace(document.uri, rangeToReplace, stripBom(stripBom(contentToReplace)))
108109
await vscode.workspace.applyEdit(edit)
109110
// Update decorations
110111
this.activeLineController.setActiveLine(endLine)
@@ -128,7 +129,11 @@ export class DiffViewProvider {
128129
}
129130
// Apply the final content
130131
const finalEdit = new vscode.WorkspaceEdit()
131-
finalEdit.replace(document.uri, new vscode.Range(0, 0, document.lineCount, 0), accumulatedContent)
132+
finalEdit.replace(
133+
document.uri,
134+
new vscode.Range(0, 0, document.lineCount, 0),
135+
stripBom(stripBom(accumulatedContent)),
136+
)
132137
await vscode.workspace.applyEdit(finalEdit)
133138
// Clear all decorations at the end (after applying final edit)
134139
this.fadedOverlayController.clear()

0 commit comments

Comments
 (0)