diff --git a/src/cm_adapter.ts b/src/cm_adapter.ts index 391f784..62cf1be 100644 --- a/src/cm_adapter.ts +++ b/src/cm_adapter.ts @@ -5,6 +5,7 @@ import { RegExpCursor, setSearchQuery, SearchQuery } from "@codemirror/search" import { insertNewlineAndIndent, indentMore, indentLess, indentSelection, cursorCharLeft, undo, redo, cursorLineBoundaryBackward, cursorLineBoundaryForward, cursorCharBackward, + toggleLineComment } from "@codemirror/commands" import {vimState, CM5RangeInterface} from "./types" @@ -135,6 +136,7 @@ export class CodeMirror { static Pos = Pos; static StringStream = StringStream as unknown as StringStream & { new(_: string): StringStream } static commands = { + toggleLineComment: function (cm: CodeMirror) { toggleLineComment(cm.cm6) }, cursorCharLeft: function (cm: CodeMirror) { cursorCharLeft(cm.cm6); }, redo: function (cm: CodeMirror) { runHistoryCommand(cm, false); }, undo: function (cm: CodeMirror) { runHistoryCommand(cm, true); }, @@ -427,7 +429,7 @@ export class CodeMirror { }; execCommand(name: string) { - if (name == "indentAuto") CodeMirror.commands.indentAuto(this); + if (CodeMirror.commands.hasOwnProperty(name)) CodeMirror.commands[name](this); else if (name == "goLineLeft") cursorLineBoundaryBackward(this.cm6); else if (name == "goLineRight") { cursorLineBoundaryForward(this.cm6); diff --git a/src/vim.js b/src/vim.js index 237858b..dee70d1 100644 --- a/src/vim.js +++ b/src/vim.js @@ -175,6 +175,7 @@ export function initVim(CM) { { keys: 'g~', type: 'operator', operator: 'changeCase' }, { keys: 'gu', type: 'operator', operator: 'changeCase', operatorArgs: {toLower: true}, isEdit: true }, { keys: 'gU', type: 'operator', operator: 'changeCase', operatorArgs: {toLower: false}, isEdit: true }, + { keys: 'gc', type: 'operator', operator: 'toggleComment', isEdit: true }, { keys: 'n', type: 'motion', motion: 'findNext', motionArgs: { forward: true, toJumplist: true }}, { keys: 'N', type: 'motion', motion: 'findNext', motionArgs: { forward: false, toJumplist: true }}, { keys: 'gn', type: 'motion', motion: 'findAndSelectNextInclusive', motionArgs: { forward: true }}, @@ -2861,6 +2862,10 @@ export function initVim(CM) { if (endRow > from && operatorArgs.linewise) endRow--; return operatorArgs.keepCursor ? oldAnchor : new Pos(endRow, 0); }, + toggleComment: function(cm, _args, ranges, oldAnchor, newHead) { + cm.execCommand("toggleLineComment"); + return newHead; + }, changeCase: function(cm, args, ranges, oldAnchor, newHead) { var selections = cm.getSelections(); var swapped = [];