Skip to content

Commit 695d008

Browse files
committed
Use cursor stickiness in scrollPosIntoView
This way, only one char's left and right edges are used, instead of its (logical) neighbour's right edges. In bidi content, this avoids trying to scroll to two logically close, but visually far apart positions in some situations.
1 parent 00367ff commit 695d008

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/display/scrolling.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ export function maybeScrollWindow(cm, rect) {
3535
export function scrollPosIntoView(cm, pos, end, margin) {
3636
if (margin == null) margin = 0
3737
let rect
38+
if (!cm.options.lineWrapping && pos == end) {
39+
// Set pos and end to the cursor positions around the character pos sticks to
40+
// If pos.sticky == "before", that is around pos.ch - 1, otherwise around pos.ch
41+
// If pos == Pos(_, 0, "before"), pos and end are unchanged
42+
pos = pos.ch ? Pos(pos.line, pos.sticky == "before" ? pos.ch - 1 : pos.ch, "after") : pos
43+
end = pos.sticky == "before" ? Pos(pos.line, pos.ch + 1, "before") : pos
44+
}
3845
for (let limit = 0; limit < 5; limit++) {
3946
let changed = false
4047
let coords = cursorCoords(cm, pos)
@@ -109,12 +116,8 @@ export function addToScrollTop(cm, top) {
109116
// shown.
110117
export function ensureCursorVisible(cm) {
111118
resolveScrollToPos(cm)
112-
let cur = cm.getCursor(), from = cur, to = cur
113-
if (!cm.options.lineWrapping) {
114-
from = cur.ch ? Pos(cur.line, cur.ch - 1) : cur
115-
to = Pos(cur.line, cur.ch + 1)
116-
}
117-
cm.curOp.scrollToPos = {from: from, to: to, margin: cm.options.cursorScrollMargin}
119+
let cur = cm.getCursor()
120+
cm.curOp.scrollToPos = {from: cur, to: cur, margin: cm.options.cursorScrollMargin}
118121
}
119122

120123
export function scrollToCoords(cm, x, y) {

test/scroll_test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,15 @@
112112
cm.scrollTo(null, 10);
113113
is(cm.getScrollInfo().top < 5);
114114
}, {lineNumbers: true});
115+
116+
testCM("bidi_ensureCursorVisible", function(cm) {
117+
cm.setValue("<dd>وضع الاستخدام. عندما لا تعطى، وهذا الافتراضي إلى الطريقة الاولى\n");
118+
cm.execCommand("goLineStart");
119+
eq(cm.getScrollInfo().left, 0);
120+
cm.execCommand("goCharRight");
121+
cm.execCommand("goCharRight");
122+
cm.execCommand("goCharRight");
123+
eqCursorPos(cm.getCursor(), Pos(0, 3, "before"));
124+
eq(cm.getScrollInfo().left, 0);
125+
}, {lineWrapping: false});
115126
})();

0 commit comments

Comments
 (0)