From c12aede33a18b9cd99789cfda4b1b4b0bab83d22 Mon Sep 17 00:00:00 2001 From: Christopher Allen Date: Tue, 11 Mar 2025 12:29:17 +0000 Subject: [PATCH 1/2] refactor: Simplify WorkspaceMovement shortcuts --- src/actions/ws_movement.ts | 87 +++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 49 deletions(-) diff --git a/src/actions/ws_movement.ts b/src/actions/ws_movement.ts index 54543ff8..05a310a2 100644 --- a/src/actions/ws_movement.ts +++ b/src/actions/ws_movement.ts @@ -33,65 +33,54 @@ export class WorkspaceMovement { this.canCurrentlyEdit = canEdit; } + private shortcuts: ShortcutRegistry.KeyboardShortcut[] = [ + /** Move the cursor on the workspace to the left. */ + { + name: Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_LEFT, + preconditionFn: (workspace) => this.canCurrentlyEdit(workspace), + callback: (workspace) => this.moveWSCursor(workspace, -1, 0), + keyCodes: [createSerializedKey(KeyCodes.A, [KeyCodes.SHIFT])], + }, + /** Move the cursor on the workspace to the right. */ + { + name: Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_RIGHT, + preconditionFn: (workspace) => this.canCurrentlyEdit(workspace), + callback: (workspace) => this.moveWSCursor(workspace, 1, 0), + keyCodes: [createSerializedKey(KeyCodes.D, [KeyCodes.SHIFT])], + }, + /** Move the cursor on the workspace up. */ + { + name: Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_UP, + preconditionFn: (workspace) => this.canCurrentlyEdit(workspace), + callback: (workspace) => this.moveWSCursor(workspace, 0, -1), + keyCodes: [createSerializedKey(KeyCodes.W, [KeyCodes.SHIFT])], + }, + + /** Move the cursor on the workspace down. */ + { + name: Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_DOWN, + preconditionFn: (workspace) => this.canCurrentlyEdit(workspace), + callback: (workspace) => this.moveWSCursor(workspace, 0, 1), + keyCodes: [createSerializedKey(KeyCodes.S, [KeyCodes.SHIFT])], + }, + ]; + /** - * Install these actions as both keyboard shortcuts and context menu items. + * Install the shortcuts. */ install() { - const shortcutList: { - [name: string]: ShortcutRegistry.KeyboardShortcut; - } = { - /** Move the cursor on the workspace to the left. */ - wsMoveLeft: { - name: Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_LEFT, - preconditionFn: (workspace) => this.canCurrentlyEdit(workspace), - callback: (workspace) => this.moveWSCursor(workspace, -1, 0), - keyCodes: [createSerializedKey(KeyCodes.A, [KeyCodes.SHIFT])], - }, - /** Move the cursor on the workspace to the right. */ - wsMoveRight: { - name: Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_RIGHT, - preconditionFn: (workspace) => this.canCurrentlyEdit(workspace), - callback: (workspace) => this.moveWSCursor(workspace, 1, 0), - keyCodes: [createSerializedKey(KeyCodes.D, [KeyCodes.SHIFT])], - }, - - /** Move the cursor on the workspace up. */ - wsMoveUp: { - name: Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_UP, - preconditionFn: (workspace) => this.canCurrentlyEdit(workspace), - callback: (workspace) => this.moveWSCursor(workspace, 0, -1), - keyCodes: [createSerializedKey(KeyCodes.W, [KeyCodes.SHIFT])], - }, - - /** Move the cursor on the workspace down. */ - wsMoveDown: { - name: Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_DOWN, - preconditionFn: (workspace) => this.canCurrentlyEdit(workspace), - callback: (workspace) => this.moveWSCursor(workspace, 0, 1), - keyCodes: [createSerializedKey(KeyCodes.S, [KeyCodes.SHIFT])], - }, - }; - for (const shortcut of Object.values(shortcutList)) { + for (const shortcut of this.shortcuts) { ShortcutRegistry.registry.register(shortcut); } } /** - * Uninstall these actions. + * Uninstall the shortcuts. */ uninstall() { - ShortcutRegistry.registry.unregister( - Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_LEFT, - ); - ShortcutRegistry.registry.unregister( - Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_RIGHT, - ); - ShortcutRegistry.registry.unregister( - Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_UP, - ); - ShortcutRegistry.registry.unregister( - Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_DOWN, - ); + for (const shortcut of this.shortcuts) { + ShortcutRegistry.registry.unregister(shortcut.name); + } } /** From 153acc29c45f5f3d107b02b5e605b16fc97c3e8a Mon Sep 17 00:00:00 2001 From: Christopher Allen Date: Tue, 11 Mar 2025 12:37:39 +0000 Subject: [PATCH 2/2] refactor: Make guard clauses more succinct --- src/actions/ws_movement.ts | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/actions/ws_movement.ts b/src/actions/ws_movement.ts index 05a310a2..6286d6a2 100644 --- a/src/actions/ws_movement.ts +++ b/src/actions/ws_movement.ts @@ -98,14 +98,9 @@ export class WorkspaceMovement { yDirection: number, ): boolean { const cursor = workspace.getCursor(); - if (!cursor) { - return false; - } - const curNode = cursor.getCurNode(); - - if (curNode.getType() !== ASTNode.types.WORKSPACE) { - return false; - } + if (!cursor) return false; + const curNode = cursor?.getCurNode(); + if (curNode.getType() !== ASTNode.types.WORKSPACE) return false; const wsCoord = curNode.getWsCoordinate(); const newX = xDirection * this.WS_MOVE_DISTANCE + wsCoord.x;