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

Commit acf0c47

Browse files
committed
for add new selection, use display column
1 parent d3ffd96 commit acf0c47

2 files changed

Lines changed: 37 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} colum Display column number
913+
* @return {number}
914+
*/
915+
Editor.prototype.getPosOffset = 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.getPosOffset(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});

0 commit comments

Comments
 (0)