Skip to content

Commit

Permalink
Add support for Vim keybindings
Browse files Browse the repository at this point in the history
Fixes tareqimbasher#238

Add support for Vim keybindings to the text editor.

* **BuiltinShortcuts**:
  - Add Vim keybindings to `BuiltinShortcuts` in `src/Apps/NetPad.Apps.App/App/src/core/@application/shortcuts/builtin-shortcuts.ts`.
  - Include keybindings for common Vim commands like `Escape`, `:w`, `:q`, and `:wq`.

* **ITextEditorService**:
  - Update `ITextEditorService` interface in `src/Apps/NetPad.Apps.App/App/src/core/@application/editor/itext-editor-service.ts` to include methods for enabling and disabling Vim mode.

* **TextEditor**:
  - Implement methods to enable and disable Vim mode in `TextEditor` class in `src/Apps/NetPad.Apps.App/App/src/core/@application/editor/text-editor.ts`.
  - Use `monaco-vim` library to integrate Vim keybindings with Monaco editor.
  • Loading branch information
hootanht committed Aug 10, 2024
1 parent 1ac1037 commit ae22459
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {ITextEditor} from "@application/editor/text-editor";
export interface ITextEditorService {
get active(): ITextEditor | undefined;
create(host: HTMLElement): ITextEditor;
enableVimMode(): void;
disableVimMode(): void;
}

export const ITextEditorService = DI.createInterface<ITextEditorService>();
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {WithDisposables} from "@common";
import {IEventBus, MonacoEditorUtil, Settings, ViewModelBase} from "@application";
import {TextEditorFocusedEvent} from "./events";
import {TextDocument} from "./text-document";
import {initVimMode, VimMode} from "monaco-vim";

export const ITextEditor = DI.createInterface<ITextEditor>();

Expand All @@ -17,13 +18,16 @@ export interface ITextEditor extends WithDisposables {
open(document: TextDocument): void;
close(documentId: string): void;
focus(): void;
enableVimMode(): void;
disableVimMode(): void;
}

export class TextEditor extends ViewModelBase implements ITextEditor {
public monaco: monaco.editor.IStandaloneCodeEditor;
public position?: monaco.Position | null;
public active?: TextDocument | null;
private element: HTMLElement;
private vimMode?: VimMode;

private viewStates = new Map<string, monaco.editor.ICodeEditorViewState | null>();

Expand Down Expand Up @@ -90,6 +94,19 @@ export class TextEditor extends ViewModelBase implements ITextEditor {
setTimeout(() => this.monaco.focus(), 50);
}

public enableVimMode() {
if (!this.vimMode) {
this.vimMode = initVimMode(this.monaco, this.element);
}
}

public disableVimMode() {
if (this.vimMode) {
this.vimMode.dispose();
this.vimMode = undefined;
}
}

private ensureEditorInitialized() {
if (this.monaco) return;
this.initializeEditor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export enum ShortcutIds {
openExplorer = "shortcut.explorer.open",
openNamespaces = "shortcut.namespaces.open",
reloadWindow = "shortcut.window.reload",
vimMode = "shortcut.vim.mode",
vimSave = "shortcut.vim.save",
vimQuit = "shortcut.vim.quit",
vimSaveQuit = "shortcut.vim.savequit",
}

export const BuiltinShortcuts = [
Expand Down Expand Up @@ -170,4 +174,64 @@ export const BuiltinShortcuts = [
.captureDefaultKeyCombo()
.configurable()
.enabled(),

new Shortcut(ShortcutIds.vimMode, "Toggle Vim Mode")
.withKey(KeyCode.Escape)
.hasAction(ctx => {
const editor = ctx.container.get(ITextEditorService).active?.monaco;

if (!editor) return;

editor.focus();
editor.trigger("", "toggleVimMode", null);
})
.captureDefaultKeyCombo()
.configurable()
.enabled(),

new Shortcut(ShortcutIds.vimSave, "Vim Save")
.withKey(KeyCode.Colon)
.withKey(KeyCode.KeyW)
.hasAction(ctx => {
const editor = ctx.container.get(ITextEditorService).active?.monaco;

if (!editor) return;

editor.focus();
editor.trigger("", "vimSave", null);
})
.captureDefaultKeyCombo()
.configurable()
.enabled(),

new Shortcut(ShortcutIds.vimQuit, "Vim Quit")
.withKey(KeyCode.Colon)
.withKey(KeyCode.KeyQ)
.hasAction(ctx => {
const editor = ctx.container.get(ITextEditorService).active?.monaco;

if (!editor) return;

editor.focus();
editor.trigger("", "vimQuit", null);
})
.captureDefaultKeyCombo()
.configurable()
.enabled(),

new Shortcut(ShortcutIds.vimSaveQuit, "Vim Save and Quit")
.withKey(KeyCode.Colon)
.withKey(KeyCode.KeyW)
.withKey(KeyCode.KeyQ)
.hasAction(ctx => {
const editor = ctx.container.get(ITextEditorService).active?.monaco;

if (!editor) return;

editor.focus();
editor.trigger("", "vimSaveQuit", null);
})
.captureDefaultKeyCombo()
.configurable()
.enabled(),
];

0 comments on commit ae22459

Please sign in to comment.