Skip to content

Commit

Permalink
refactor: move to using specific range for all id based commands
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-z committed Nov 24, 2023
1 parent 6064308 commit c27cf80
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 36 deletions.
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,6 @@
{
"command": "vscode-harpoon.gotoPreviousGlobalHarpoonEditor",
"title": "VSCode Harpoon: Go to previous global harpoon editor"
},
{
"command": "vscode-harpoon.isQuickPick",
"title": "VSCode Harpoon: Quick Pick Visible"
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion src/commands/command-factory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as vscode from "vscode";

type EditorRange = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
export type EditorRange = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;

type CommandName =
| "addEditor"
Expand Down
53 changes: 22 additions & 31 deletions src/harpoon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import * as vscode from "vscode";
import ActiveProjectService, { Editor } from "./service/active-project-service";
import WorkspaceService from "./service/workspace-service";
import CommandFactory from "./commands/command-factory";
import CommandFactory, { EditorRange } from "./commands/command-factory";
import { createGotoEditorCommand } from "./commands/goto-editor";
import createAddEditorCommand from "./commands/add-editor";
import createEditEditorsCommand from "./commands/edit-editors";
Expand All @@ -11,6 +11,12 @@ import createGotoPreviousHarpoonEditorCommand from "./commands/goto-previous-har
import { createRemoveCurrentEditorCommand } from "./commands/remove-current-editor";
import { createRemoveEditorByIdCommand } from "./commands/remove-editor-by-id";

function loopEditorIds(fn: (id: EditorRange) => void) {
for (let i = 1; i <= 9; i++) {
fn(i as EditorRange);
}
}

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

export type ProjectState = {
Expand Down Expand Up @@ -47,34 +53,26 @@ function registerCommands(
prevState.previousEditor
);
const workspaceService = new WorkspaceService(activeProjectService, context, state);
const gotoEditor = createGotoEditorCommand(workspaceService);

const key = state === "globalState" ? "Global" : "";

const addEditor = createAddEditorCommand(activeProjectService, workspaceService);

commandFactory.registerCommand(`add${key}Editor`, addEditor());
commandFactory.registerCommand(
`edit${key}Editors`,
createEditEditorsCommand(activeProjectService, workspaceService)
);
commandFactory.registerCommand(`add${key}Editor1`, addEditor(1));
commandFactory.registerCommand(`add${key}Editor2`, addEditor(2));
commandFactory.registerCommand(`add${key}Editor3`, addEditor(3));
commandFactory.registerCommand(`add${key}Editor4`, addEditor(4));
commandFactory.registerCommand(`add${key}Editor5`, addEditor(5));
commandFactory.registerCommand(`add${key}Editor6`, addEditor(6));
commandFactory.registerCommand(`add${key}Editor7`, addEditor(7));
commandFactory.registerCommand(`add${key}Editor8`, addEditor(8));
commandFactory.registerCommand(`add${key}Editor9`, addEditor(9));
commandFactory.registerCommand(`goto${key}Editor1`, gotoEditor(1));
commandFactory.registerCommand(`goto${key}Editor2`, gotoEditor(2));
commandFactory.registerCommand(`goto${key}Editor3`, gotoEditor(3));
commandFactory.registerCommand(`goto${key}Editor4`, gotoEditor(4));
commandFactory.registerCommand(`goto${key}Editor5`, gotoEditor(5));
commandFactory.registerCommand(`goto${key}Editor6`, gotoEditor(6));
commandFactory.registerCommand(`goto${key}Editor7`, gotoEditor(7));
commandFactory.registerCommand(`goto${key}Editor8`, gotoEditor(8));
commandFactory.registerCommand(`goto${key}Editor9`, gotoEditor(9));

const gotoEditor = createGotoEditorCommand(workspaceService);
const removeById = createRemoveEditorByIdCommand(workspaceService, activeProjectService);

loopEditorIds(id => {
commandFactory.registerCommand(`add${key}Editor${id}`, addEditor(id));
commandFactory.registerCommand(`goto${key}Editor${id}`, gotoEditor(id));
commandFactory.registerCommand(`remove${key}Editor${id}`, removeById(id));
});

commandFactory.registerCommand(
`editor${key}QuickPick`,
createEditorQuickPickCommand(activeProjectService, workspaceService)
Expand All @@ -84,16 +82,9 @@ function registerCommands(
createGotoPreviousHarpoonEditorCommand(activeProjectService, workspaceService)
);

commandFactory.registerCommand(`removeCurrent${key}Editor`, createRemoveCurrentEditorCommand(workspaceService, activeProjectService));
commandFactory.registerCommand(
`removeCurrent${key}Editor`,
createRemoveCurrentEditorCommand(workspaceService, activeProjectService)
);

const removeById = createRemoveEditorByIdCommand(workspaceService, activeProjectService);
commandFactory.registerCommand(`remove${key}Editor1`, removeById(1));
commandFactory.registerCommand(`remove${key}Editor2`, removeById(2));
commandFactory.registerCommand(`remove${key}Editor3`, removeById(3));
commandFactory.registerCommand(`remove${key}Editor4`, removeById(4));
commandFactory.registerCommand(`remove${key}Editor5`, removeById(5));
commandFactory.registerCommand(`remove${key}Editor6`, removeById(6));
commandFactory.registerCommand(`remove${key}Editor7`, removeById(7));
commandFactory.registerCommand(`remove${key}Editor8`, removeById(8));
commandFactory.registerCommand(`remove${key}Editor9`, removeById(9));
}

0 comments on commit c27cf80

Please sign in to comment.