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 = []; diff --git a/test/vim_test.js b/test/vim_test.js index 52a42ca..79c6464 100644 --- a/test/vim_test.js +++ b/test/vim_test.js @@ -1290,6 +1290,12 @@ testVim('s_visual_block', function(cm, vim, helpers) { eq('1hello{\n world\n', cm.getValue()); }, {value: '1234\n5678\nabcdefg\n'}); +testVim('gcc', function(cm, vim, helpers) { + helpers.doKeys('g', 'c', 'c'); + eq('// var line1\nline2\nline3', cm.getValue()); + helpers.doKeys('2', 'g', 'c', 'c'); + eq('// // var line1\n// line2\nline3', cm.getValue()); +}, { value: 'var line1\nline2\nline3', mode: ''}); // Test mode change event. It should only fire once per mode transition. testVim('on_mode_change', async function(cm, vim, helpers) { var modeHist = [];