Skip to content

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

added `K` keymap to vim (#403)

ggrandidb9b4de24696
diff --git a/packages/monaco/monaco-vim.d.ts b/packages/monaco/monaco-vim.d.ts
index 94612670..c2a4650f 100644
--- a/packages/monaco/monaco-vim.d.ts
+++ b/packages/monaco/monaco-vim.d.ts
@@ -1,13 +1,52 @@
-declare module "monaco-vim" {
- import type * as monaco from "monaco-editor";
-
- export function initVimMode(
- editor: monaco.editor.IStandaloneCodeEditor,
- statusbarNode?: Element | null,
- ): VimMode;
-
- class VimMode {
- /** removes the attached vim bindings */
- dispose(): void;
- }
+declare module 'monaco-vim' {
+ import type * as monaco from 'monaco-editor';
+
+ export function initVimMode(
+ editor: monaco.editor.IStandaloneCodeEditor,
+ statusbarNode?: Element | null,
+ ): VimMode;
+
+ type VimModes = 'insert' | 'normal' | 'visual';
+
+ export class VimMode {
+ /** removes the attached vim bindings */
+ dispose(): void;
+
+ static Vim: {
+ map(lhs: string, rhs: string, ctx: VimModes): void;
+ unmap(lhs: string, ctx: VimModes): boolean;
+ noremap(lhs: string, rhs: string, ctx: VimModes): void;
+
+ mapCommand(
+ keys: string,
+ type: 'action',
+ name: string,
+ args?: Record<PropertyKey, unknown>,
+ extra?: Record<PropertyKey, unknown>,
+ ): void;
+
+ defineAction(
+ name: string,
+ fn: (
+ cm: { editor: monaco.editor.IStandaloneCodeEditor },
+ arg2: { registerName: null | string; repeat: number; repeatIsExplicit: boolean },
+ arg3: unknown,
+ ) => void,
+ ): void;
+
+ defineEx(
+ name: string,
+ prefix: string | undefined,
+ fn: (
+ ctx: { editor: monaco.editor.IStandaloneCodeEditor },
+ data: {
+ argString: string[];
+ args: string;
+ commandName: string;
+ input: string;
+ } & ({ line: undefined } | { line: number; lineEnd: number }),
+ ) => void,
+ ): void;
+ };
+ }
}
diff --git a/packages/monaco/src/index.tsx b/packages/monaco/src/index.tsx
index 534e8de2..0cf0fe04 100644
--- a/packages/monaco/src/index.tsx
+++ b/packages/monaco/src/index.tsx
@@ -11,7 +11,6 @@ import lzstring from 'lz-string';
import { Button, ToastAction, useToast, Tooltip, TooltipContent, TooltipTrigger } from '@repo/ui';
import { useLocalStorage } from './useLocalStorage';
import SplitEditor, { TESTS_PATH } from './split-editor';
-import { USER_CODE_START_REGEX } from './constants';
import { createTwoslashInlayProvider } from './twoslash';
const VimStatusBar = dynamic(() => import('./vim-mode').then((v) => v.VimStatusBar), {
diff --git a/packages/monaco/src/vim-mode.tsx b/packages/monaco/src/vim-mode.tsx
index 2a70f057..778a6164 100644
--- a/packages/monaco/src/vim-mode.tsx
+++ b/packages/monaco/src/vim-mode.tsx
@@ -2,13 +2,20 @@
import type * as monaco from 'monaco-editor';
import { useEffect, useRef } from 'react';
-import { type VimMode, initVimMode } from 'monaco-vim';
+import { VimMode, initVimMode } from 'monaco-vim';
import { useEditorSettingsStore } from './settings-store';
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>();
diff --git a/packages/monaco/tsconfig.json b/packages/monaco/tsconfig.json
index 0b6bd587..19ecf158 100644
--- a/packages/monaco/tsconfig.json
+++ b/packages/monaco/tsconfig.json
@@ -1,8 +1,14 @@
{
"extends": "@repo/tsconfig/react-library.json",
"compilerOptions": {
- "tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json",
+ "tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json"
},
- "include": ["**/*.ts", "**/*.tsx", "monaco-editor.d.ts", "monaco-vim.d.ts", "../../monaco-editor.d copy.ts"],
+ "include": [
+ "**/*.ts",
+ "**/*.tsx",
+ "monaco-editor.d.ts",
+ "monaco-vim.d.ts",
+ "../../monaco-editor.d copy.ts"
+ ],
"exclude": ["node_modules"]
}