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
47 changes: 47 additions & 0 deletions src/commands/cd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as vscode from "vscode";
import { getOilState, getCurrentPath } from "../state/oilState";
import { uriPathToDiskPath } from "../utils/pathUtils";

export async function cd() {
const oilState = getOilState();
if (!oilState) {
vscode.window.showErrorMessage("Failed to get oil state.");
return;
}
const currentPath = getCurrentPath();
if (!currentPath) {
vscode.window.showErrorMessage("No current path found.");
return;
}

// Check if we have pending changes
if (
oilState.editedPaths.size > 0 ||
vscode.window.activeTextEditor?.document.isDirty
) {
const result = await vscode.window.showWarningMessage(
"Discard changes?",
{ modal: true },
"Yes"
);
if (result !== "Yes") {
return;
}
}

const currentPathDisk = uriPathToDiskPath(currentPath);
// Update VS Code's workspace folders
try {
const folderUri = vscode.Uri.file(currentPathDisk);

// Update the first workspace folder to the new location
// Open the new directory instead of updating workspace folders
await vscode.commands.executeCommand("vscode.openFolder", folderUri, {
forceReuseWindow: false,
});
} catch (error) {
vscode.window.showErrorMessage(
`Failed to change working directory: ${error}`
);
}
}
16 changes: 16 additions & 0 deletions src/commands/close.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as vscode from "vscode";
import { getOilState } from "../state/oilState";
import { checkAndEnableAutoSave } from "../utils/settings";
import { hasPendingChanges } from "../utils/oilUtils";

export function closeOil() {
if (vscode.window.activeTextEditor?.document.languageId === "oil") {
const oilState = getOilState();
if (oilState) {
if (!hasPendingChanges(oilState)) {
checkAndEnableAutoSave();
}
}
vscode.commands.executeCommand("workbench.action.closeActiveEditor");
}
}
9 changes: 9 additions & 0 deletions src/commands/disableUpdatePreview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let disableUpdatePreview = false;

export function updateDisableUpdatePreview(value: boolean) {
disableUpdatePreview = value;
}

export function isUpdatePreviewDisabled(): boolean {
return disableUpdatePreview;
}
90 changes: 90 additions & 0 deletions src/commands/help.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import * as vscode from "vscode";

let helpPanel: vscode.WebviewPanel | null = null;

export async function help() {
helpPanel?.dispose(); // Close any existing help panel
const helpHeaders = [
"Command",
"Vim Key Binding",
"Default Shortcut",
"Description",
];
// Create a table of commands and default keymaps
const helpTable = [
["open", "-", "alt+-", "Open oil from the currents file parent directory"],
["help", "", "alt+shift+h", "Show this help information"],
["close", "", "alt+c", "Close oil explorer"],
["select", "Enter", "alt+Enter", "Open selected file/directory"],
["selectTab", "ctrl+t", "alt+t", "Open selected file in a new tab"],
["selectVertical", "", "alt+s", "Open selected file in a vertical split"],
["openParent", "-", "alt+-", "Navigate to parent directory"],
["openCwd", "_", "alt_shift+-", "Navigate to workspace root"],
["preview", "ctrl+p", "alt+p", "Preview file/directory at cursor"],
["refresh", "ctrl+l", "alt+l", "Refresh current directory view"],
[
"cd",
"`",
"alt+`",
"Change VSCode working directory to current oil directory",
],
];

// Display the message in a Markdown preview panel
const panel = vscode.window.createWebviewPanel(
"oilHelp",
"Oil Help",
vscode.ViewColumn.Active,
{
enableScripts: false,
}
);

panel.webview.html = `
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: var(--vscode-editor-font-family); padding: 20px; }
h1 { margin-bottom: 20px; margin-top: 0; }
p { margin-bottom: 10px; max-width: 600px; }
table { border-collapse: collapse; margin: 20px 0; }
caption { font-weight: bold; margin-bottom: 10px; }
th, td { padding: 8px 16px; text-align: left; }
th { border-bottom: 2px solid var(--vscode-list-hoverBackground); }
td { border-bottom: 1px solid var(--vscode-list-hoverBackground); }
.horizontal-links { display: flex; list-style: none; padding: 0; }
.horizontal-links li { margin-right: 20px; }
</style>
</head>
<body>
<div class="markdown-preview">
<h1>Oil Help</h1>
<p>Oil.code is a file explorer for VSCode that allows you to navigate and manage files and directories directly in your editor window.</p>
<table>
<caption>Available Commands with default keybinding/shortcut</caption>
<tr>
${helpHeaders.map((header) => `<th>${header}</th>`).join("")}
</tr>
${helpTable
.map(
(row) =>
`<tr>${row.map((cell) => `<td>${cell}</td>`).join("")}</tr>`
)
.join("")}
</table>
<h2 id="oil-links">Links</h2>
<ul class="horizontal-links" aria-labelledby="oil-links">
<li><a href="https://github.com/corwinm/oil.code">GitHub Repository</a></li>
<li><a href="https://marketplace.visualstudio.com/items?itemName=haphazarddev.oil-code">VS Code Marketplace</a></li>
<li><a href="https://github.com/corwinm/oil.code/issues">Issue Tracker</a></li>
<li><a href="https://github.com/corwinm/oil.code/issues/new?template=bug_report.yml">Report a Bug</a></li>
<li><a href="https://github.com/corwinm/oil.code/issues/new?template=feature_request.yml">Request a Feature</a></li>
</ul>
<p>If you find oil.code useful, please consider starring the repository on <a href="https://github.com/corwinm/oil.code">GitHub</a> and reviewing it on the <a href="https://marketplace.visualstudio.com/items?itemName=haphazarddev.oil-code">VS Code Marketplace</a>.</p>
</div>
</body>
</html>`;

helpPanel = panel;
}
9 changes: 9 additions & 0 deletions src/commands/openCwd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as vscode from "vscode";
import { select } from "./select";
import { logger } from "../logger";

export async function openCwd() {
logger.trace("Opening current working directory...");
const cwd = vscode.workspace.workspaceFolders?.at(0)?.uri.fsPath;
await select({ overRideLineText: "../", overRideTargetPath: cwd });
}
49 changes: 49 additions & 0 deletions src/commands/openOil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as path from "path";
import * as vscode from "vscode";
import { oilFileProvider } from "../providers/providers";
import { initOilStateWithPath, initOilState } from "../state/initState";
import { setOilState } from "../state/oilState";
import { checkAndDisableAutoSave } from "../utils/settings";
import { logger } from "../logger";
import { openParent } from "./openParent";
import { positionCursorOnFile } from "../utils/oilUtils";
import { resetPreviewState } from "../state/previewState";

export async function openOil(atPath?: string | undefined) {
logger.trace("Opening oil file...");
const activeEditor = vscode.window.activeTextEditor;

if (activeEditor?.document.languageId === "oil" && !atPath) {
openParent();
return;
}

const oilState = atPath ? initOilStateWithPath(atPath) : initOilState();
setOilState(oilState);
resetPreviewState();

const activeFile = path.basename(activeEditor?.document.uri.fsPath || "");

const folderPath = oilState.currentPath;

if (folderPath) {
try {
oilFileProvider.delete(oilState.tempFileUri);
// Open the in-memory document
const doc = await vscode.workspace.openTextDocument(oilState.tempFileUri);
await vscode.languages.setTextDocumentLanguage(doc, "oil");

const editor = await vscode.window.showTextDocument(doc, {
preview: false,
});

// Position cursor on the active file if it exists
positionCursorOnFile(editor, activeFile);
await checkAndDisableAutoSave();
} catch (error) {
vscode.window.showErrorMessage(`Failed to open oil file: ${error}`);
}
} else {
vscode.window.showErrorMessage("Unable to determine the folder to open.");
}
}
7 changes: 7 additions & 0 deletions src/commands/openParent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { select } from "./select";
import { logger } from "../logger";

export async function openParent() {
logger.trace("Opening parent directory...");
await select({ overRideLineText: "../" });
}
Loading
Loading