Skip to content

Commit

Permalink
Merge pull request #38 from gm-m/pr/navigate-editor-command
Browse files Browse the repository at this point in the history
feat: navigate next and navigate previous commands
  • Loading branch information
tobias-z authored Jun 16, 2024
2 parents b59d76b + 39d4dc5 commit c70ba1e
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ You are then able to jump to `editor 1` or `editor 2` from anywhere in your work
- `VSCode Harpoon: Editor Global Quick Pick (vscode-harpoon.editorGlobalQuickPick)` Opens a quick
pick menu to pick between your global editors
- `VSCode Harpoon: Go to previous global harpoon editor (vscode-harpoon.gotoPreviousGlobalHarpoonEditor)` Jumps to the previous global editor which was last jumped from using harpoon.
- `VSCode Harpoon: Navigate Next Editor (vscode-harpoon.navigateNextEditor)` Jumps to the next workspace editor.
- `VSCode Harpoon: Navigate Previous Editor (vscode-harpoon.navigatePreviousEditor)` Jumps to the previous workspace editor.

### Available Contexts

Expand Down
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
"main": "./dist/harpoon.js",
"contributes": {
"commands": [
{
"command": "vscode-harpoon.navigateNextEditor",
"title": "VSCode Harpoon: Navigate Next Editor"
},
{
"command": "vscode-harpoon.navigatePreviousEditor",
"title": "VSCode Harpoon: Navigate Previous Editor"
},
{
"command": "vscode-harpoon.addEditor",
"title": "VSCode Harpoon: Add Editor"
Expand Down
4 changes: 4 additions & 0 deletions src/commands/command-factory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import * as vscode from "vscode";

type CommandName =
| "navigateNextEditor"
| "navigateGlobalNextEditor"
| "navigatePreviousEditor"
| "navigateGlobalPreviousEditor"
| "addEditor"
| "editEditors"
| "addEditor1"
Expand Down
38 changes: 38 additions & 0 deletions src/commands/navigate-editor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as vscode from "vscode";
import ActiveProjectService, { Editor } from "../service/active-project-service";
import WorkspaceService from "../service/workspace-service";

export type NavigationDirection = "navigateNext" | "navigatePrevious";

export default function createNavigateEditorCommand(
activeProjectService: ActiveProjectService,
workspaceService: WorkspaceService,
direction: NavigationDirection
) {
return () => {
if (activeProjectService.activeEditors.length < 1) {
return;
}
const activeEditor = vscode.window.activeTextEditor;
if (!activeEditor) {
return;
}
const currentEditor: Editor = {
fileName: activeEditor.document.fileName,
};
const currentEditorIndex = activeProjectService.activeEditors.findIndex(
editor => editor.fileName === currentEditor.fileName
);
if (currentEditorIndex !== -1 && activeProjectService.activeEditors.length === 1) {
return;
}

const nextNonFillerEditorIndex = activeProjectService.getNextNonFillerEditorIndex(
currentEditorIndex,
direction
);
if (nextNonFillerEditorIndex !== -1) {
workspaceService.changeEditorById(nextNonFillerEditorIndex + 1);
}
};
}
9 changes: 9 additions & 0 deletions src/harpoon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import createAddEditorCommand from "./commands/add-editor";
import createEditEditorsCommand from "./commands/edit-editors";
import createEditorQuickPickCommand from "./commands/editor-quick-pick";
import createGotoPreviousHarpoonEditorCommand from "./commands/goto-previous-harpoon-editor";
import createNavigateEditorCommand from "./commands/navigate-editor";

export type State = "workspaceState" | "globalState";

Expand Down Expand Up @@ -81,4 +82,12 @@ function registerCommands(
`gotoPrevious${key}HarpoonEditor`,
createGotoPreviousHarpoonEditorCommand(activeProjectService, workspaceService)
);
commandFactory.registerCommand(
`navigate${key}NextEditor`,
createNavigateEditorCommand(activeProjectService, workspaceService, "navigateNext")
);
commandFactory.registerCommand(
`navigate${key}PreviousEditor`,
createNavigateEditorCommand(activeProjectService, workspaceService, "navigatePrevious")
);
}
40 changes: 40 additions & 0 deletions src/service/active-project-service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { NavigationDirection } from "../commands/navigate-editor";

export type Editor = {
editorId?: number;
fileName: string;
Expand Down Expand Up @@ -40,6 +42,44 @@ export default class ActiveProjectService {
return this._activeEditors[id - 1];
}

public getNextNonFillerEditorIndex(currentEditorIndex: number, direction: NavigationDirection) {
if (direction === "navigateNext") {
const nextIndex = this.findNextNonFillerEditorIndex(currentEditorIndex + 1);
return nextIndex !== -1
? nextIndex
: this.findNextNonFillerEditorIndex(0, currentEditorIndex);
}

const nextIndex = this.findPreviousNonFillerEditorIndex(currentEditorIndex - 1);
return nextIndex !== -1
? nextIndex
: this.findPreviousNonFillerEditorIndex(this._activeEditors.length - 1, currentEditorIndex + 1);
}

private findNextNonFillerEditorIndex(fromIndex: number, toIndex: number = this._activeEditors.length) {
for (let i = fromIndex; i < toIndex; i++) {
if (!this.isFillerEditor(this._activeEditors[i])) {
return i;
}
}

return -1;
}

private findPreviousNonFillerEditorIndex(fromIndex: number, toIndex: number = 0) {
for (let i = fromIndex; i >= toIndex; i--) {
if (!this.isFillerEditor(this._activeEditors[i])) {
return i;
}
}

return -1;
}

private isFillerEditor(editor: Editor): boolean {
return editor.fileName === "_";
}

public set activeEditors(editors: Editor[]) {
const hasActualEditor = editors.some(e => e.fileName !== "_");
if (hasActualEditor) {
Expand Down

0 comments on commit c70ba1e

Please sign in to comment.