From ba06661a0e46271721aaa1f13a14b6a2bd90712f Mon Sep 17 00:00:00 2001 From: Corwin Marsh Date: Sun, 20 Jul 2025 14:58:06 -0700 Subject: [PATCH] feat: Add setting to triger Workspace Edits when moving files (import updates) --- package.json | 5 +++++ src/handlers/onDidSaveTextDocument.ts | 29 +++++++++++++++++++++++++-- src/utils/settings.ts | 5 +++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 63fb038..cd3a6b0 100644 --- a/package.json +++ b/package.json @@ -154,6 +154,11 @@ "type": "boolean", "default": false, "description": "Set this to true if you have nerd font installed. Default is false. Set this to true if you want to use nerd font icons." + }, + "oil-code.enableWorkspaceEdit": { + "type": "boolean", + "default": false, + "description": "Enable workspace edit for file move/rename operations. When enabled, VS Code will ask to update references when a file is moved or renamed. Default is false." } } } diff --git a/src/handlers/onDidSaveTextDocument.ts b/src/handlers/onDidSaveTextDocument.ts index c88eafb..104d792 100644 --- a/src/handlers/onDidSaveTextDocument.ts +++ b/src/handlers/onDidSaveTextDocument.ts @@ -12,6 +12,7 @@ import { import { select } from "../commands/select"; import { newline } from "../newline"; import { logger } from "../logger"; +import { getEnableWorkspaceEditSetting } from "../utils/settings"; export async function onDidSaveTextDocument(document: vscode.TextDocument) { // Check if the saved document is our oil file @@ -165,6 +166,10 @@ export async function onDidSaveTextDocument(document: vscode.TextDocument) { return; } logger.debug("Processing changes..."); + // Get the workspace edit setting + const useWorkspaceEdit = getEnableWorkspaceEditSetting(); + + const moveEdits = new vscode.WorkspaceEdit(); // Process the changes // Move files for (const [oldPath, newPath] of movedLines) { @@ -174,8 +179,21 @@ export async function onDidSaveTextDocument(document: vscode.TextDocument) { if (!fs.existsSync(dirPath)) { fs.mkdirSync(dirPath, { recursive: true }); } - // Move the file to the new location - fs.renameSync(oldPath, newPath); + + if (useWorkspaceEdit) { + // Use workspace edit for better VS Code integration + // Removing tailing slashes triggers lsp update references correctly + moveEdits.renameFile( + vscode.Uri.file(oldPath.replace(/\/$/, "")), + vscode.Uri.file(newPath.replace(/\/$/, "")), + { + overwrite: false, + } + ); + } else { + // Use file system rename directly + fs.renameSync(oldPath, newPath); + } } catch (error) { vscode.window.showErrorMessage( `Failed to move file: ${formatPath(oldPath)} to ${newPath.replace( @@ -185,6 +203,13 @@ export async function onDidSaveTextDocument(document: vscode.TextDocument) { ); } } + try { + await vscode.workspace.applyEdit(moveEdits); + } catch (error) { + vscode.window.showErrorMessage( + `Failed to apply workspace edit: ${error}` + ); + } // Copy files for (const [oldPath, newPath] of copiedLines) { try { diff --git a/src/utils/settings.ts b/src/utils/settings.ts index d39a918..3ff3c96 100644 --- a/src/utils/settings.ts +++ b/src/utils/settings.ts @@ -10,6 +10,11 @@ export function getDisableOpenCwdNothingOpenSetting(): boolean { return config.get("disableOpenCwdNothingOpen") || false; } +export function getEnableWorkspaceEditSetting(): boolean { + const config = vscode.workspace.getConfiguration("oil-code"); + return config.get("enableWorkspaceEdit") || false; +} + let restoreAutoSave = false; export async function checkAndDisableAutoSave() {