Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit bcab0a2

Browse files
author
Narciso Jaramillo
committed
Merge pull request #7312 from adobe/randy/issue-7286
For add new selection, use display column
2 parents df26beb + 8c8a627 commit bcab0a2

3 files changed

Lines changed: 62 additions & 1 deletion

File tree

src/editor/Editor.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,34 @@ define(function (require, exports, module) {
905905
return column;
906906
};
907907

908+
/**
909+
* Returns the string-based pos for a given display column (zero-based) in given line. Differs from column
910+
* only when the line contains preceding \t chars. Result depends on the current tab size setting.
911+
* @param {number} lineNum Line number
912+
* @param {number} column Display column number
913+
* @return {number}
914+
*/
915+
Editor.prototype.getCharIndexForColumn = function (lineNum, column) {
916+
var line = this._codeMirror.getLine(lineNum),
917+
tabSize = null,
918+
iCol = 0,
919+
i;
920+
921+
for (i = 0; iCol < column; i++) {
922+
if (line[i] === '\t') {
923+
if (tabSize === null) {
924+
tabSize = Editor.getTabSize();
925+
}
926+
if (tabSize > 0) {
927+
iCol += (tabSize - (iCol % tabSize));
928+
}
929+
} else {
930+
iCol++;
931+
}
932+
}
933+
return i;
934+
};
935+
908936
/**
909937
* Sets the cursor position within the editor. Removes any selection.
910938
* @param {number} line The 0 based line number.

src/editor/EditorCommandHandlers.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -979,14 +979,22 @@ define(function (require, exports, module) {
979979
var origSels = editor.getSelections(),
980980
newSels = [];
981981
_.each(origSels, function (sel) {
982-
var pos;
982+
var pos, colOffset;
983983
if ((dir === -1 && sel.start.line > editor.getFirstVisibleLine()) || (dir === 1 && sel.end.line < editor.getLastVisibleLine())) {
984984
// Add a new cursor on the next line up/down. It's okay if it overlaps another selection, because CM
985985
// will take care of throwing it away in that case. It will also take care of clipping the char position
986986
// to the end of the new line if the line is shorter.
987987
pos = _.clone(dir === -1 ? sel.start : sel.end);
988+
989+
// get sel column of current selection
990+
colOffset = editor.getColOffset(pos);
991+
988992
pos.line += dir;
989993

994+
// translate column to ch in line of new selection
995+
pos.ch = editor.getCharIndexForColumn(pos.line, colOffset);
996+
997+
990998
// If this is the primary selection, we want the new cursor we're adding to become the
991999
// primary selection.
9921000
newSels.push({start: pos, end: pos, primary: sel.primary});

test/spec/EditorCommandHandlers-test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ define(function (require, exports, module) {
4747
"\n" +
4848
"}";
4949

50+
var tabbedContent = "function funcWithTabs() {\n" +
51+
" var i = 0;\n" +
52+
" var offset = 0;\n" +
53+
"}";
54+
5055
var myDocument, myEditor;
5156

5257
var testPath = SpecRunnerUtils.getTestPath("/spec/EditorCommandHandlers-test-files"),
@@ -3316,6 +3321,26 @@ define(function (require, exports, module) {
33163321

33173322
});
33183323

3324+
describe("Add Line to Selection with Tabs", function () {
3325+
beforeEach(function () {
3326+
setupFullEditor(tabbedContent);
3327+
});
3328+
3329+
it("should add a cursor on the next line before a single cursor in same visual position", function () {
3330+
myEditor.setSelection({line: 1, ch: 8}, {line: 1, ch: 8});
3331+
CommandManager.execute(Commands.EDIT_ADD_NEXT_LINE_TO_SEL, myEditor);
3332+
expectSelections([{start: {line: 1, ch: 8}, end: {line: 1, ch: 8}, primary: false, reversed: false},
3333+
{start: {line: 2, ch: 12}, end: {line: 2, ch: 12}, primary: true, reversed: false}]);
3334+
});
3335+
3336+
it("should add a cursor on the previous line before a single cursor selection in same visual position", function () {
3337+
myEditor.setSelection({line: 2, ch: 12}, {line: 2, ch: 12});
3338+
CommandManager.execute(Commands.EDIT_ADD_PREV_LINE_TO_SEL, myEditor);
3339+
expectSelections([{start: {line: 1, ch: 8}, end: {line: 1, ch: 8}, primary: true, reversed: false},
3340+
{start: {line: 2, ch: 12}, end: {line: 2, ch: 12}, primary: false, reversed: false}]);
3341+
});
3342+
});
3343+
33193344
describe("EditorCommandHandlers Integration", function () {
33203345
this.category = "integration";
33213346

0 commit comments

Comments
 (0)