Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
}
}
Expand Down
29 changes: 27 additions & 2 deletions src/handlers/onDidSaveTextDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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(
Expand All @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions src/utils/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export function getDisableOpenCwdNothingOpenSetting(): boolean {
return config.get<boolean>("disableOpenCwdNothingOpen") || false;
}

export function getEnableWorkspaceEditSetting(): boolean {
const config = vscode.workspace.getConfiguration("oil-code");
return config.get<boolean>("enableWorkspaceEdit") || false;
}

let restoreAutoSave = false;

export async function checkAndDisableAutoSave() {
Expand Down
Loading