Skip to content

Commit 931b0bd

Browse files
committed
Refactor vim mode naming
Rebind "o" to follow markdown editor's newline and continue behavior
1 parent ea41d19 commit 931b0bd

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

plug-api/syscalls/editor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,8 @@ export function vimEx(exCommand: string): Promise<any> {
478478
/**
479479
* Execute a vim config using the CodeMirror Vim Mode API
480480
*/
481-
export function vimConfig(): Promise<any> {
482-
return syscall("editor.vimConfig");
481+
export function configureVimMode(): Promise<any> {
482+
return syscall("editor.configureVimMode");
483483
}
484484

485485
// Document editor specific syscalls

plugs/editor/vim.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export async function loadVimConfig() {
4242
try {
4343
await editor.save();
4444
await editor.reloadConfigAndCommands();
45-
await editor.vimConfig();
45+
await editor.configureVimMode();
4646
} catch (e: any) {
4747
await editor.flashNotification(e.message, "error");
4848
}

web/editor_state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ export function createCommandKeyBindings(client: Client): KeyBinding[] {
307307

308308
// Track which keyboard shortcuts for which commands we've overridden, so we can skip them later
309309
const overriddenCommands = new Set<string>();
310-
// Keyboard shortcuts from SETTINGS take precedense
310+
// Keyboard shortcuts from SETTINGS take precedence
311311
if (client.config.has("shortcuts")) {
312312
for (const shortcut of client.config.get<Shortcut[]>("shortcuts", [])) {
313313
overriddenCommands.add(shortcut.command);

web/syscalls/editor.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from "@codemirror/language";
99
import {
1010
deleteLine,
11+
insertNewlineAndIndent,
1112
moveLineDown,
1213
moveLineUp,
1314
redo,
@@ -24,6 +25,8 @@ import type {
2425
import type { PageMeta, UploadFile } from "../../plug-api/types.ts";
2526
import { openSearchPanel } from "@codemirror/search";
2627
import { parseRef, type Ref } from "@silverbulletmd/silverbullet/lib/page_ref";
28+
import { insertNewlineContinueMarkup } from "@codemirror/lang-markdown";
29+
import { configureVimMode } from "../../plug-api/syscalls/editor.ts";
2730

2831
export function editorSyscalls(client: Client): SysCallMapping {
2932
const syscalls: SysCallMapping = {
@@ -400,7 +403,16 @@ export function editorSyscalls(client: Client): SysCallMapping {
400403
throw new Error("Vim mode not active or not initialized.");
401404
}
402405
},
403-
"editor.vimConfig": () => {
406+
"editor.configureVimMode": () => {
407+
// Override the default "o" binding to be more intelligent and follow the markdown editor's behavior
408+
Vim.mapCommand("o", "action", "newline-continue-markup", {}, {});
409+
Vim.defineAction("newline-continue-markup", (cm) => {
410+
Vim.handleKey(cm, "A", "+input");
411+
insertNewlineContinueMarkup(client.editorView) ||
412+
insertNewlineAndIndent(client.editorView);
413+
});
414+
415+
// Load the config if any
404416
const config = client.config.get<VimConfig>("vim", {});
405417
if (config) {
406418
config.unmap?.forEach((binding) => {

0 commit comments

Comments
 (0)