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

Commit 7b0d56b

Browse files
committed
Merge pull request #2066 from adobe/pflynn/select-line-tests
Unit tests for new Select Line command (pull #2002)
2 parents a449602 + 9ff17db commit 7b0d56b

File tree

2 files changed

+105
-11
lines changed

2 files changed

+105
-11
lines changed

src/editor/EditorCommandHandlers.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,20 @@ define(function (require, exports, module) {
295295
editor._codeMirror.execCommand("indentLess");
296296
}
297297

298-
function selectLine() {
299-
var editor = EditorManager.getFocusedEditor();
298+
function selectLine(editor) {
299+
editor = editor || EditorManager.getFocusedEditor();
300300
if (editor) {
301301
var sel = editor.getSelection();
302302
var from = {line: sel.start.line, ch: 0};
303303
var to = {line: sel.end.line + 1, ch: 0};
304+
305+
if (to.line === editor.getLastVisibleLine() + 1) {
306+
// Last line: select to end of line instead of start of (hidden/nonexistent) following line,
307+
// which due to how CM clips coords would only work some of the time
308+
to.line -= 1;
309+
to.ch = editor.document.getLine(to.line).length;
310+
}
311+
304312
editor.setSelection(from, to);
305313
}
306314
}

test/spec/EditorCommandHandlers-test.js

Lines changed: 95 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,16 @@ define(function (require, exports, module) {
5656

5757
myEditor.focus();
5858
}
59-
59+
60+
function makeEditorWithRange(range) {
61+
// create editor with a visible range
62+
var mocks = SpecRunnerUtils.createMockEditor(defaultContent, "javascript", range);
63+
myDocument = mocks.doc;
64+
myEditor = mocks.editor;
65+
66+
myEditor.focus();
67+
}
68+
6069
afterEach(function () {
6170
SpecRunnerUtils.destroyMockEditor(myDocument);
6271
myEditor = null;
@@ -501,6 +510,7 @@ define(function (require, exports, module) {
501510
});
502511
});
503512

513+
504514
describe("Move Lines Up/Down", function () {
505515
beforeEach(setupFullEditor);
506516

@@ -810,6 +820,7 @@ define(function (require, exports, module) {
810820
});
811821
});
812822

823+
813824
describe("Delete Line", function () {
814825
beforeEach(setupFullEditor);
815826

@@ -887,14 +898,6 @@ define(function (require, exports, module) {
887898
});
888899

889900
describe("Delete Line - editor with visible range", function () {
890-
function makeEditorWithRange(range) {
891-
// create editor with a visible range
892-
var mocks = SpecRunnerUtils.createMockEditor(defaultContent, "javascript", range);
893-
myDocument = mocks.doc;
894-
myEditor = mocks.editor;
895-
896-
myEditor.focus();
897-
}
898901

899902
it("should delete the top line of the visible range", function () {
900903
makeEditorWithRange({startLine: 1, endLine: 5});
@@ -945,5 +948,88 @@ define(function (require, exports, module) {
945948
});
946949
});
947950

951+
952+
describe("Select Line", function () {
953+
beforeEach(setupFullEditor);
954+
955+
it("should select the first line with IP in that line", function () {
956+
myEditor.setSelection({line: 0, ch: 5}, {line: 0, ch: 5});
957+
CommandManager.execute(Commands.EDIT_SELECT_LINE, myEditor);
958+
959+
expectSelection({start: {line: 0, ch: 0}, end: {line: 1, ch: 0}});
960+
});
961+
962+
it("should select the last line with IP in that line", function () {
963+
myEditor.setSelection({line: 7, ch: 0}, {line: 7, ch: 0});
964+
CommandManager.execute(Commands.EDIT_SELECT_LINE, myEditor);
965+
966+
expectSelection({start: {line: 7, ch: 0}, end: {line: 7, ch: 1}});
967+
});
968+
969+
it("should select all in one-line file", function () {
970+
myDocument.setText("// x");
971+
myEditor.setSelection({line: 0, ch: 0}, {line: 0, ch: 0});
972+
CommandManager.execute(Commands.EDIT_SELECT_LINE, myEditor);
973+
974+
expectSelection({start: {line: 0, ch: 0}, end: {line: 0, ch: 4}});
975+
});
976+
977+
it("should extend selection to whole line", function () {
978+
myEditor.setSelection({line: 1, ch: 4}, {line: 1, ch: 8});
979+
CommandManager.execute(Commands.EDIT_SELECT_LINE, myEditor);
980+
981+
expectSelection({start: {line: 1, ch: 0}, end: {line: 2, ch: 0}});
982+
});
983+
984+
it("should extend whole line selection to next line", function () {
985+
myEditor.setSelection({line: 1, ch: 0}, {line: 2, ch: 0});
986+
CommandManager.execute(Commands.EDIT_SELECT_LINE, myEditor);
987+
988+
expectSelection({start: {line: 1, ch: 0}, end: {line: 3, ch: 0}});
989+
});
990+
991+
it("should extend multi-line selection to full lines", function () {
992+
myEditor.setSelection({line: 1, ch: 4}, {line: 3, ch: 9});
993+
CommandManager.execute(Commands.EDIT_SELECT_LINE, myEditor);
994+
995+
expectSelection({start: {line: 1, ch: 0}, end: {line: 4, ch: 0}});
996+
});
997+
998+
it("should extend full multi-line selection to one more line", function () {
999+
myEditor.setSelection({line: 1, ch: 0}, {line: 4, ch: 0});
1000+
CommandManager.execute(Commands.EDIT_SELECT_LINE, myEditor);
1001+
1002+
expectSelection({start: {line: 1, ch: 0}, end: {line: 5, ch: 0}});
1003+
});
1004+
1005+
});
1006+
1007+
describe("Select Line - editor with visible range", function () {
1008+
1009+
it("shouldn't select past end of visible range, IP in middle of last visible line", function () {
1010+
makeEditorWithRange({startLine: 1, endLine: 5});
1011+
myEditor.setSelection({line: 5, ch: 4}, {line: 5, ch: 4});
1012+
CommandManager.execute(Commands.EDIT_SELECT_LINE, myEditor);
1013+
1014+
expectSelection({start: {line: 5, ch: 0}, end: {line: 5, ch: 5}});
1015+
});
1016+
1017+
it("shouldn't select past end of visible range, IP at start of last visible line", function () {
1018+
makeEditorWithRange({startLine: 1, endLine: 5});
1019+
myEditor.setSelection({line: 5, ch: 0}, {line: 5, ch: 0});
1020+
CommandManager.execute(Commands.EDIT_SELECT_LINE, myEditor);
1021+
1022+
expectSelection({start: {line: 5, ch: 0}, end: {line: 5, ch: 5}});
1023+
});
1024+
1025+
it("should extend selection to include last line of visible range", function () {
1026+
makeEditorWithRange({startLine: 1, endLine: 5});
1027+
myEditor.setSelection({line: 4, ch: 4}, {line: 4, ch: 4});
1028+
CommandManager.execute(Commands.EDIT_SELECT_LINE, myEditor);
1029+
1030+
expectSelection({start: {line: 4, ch: 0}, end: {line: 5, ch: 0}});
1031+
});
1032+
});
1033+
9481034
});
9491035
});

0 commit comments

Comments
 (0)