Skip to content

Commit 8562f94

Browse files
authored
fix: Fix vim keybindings scroll to the selected line (#4980)
* Fix vim keybindings scroll to the selected line * Test added for vim gotoline * Fix vim keybindings scroll to the selected line
1 parent 09505f3 commit 8562f94

File tree

2 files changed

+48
-29
lines changed

2 files changed

+48
-29
lines changed

src/keyboard/vim.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@
205205
var result = fn();
206206
if (this.ace.curOp && this.ace.curOp.command.name == "vim") {
207207
if (this.state.dialog)
208-
this.ace.curOp.command.scrollIntoView = false;
208+
this.ace.curOp.command.scrollIntoView = this.ace.curOp.vimDialogScroll;
209209
this.ace.endOperation();
210210
if (!curOp.cursorActivity && !curOp.lastChange && prevOp)
211211
this.ace.prevOp = prevOp;
@@ -2521,6 +2521,8 @@ domLib.importCssString(`.normal-mode .ace_cursor{
25212521
cursorIsBefore(newAnchor, newHead) ? newHead
25222522
: newAnchor);
25232523
} else if (!operator) {
2524+
if (cm.ace.curOp)
2525+
cm.ace.curOp.vimDialogScroll = "center-animate"; // ace_patch
25242526
newHead = clipCursorToContent(cm, newHead, oldHead);
25252527
cm.setCursor(newHead.line, newHead.ch);
25262528
}

src/keyboard/vim_ace_test.js

+45-28
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ function applyEvent(data) {
8686
editor.resize(true);
8787
}
8888

89+
function scrollTop() {
90+
editor.endOperation();
91+
editor.renderer.$loop._flush();
92+
return editor.renderer.scrollTop / editor.renderer.lineHeight;
93+
}
94+
8995
module.exports = {
9096
setUp: function() {
9197
if (!editor) {
@@ -119,7 +125,7 @@ module.exports = {
119125
{ _: "keydown", range: [12,12], value: "\thello world\n\n", key: { code: "ControlLeft", key: "Control", keyCode: 17}, modifier: "ctrl-"},
120126
{ _: "keydown", range: [12,12], value: "\thello world\n\n", key: { code: "AltLeft", key: "Alt", keyCode: 18}, modifier: "ctrl-alt-"},
121127
{ _: "keydown", range: [6,11], value: "hello world\n\n", key: { code: "KeyL", key: "fi", keyCode: 76}, modifier: "ctrl-alt-"},
122-
128+
123129
{ _: "keydown", range: [6,11], value: "hello world\n\n", key: { code: "KeyC", key: "c", keyCode: 67}},
124130
{ _: "input", range: [7,7], value: "hello c\n\n"},
125131
{ _: "keydown", range: [7,7], value: "hello c\n\n", key: { code: "KeyX", key: "x", keyCode: 88}},
@@ -136,10 +142,10 @@ module.exports = {
136142
},
137143
{ _: "keydown", range: [7,7], value: "hello ^x\n\n", key: { code: "Escape", key: "Escape", keyCode: 27}},
138144
{ _: "keydown", key: { code: "KeyH", key: "˛", keyCode: 72}, modifier: "ctrl-alt-"},
139-
145+
140146
{ _: "keydown", range: [1,6], value: "\thello x\n\n", key: { code: "AltRight", key: "Alt", keyCode: 18}, modifier: "alt-"},
141147
{ _: "keydown", range: [1,6], value: "\thello x\n\n", key: { code: "Digit4", key: "$", keyCode: 52}, modifier: "alt-"},
142-
148+
143149
{ _: "input", range: [2,2], value: "\t$ x\n\n"},
144150
function() {
145151
testSelection(editor, [[1,5,1,8], [0,4,0,7]]);
@@ -149,7 +155,7 @@ module.exports = {
149155
testSelection(editor, [[1,7],[0,6]]);
150156
},
151157
{ _: "keydown", key: { code: "Escape", key: "Escape", keyCode: 27}},
152-
158+
153159
].forEach(function(data) {
154160
applyEvent(data);
155161
});
@@ -162,7 +168,7 @@ module.exports = {
162168
{ _: "keydown", range: [12,12], value: "\thello world\n\n", key: { code: "ControlLeft", key: "Control", keyCode: 17}, modifier: "ctrl-"},
163169
{ _: "keydown", range: [12,12], value: "\thello world\n\n", key: { code: "AltLeft", key: "Alt", keyCode: 18}, modifier: "ctrl-alt-"},
164170
{ _: "keydown", range: [6,11], value: "hello world\n\n", key: { code: "KeyL", key: "fi", keyCode: 76}, modifier: "ctrl-alt-"},
165-
171+
166172
{ _: "keydown", range: [6,11], value: "hello world\n\n", key: { code: "KeyC", key: "c", keyCode: 67}},
167173
{ _: "input", range: [7,7], value: "hello c\n\n"},
168174
{ _: "keydown", range: [7,7], value: "hello c\n\n", key: { code: "KeyX", key: "x", keyCode: 88}},
@@ -179,10 +185,10 @@ module.exports = {
179185
},
180186
{ _: "keydown", range: [7,7], value: "hello ^x\n\n", key: { code: "Escape", key: "Escape", keyCode: 27}},
181187
{ _: "keydown", key: { code: "KeyH", key: "˛", keyCode: 72}, modifier: "ctrl-alt-"},
182-
188+
183189
{ _: "keydown", range: [1,6], value: "\thello x\n\n", key: { code: "AltRight", key: "Alt", keyCode: 18}, modifier: "alt-"},
184190
{ _: "keydown", range: [1,6], value: "\thello x\n\n", key: { code: "Digit4", key: "$", keyCode: 52}, modifier: "alt-"},
185-
191+
186192
{ _: "input", range: [2,2], value: "\t$ x\n\n"},
187193
function() {
188194
testSelection(editor, [[1,5,1,8], [0,4,0,7]]);
@@ -192,7 +198,7 @@ module.exports = {
192198
testSelection(editor, [[1,7],[0,6]]);
193199
},
194200
{ _: "keydown", key: { code: "Escape", key: "Escape", keyCode: 27}},
195-
201+
196202
].forEach(function(data) {
197203
applyEvent(data);
198204
});
@@ -206,15 +212,15 @@ module.exports = {
206212
{ _: "keyup", range: [1,1], value: "V\n\n", key: { code: "KeyV", key: "V", keyCode: 86}, modifier: "shift-"},
207213
{ _: "keyup", range: [1,1], value: "V\n\n", key: { code: "ShiftLeft", key: "Shift", keyCode: 16}},
208214
{ _: "keydown", range: [1,1], value: "V\n\n", key: { code: "KeyK", key: "k", keyCode: 75}},
209-
{ _: "keypress", range: [1,1], value: "V\n\n", key: { code: "KeyK", key: "k", keyCode: 107}},
215+
{ _: "keypress", range: [1,1], value: "V\n\n", key: { code: "KeyK", key: "k", keyCode: 107}},
210216
{ _: "input", range: [2,2], value: "Vk\n\n"},
211217
{ _: "keyup", range: [2,2], value: "Vk\n\n", key: { code: "KeyK", key: "k", keyCode: 75}},
212218
{ _: "keydown", range: [2,2], value: "Vk\n\n", key: { code: "KeyC", key: "c", keyCode: 67}},
213-
{ _: "keypress", range: [2,2], value: "Vk\n\n", key: { code: "KeyC", key: "c", keyCode: 99}},
219+
{ _: "keypress", range: [2,2], value: "Vk\n\n", key: { code: "KeyC", key: "c", keyCode: 99}},
214220
{ _: "input", range: [3,3], value: "Vkc\n\n"},
215221
{ _: "keyup", range: [3,3], value: "Vkc\n\n", key: { code: "KeyC", key: "c", keyCode: 67}},
216222
{ _: "keydown", range: [3,3], value: "Vkc\n\n", key: { code: "KeyO", key: "o", keyCode: 79}},
217-
{ _: "keypress", range: [3,3], value: "Vkc\n\n", key: { code: "KeyO", key: "o", keyCode: 111}},
223+
{ _: "keypress", range: [3,3], value: "Vkc\n\n", key: { code: "KeyO", key: "o", keyCode: 111}},
218224
{ _: "input", range: [4,4], value: "Vkco\n\n"},
219225
{ _: "keyup", range: [4,4], value: "Vkco\n\n", key: { code: "KeyO", key: "o", keyCode: 79}},
220226
function() {
@@ -224,12 +230,12 @@ module.exports = {
224230
{ _: "keydown", range: [0,0], value: "ozzz\n\n", key: { code: "Escape", key: "Escape", keyCode: 27}},
225231
{ _: "keyup", range: [0,0], value: "ozzz\n\n", key: { code: "Escape", key: "Escape", keyCode: 27}},
226232
{ _: "keydown", range: [0,0], value: "ozzz\n\n", key: { code: "ControlLeft", key: "Control", keyCode: 17}, modifier: "ctrl-"},
227-
233+
228234
{ _: "keydown", range: [0,1], value: "ozzz\n\n", key: { code: "KeyV", key: "v", keyCode: 86}, modifier: "ctrl-"},
229235
{ _: "select", range: [0,1], value: "ozzz\n\n"},
230236
{ _: "keyup", range: [0,1], value: "ozzz\n\n", key: { code: "KeyV", key: "v", keyCode: 86}, modifier: "ctrl-"},
231237
{ _: "keyup", range: [0,1], value: "ozzz\n\n", key: { code: "ControlLeft", key: "Control", keyCode: 17}},
232-
238+
233239
{ _: "keydown", range: [0,1], value: "ccc\n\n", key: { code: "ArrowDown", key: "ArrowDown", keyCode: 40}},
234240
{ _: "select", range: [0,1], value: "ccc\n\n"},
235241
{ _: "keyup", range: [0,1], value: "ccc\n\n", key: { code: "ArrowDown", key: "ArrowDown", keyCode: 40}},
@@ -239,7 +245,7 @@ module.exports = {
239245
},
240246
{ _: "keydown", range: [0,1], value: "ccc\n\n", key: { code: "Period", key: ".", keyCode: 190}},
241247
{ _: "keypress", range: [0,1], value: "ccc\n\n", key: { code: "Period", key: ".", keyCode: 46}},
242-
248+
243249
{ _: "input", range: [1,1], value: ".cc\n\n"},
244250
{ _: "keyup", range: [1,1], value: ".cc\n\n", key: { code: "Period", key: ".", keyCode: 190}},
245251
function() {
@@ -252,8 +258,8 @@ module.exports = {
252258
},
253259
"test vim gq": function() {
254260
editor.setValue(
255-
"1\n2\nhello world\n"
256-
+ "xxx ".repeat(20) + "\nyyy"
261+
"1\n2\nhello world\n"
262+
+ "xxx ".repeat(20) + "\nyyy"
257263
+ "\n\nnext\nparagraph"
258264
);
259265
editor.selection.moveTo(2,5)
@@ -298,25 +304,19 @@ module.exports = {
298304
var screenSize = editor.renderer.layerConfig.height / editor.renderer.lineHeight;
299305

300306
user.type("Escape", "gg");
301-
assert.equal(scollTop(), 0);
307+
assert.equal(scrollTop(), 0);
302308
user.type("/", "needle");
303-
assert.ok(scollTop() > 40 - screenSize);
309+
assert.ok(scrollTop() > 40 - screenSize);
304310
editor.endOperation();
305311
user.type("Escape");
306-
assert.equal(scollTop(), 0);
312+
assert.equal(scrollTop(), 0);
307313

308314
user.type("/", "needle", "Enter");
309-
assert.ok(scollTop() > 40 - screenSize);
310-
assert.ok(scollTop() < 40);
315+
assert.ok(scrollTop() > 40 - screenSize);
316+
assert.ok(scrollTop() < 40);
311317
user.type("6", "/", "more", "Enter");
312318
editor.endOperation();
313-
assert.ok(scollTop() > 40 + 16 - screenSize);
314-
315-
function scollTop() {
316-
editor.endOperation();
317-
editor.renderer.$loop._flush();
318-
return editor.renderer.scrollTop / editor.renderer.lineHeight;
319-
}
319+
assert.ok(scrollTop() > 40 + 16 - screenSize);
320320
},
321321
"test: vim normal mode brackets highlighting": function () {
322322
editor.setValue("{((hello, world))}");
@@ -345,6 +345,23 @@ module.exports = {
345345
}, isBackwards);
346346
assert.range(ranges[1], el.startRow, el.startColumn, el.endRow, el.endColumn);
347347
});
348+
},
349+
"test: gotoline": function () {
350+
editor.renderer.setOption("animatedScroll", false);
351+
editor.setValue(
352+
"very\nlong\n\ntext\n".repeat(20),
353+
-1
354+
);
355+
356+
editor.focus();
357+
var screenSize = editor.renderer.layerConfig.height / editor.renderer.lineHeight;
358+
359+
assert.equal(scrollTop(), 0);
360+
user.type(":30", "Enter");
361+
editor.endOperation();
362+
363+
assert.ok(scrollTop() > 30 - screenSize);
364+
assert.ok(scrollTop() < 30);
348365
}
349366
};
350367

0 commit comments

Comments
 (0)