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

Commit 6a18975

Browse files
committed
Merge pull request #7167 from adobe/nj/cmv4-tab-empty-line
[cmv4] Fix case of hitting Tab at the beginning of a whitespace-only line
2 parents 7e41cde + aa0ac81 commit 6a18975

2 files changed

Lines changed: 94 additions & 1 deletion

File tree

src/editor/Editor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ define(function (require, exports, module) {
477477
// Case 1 - we found a multiline selection. We can bail as soon as we find one of these.
478478
selectionType = "indentAtBeginning";
479479
return false;
480-
} else if (sel.end.ch >= instance.getLine(sel.end.line).search(/\S/)) {
480+
} else if (sel.end.ch > 0 && sel.end.ch >= instance.getLine(sel.end.line).search(/\S/)) {
481481
// Case 2 - we found a selection that ends at or after the first non-whitespace
482482
// character on the line. We need to keep looking in case we find a later multiline
483483
// selection though.

test/spec/Editor-test.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,99 @@ define(function (require, exports, module) {
13231323
instance.setOption("indentWithTabs", useTabs);
13241324
instance.setOption("indentUnit", 4);
13251325
}
1326+
1327+
it("should indent and move cursor to correct position if at beginning of an empty line - spaces", function () {
1328+
var content = "function foo() {\n" +
1329+
" if (bar) {\n" +
1330+
"\n" +
1331+
" }\n" +
1332+
"}";
1333+
makeEditor(content);
1334+
myEditor.setCursorPos({line: 2, ch: 0});
1335+
myEditor._handleTabKey();
1336+
expect(myEditor.getSelection()).toEqual({start: {line: 2, ch: 8}, end: {line: 2, ch: 8}, reversed: false});
1337+
1338+
var lines = content.split("\n");
1339+
lines[2] = " ";
1340+
expect(myEditor.document.getText()).toEqual(lines.join("\n"));
1341+
});
1342+
1343+
it("should indent and move cursor to correct position if at beginning of an empty line - tabs", function () {
1344+
var content = "function foo() {\n" +
1345+
"\tif (bar) {\n" +
1346+
"\n" +
1347+
"\t}\n" +
1348+
"}";
1349+
makeEditor(content, true);
1350+
myEditor.setCursorPos({line: 2, ch: 0});
1351+
myEditor._handleTabKey();
1352+
expect(myEditor.getSelection()).toEqual({start: {line: 2, ch: 2}, end: {line: 2, ch: 2}, reversed: false});
1353+
1354+
var lines = content.split("\n");
1355+
lines[2] = "\t\t";
1356+
expect(myEditor.document.getText()).toEqual(lines.join("\n"));
1357+
});
1358+
1359+
it("should move cursor to end of whitespace (without adding more) if at beginning of a line with correct amount of whitespace - spaces", function () {
1360+
var content = "function foo() {\n" +
1361+
" if (bar) {\n" +
1362+
" \n" +
1363+
" }\n" +
1364+
"}";
1365+
makeEditor(content);
1366+
myEditor.setCursorPos({line: 2, ch: 0});
1367+
myEditor._handleTabKey();
1368+
expect(myEditor.getSelection()).toEqual({start: {line: 2, ch: 8}, end: {line: 2, ch: 8}, reversed: false});
1369+
1370+
expect(myEditor.document.getText()).toEqual(content);
1371+
});
1372+
1373+
it("should move cursor to end of whitespace (without adding more) if at beginning of a line with correct amount of whitespace - tabs", function () {
1374+
var content = "function foo() {\n" +
1375+
"\tif (bar) {\n" +
1376+
"\t\t\n" +
1377+
"\t}\n" +
1378+
"}";
1379+
makeEditor(content, true);
1380+
myEditor.setCursorPos({line: 2, ch: 0});
1381+
myEditor._handleTabKey();
1382+
expect(myEditor.getSelection()).toEqual({start: {line: 2, ch: 2}, end: {line: 2, ch: 2}, reversed: false});
1383+
expect(myEditor.document.getText()).toEqual(content);
1384+
});
1385+
1386+
it("should add another indent whitespace if already past correct indent level on an all whitespace line - spaces", function () {
1387+
var content = "function foo() {\n" +
1388+
" if (bar) {\n" +
1389+
" \n" +
1390+
" }\n" +
1391+
"}";
1392+
makeEditor(content);
1393+
myEditor.setCursorPos({line: 2, ch: 12});
1394+
myEditor._handleTabKey();
1395+
expect(myEditor.getSelection()).toEqual({start: {line: 2, ch: 16}, end: {line: 2, ch: 16}, reversed: false});
1396+
1397+
var lines = content.split("\n");
1398+
lines[2] = " " + lines[2];
1399+
expect(myEditor.document.getText()).toEqual(lines.join("\n"));
1400+
1401+
});
1402+
1403+
it("should add another indent whitespace if already past correct indent level on an all whitespace line - tabs", function () {
1404+
var content = "function foo() {\n" +
1405+
"\tif (bar) {\n" +
1406+
"\t\t\t\n" +
1407+
"\t}\n" +
1408+
"}";
1409+
makeEditor(content, true);
1410+
myEditor.setCursorPos({line: 2, ch: 3});
1411+
myEditor._handleTabKey();
1412+
expect(myEditor.getSelection()).toEqual({start: {line: 2, ch: 4}, end: {line: 2, ch: 4}, reversed: false});
1413+
1414+
var lines = content.split("\n");
1415+
lines[2] = "\t" + lines[2];
1416+
expect(myEditor.document.getText()).toEqual(lines.join("\n"));
1417+
1418+
});
13261419

13271420
it("should indent improperly indented line to proper level and move cursor to beginning of content if cursor is in whitespace before content - spaces", function () {
13281421
var content = "function foo() {\n" +

0 commit comments

Comments
 (0)