Skip to content

Commit

Permalink
feat: create way to remove editor by id and current editor
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-z committed Nov 24, 2023
1 parent daa2e85 commit 6064308
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 40 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"Other"
],
"main": "./dist/harpoon.js",
"activationEvents": [],
"contributes": {
"commands": [
{
Expand Down Expand Up @@ -239,7 +240,7 @@
"prettier": "^2.5.1",
"ts-loader": "^9.2.6",
"typescript": "^4.5.5",
"@vscode/vsce": "^2.7.0",
"@vscode/vsce": "^2.22.0",
"webpack": "^5.69.1",
"webpack-cli": "^4.9.2"
},
Expand Down
50 changes: 12 additions & 38 deletions src/commands/command-factory.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,31 @@
import * as vscode from "vscode";

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

type CommandName =
| "addEditor"
| "editEditors"
| "addEditor1"
| "addEditor2"
| "addEditor3"
| "addEditor4"
| "addEditor5"
| "addEditor6"
| "addEditor7"
| "addEditor8"
| "addEditor9"
| "gotoEditor1"
| "gotoEditor2"
| "gotoEditor3"
| "gotoEditor4"
| "gotoEditor5"
| "gotoEditor6"
| "gotoEditor7"
| "gotoEditor8"
| "gotoEditor9"
| `addEditor${EditorRange}`
| `gotoEditor${EditorRange}`
| "editorQuickPick"
| "addGlobalEditor"
| "gotoPreviousHarpoonEditor"
| "removeCurrentEditor"
| `removeEditor${EditorRange}`
| "editGlobalEditors"
| "addGlobalEditor1"
| "addGlobalEditor2"
| "addGlobalEditor3"
| "addGlobalEditor4"
| "addGlobalEditor5"
| "addGlobalEditor6"
| "addGlobalEditor7"
| "addGlobalEditor8"
| "addGlobalEditor9"
| "gotoGlobalEditor1"
| "gotoGlobalEditor2"
| "gotoGlobalEditor3"
| "gotoGlobalEditor4"
| "gotoGlobalEditor5"
| "gotoGlobalEditor6"
| "gotoGlobalEditor7"
| "gotoGlobalEditor8"
| "gotoGlobalEditor9"
| `addGlobalEditor${EditorRange}`
| `gotoGlobalEditor${EditorRange}`
| "editorGlobalQuickPick"
| "gotoPreviousGlobalHarpoonEditor";
| "gotoPreviousGlobalHarpoonEditor"
| "removeCurrentGlobalEditor"
| `removeGlobalEditor${EditorRange}`;

export default class CommandFactory {
constructor(private readonly context: vscode.ExtensionContext) {}

public registerCommand(commandName: CommandName, command: () => any | Promise<any>) {
const disposable = vscode.commands.registerCommand(
`vscode-harpoon.${commandName}`,
`vscode - harpoon.${ commandName } `,
async () => {
try {
return await Promise.resolve(command());
Expand Down
17 changes: 17 additions & 0 deletions src/commands/remove-current-editor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as vscode from "vscode";
import ActiveProjectService from "../service/active-project-service";
import WorkspaceService from "../service/workspace-service";

export function createRemoveCurrentEditorCommand(
workspaceService: WorkspaceService,
activeProjectService: ActiveProjectService
) {
return async () => {
let activeEditor = vscode.window.activeTextEditor;
if (!activeEditor) {
return;
}
activeProjectService.removeEditorByName(activeEditor.document.fileName);
workspaceService.saveWorkspace();
};
}
16 changes: 16 additions & 0 deletions src/commands/remove-editor-by-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import ActiveProjectService from "../service/active-project-service";
import WorkspaceService from "../service/workspace-service";

export function createRemoveEditorByIdCommand(
workspaceService: WorkspaceService,
activeProjectService: ActiveProjectService
) {
return (id: number) => {
return async () => {
const didRemove = activeProjectService.removeEditorById(id);
if (didRemove) {
workspaceService.saveWorkspace();
}
};
};
}
15 changes: 15 additions & 0 deletions src/harpoon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ 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 { createRemoveCurrentEditorCommand } from "./commands/remove-current-editor";
import { createRemoveEditorByIdCommand } from "./commands/remove-editor-by-id";

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

Expand Down Expand Up @@ -81,4 +83,17 @@ function registerCommands(
`gotoPrevious${key}HarpoonEditor`,
createGotoPreviousHarpoonEditorCommand(activeProjectService, workspaceService)
);

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));
}
23 changes: 22 additions & 1 deletion src/service/active-project-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function getTrimmedEditor(editor: Editor) {
}

export default class ActiveProjectService {
constructor(private _activeEditors: Editor[], private _previousEditor?: Editor) {}
constructor(private _activeEditors: Editor[], private _previousEditor?: Editor) { }

public addEditor(editor: Editor) {
editor = getTrimmedEditor(editor);
Expand Down Expand Up @@ -40,6 +40,27 @@ export default class ActiveProjectService {
return this._activeEditors[id - 1];
}

public removeEditorByName(fileName: string) {
this._activeEditors = this._activeEditors.map(editor => {
if (editor.fileName !== fileName) {
return editor;
}
return {
fileName: "_",
};
});
}

public removeEditorById(id: number): boolean {
if (id > this._activeEditors.length) {
return false;
}
this._activeEditors[id] = {
fileName: "_",
};
return true;
}

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

0 comments on commit 6064308

Please sign in to comment.