From 6064308ffee0cdcf3d79063c27c8e14319c251c8 Mon Sep 17 00:00:00 2001 From: Tobias Zimmermann Date: Fri, 24 Nov 2023 23:19:22 +0100 Subject: [PATCH] feat: create way to remove editor by id and current editor --- package.json | 3 +- src/commands/command-factory.ts | 50 +++++++-------------------- src/commands/remove-current-editor.ts | 17 +++++++++ src/commands/remove-editor-by-id.ts | 16 +++++++++ src/harpoon.ts | 15 ++++++++ src/service/active-project-service.ts | 23 +++++++++++- 6 files changed, 84 insertions(+), 40 deletions(-) create mode 100644 src/commands/remove-current-editor.ts create mode 100644 src/commands/remove-editor-by-id.ts diff --git a/package.json b/package.json index f1d2eb7..d7ee003 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "Other" ], "main": "./dist/harpoon.js", + "activationEvents": [], "contributes": { "commands": [ { @@ -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" }, diff --git a/src/commands/command-factory.ts b/src/commands/command-factory.ts index d4bdaeb..2fbd5d2 100644 --- a/src/commands/command-factory.ts +++ b/src/commands/command-factory.ts @@ -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) { const disposable = vscode.commands.registerCommand( - `vscode-harpoon.${commandName}`, + `vscode - harpoon.${ commandName } `, async () => { try { return await Promise.resolve(command()); diff --git a/src/commands/remove-current-editor.ts b/src/commands/remove-current-editor.ts new file mode 100644 index 0000000..eeb131f --- /dev/null +++ b/src/commands/remove-current-editor.ts @@ -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(); + }; +} diff --git a/src/commands/remove-editor-by-id.ts b/src/commands/remove-editor-by-id.ts new file mode 100644 index 0000000..13757ed --- /dev/null +++ b/src/commands/remove-editor-by-id.ts @@ -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(); + } + }; + }; +} diff --git a/src/harpoon.ts b/src/harpoon.ts index bebc17e..41a6f10 100644 --- a/src/harpoon.ts +++ b/src/harpoon.ts @@ -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"; @@ -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)); } diff --git a/src/service/active-project-service.ts b/src/service/active-project-service.ts index dab79c2..180f83f 100644 --- a/src/service/active-project-service.ts +++ b/src/service/active-project-service.ts @@ -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); @@ -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) {