Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

feat: add more vim keybindings to editor (#434)

Dylan Fontaine5bd5a1bbe8c2
diff --git a/packages/monaco/src/vim-keybindings.ts b/packages/monaco/src/vim-keybindings.ts
new file mode 100644
index 00000000..284e4699
--- /dev/null
+++ b/packages/monaco/src/vim-keybindings.ts
@@ -0,0 +1,54 @@
+import { VimMode } from 'monaco-vim';
+
+type Keybind = {
+ key: string;
+ action: string;
+ name: string;
+};
+
+function registerKeybind({ key, action, name }: Keybind) {
+ VimMode.Vim.defineAction(name, function (ctx) {
+ ctx.editor.trigger('action', action, null);
+ });
+ VimMode.Vim.mapCommand(key, 'action', name);
+}
+
+const KEYBINDS: Keybind[] = [
+ {
+ key: 'K',
+ action: 'editor.action.showHover',
+ name: 'hover',
+ },
+ {
+ key: 'gd',
+ action: 'editor.action.goToDeclaration',
+ name: 'goToDeclaration',
+ },
+ {
+ key: 'gr',
+ action: 'editor.action.goToReferences',
+ name: 'goToReferences',
+ },
+ {
+ key: 'gi',
+ action: 'editor.action.goToImplementation',
+ name: 'goToImplementation',
+ },
+ {
+ key: 'gx',
+ action: 'editor.action.openLink',
+ name: 'goToLink',
+ },
+ {
+ key: ']d',
+ action: 'editor.action.marker.next',
+ name: 'nextDiagnostic',
+ },
+ {
+ key: '[d',
+ action: 'editor.action.marker.prev',
+ name: 'prevDiagnostic',
+ },
+];
+
+KEYBINDS.forEach(registerKeybind);
diff --git a/packages/monaco/src/vim-mode.tsx b/packages/monaco/src/vim-mode.tsx
index 778a6164..98e87605 100644
--- a/packages/monaco/src/vim-mode.tsx
+++ b/packages/monaco/src/vim-mode.tsx
@@ -4,18 +4,12 @@ import type * as monaco from 'monaco-editor';
import { useEffect, useRef } from 'react';
import { VimMode, initVimMode } from 'monaco-vim';
import { useEditorSettingsStore } from './settings-store';
+import './vim-keybindings';
interface VimStatusBarProps {
editor: monaco.editor.IStandaloneCodeEditor;
}
-// `K` -> hover
-VimMode.Vim.defineAction('hover', function (ctx) {
- // it didn't work without the set timeout, it probably handles the key after and dismisses it
- setTimeout(() => ctx.editor.getAction('editor.action.showHover')?.run());
-});
-VimMode.Vim.mapCommand('K', 'action', 'hover');
-
export function VimStatusBar({ editor }: VimStatusBarProps) {
const statusBarRef = useRef<HTMLDivElement | null>(null);
const vimModeRef = useRef<VimMode>();