multi_select_test.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. if (typeof process !== "undefined") {
  31. ace_require("amd-loader");
  32. ace_require("./test/mockdom");
  33. }
  34. define(function(ace_require, exports, module) {
  35. "use strict";
  36. ace_require("./multi_select");
  37. var assert = ace_require("./test/assertions");
  38. var Range = ace_require("./range").Range;
  39. var Editor = ace_require("./editor").Editor;
  40. var EditSession = ace_require("./edit_session").EditSession;
  41. var MockRenderer = ace_require("./test/mockrenderer").MockRenderer;
  42. var UndoManager = ace_require("./undomanager").UndoManager;
  43. var editor;
  44. var exec = function(name, times, args) {
  45. do {
  46. editor.commands.exec(name, editor, args);
  47. } while(times --> 1);
  48. };
  49. var testRanges = function(str) {
  50. assert.equal(editor.selection.getAllRanges() + "", str + "");
  51. };
  52. function getSelection(editor) {
  53. var data = editor.multiSelect.toJSON();
  54. if (!data.length) data = [data];
  55. data = data.map(function(x) {
  56. var a, c;
  57. if (x.isBackwards) {
  58. a = x.end;
  59. c = x.start;
  60. } else {
  61. c = x.end;
  62. a = x.start;
  63. }
  64. return Range.comparePoints(a, c)
  65. ? [a.row, a.column, c.row, c.column]
  66. : [a.row, a.column];
  67. });
  68. return data.length > 1 ? data : data[0];
  69. }
  70. function testSelection(editor, data) {
  71. assert.equal(getSelection(editor) + "", data + "");
  72. }
  73. function setSelection(editor, data) {
  74. if (typeof data[0] == "number")
  75. data = [data];
  76. editor.selection.fromJSON(data.map(function(x) {
  77. var start = {row: x[0], column: x[1]};
  78. var end = x.length == 2 ? start : {row: x[2], column: x[3]};
  79. var isBackwards = Range.comparePoints(start, end) > 0;
  80. return isBackwards ? {
  81. start: end,
  82. end: start,
  83. isBackwards: isBackwards
  84. } : {
  85. start: start,
  86. end: end,
  87. isBackwards: isBackwards
  88. };
  89. }));
  90. }
  91. module.exports = {
  92. name: "ACE multi_select.js",
  93. "test: multiselect editing": function() {
  94. var doc = new EditSession([
  95. "w1.w2",
  96. " wtt.w",
  97. " wtt.w"
  98. ]);
  99. editor = new Editor(new MockRenderer(), doc);
  100. editor.navigateFileEnd();
  101. exec("selectMoreBefore", 3);
  102. assert.ok(editor.inMultiSelectMode);
  103. assert.equal(editor.selection.getAllRanges().length, 4);
  104. var newLine = editor.session.getDocument().getNewLineCharacter();
  105. var copyText = "wwww".split("").join(newLine);
  106. assert.equal(editor.getCopyText(), copyText);
  107. exec("insertstring", 1, "a");
  108. exec("backspace", 2);
  109. assert.equal(editor.session.getValue(), "w1.w2\ntt\ntt");
  110. assert.equal(editor.selection.getAllRanges().length, 4);
  111. exec("selectall");
  112. assert.ok(!editor.inMultiSelectMode);
  113. //assert.equal(editor.selection.getAllRanges().length, 1);
  114. },
  115. "test: multiselect navigation": function() {
  116. var doc = new EditSession([
  117. "w1.w2",
  118. " wtt.w",
  119. " wtt.we"
  120. ]);
  121. editor = new Editor(new MockRenderer(), doc);
  122. editor.selectMoreLines(1);
  123. testRanges("Range: [0/0] -> [0/0],Range: [1/0] -> [1/0]");
  124. assert.ok(editor.inMultiSelectMode);
  125. exec("golinedown");
  126. exec("gotolineend");
  127. testRanges("Range: [1/9] -> [1/9],Range: [2/10] -> [2/10]");
  128. exec("selectwordleft");
  129. testRanges("Range: [1/8] -> [1/9],Range: [2/8] -> [2/10]");
  130. exec("golinedown", 2);
  131. assert.ok(!editor.inMultiSelectMode);
  132. },
  133. "test: multiselect session change": function() {
  134. var doc = new EditSession([
  135. "w1.w2",
  136. " wtt.w",
  137. " wtt.w"
  138. ]);
  139. editor = new Editor(new MockRenderer(), doc);
  140. editor.selectMoreLines(1);
  141. testRanges("Range: [0/0] -> [0/0],Range: [1/0] -> [1/0]");
  142. assert.ok(editor.inMultiSelectMode);
  143. var doc2 = new EditSession(["w1"]);
  144. editor.setSession(doc2);
  145. assert.ok(!editor.inMultiSelectMode);
  146. editor.setSession(doc);
  147. assert.ok(editor.inMultiSelectMode);
  148. },
  149. "test: multiselect addRange": function() {
  150. var doc = new EditSession([
  151. "w1.w2",
  152. " wtt.w",
  153. " wtt.w"
  154. ]);
  155. editor = new Editor(new MockRenderer(), doc);
  156. var selection = editor.selection;
  157. var range1 = new Range(0, 2, 0, 4);
  158. editor.selection.fromOrientedRange(range1);
  159. var range2 = new Range(0, 3, 0, 4);
  160. selection.addRange(range2);
  161. assert.ok(!editor.inMultiSelectMode);
  162. assert.ok(range2.isEqual(editor.selection.getRange()));
  163. var range3 = new Range(0, 1, 0, 1);
  164. selection.addRange(range3);
  165. assert.ok(editor.inMultiSelectMode);
  166. testRanges([range3, range2]);
  167. var range4 = new Range(0, 0, 4, 0);
  168. selection.addRange(range4);
  169. assert.ok(!editor.inMultiSelectMode);
  170. },
  171. "test: multiselect paste": function() {
  172. editor = new Editor(new MockRenderer());
  173. editor.setValue("l1\nl2\nl3", -1);
  174. editor.selectMoreLines(1);
  175. editor.$handlePaste("x\n");
  176. assert.equal("x\nl1\nx\nl2\nl3", editor.getValue());
  177. editor.execCommand("gotolineend");
  178. editor.$handlePaste("\ny");
  179. assert.equal("x\nl1\ny\nx\nl2\ny\nl3", editor.getValue());
  180. editor.selectMoreLines(-1);
  181. editor.$handlePaste("4\n5\n6");
  182. assert.equal("x\nl1\ny4\nx\nl52\ny6\nl3", editor.getValue());
  183. editor.$handlePaste("7\n\n8");
  184. assert.equal("x\nl1\ny47\nx\nl52\ny68\nl3", editor.getValue());
  185. editor.execCommand("selectleft");
  186. editor.execCommand("selectleft");
  187. editor.$handlePaste("t\nz");
  188. assert.equal("x\nl1\nyt\nz\nx\nt\nz2\nyt\nz\nl3", editor.getValue());
  189. editor.setValue("l1\nl2\nl3", -1);
  190. editor.selectMoreLines(1);
  191. editor.selectMoreLines(1);
  192. editor.$handlePaste("a\nb\nc\nd");
  193. assert.equal("a\nb\nc\ndl1\na\nb\nc\ndl2\na\nb\nc\ndl3", editor.getValue());
  194. },
  195. "test: onPaste in command with multiselect": function() {
  196. var doc = new EditSession(["l1", "l2"]);
  197. editor = new Editor(new MockRenderer(), doc);
  198. editor.commands.addCommand({
  199. name: 'insertfoo',
  200. exec: function(ed) { ed.onPaste('foo'); },
  201. multiSelectAction: "forEach"
  202. });
  203. var selection = editor.selection;
  204. var range1 = new Range(0,2,0,2);
  205. var range2 = new Range(1,2,1,2);
  206. selection.fromOrientedRange(range1);
  207. selection.addRange(range2);
  208. editor.execCommand('insertfoo');
  209. assert.equal('l1foo\nl2foo', editor.getValue());
  210. },
  211. "test multiselect move lines": function() {
  212. editor = new Editor(new MockRenderer());
  213. editor.setValue("l1\nl2\nl3\nl4", -1);
  214. setSelection(editor, [[0,2],[1,2],[2,2],[3,2]]);
  215. exec("copylinesdown");
  216. assert.equal(editor.getValue(),"l1\nl1\nl2\nl2\nl3\nl3\nl4\nl4");
  217. testSelection(editor, [[1,2],[3,2],[5,2],[7,2]]);
  218. exec("copylinesup");
  219. assert.equal(editor.getValue(),"l1\nl1\nl1\nl2\nl2\nl2\nl3\nl3\nl3\nl4\nl4\nl4");
  220. testSelection(editor, [[1,2],[4,2],[7,2],[10,2]]);
  221. exec("removeline");
  222. assert.equal(editor.getValue(),"l1\nl1\nl2\nl2\nl3\nl3\nl4\nl4");
  223. testSelection(editor, [[1,0],[3,0],[5,0],[7,0]]);
  224. setSelection(editor, [[1,2],[1,1,1,0],[3,0,3,1],[5,0,5,1],[7,0,7,1]]);
  225. exec("copylinesdown");
  226. exec("copylinesup");
  227. assert.equal(editor.getValue(),"l1\nl1\nl1\nl1\nl2\nl2\nl2\nl2\nl3\nl3\nl3\nl3\nl4\nl4\nl4\nl4");
  228. testSelection(editor, [[2,2],[2,1,2,0],[6,0,6,1],[10,0,10,1],[14,0,14,1]]);
  229. exec("movelinesdown", 12);
  230. assert.equal(editor.getValue(),"l1\nl1\nl1\nl2\nl2\nl2\nl3\nl3\nl3\nl4\nl4\nl4\nl1\nl2\nl3\nl4");
  231. testSelection(editor, [[12,2],[12,1,12,0],[13,0,13,1],[14,0,14,1],[15,0,15,1]]);
  232. exec("movelinesup", 12);
  233. assert.equal(editor.getValue(),"l1\nl2\nl3\nl4\nl1\nl1\nl1\nl2\nl2\nl2\nl3\nl3\nl3\nl4\nl4\nl4");
  234. testSelection(editor, [[0,2],[0,1,0,0],[1,0,1,1],[2,0,2,1],[3,0,3,1]]);
  235. },
  236. "test multiselect fromJSON/toJSON": function() {
  237. var doc = new EditSession(["l1", "l2"]);
  238. editor = new Editor(new MockRenderer(), doc);
  239. var selection = editor.selection;
  240. var before = selection.toJSON();
  241. var range1 = new Range(0,2,0,2);
  242. var range2 = new Range(1,2,1,2);
  243. selection.fromOrientedRange(range1);
  244. selection.addRange(range2);
  245. var after = selection.toJSON();
  246. selection.fromJSON(before);
  247. assert.ok(!selection.isEqual(after));
  248. assert.ok(selection.isEqual(before));
  249. selection.fromJSON(after);
  250. assert.ok(!selection.isEqual(before));
  251. assert.ok(selection.isEqual(after));
  252. },
  253. "test multiselect align": function() {
  254. var doc = new EditSession(["l1", "l2", "l3"]);
  255. doc.setUndoManager(new UndoManager());
  256. editor = new Editor(new MockRenderer(), doc);
  257. var selection = editor.selection;
  258. selection.addRange(new Range(1,0,1,0));
  259. selection.addRange(new Range(2,2,2,2));
  260. editor.execCommand("alignCursors");
  261. assert.equal(' l1\n l2\nl3', editor.getValue());
  262. doc.markUndoGroup();
  263. editor.execCommand("undo");
  264. assert.equal('l1\nl2\nl3', editor.getValue());
  265. },
  266. "test multiselect transpose": function() {
  267. editor = new Editor(new MockRenderer());
  268. editor.setValue("ay caramba");
  269. var selection = editor.selection;
  270. selection.fromJSON([new Range(0,3,0,10), new Range(0,0,0,2)]);
  271. editor.execCommand("transposeletters");
  272. assert.equal('caramba ay', editor.getValue());
  273. editor.execCommand("transposeletters");
  274. assert.ok(!editor.getSelectionRange().isEmpty());
  275. assert.equal('ay caramba', editor.getValue());
  276. },
  277. "test select next": function() {
  278. editor = new Editor(new MockRenderer());
  279. editor.setValue("a\na\na", 1);
  280. editor.execCommand("selectMoreBefore");
  281. editor.execCommand("selectMoreBefore");
  282. testSelection(editor, [[0,1,0,0],[1,1,1,0],[2,1,2,0]]);
  283. assert.equal(editor.session.$highlightLineMarker.start.row, 0);
  284. },
  285. "test multiSelect delete": function() {
  286. editor.setValue("\n" + "a\nb\nc/\n".repeat(4), -1);
  287. exec("selectdown", 4);
  288. exec("selectleft", 2);
  289. exec("selectMoreAfter", 3);
  290. exec("del", 1);
  291. assert.equal(editor.getValue(), "////\n");
  292. testSelection(editor, [[0,3],[0,2],[0,1],[0,0]]);
  293. exec("selectright", 1);
  294. exec("insertstring", 1, "a");
  295. assert.equal(editor.getValue(), "aaaa\n");
  296. testSelection(editor, [[0,4],[0,3],[0,2],[0,1]]);
  297. editor.setValue("w1.w2\n\n 0\n1\n2\n3\n4\n5\n6\n qwe\n\n\n\n\n\n");
  298. editor.selection.fromJSON([
  299. new Range(2,4,9,7),
  300. new Range(0,3,0,4),
  301. new Range(0,0,0,1)
  302. ]);
  303. editor.session.remove(new Range(4,0,5,0));
  304. testSelection(editor, [[2,4,8,7],[0,3,0,4],[0,0,0,1]]);
  305. },
  306. "test splitIntoLines": function() {
  307. var session = new EditSession(["l1", "l2", "l3"]);
  308. var selection = session.selection;
  309. editor = new Editor(new MockRenderer(), session);
  310. selection.moveTo(0, 0);
  311. selection.selectDown();
  312. selection.splitIntoLines();
  313. assert.equal(editor.selection.ranges + "", "Range: [1/0] -> [1/0],Range: [0/0] -> [0/2]", 2);
  314. selection.selectAll();
  315. assert.equal(editor.selection.rangeCount, 0);
  316. selection.splitIntoLines();
  317. assert.equal(editor.selection.rangeCount, 3);
  318. editor.execCommand("gotolineend");
  319. editor.execCommand("addLineAfter");
  320. editor.execCommand("insertstring", "x");
  321. editor.execCommand("selectup");
  322. editor.execCommand("splitSelectionIntoLines");
  323. assert.equal(editor.selection.rangeCount, 6);
  324. assert.equal(editor.selection.cursor.row, 4);
  325. editor.execCommand("toggleSplitSelectionIntoLines");
  326. assert.equal(editor.selection.getAllRanges() + "", "Range: [0/1] -> [5/1]");
  327. editor.setValue("");
  328. assert.equal(editor.selection.inMultiSelectMode, false);
  329. assert.equal(editor.selection.rangeCount, 0);
  330. }
  331. };
  332. });
  333. if (typeof module !== "undefined" && module === ace_require.main) {
  334. ace_require("asyncjs").test.testcase(module.exports).exec();
  335. }