Skip to content

Commit c269362

Browse files
committed
implement gcc
1 parent 71919db commit c269362

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

src/cm_adapter.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { RegExpCursor, setSearchQuery, SearchQuery } from "@codemirror/search"
55
import {
66
insertNewlineAndIndent, indentMore, indentLess, indentSelection, cursorCharLeft,
77
undo, redo, cursorLineBoundaryBackward, cursorLineBoundaryForward, cursorCharBackward,
8+
toggleLineComment
89
} from "@codemirror/commands"
910
import {vimState, CM5RangeInterface} from "./types"
1011

@@ -135,6 +136,7 @@ export class CodeMirror {
135136
static Pos = Pos;
136137
static StringStream = StringStream as unknown as StringStream & { new(_: string): StringStream }
137138
static commands = {
139+
toggleLineComment: function (cm: CodeMirror) { toggleLineComment(cm.cm6) },
138140
cursorCharLeft: function (cm: CodeMirror) { cursorCharLeft(cm.cm6); },
139141
redo: function (cm: CodeMirror) { runHistoryCommand(cm, false); },
140142
undo: function (cm: CodeMirror) { runHistoryCommand(cm, true); },
@@ -427,7 +429,7 @@ export class CodeMirror {
427429
};
428430

429431
execCommand(name: string) {
430-
if (name == "indentAuto") CodeMirror.commands.indentAuto(this);
432+
if (CodeMirror.commands.hasOwnProperty(name)) CodeMirror.commands[name](this);
431433
else if (name == "goLineLeft") cursorLineBoundaryBackward(this.cm6);
432434
else if (name == "goLineRight") {
433435
cursorLineBoundaryForward(this.cm6);

src/vim.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ export function initVim(CM) {
175175
{ keys: 'g~', type: 'operator', operator: 'changeCase' },
176176
{ keys: 'gu', type: 'operator', operator: 'changeCase', operatorArgs: {toLower: true}, isEdit: true },
177177
{ keys: 'gU', type: 'operator', operator: 'changeCase', operatorArgs: {toLower: false}, isEdit: true },
178+
{ keys: 'gc', type: 'operator', operator: 'toggleComment', isEdit: true },
178179
{ keys: 'n', type: 'motion', motion: 'findNext', motionArgs: { forward: true, toJumplist: true }},
179180
{ keys: 'N', type: 'motion', motion: 'findNext', motionArgs: { forward: false, toJumplist: true }},
180181
{ keys: 'gn', type: 'motion', motion: 'findAndSelectNextInclusive', motionArgs: { forward: true }},
@@ -2861,6 +2862,10 @@ export function initVim(CM) {
28612862
if (endRow > from && operatorArgs.linewise) endRow--;
28622863
return operatorArgs.keepCursor ? oldAnchor : new Pos(endRow, 0);
28632864
},
2865+
toggleComment: function(cm, _args, ranges, oldAnchor, newHead) {
2866+
cm.execCommand("toggleLineComment");
2867+
return newHead;
2868+
},
28642869
changeCase: function(cm, args, ranges, oldAnchor, newHead) {
28652870
var selections = cm.getSelections();
28662871
var swapped = [];

test/vim_test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,12 @@ testVim('s_visual_block', function(cm, vim, helpers) {
12901290
eq('1hello{\n world\n', cm.getValue());
12911291
}, {value: '1234\n5678\nabcdefg\n'});
12921292

1293+
testVim('gcc', function(cm, vim, helpers) {
1294+
helpers.doKeys('g', 'c', 'c');
1295+
eq('// var line1\nline2\nline3', cm.getValue());
1296+
helpers.doKeys('2', 'g', 'c', 'c');
1297+
eq('// // var line1\n// line2\nline3', cm.getValue());
1298+
}, { value: 'var line1\nline2\nline3', mode: ''});
12931299
// Test mode change event. It should only fire once per mode transition.
12941300
testVim('on_mode_change', async function(cm, vim, helpers) {
12951301
var modeHist = [];

0 commit comments

Comments
 (0)