diff --git a/package.json b/package.json index 060491d..3b92806 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() {