Skip to content

Commit

Permalink
Make goLineStart[Smart] and goLineEnd work in multi-selection env
Browse files Browse the repository at this point in the history
Issue #778
  • Loading branch information
marijnh committed Jan 23, 2014
1 parent 25197f4 commit 246e27c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
49 changes: 33 additions & 16 deletions lib/codemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -2625,6 +2625,10 @@ window.CodeMirror = (function() {
else return pos;
}
function isLine(doc, l) {return l >= doc.first && l < doc.first + doc.size;}
function clipPosArray(doc, array) {
for (var out = [], i = 0; i < array.length; i++) out[i] = clipPos(doc, array[i]);
return out;
}

function extendRange(doc, range, pos, other) {
if (doc.cm && doc.cm.display.shift || doc.extend) {
Expand Down Expand Up @@ -3738,28 +3742,35 @@ window.CodeMirror = (function() {
goDocStart: function(cm) {cm.extendSelection(Pos(cm.firstLine(), 0));},
goDocEnd: function(cm) {cm.extendSelection(Pos(cm.lastLine()));},
goLineStart: function(cm) {
cm.extendSelection(lineStart(cm, cm.getCursor().line));
cm.extendSelectionsBy(function(range) { return lineStart(cm, range.head.line); });
},
goLineStartSmart: function(cm) {
var cur = cm.getCursor(), start = lineStart(cm, cur.line);
var line = cm.getLineHandle(start.line);
var order = getOrder(line);
if (!order || order[0].level == 0) {
var firstNonWS = Math.max(0, line.text.search(/\S/));
var inWS = cur.line == start.line && cur.ch <= firstNonWS && cur.ch;
cm.extendSelection(Pos(start.line, inWS ? 0 : firstNonWS));
} else cm.extendSelection(start);
cm.extendSelectionsBy(function(range) {
var start = lineStart(cm, range.head.line);
var line = cm.getLineHandle(start.line);
var order = getOrder(line);
if (!order || order[0].level == 0) {
var firstNonWS = Math.max(0, line.text.search(/\S/));
var inWS = range.head.line == start.line && range.head.ch <= firstNonWS && range.head.ch;
return Pos(start.line, inWS ? 0 : firstNonWS);
}
return start;
});
},
goLineEnd: function(cm) {
cm.extendSelection(lineEnd(cm, cm.getCursor().line));
cm.extendSelectionsBy(function(range) { return lineEnd(cm, range.head.line); });
},
goLineRight: function(cm) {
var top = cm.charCoords(cm.getCursor(), "div").top + 5;
cm.extendSelection(cm.coordsChar({left: cm.display.lineDiv.offsetWidth + 100, top: top}, "div"));
cm.extendSelectionsBy(function(range) {
var top = cm.charCoords(range.head, "div").top + 5;
return cm.coordsChar({left: cm.display.lineDiv.offsetWidth + 100, top: top}, "div");
});
},
goLineLeft: function(cm) {
var top = cm.charCoords(cm.getCursor(), "div").top + 5;
cm.extendSelection(cm.coordsChar({left: 0, top: top}, "div"));
cm.extendSelectionsBy(function(range) {
var top = cm.charCoords(range.head, "div").top + 5;
cm.extendSelection(cm.coordsChar({left: 0, top: top}, "div"));
});
},
goLineUp: function(cm) {cm.moveV(-1, "line");},
goLineDown: function(cm) {cm.moveV(1, "line");},
Expand Down Expand Up @@ -5179,6 +5190,12 @@ window.CodeMirror = (function() {
extendSelection: docOperation(function(from, to, bias) {
extendSelection(this, clipPos(this, from), to && clipPos(this, to), bias);
}),
extendSelections: docOperation(function(from, to, bias) {
extendSelections(this, clipPosArray(this, from), to && clipPosArray(this, to), bias);
}),
extendSelectionsBy: docOperation(function(f, bias) {
extendSelections(this, map(this.sel.ranges, f), null, bias);
}),
setSelections: docOperation(function(ranges, primary, bias) {
for (var i = 0, out = []; i < ranges.length; i++)
out[i] = new Range(clipPos(this, ranges[i].anchor),
Expand Down Expand Up @@ -5400,6 +5417,7 @@ window.CodeMirror = (function() {
n -= chunk.first;
while (!chunk.lines) {
for (var i = 0;; ++i) {
if (!chunk.children[i]) debugger;
var child = chunk.children[i], sz = child.chunkSize();
if (n < sz) { chunk = child; break; }
n -= sz;
Expand Down Expand Up @@ -5824,8 +5842,7 @@ window.CodeMirror = (function() {
}
function map(array, f) {
var out = [];
for (var i = 0; i < array.length; i++)
out[i] = f(array[i], i);
for (var i = 0; i < array.length; i++) out[i] = f(array[i], i);
return out;
}

Expand Down
14 changes: 14 additions & 0 deletions test/multi_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,18 @@
cm.execCommand("delWordAfter");
eq(cm.getValue(), "fo \nab ghi\n");
}, {value: "foo bar baz\nabc def ghi\n"});

testCM("goLineStart", function(cm) {
select(cm, Pos(0, 2), Pos(0, 3), Pos(1, 1));
cm.execCommand("goLineStart");
hasSelections(cm, 0, 0, 0, 0,
1, 0, 1, 0);
select(cm, Pos(1, 1), Pos(0, 1));
cm.setExtending(true);
cm.execCommand("goLineStart");
hasSelections(cm, 0, 1, 0, 0,
1, 1, 1, 0);
}, {value: "foo\nbar\nbaz"});


})();

0 comments on commit 246e27c

Please sign in to comment.