Skip to content

Commit fae1c7a

Browse files
committed
Refactor file content reading across commands to consistently use vscode.workspace.openTextDocument instead of direct fs.readFileSync calls
1 parent 455e22b commit fae1c7a

5 files changed

Lines changed: 23 additions & 10 deletions

File tree

‎packages/vscode/src/commands/apply-chat-response-command/apply-chat-response-command.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,10 @@ export const apply_chat_response_command = (params: {
178178
let current_content = ''
179179
try {
180180
if (fs.existsSync(sanitized_file_path)) {
181-
current_content = fs.readFileSync(sanitized_file_path, 'utf8')
181+
const document = await vscode.workspace.openTextDocument(
182+
sanitized_file_path
183+
)
184+
current_content = document.getText()
182185
}
183186
} catch (error) {
184187
continue

‎packages/vscode/src/commands/apply-chat-response-command/handlers/diff-handler.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ export const store_original_file_states = async (
119119

120120
if (fs.existsSync(safe_path)) {
121121
try {
122-
const content = fs.readFileSync(safe_path, 'utf8')
122+
const document = await vscode.workspace.openTextDocument(safe_path)
123+
const content = document.getText()
123124
original_states.push({
124125
file_path,
125126
content,
@@ -238,7 +239,8 @@ async function process_modified_files(
238239
}
239240

240241
if (fs.existsSync(safe_path)) {
241-
if (fs.readFileSync(safe_path, 'utf8').trim() == '') {
242+
const document = await vscode.workspace.openTextDocument(safe_path)
243+
if (document.getText().trim() == '') {
242244
try {
243245
const uri = vscode.Uri.file(safe_path)
244246
const text_editors = vscode.window.visibleTextEditors.filter(

‎packages/vscode/src/commands/apply-chat-response-command/handlers/restore-preview-handler.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ export const handle_restore_preview = async (
6565
let original_content = ''
6666
if (file_exists) {
6767
try {
68-
original_content = fs.readFileSync(safe_path, 'utf8')
68+
const document = await vscode.workspace.openTextDocument(safe_path)
69+
original_content = document.getText()
6970
} catch (e) {
7071
Logger.warn({
7172
function_name: 'handle_restore_review',

‎packages/vscode/src/commands/apply-chat-response-command/utils/preview/file-preparer.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as fs from 'fs'
2+
import * as vscode from 'vscode'
23
import * as crypto from 'crypto'
34
import * as path from 'path'
45
import * as os from 'os'
@@ -34,7 +35,10 @@ export const prepare_files_from_original_states = async (params: {
3435
let current_content = ''
3536
try {
3637
if (fs.existsSync(sanitized_file_path)) {
37-
current_content = fs.readFileSync(sanitized_file_path, 'utf8')
38+
const document = await vscode.workspace.openTextDocument(
39+
sanitized_file_path
40+
)
41+
current_content = document.getText()
3842
}
3943
} catch (error) {
4044
continue

‎packages/vscode/src/commands/apply-chat-response-command/utils/preview/workspace-listener.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ export const setup_workspace_listeners = (
130130
is_new = true
131131
} else {
132132
try {
133-
original_content = fs.readFileSync(doc.uri.fsPath, 'utf8')
133+
const contentBytes = await vscode.workspace.fs.readFile(doc.uri)
134+
original_content = Buffer.from(contentBytes).toString('utf8')
134135
} catch (e) {
135136
is_new = true // File on disk does not exist, but buffer does.
136137
}
@@ -280,7 +281,7 @@ export const setup_workspace_listeners = (
280281
}
281282
})
282283

283-
const file_created_listener = vscode.workspace.onDidCreateFiles((event) => {
284+
const file_created_listener = vscode.workspace.onDidCreateFiles(async (event) => {
284285
for (const uri of event.files) {
285286
if (uri.scheme !== 'file') continue
286287

@@ -295,7 +296,8 @@ export const setup_workspace_listeners = (
295296
// Read the content of the newly created file (may be empty)
296297
let new_content = ''
297298
try {
298-
new_content = fs.readFileSync(uri.fsPath, 'utf8')
299+
const doc = await vscode.workspace.openTextDocument(uri)
300+
new_content = doc.getText()
299301
} catch (e) {
300302
new_content = ''
301303
}
@@ -361,7 +363,7 @@ export const setup_workspace_listeners = (
361363
}
362364
})
363365

364-
const file_renamed_listener = vscode.workspace.onDidRenameFiles((event) => {
366+
const file_renamed_listener = vscode.workspace.onDidRenameFiles(async (event) => {
365367
for (const { oldUri, newUri } of event.files) {
366368
if (oldUri.scheme !== 'file' || newUri.scheme !== 'file') continue
367369

@@ -396,7 +398,8 @@ export const setup_workspace_listeners = (
396398
// Read new file content (post-rename)
397399
let new_content = ''
398400
try {
399-
new_content = fs.readFileSync(newUri.fsPath, 'utf8')
401+
const doc = await vscode.workspace.openTextDocument(newUri)
402+
new_content = doc.getText()
400403
} catch {
401404
new_content = ''
402405
}

0 commit comments

Comments
 (0)