Skip to content

Commit

Permalink
Merge pull request #16 from carlosflorencio/feature/open-horizontal-s…
Browse files Browse the repository at this point in the history
…plit

Added new command openFileUnderCursorHorizontalSplit
  • Loading branch information
danprince authored Jan 30, 2024
2 parents 65fff69 + cf216a2 commit fa5a9aa
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 124 deletions.
37 changes: 31 additions & 6 deletions extension.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
let assert = require("node:assert");
let path = require("node:path");
let { homedir } = require("node:os");
let { window, workspace, commands, Uri, EventEmitter, FileType, Selection, languages, Range, Diagnostic, DiagnosticRelatedInformation, Location } = require("vscode");
let { window, workspace, commands, Uri, EventEmitter, FileType, Selection, languages, Range, Diagnostic, DiagnosticRelatedInformation, Location, ViewColumn } = require("vscode");

/**
* The scheme is used to associate vsnetrw documents with the text content provider
Expand Down Expand Up @@ -182,11 +182,17 @@ function getLinesUnderCursor() {
/**
* Opens a file in a vscode editor.
* @param {string} fileName
* @param {ViewColumn} [viewColumn]
*/
async function openFileInVscodeEditor(fileName) {
async function openFileInVscodeEditor(fileName, viewColumn) {
let uri = Uri.file(fileName);
await closeExplorer();
await commands.executeCommand("vscode.open", uri);

if (viewColumn) {
await commands.executeCommand("vscode.open", uri, viewColumn);
} else {
await commands.executeCommand("vscode.open", uri);
}
}

/**
Expand Down Expand Up @@ -346,12 +352,13 @@ async function openNewExplorer(dir = getInitialDir()) {

/**
* Attempt to open the file that is currently under the cursor.
*
*
* If there is a file under the cursor, it will open in a vscode text
* editor. If there is a directory under the cursor, then it will open in a
* new vsnetrw document.
* @param {ViewColumn} [viewColumn]
*/
async function openFileUnderCursor() {
async function openFileUnderCursor(viewColumn) {
let relativePath = getLineUnderCursor();
let basePath = getCurrentDir();
let newPath = path.resolve(basePath, relativePath);
Expand All @@ -361,7 +368,23 @@ async function openFileUnderCursor() {
if (stat.type & FileType.Directory) {
await openExplorer(newPath);
} else {
await openFileInVscodeEditor(newPath);
await openFileInVscodeEditor(newPath, viewColumn);
}
}

async function openFileUnderCursorInHorizontalSplit() {
await openFileUnderCursor(ViewColumn.Beside);
}

async function openFileUnderCursorInVerticalSplit() {
await openFileUnderCursor(ViewColumn.Beside);
// saving the reference
// toggling the editor layout (vertical split) will make the editor lose focus
const lastActiveEditor = window.activeTextEditor;
await commands.executeCommand("workbench.action.toggleEditorGroupLayout");
if (lastActiveEditor) {
// focus the editor again
await window.showTextDocument(lastActiveEditor.document);
}
}

Expand Down Expand Up @@ -499,6 +522,8 @@ function activate(context) {
context.subscriptions.push(
commands.registerCommand("vsnetrw.open", openNewExplorer),
commands.registerCommand("vsnetrw.openAtCursor", openFileUnderCursor),
commands.registerCommand("vsnetrw.openAtCursorInHorizontalSplit", openFileUnderCursorInHorizontalSplit),
commands.registerCommand("vsnetrw.openAtCursorInVerticalSplit", openFileUnderCursorInVerticalSplit),
commands.registerCommand("vsnetrw.openParent", openParentDirectory),
commands.registerCommand("vsnetrw.openHome", openHomeDirectory),
commands.registerCommand("vsnetrw.rename", renameFileUnderCursor),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
"@types/mocha": "^9.1.1",
"@types/node": "^18.0.0",
"@types/vscode": "^1.68.1",
"@vscode/test-electron": "^2.1.5",
"@vscode/test-electron": "^2.3.8",
"fast-glob": "^3.2.11",
"mocha": "^10.0.0",
"typescript": "^4.7.4",
Expand Down
17 changes: 17 additions & 0 deletions tests/extension.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,23 @@ describe("navigation", () => {
let text = getActiveEditorText();
assert.doesNotMatch(text, /\.\.\//);
});

["vsnetrw.openAtCursorInHorizontalSplit", "vsnetrw.openAtCursorInVerticalSplit"].forEach((command) => {
test(`open the a file with ${command}`, async () => {
const dir = await createTempWorkspace(["a.txt", "b.txt"]);
await execCommand("vscode.open", vscode.Uri.file(path.join(dir, "a.txt")));
assert.equal(vscode.window.tabGroups.all.length, 1);
await openExplorer();
await moveToLine("b.txt");
await execCommand(command);
assert.equal(vscode.window.tabGroups.all.length, 2);
assert.equal(
vscode.window.activeTextEditor?.document.fileName,
path.join(dir, "b.txt"),
);
await execCommand("workbench.action.closeActiveEditor");
});
})
});

describe("refresh", () => {
Expand Down
Loading

0 comments on commit fa5a9aa

Please sign in to comment.