Skip to content

Commit 8d78159

Browse files
authored
refactor: Simplify WorkspaceMovement shortcuts (#310)
* refactor: Simplify WorkspaceMovement shortcuts * refactor: Make guard clauses more succinct
1 parent fc17d3d commit 8d78159

File tree

1 file changed

+41
-57
lines changed

1 file changed

+41
-57
lines changed

src/actions/ws_movement.ts

Lines changed: 41 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -33,65 +33,54 @@ export class WorkspaceMovement {
3333
this.canCurrentlyEdit = canEdit;
3434
}
3535

36+
private shortcuts: ShortcutRegistry.KeyboardShortcut[] = [
37+
/** Move the cursor on the workspace to the left. */
38+
{
39+
name: Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_LEFT,
40+
preconditionFn: (workspace) => this.canCurrentlyEdit(workspace),
41+
callback: (workspace) => this.moveWSCursor(workspace, -1, 0),
42+
keyCodes: [createSerializedKey(KeyCodes.A, [KeyCodes.SHIFT])],
43+
},
44+
/** Move the cursor on the workspace to the right. */
45+
{
46+
name: Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_RIGHT,
47+
preconditionFn: (workspace) => this.canCurrentlyEdit(workspace),
48+
callback: (workspace) => this.moveWSCursor(workspace, 1, 0),
49+
keyCodes: [createSerializedKey(KeyCodes.D, [KeyCodes.SHIFT])],
50+
},
51+
/** Move the cursor on the workspace up. */
52+
{
53+
name: Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_UP,
54+
preconditionFn: (workspace) => this.canCurrentlyEdit(workspace),
55+
callback: (workspace) => this.moveWSCursor(workspace, 0, -1),
56+
keyCodes: [createSerializedKey(KeyCodes.W, [KeyCodes.SHIFT])],
57+
},
58+
59+
/** Move the cursor on the workspace down. */
60+
{
61+
name: Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_DOWN,
62+
preconditionFn: (workspace) => this.canCurrentlyEdit(workspace),
63+
callback: (workspace) => this.moveWSCursor(workspace, 0, 1),
64+
keyCodes: [createSerializedKey(KeyCodes.S, [KeyCodes.SHIFT])],
65+
},
66+
];
67+
3668
/**
37-
* Install these actions as both keyboard shortcuts and context menu items.
69+
* Install the shortcuts.
3870
*/
3971
install() {
40-
const shortcutList: {
41-
[name: string]: ShortcutRegistry.KeyboardShortcut;
42-
} = {
43-
/** Move the cursor on the workspace to the left. */
44-
wsMoveLeft: {
45-
name: Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_LEFT,
46-
preconditionFn: (workspace) => this.canCurrentlyEdit(workspace),
47-
callback: (workspace) => this.moveWSCursor(workspace, -1, 0),
48-
keyCodes: [createSerializedKey(KeyCodes.A, [KeyCodes.SHIFT])],
49-
},
50-
/** Move the cursor on the workspace to the right. */
51-
wsMoveRight: {
52-
name: Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_RIGHT,
53-
preconditionFn: (workspace) => this.canCurrentlyEdit(workspace),
54-
callback: (workspace) => this.moveWSCursor(workspace, 1, 0),
55-
keyCodes: [createSerializedKey(KeyCodes.D, [KeyCodes.SHIFT])],
56-
},
57-
58-
/** Move the cursor on the workspace up. */
59-
wsMoveUp: {
60-
name: Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_UP,
61-
preconditionFn: (workspace) => this.canCurrentlyEdit(workspace),
62-
callback: (workspace) => this.moveWSCursor(workspace, 0, -1),
63-
keyCodes: [createSerializedKey(KeyCodes.W, [KeyCodes.SHIFT])],
64-
},
65-
66-
/** Move the cursor on the workspace down. */
67-
wsMoveDown: {
68-
name: Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_DOWN,
69-
preconditionFn: (workspace) => this.canCurrentlyEdit(workspace),
70-
callback: (workspace) => this.moveWSCursor(workspace, 0, 1),
71-
keyCodes: [createSerializedKey(KeyCodes.S, [KeyCodes.SHIFT])],
72-
},
73-
};
74-
for (const shortcut of Object.values(shortcutList)) {
72+
for (const shortcut of this.shortcuts) {
7573
ShortcutRegistry.registry.register(shortcut);
7674
}
7775
}
7876

7977
/**
80-
* Uninstall these actions.
78+
* Uninstall the shortcuts.
8179
*/
8280
uninstall() {
83-
ShortcutRegistry.registry.unregister(
84-
Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_LEFT,
85-
);
86-
ShortcutRegistry.registry.unregister(
87-
Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_RIGHT,
88-
);
89-
ShortcutRegistry.registry.unregister(
90-
Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_UP,
91-
);
92-
ShortcutRegistry.registry.unregister(
93-
Constants.SHORTCUT_NAMES.MOVE_WS_CURSOR_DOWN,
94-
);
81+
for (const shortcut of this.shortcuts) {
82+
ShortcutRegistry.registry.unregister(shortcut.name);
83+
}
9584
}
9685

9786
/**
@@ -109,14 +98,9 @@ export class WorkspaceMovement {
10998
yDirection: number,
11099
): boolean {
111100
const cursor = workspace.getCursor();
112-
if (!cursor) {
113-
return false;
114-
}
115-
const curNode = cursor.getCurNode();
116-
117-
if (curNode.getType() !== ASTNode.types.WORKSPACE) {
118-
return false;
119-
}
101+
if (!cursor) return false;
102+
const curNode = cursor?.getCurNode();
103+
if (curNode.getType() !== ASTNode.types.WORKSPACE) return false;
120104

121105
const wsCoord = curNode.getWsCoordinate();
122106
const newX = xDirection * this.WS_MOVE_DISTANCE + wsCoord.x;

0 commit comments

Comments
 (0)