Skip to content

Commit

Permalink
Ignore shift status in API-driven selection changes
Browse files Browse the repository at this point in the history
Holding shift and then moving the cursor through the API would cause a
'selection extension' to happen, instead of simply moving the
selection to its new state. Most visible when bindin shift-key combos
to some editing function.
  • Loading branch information
marijnh committed Apr 28, 2011
1 parent a3dbe9e commit d9ed70f
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions lib/codemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ var CodeMirror = (function() {
e.stop();
if (ld && +new Date - ld < 400) return selectLine(start.line);

setCursor(start.line, start.ch, false);
setCursor(start.line, start.ch, true);
// And then we have to see if it's a drag event, in which case
// the dragged-over text must be selected.
function end() {
Expand All @@ -231,7 +231,7 @@ var CodeMirror = (function() {
if (cur && !posEq(cur, last)) {
if (!focused) onFocus();
last = cur;
setSelection(start, cur);
setSelectionUser(start, cur);
updateInput = false;
var visible = visibleLines();
if (cur.line >= visible.to || cur.line < visible.from)
Expand All @@ -247,7 +247,7 @@ var CodeMirror = (function() {
var up = connect(document, "mouseup", operation(function(e) {
clearTimeout(going);
var cur = posFromMouse(e);
if (cur) setSelection(start, cur);
if (cur) setSelectionUser(start, cur);
e.stop();
end();
}), true);
Expand Down Expand Up @@ -820,17 +820,20 @@ var CodeMirror = (function() {
else cursor.style.display = "none";
}

function setSelectionUser(from, to) {
var sh = shiftSelecting && clipPos(shiftSelecting);
if (sh) {
if (posLess(sh, from)) from = sh;
else if (posLess(to, sh)) to = sh;
}
setSelection(from, to);
}
// Update the selection. Last two args are only used by
// updateLines, since they have to be expressed in the line
// numbers before the update.
function setSelection(from, to, oldFrom, oldTo) {
if (posEq(sel.from, from) && posEq(sel.to, to)) return;
var sh = shiftSelecting && clipPos(shiftSelecting);
if (posLess(to, from)) {var tmp = to; to = from; from = tmp;}
if (sh) {
if (posLess(sh, from)) from = sh;
else if (posLess(to, sh)) to = sh;
}

var startEq = posEq(sel.to, to), endEq = posEq(sel.from, from);
if (posEq(from, to)) sel.inverted = false;
Expand Down Expand Up @@ -865,9 +868,9 @@ var CodeMirror = (function() {
sel.from = from; sel.to = to;
selectionChanged = true;
}
function setCursor(line, ch) {
function setCursor(line, ch, user) {
var pos = clipPos({line: line, ch: ch || 0});
setSelection(pos, pos);
(user ? setSelectionUser : setSelection)(pos, pos);
}

function clipLine(n) {return Math.max(0, Math.min(n, lines.length-1));}
Expand All @@ -882,10 +885,10 @@ var CodeMirror = (function() {

function scrollPage(down) {
var linesPerPage = Math.floor(wrapper.clientHeight / lineHeight()), head = sel.inverted ? sel.from : sel.to;
setCursor(head.line + (Math.max(linesPerPage - 1, 1) * (down ? 1 : -1)), head.ch);
setCursor(head.line + (Math.max(linesPerPage - 1, 1) * (down ? 1 : -1)), head.ch, true);
}
function scrollEnd(top) {
setCursor(top ? 0 : lines.length - 1);
setCursor(top ? 0 : lines.length - 1, true);
}
function selectAll() {
var endLine = lines.length - 1;
Expand All @@ -896,10 +899,10 @@ var CodeMirror = (function() {
var start = pos.ch, end = pos.ch;
while (start > 0 && /\w/.test(line.charAt(start - 1))) --start;
while (end < line.length && /\w/.test(line.charAt(end))) ++end;
setSelection({line: pos.line, ch: start}, {line: pos.line, ch: end});
setSelectionUser({line: pos.line, ch: start}, {line: pos.line, ch: end});
}
function selectLine(line) {
setSelection({line: line, ch: 0}, {line: line, ch: lines[line].text.length});
setSelectionUser({line: line, ch: 0}, {line: line, ch: lines[line].text.length});
}
function handleEnter() {
replaceSelection("\n", "end");
Expand Down

0 comments on commit d9ed70f

Please sign in to comment.