From 695d00863086e25c73ff50e0f1045bdd3cb3417d Mon Sep 17 00:00:00 2001 From: Adrian Heine <mail@adrianheine.de> Date: Mon, 26 Jun 2017 14:15:01 +0200 Subject: [PATCH] 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. --- src/display/scrolling.js | 15 +++++++++------ test/scroll_test.js | 11 +++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/display/scrolling.js b/src/display/scrolling.js index 96192cab..e16cf9ec 100644 --- a/src/display/scrolling.js +++ b/src/display/scrolling.js @@ -35,6 +35,13 @@ export function maybeScrollWindow(cm, rect) { export function scrollPosIntoView(cm, pos, end, margin) { if (margin == null) margin = 0 let rect + if (!cm.options.lineWrapping && pos == end) { + // Set pos and end to the cursor positions around the character pos sticks to + // If pos.sticky == "before", that is around pos.ch - 1, otherwise around pos.ch + // If pos == Pos(_, 0, "before"), pos and end are unchanged + pos = pos.ch ? Pos(pos.line, pos.sticky == "before" ? pos.ch - 1 : pos.ch, "after") : pos + end = pos.sticky == "before" ? Pos(pos.line, pos.ch + 1, "before") : pos + } for (let limit = 0; limit < 5; limit++) { let changed = false let coords = cursorCoords(cm, pos) @@ -109,12 +116,8 @@ export function addToScrollTop(cm, top) { // shown. export function ensureCursorVisible(cm) { resolveScrollToPos(cm) - let cur = cm.getCursor(), from = cur, to = cur - if (!cm.options.lineWrapping) { - from = cur.ch ? Pos(cur.line, cur.ch - 1) : cur - to = Pos(cur.line, cur.ch + 1) - } - cm.curOp.scrollToPos = {from: from, to: to, margin: cm.options.cursorScrollMargin} + let cur = cm.getCursor() + cm.curOp.scrollToPos = {from: cur, to: cur, margin: cm.options.cursorScrollMargin} } export function scrollToCoords(cm, x, y) { diff --git a/test/scroll_test.js b/test/scroll_test.js index 55aac78e..d1d21900 100644 --- a/test/scroll_test.js +++ b/test/scroll_test.js @@ -112,4 +112,15 @@ cm.scrollTo(null, 10); is(cm.getScrollInfo().top < 5); }, {lineNumbers: true}); + + testCM("bidi_ensureCursorVisible", function(cm) { + cm.setValue("<dd>وضع الاستخدام. عندما لا تعطى، وهذا الاÙتراضي إلى الطريقة الاولى\n"); + cm.execCommand("goLineStart"); + eq(cm.getScrollInfo().left, 0); + cm.execCommand("goCharRight"); + cm.execCommand("goCharRight"); + cm.execCommand("goCharRight"); + eqCursorPos(cm.getCursor(), Pos(0, 3, "before")); + eq(cm.getScrollInfo().left, 0); + }, {lineWrapping: false}); })(); -- GitLab