Skip to content

Commit e705827

Browse files
author
Zef Hemel
committed
Merge pull request #951 from ajaxorg/vim_improvements
Make margin preservation async in vim mode
2 parents b20e476 + 1f981c3 commit e705827

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

client/ext/vim/commands.js

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ var toggleCase = function toggleCase(ch) {
4747
return ch.toUpperCase();
4848
};
4949

50+
var ensureScrollMargin = function(editor) {
51+
setTimeout(function() {
52+
var curPos = editor.getCursorPosition().row;
53+
var topRow = editor.renderer.layerConfig.firstRow;
54+
var linesToBottom = editor.renderer.layerConfig.lastRow - curPos;
55+
var linesToTop = curPos - topRow;
56+
57+
if (linesToBottom >= 0 && linesToBottom < HMARGIN) {
58+
editor.scrollToRow(topRow + (HMARGIN - linesToBottom));
59+
}
60+
else if (linesToTop >= 0 && linesToTop < HMARGIN) {
61+
editor.scrollToRow(topRow - (HMARGIN - linesToTop));
62+
}
63+
}, 20); // Delay introduced to ensure scroll after async find operation.
64+
};
65+
5066
var actions = {
5167
"z": {
5268
param: true,
@@ -86,12 +102,7 @@ var actions = {
86102
fn: function(editor, range, count, param) {
87103
editor.selection.selectWord();
88104
editor.findNext();
89-
var bottomRow = editor.renderer.getLastFullyVisibleRow();
90-
var marginLines = bottomRow - editor.getCursorPosition().row;
91-
if (marginLines < HMARGIN) {
92-
var scrollTopRow = editor.renderer.getScrollTopRow();
93-
editor.scrollToRow(scrollTopRow + (HMARGIN - marginLines));
94-
}
105+
ensureScrollMargin(editor);
95106
var cursor = editor.selection.getCursor();
96107
range = editor.session.getWordRange(cursor.row, cursor.column);
97108
editor.selection.setSelectionRange(range, true);
@@ -101,12 +112,7 @@ var actions = {
101112
fn: function(editor, range, count, param) {
102113
editor.selection.selectWord();
103114
editor.findPrevious();
104-
var topRow = editor.renderer.getFirstVisibleRow();
105-
var marginLines = editor.getCursorPosition().row - topRow;
106-
if (marginLines < HMARGIN) {
107-
var scrollTopRow = editor.renderer.getScrollTopRow();
108-
editor.scrollToRow(scrollTopRow - (HMARGIN - marginLines));
109-
}
115+
ensureScrollMargin(editor);
110116
var cursor = editor.selection.getCursor();
111117
range = editor.session.getWordRange(cursor.row, cursor.column);
112118
editor.selection.setSelectionRange(range, true);
@@ -118,12 +124,8 @@ var actions = {
118124
options.backwards = false;
119125

120126
editor.findNext(options);
121-
var bottomRow = editor.renderer.getLastFullyVisibleRow();
122-
var marginLines = bottomRow - editor.getCursorPosition().row;
123-
if (marginLines < HMARGIN) {
124-
var scrollTopRow = editor.renderer.getScrollTopRow();
125-
editor.scrollToRow(scrollTopRow + (HMARGIN - marginLines));
126-
}
127+
128+
ensureScrollMargin(editor);
127129
editor.selection.clearSelection();
128130
}
129131
},
@@ -134,12 +136,7 @@ var actions = {
134136

135137
editor.navigateWordLeft();
136138
editor.findPrevious(options);
137-
var topRow = editor.renderer.getFirstVisibleRow();
138-
var marginLines = editor.getCursorPosition().row - topRow;
139-
if (marginLines < HMARGIN) {
140-
var scrollTopRow = editor.renderer.getScrollTopRow();
141-
editor.scrollToRow(scrollTopRow - (HMARGIN - marginLines));
142-
}
139+
ensureScrollMargin(editor);
143140
editor.selection.clearSelection();
144141
}
145142
},

0 commit comments

Comments
 (0)