fix copyLines* for multiple selections
This commit is contained in:
parent
852e0673ca
commit
3fa54ce97e
4 changed files with 159 additions and 79 deletions
|
|
@ -91,52 +91,57 @@ exports.addGlobals = function() {
|
||||||
window.EditSession = require("ace/edit_session").EditSession;
|
window.EditSession = require("ace/edit_session").EditSession;
|
||||||
window.MockRenderer = require("ace/test/mockrenderer").MockRenderer;
|
window.MockRenderer = require("ace/test/mockrenderer").MockRenderer;
|
||||||
window.EventEmitter = require("ace/lib/event_emitter").EventEmitter;
|
window.EventEmitter = require("ace/lib/event_emitter").EventEmitter;
|
||||||
|
|
||||||
|
window.getSelection = getSelection;
|
||||||
|
window.setSelection = setSelection;
|
||||||
|
window.testSelection = testSelection;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function getSelection(editor) {
|
||||||
|
var data = editor.multiSelect.toJSON();
|
||||||
|
if (!data.length) data = [data];
|
||||||
|
data = data.map(function(x) {
|
||||||
|
var a, c;
|
||||||
|
if (x.isBackwards) {
|
||||||
|
a = x.end;
|
||||||
|
c = x.start;
|
||||||
|
} else {
|
||||||
|
c = x.end;
|
||||||
|
a = x.start;
|
||||||
|
}
|
||||||
|
return Range.comparePoints(a, c)
|
||||||
|
? [a.row, a.column, c.row, c.column]
|
||||||
|
: [a.row, a.column];
|
||||||
|
});
|
||||||
|
return data.length > 1 ? data : data[0];
|
||||||
|
}
|
||||||
|
function setSelection(editor, data) {
|
||||||
|
if (typeof data[0] == "number")
|
||||||
|
data = [data];
|
||||||
|
editor.selection.fromJSON(data.map(function(x) {
|
||||||
|
var start = {row: x[0], column: x[1]};
|
||||||
|
var end = x.length == 2 ? start : {row: x[2], column: x[3]};
|
||||||
|
var isBackwards = Range.comparePoints(start, end) > 0;
|
||||||
|
return isBackwards ? {
|
||||||
|
start: end,
|
||||||
|
end: start,
|
||||||
|
isBackwards: true
|
||||||
|
} : {
|
||||||
|
start: start,
|
||||||
|
end: end,
|
||||||
|
isBackwards: true
|
||||||
|
};
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
function testSelection(editor, data) {
|
||||||
|
assert.equal(getSelection(editor) + "", data + "");
|
||||||
|
}
|
||||||
|
|
||||||
exports.recordTestCase = function() {
|
exports.recordTestCase = function() {
|
||||||
exports.addGlobals();
|
exports.addGlobals();
|
||||||
var editor = window.editor;
|
var editor = window.editor;
|
||||||
var testcase = window.testcase = [];
|
var testcase = window.testcase = [];
|
||||||
var assert;
|
var assert;
|
||||||
function getSelection(editor) {
|
|
||||||
var data = editor.multiSelect.toJSON();
|
|
||||||
if (!data.length) data = [data];
|
|
||||||
data = data.map(function(x) {
|
|
||||||
var a, c;
|
|
||||||
if (x.isBackwards) {
|
|
||||||
a = x.end;
|
|
||||||
c = x.start;
|
|
||||||
} else {
|
|
||||||
c = x.end;
|
|
||||||
a = x.start;
|
|
||||||
}
|
|
||||||
return Range.comparePoints(a, c)
|
|
||||||
? [a.row, a.column, c.row, c.column]
|
|
||||||
: [a.row, a.column];
|
|
||||||
});
|
|
||||||
return data.length > 1 ? data : data[0];
|
|
||||||
}
|
|
||||||
function setSelection(editor, data) {
|
|
||||||
if (typeof data[0] == "number")
|
|
||||||
data = [data];
|
|
||||||
editor.selection.fromJSON(data.map(function(x) {
|
|
||||||
var start = {row: x[0], column: x[1]};
|
|
||||||
var end = x.length == 2 ? start : {row: x[2], column: x[3]};
|
|
||||||
var isBackwards = Range.comparePoints(start, end) > 0;
|
|
||||||
return isBackwards ? {
|
|
||||||
start: end,
|
|
||||||
end: start,
|
|
||||||
isBackwards: true
|
|
||||||
} : {
|
|
||||||
start: start,
|
|
||||||
end: end,
|
|
||||||
isBackwards: true
|
|
||||||
};
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
function testSelection(editor, data) {
|
|
||||||
assert.equal(getSelection(editor) + "", data + "");
|
|
||||||
}
|
|
||||||
|
|
||||||
testcase.push({
|
testcase.push({
|
||||||
type: "setValue",
|
type: "setValue",
|
||||||
|
|
|
||||||
|
|
@ -257,7 +257,9 @@
|
||||||
<a href="http://ace.c9.io">
|
<a href="http://ace.c9.io">
|
||||||
<img id="ace-logo" src="doc/site/images/ace-logo.png" style="width: 134px;margin: 46px 0px 4px 66px;">
|
<img id="ace-logo" src="doc/site/images/ace-logo.png" style="width: 134px;margin: 46px 0px 4px 66px;">
|
||||||
</a>
|
</a>
|
||||||
|
<div><a target="_test" href="./lib/ace/test/tests.html"
|
||||||
|
style="color: whitesmoke; text-align: left; padding: 1em;">tests</a>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -1655,9 +1655,7 @@ var Editor = function(renderer, session) {
|
||||||
* @related EditSession.moveLinesUp
|
* @related EditSession.moveLinesUp
|
||||||
**/
|
**/
|
||||||
this.moveLinesDown = function() {
|
this.moveLinesDown = function() {
|
||||||
this.$moveLines(function(firstRow, lastRow) {
|
this.$moveLines(1, false);
|
||||||
return this.session.moveLinesDown(firstRow, lastRow);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -1666,9 +1664,7 @@ var Editor = function(renderer, session) {
|
||||||
* @related EditSession.moveLinesDown
|
* @related EditSession.moveLinesDown
|
||||||
**/
|
**/
|
||||||
this.moveLinesUp = function() {
|
this.moveLinesUp = function() {
|
||||||
this.$moveLines(function(firstRow, lastRow) {
|
this.$moveLines(-1, false);
|
||||||
return this.session.moveLinesUp(firstRow, lastRow);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -1692,10 +1688,7 @@ var Editor = function(renderer, session) {
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
this.copyLinesUp = function() {
|
this.copyLinesUp = function() {
|
||||||
this.$moveLines(function(firstRow, lastRow) {
|
this.$moveLines(-1, true);
|
||||||
this.session.duplicateLines(firstRow, lastRow);
|
|
||||||
return 0;
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -1705,51 +1698,61 @@ var Editor = function(renderer, session) {
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
this.copyLinesDown = function() {
|
this.copyLinesDown = function() {
|
||||||
this.$moveLines(function(firstRow, lastRow) {
|
this.$moveLines(1, true);
|
||||||
return this.session.duplicateLines(firstRow, lastRow);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes a specific function, which can be anything that manipulates selected lines, such as copying them, duplicating them, or shifting them.
|
* for internal use
|
||||||
* @param {Function} mover A method to call on each selected row
|
* @ignore
|
||||||
*
|
|
||||||
*
|
*
|
||||||
**/
|
**/
|
||||||
this.$moveLines = function(mover) {
|
this.$moveLines = function(dir, copy) {
|
||||||
|
var rows, moved;
|
||||||
var selection = this.selection;
|
var selection = this.selection;
|
||||||
if (!selection.inMultiSelectMode || this.inVirtualSelectionMode) {
|
if (!selection.inMultiSelectMode || this.inVirtualSelectionMode) {
|
||||||
var range = selection.toOrientedRange();
|
var range = selection.toOrientedRange();
|
||||||
var rows = this.$getSelectedRows(range);
|
rows = this.$getSelectedRows(range);
|
||||||
var linesMoved = mover.call(this, rows.first, rows.last);
|
moved = this.session.$moveLines(rows.first, rows.last, copy ? 0 : dir);
|
||||||
range.moveBy(linesMoved, 0);
|
if (copy && dir == -1) moved = 0;
|
||||||
|
range.moveBy(moved, 0);
|
||||||
selection.fromOrientedRange(range);
|
selection.fromOrientedRange(range);
|
||||||
} else {
|
} else {
|
||||||
var ranges = selection.rangeList.ranges;
|
var ranges = selection.rangeList.ranges;
|
||||||
selection.rangeList.detach(this.session);
|
selection.rangeList.detach(this.session);
|
||||||
|
this.inVirtualSelectionMode = true;
|
||||||
|
|
||||||
for (var i = ranges.length; i--; ) {
|
var diff = 0;
|
||||||
|
var totalDiff = 0;
|
||||||
|
var l = ranges.length;
|
||||||
|
for (var i = 0; i < l; i++) {
|
||||||
var rangeIndex = i;
|
var rangeIndex = i;
|
||||||
var rows = ranges[i].collapseRows();
|
ranges[i].moveBy(diff, 0);
|
||||||
var last = rows.end.row;
|
rows = this.$getSelectedRows(ranges[i]);
|
||||||
var first = rows.start.row;
|
var first = rows.first;
|
||||||
while (i--) {
|
var last = rows.last;
|
||||||
rows = ranges[i].collapseRows();
|
while (++i < l) {
|
||||||
if (first - rows.end.row <= 1)
|
if (totalDiff) ranges[i].moveBy(totalDiff, 0);
|
||||||
first = rows.end.row;
|
var subRows = this.$getSelectedRows(ranges[i]);
|
||||||
else
|
if (copy && subRows.first != last)
|
||||||
break;
|
break;
|
||||||
|
else if (!copy && subRows.first > last + 1)
|
||||||
|
break;
|
||||||
|
last = subRows.last;
|
||||||
}
|
}
|
||||||
i++;
|
i--;
|
||||||
|
diff = this.session.$moveLines(first, last, copy ? 0 : dir);
|
||||||
var linesMoved = mover.call(this, first, last);
|
if (copy && dir == -1) rangeIndex = i + 1;
|
||||||
while (rangeIndex >= i) {
|
while (rangeIndex <= i) {
|
||||||
ranges[rangeIndex].moveBy(linesMoved, 0);
|
ranges[rangeIndex].moveBy(diff, 0);
|
||||||
rangeIndex--;
|
rangeIndex++;
|
||||||
}
|
}
|
||||||
|
if (!copy) diff = 0;
|
||||||
|
totalDiff += diff;
|
||||||
}
|
}
|
||||||
|
|
||||||
selection.fromOrientedRange(selection.ranges[0]);
|
selection.fromOrientedRange(selection.ranges[0]);
|
||||||
selection.rangeList.attach(this.session);
|
selection.rangeList.attach(this.session);
|
||||||
|
this.inVirtualSelectionMode = false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -1762,8 +1765,8 @@ var Editor = function(renderer, session) {
|
||||||
*
|
*
|
||||||
* @returns {Object}
|
* @returns {Object}
|
||||||
**/
|
**/
|
||||||
this.$getSelectedRows = function() {
|
this.$getSelectedRows = function(range) {
|
||||||
var range = this.getSelectionRange().collapseRows();
|
range = (range || this.getSelectionRange()).collapseRows();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
first: this.session.getRowFoldStart(range.start.row),
|
first: this.session.getRowFoldStart(range.start.row),
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,45 @@ var exec = function(name, times, args) {
|
||||||
var testRanges = function(str) {
|
var testRanges = function(str) {
|
||||||
assert.equal(editor.selection.getAllRanges() + "", str + "");
|
assert.equal(editor.selection.getAllRanges() + "", str + "");
|
||||||
};
|
};
|
||||||
|
function getSelection(editor) {
|
||||||
|
var data = editor.multiSelect.toJSON();
|
||||||
|
if (!data.length) data = [data];
|
||||||
|
data = data.map(function(x) {
|
||||||
|
var a, c;
|
||||||
|
if (x.isBackwards) {
|
||||||
|
a = x.end;
|
||||||
|
c = x.start;
|
||||||
|
} else {
|
||||||
|
c = x.end;
|
||||||
|
a = x.start;
|
||||||
|
}
|
||||||
|
return Range.comparePoints(a, c)
|
||||||
|
? [a.row, a.column, c.row, c.column]
|
||||||
|
: [a.row, a.column];
|
||||||
|
});
|
||||||
|
return data.length > 1 ? data : data[0];
|
||||||
|
}
|
||||||
|
function testSelection(editor, data) {
|
||||||
|
assert.equal(getSelection(editor) + "", data + "");
|
||||||
|
}
|
||||||
|
function setSelection(editor, data) {
|
||||||
|
if (typeof data[0] == "number")
|
||||||
|
data = [data];
|
||||||
|
editor.selection.fromJSON(data.map(function(x) {
|
||||||
|
var start = {row: x[0], column: x[1]};
|
||||||
|
var end = x.length == 2 ? start : {row: x[2], column: x[3]};
|
||||||
|
var isBackwards = Range.comparePoints(start, end) > 0;
|
||||||
|
return isBackwards ? {
|
||||||
|
start: end,
|
||||||
|
end: start,
|
||||||
|
isBackwards: true
|
||||||
|
} : {
|
||||||
|
start: start,
|
||||||
|
end: end,
|
||||||
|
isBackwards: true
|
||||||
|
};
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
||||||
|
|
@ -168,6 +207,37 @@ module.exports = {
|
||||||
assert.equal('l1foo\nl2foo', editor.getValue());
|
assert.equal('l1foo\nl2foo', editor.getValue());
|
||||||
},
|
},
|
||||||
|
|
||||||
|
"test multiselect move lines": function() {
|
||||||
|
editor = new Editor(new MockRenderer());
|
||||||
|
|
||||||
|
editor.setValue("l1\nl2\nl3\nl4", -1);
|
||||||
|
setSelection(editor, [[0,2],[1,2],[2,2],[3,2]]);
|
||||||
|
|
||||||
|
exec("copylinesdown");
|
||||||
|
assert.equal(editor.getValue(),"l1\nl1\nl2\nl2\nl3\nl3\nl4\nl4");
|
||||||
|
testSelection(editor, [[1,2],[3,2],[5,2],[7,2]]);
|
||||||
|
exec("copylinesup");
|
||||||
|
assert.equal(editor.getValue(),"l1\nl1\nl1\nl2\nl2\nl2\nl3\nl3\nl3\nl4\nl4\nl4");
|
||||||
|
testSelection(editor, [[1,2],[4,2],[7,2],[10,2]]);
|
||||||
|
exec("removeline");
|
||||||
|
assert.equal(editor.getValue(),"l1\nl1\nl2\nl2\nl3\nl3\nl4\nl4");
|
||||||
|
testSelection(editor, [[1,0],[3,0],[5,0],[7,0]]);
|
||||||
|
|
||||||
|
setSelection(editor, [[1,2],[1,0,1,1],[3,0,3,1],[5,0,5,1],[7,0,7,1]]);
|
||||||
|
exec("copylinesdown");
|
||||||
|
exec("copylinesup");
|
||||||
|
assert.equal(editor.getValue(),"l1\nl1\nl1\nl1\nl2\nl2\nl2\nl2\nl3\nl3\nl3\nl3\nl4\nl4\nl4\nl4");
|
||||||
|
testSelection(editor, [[2,2],[2,0,2,1],[6,0,6,1],[10,0,10,1],[14,0,14,1]]);
|
||||||
|
|
||||||
|
exec("movelinesdown", 12);
|
||||||
|
assert.equal(editor.getValue(),"l1\nl1\nl1\nl2\nl2\nl2\nl3\nl3\nl3\nl4\nl4\nl4\nl1\nl2\nl3\nl4");
|
||||||
|
testSelection(editor, [[12,2],[12,0,12,1],[13,0,13,1],[14,0,14,1],[15,0,15,1]]);
|
||||||
|
|
||||||
|
exec("movelinesup", 12);
|
||||||
|
assert.equal(editor.getValue(),"l1\nl2\nl3\nl4\nl1\nl1\nl1\nl2\nl2\nl2\nl3\nl3\nl3\nl4\nl4\nl4");
|
||||||
|
testSelection(editor, [[0,2],[0,0,0,1],[1,0,1,1],[2,0,2,1],[3,0,3,1]]);
|
||||||
|
},
|
||||||
|
|
||||||
"test multiselect fromJSON/toJSON": function() {
|
"test multiselect fromJSON/toJSON": function() {
|
||||||
var doc = new EditSession(["l1", "l2"]);
|
var doc = new EditSession(["l1", "l2"]);
|
||||||
editor = new Editor(new MockRenderer(), doc);
|
editor = new Editor(new MockRenderer(), doc);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue