selection_test.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. }
  33. define(function(ace_require, exports, module) {
  34. "use strict";
  35. var LineWidgets = ace_require("./line_widgets").LineWidgets;
  36. var EditSession = ace_require("./edit_session").EditSession;
  37. var assert = ace_require("./test/assertions");
  38. var Range = ace_require("./range").Range;
  39. module.exports = {
  40. createSession : function(rows, cols) {
  41. var line = new Array(cols + 1).join("a");
  42. var text = new Array(rows).join(line + "\n") + line;
  43. return new EditSession(text);
  44. },
  45. "test: selectAll" : function() {
  46. var session = this.createSession(10, 10);
  47. var selection = session.selection;
  48. session.selection.selectAll();
  49. assert.position(selection.getAnchor(), 0, 0);
  50. assert.position(selection.getCursor(), 9, 10);
  51. assert.position(selection.getRange().end, 9, 10);
  52. assert.position(selection.getRange().start, 0, 0);
  53. },
  54. "test: move cursor to end of file should place the cursor on last row and column" : function() {
  55. var session = this.createSession(200, 10);
  56. var selection = session.getSelection();
  57. selection.moveCursorFileEnd();
  58. assert.position(selection.getCursor(), 199, 10);
  59. },
  60. "test: moveCursor to start of file should place the cursor on the first row and column" : function() {
  61. var session = this.createSession(200, 10);
  62. var selection = session.getSelection();
  63. selection.moveCursorFileStart();
  64. assert.position(selection.getCursor(), 0, 0);
  65. },
  66. "test: move selection lead to end of file" : function() {
  67. var session = this.createSession(200, 10);
  68. var selection = session.getSelection();
  69. selection.moveCursorTo(100, 5);
  70. selection.selectFileEnd();
  71. var range = selection.getRange();
  72. assert.position(range.start, 100, 5);
  73. assert.position(range.end, 199, 10);
  74. },
  75. "test: move selection lead to start of file" : function() {
  76. var session = this.createSession(200, 10);
  77. var selection = session.getSelection();
  78. selection.moveCursorTo(100, 5);
  79. selection.selectFileStart();
  80. var range = selection.getRange();
  81. assert.position(range.start, 0, 0);
  82. assert.position(range.end, 100, 5);
  83. },
  84. "test: move cursor word right" : function() {
  85. var session = new EditSession([
  86. "ab",
  87. " Juhu Kinners (abc, 12)",
  88. " cde"
  89. ].join("\n"));
  90. var selection = session.getSelection();
  91. session.$selectLongWords = true;
  92. selection.moveCursorDown();
  93. assert.position(selection.getCursor(), 1, 0);
  94. selection.moveCursorWordRight();
  95. assert.position(selection.getCursor(), 1, 5);
  96. selection.moveCursorWordRight();
  97. assert.position(selection.getCursor(), 1, 13);
  98. selection.moveCursorWordRight();
  99. assert.position(selection.getCursor(), 1, 18);
  100. selection.moveCursorWordRight();
  101. assert.position(selection.getCursor(), 1, 22);
  102. // wrap line
  103. selection.moveCursorWordRight();
  104. assert.position(selection.getCursor(), 2, 4);
  105. selection.moveCursorWordRight();
  106. assert.position(selection.getCursor(), 2, 4);
  107. },
  108. "test: select word right if cursor in word" : function() {
  109. var session = new EditSession("Juhu Kinners");
  110. var selection = session.getSelection();
  111. selection.moveCursorTo(0, 2);
  112. selection.moveCursorWordRight();
  113. assert.position(selection.getCursor(), 0, 4);
  114. },
  115. "test: moveCursor word left" : function() {
  116. var session = new EditSession([
  117. "ab",
  118. " Juhu Kinners (abc, 12)",
  119. " cde"
  120. ].join("\n"));
  121. var selection = session.getSelection();
  122. session.$selectLongWords = true;
  123. selection.moveCursorDown();
  124. selection.moveCursorLineEnd();
  125. assert.position(selection.getCursor(), 1, 23);
  126. selection.moveCursorWordLeft();
  127. assert.position(selection.getCursor(), 1, 20);
  128. selection.moveCursorWordLeft();
  129. assert.position(selection.getCursor(), 1, 15);
  130. selection.moveCursorWordLeft();
  131. assert.position(selection.getCursor(), 1, 6);
  132. selection.moveCursorWordLeft();
  133. assert.position(selection.getCursor(), 1, 1);
  134. // wrap line
  135. selection.moveCursorWordLeft();
  136. assert.position(selection.getCursor(), 0, 0);
  137. selection.moveCursorWordLeft();
  138. assert.position(selection.getCursor(), 0, 0);
  139. },
  140. "test: moveCursor word left with umlauts" : function() {
  141. var session = new EditSession(" Fuß Füße");
  142. session.$selectLongWords = true;
  143. var selection = session.getSelection();
  144. selection.moveCursorTo(0, 9);
  145. selection.moveCursorWordLeft();
  146. assert.position(selection.getCursor(), 0, 5);
  147. selection.moveCursorWordLeft();
  148. assert.position(selection.getCursor(), 0, 1);
  149. },
  150. "test: select word left if cursor in word" : function() {
  151. var session = new EditSession("Juhu Kinners");
  152. var selection = session.getSelection();
  153. session.$selectLongWords = true;
  154. selection.moveCursorTo(0, 8);
  155. selection.moveCursorWordLeft();
  156. assert.position(selection.getCursor(), 0, 5);
  157. },
  158. "test: select word right and select" : function() {
  159. var session = new EditSession("Juhu Kinners");
  160. var selection = session.getSelection();
  161. selection.moveCursorTo(0, 0);
  162. selection.selectWordRight();
  163. var range = selection.getRange();
  164. assert.position(range.start, 0, 0);
  165. assert.position(range.end, 0, 4);
  166. },
  167. "test: select word left and select" : function() {
  168. var session = new EditSession("Juhu Kinners");
  169. var selection = session.getSelection();
  170. selection.moveCursorTo(0, 3);
  171. selection.selectWordLeft();
  172. var range = selection.getRange();
  173. assert.position(range.start, 0, 0);
  174. assert.position(range.end, 0, 3);
  175. },
  176. "test: select word with cursor in word should select the word" : function() {
  177. var session = new EditSession("Juhu Kinners 123");
  178. var selection = session.getSelection();
  179. selection.moveCursorTo(0, 8);
  180. selection.selectWord();
  181. var range = selection.getRange();
  182. assert.position(range.start, 0, 5);
  183. assert.position(range.end, 0, 12);
  184. },
  185. "test: select word with cursor in word including right whitespace should select the word" : function() {
  186. var session = new EditSession("Juhu Kinners 123");
  187. var selection = session.getSelection();
  188. selection.moveCursorTo(0, 8);
  189. selection.selectAWord();
  190. var range = selection.getRange();
  191. assert.position(range.start, 0, 5);
  192. assert.position(range.end, 0, 18);
  193. },
  194. "test: select word with cursor betwen white space and word should select the word" : function() {
  195. var session = new EditSession("Juhu Kinners");
  196. var selection = session.getSelection();
  197. session.$selectLongWords = true;
  198. selection.moveCursorTo(0, 4);
  199. selection.selectWord();
  200. var range = selection.getRange();
  201. assert.position(range.start, 0, 0);
  202. assert.position(range.end, 0, 4);
  203. selection.moveCursorTo(0, 5);
  204. selection.selectWord();
  205. var range = selection.getRange();
  206. assert.position(range.start, 0, 5);
  207. assert.position(range.end, 0, 12);
  208. },
  209. "test: select word with cursor in white space should select white space" : function() {
  210. var session = new EditSession("Juhu Kinners");
  211. var selection = session.getSelection();
  212. session.$selectLongWords = true;
  213. selection.moveCursorTo(0, 5);
  214. selection.selectWord();
  215. var range = selection.getRange();
  216. assert.position(range.start, 0, 4);
  217. assert.position(range.end, 0, 6);
  218. },
  219. "test: moving cursor should fire a 'changeCursor' event" : function() {
  220. var session = new EditSession("Juhu Kinners");
  221. var selection = session.getSelection();
  222. session.$selectLongWords = true;
  223. selection.moveCursorTo(0, 5);
  224. var called = false;
  225. selection.addEventListener("changeCursor", function() {
  226. called = true;
  227. });
  228. selection.moveCursorTo(0, 6);
  229. assert.ok(called);
  230. },
  231. "test: calling setCursor with the same position should not fire an event": function() {
  232. var session = new EditSession("Juhu Kinners");
  233. var selection = session.getSelection();
  234. session.$selectLongWords = true;
  235. selection.moveCursorTo(0, 5);
  236. var called = false;
  237. selection.addEventListener("changeCursor", function() {
  238. called = true;
  239. });
  240. selection.moveCursorTo(0, 5);
  241. assert.notOk(called);
  242. },
  243. "test: moveWordright should move past || and [": function() {
  244. var session = new EditSession("||foo[");
  245. var selection = session.getSelection();
  246. session.$selectLongWords = true;
  247. // Move behind ||foo
  248. selection.moveCursorWordRight();
  249. assert.position(selection.getCursor(), 0, 5);
  250. // Move behind [
  251. selection.moveCursorWordRight();
  252. assert.position(selection.getCursor(), 0, 6);
  253. },
  254. "test: moveWordLeft should move past || and [": function() {
  255. var session = new EditSession("||foo[");
  256. var selection = session.getSelection();
  257. session.$selectLongWords = true;
  258. selection.moveCursorTo(0, 6);
  259. // Move behind [foo
  260. selection.moveCursorWordLeft();
  261. assert.position(selection.getCursor(), 0, 2);
  262. // Move behind ||
  263. selection.moveCursorWordLeft();
  264. assert.position(selection.getCursor(), 0, 0);
  265. },
  266. "test: move cursor to line start should move cursor to end of the indentation first": function() {
  267. var session = new EditSession("12\n Juhu\n12");
  268. var selection = session.getSelection();
  269. selection.moveCursorTo(1, 6);
  270. selection.moveCursorLineStart();
  271. assert.position(selection.getCursor(), 1, 4);
  272. },
  273. "test: move cursor to line start when the cursor is at the end of the indentation should move cursor to column 0": function() {
  274. var session = new EditSession("12\n Juhu\n12");
  275. var selection = session.getSelection();
  276. selection.moveCursorTo(1, 4);
  277. selection.moveCursorLineStart();
  278. assert.position(selection.getCursor(), 1, 0);
  279. },
  280. "test: move cursor to line start when the cursor is at column 0 should move cursor to the end of the indentation": function() {
  281. var session = new EditSession("12\n Juhu\n12");
  282. var selection = session.getSelection();
  283. selection.moveCursorTo(1, 0);
  284. selection.moveCursorLineStart();
  285. assert.position(selection.getCursor(), 1, 4);
  286. },
  287. // Eclipse style
  288. "test: move cursor to line start when the cursor is before the initial indentation should move cursor to the end of the indentation": function() {
  289. var session = new EditSession("12\n Juhu\n12");
  290. var selection = session.getSelection();
  291. selection.moveCursorTo(1, 2);
  292. selection.moveCursorLineStart();
  293. assert.position(selection.getCursor(), 1, 4);
  294. },
  295. "test go line up when in the middle of the first line should go to document start": function() {
  296. var session = new EditSession("juhu kinners");
  297. var selection = session.getSelection();
  298. selection.moveCursorTo(0, 4);
  299. selection.moveCursorUp();
  300. assert.position(selection.getCursor(), 0, 0);
  301. },
  302. "test: (wrap) go line up when in the middle of the first line should go to document start": function() {
  303. var session = new EditSession("juhu kinners");
  304. session.setWrapLimitRange(5, 5);
  305. session.adjustWrapLimit(80);
  306. var selection = session.getSelection();
  307. selection.moveCursorTo(0, 4);
  308. selection.moveCursorUp();
  309. assert.position(selection.getCursor(), 0, 0);
  310. },
  311. "test go line down when in the middle of the last line should go to document end": function() {
  312. var session = new EditSession("juhu kinners");
  313. var selection = session.getSelection();
  314. selection.moveCursorTo(0, 4);
  315. selection.moveCursorDown();
  316. assert.position(selection.getCursor(), 0, 12);
  317. },
  318. "test (wrap) go line down when in the middle of the last line should go to document end": function() {
  319. var session = new EditSession("juhu kinners");
  320. session.setWrapLimitRange(8, 8);
  321. session.adjustWrapLimit(80);
  322. var selection = session.getSelection();
  323. selection.moveCursorTo(0, 10);
  324. selection.moveCursorDown();
  325. assert.position(selection.getCursor(), 0, 12);
  326. },
  327. "test go line up twice and then once down when in the second should go back to the previous column": function() {
  328. var session = new EditSession("juhu\nkinners");
  329. var selection = session.getSelection();
  330. selection.moveCursorTo(1, 4);
  331. selection.moveCursorUp();
  332. selection.moveCursorUp();
  333. selection.moveCursorDown();
  334. assert.position(selection.getCursor(), 1, 4);
  335. },
  336. "test (keyboard navigation) when curLine is not EOL and targetLine is all whitespace new column should be current column": function() {
  337. var session = new EditSession("function (a) {\n \n}");
  338. var selection = session.getSelection();
  339. selection.moveCursorTo(2, 0);
  340. selection.moveCursorUp();
  341. assert.position(selection.getCursor(), 1, 0);
  342. },
  343. "test (keyboard navigation) when curLine is EOL and targetLine is shorter than current column, new column should be targetLine's EOL": function() {
  344. var session = new EditSession("function (a) {\n \n}");
  345. var selection = session.getSelection();
  346. selection.moveCursorTo(0, 14);
  347. selection.moveCursorDown();
  348. assert.position(selection.getCursor(), 1, 4);
  349. },
  350. "test fromJSON/toJSON": function() {
  351. var copy = function(data) { return JSON.parse(JSON.stringify(data)); };
  352. var session = new EditSession("function (a) {\n \n}");
  353. var selection = session.getSelection();
  354. selection.moveCursorTo(0, 14);
  355. selection.moveCursorDown();
  356. assert.position(selection.getCursor(), 1, 4);
  357. var data = selection.toJSON();
  358. selection.moveCursorDown();
  359. assert.position(selection.getCursor(), 2, 1);
  360. assert.ok(!selection.isEqual(data));
  361. var nCursor = 0;
  362. var nSelection = 0;
  363. selection.on("changeCursor", function() { nCursor++; });
  364. selection.on("changeSelection", function() { nSelection++; });
  365. selection.fromJSON(copy(data));
  366. assert.equal(nCursor, 1);
  367. assert.equal(nSelection, 1);
  368. assert.position(selection.getCursor(), 1, 4);
  369. assert.ok(selection.isEqual(data));
  370. data.end.column = 10;
  371. selection.fromJSON(copy(data));
  372. assert.equal(nCursor, 1);
  373. assert.equal(nSelection, 1);
  374. data.end.column = 4;
  375. assert.ok(selection.isEqual(data));
  376. data.start.row = 0;
  377. selection.fromJSON(copy(data));
  378. assert.equal(nCursor, 1);
  379. assert.equal(nSelection, 2);
  380. assert.ok(selection.isEqual(data));
  381. data.isBackwards = true;
  382. selection.fromJSON(copy(data));
  383. assert.equal(nCursor, 2);
  384. assert.equal(nSelection, 3);
  385. assert.ok(selection.isEqual(data));
  386. selection.moveTo(0, 0);
  387. nCursor = nSelection = 0;
  388. selection.selectAll();
  389. assert.equal(nCursor, 1);
  390. assert.equal(nSelection, 1);
  391. selection.moveCursorRight();
  392. selection.clearSelection();
  393. nCursor = nSelection = 0;
  394. selection.selectAll();
  395. assert.equal(nCursor, 0);
  396. assert.equal(nSelection, 1);
  397. },
  398. "test setRange inside fold": function() {
  399. var session = new EditSession("-\n-fold-\n-");
  400. var selection = session.getSelection();
  401. session.addFold(".", new Range(0, 1, 2, 0));
  402. selection.setRange(new Range(1, 1, 1, 5));
  403. assert.equal(session.getTextRange(), "fold");
  404. },
  405. "test navigate around line widgets": function() {
  406. var session = new EditSession(["a", "b", "", "c", "d"]);
  407. session.widgetManager = new LineWidgets(session);
  408. var selection = session.getSelection();
  409. session.widgetManager.addLineWidget({
  410. row: 0,
  411. rowCount: 5,
  412. rowsAbove: 2
  413. });
  414. session.widgetManager.addLineWidget({
  415. row: 1,
  416. rowCount: 3,
  417. rowsAbove: 1
  418. });
  419. session.widgetManager.addLineWidget({
  420. row: 3,
  421. rowCount: 4
  422. });
  423. assert.position(session.documentToScreenPosition(3, 1), 11, 1);
  424. session.selection.moveCursorLineEnd();
  425. session.selection.moveCursorUp();
  426. assert.position(selection.cursor, 0, 0);
  427. session.selection.moveCursorDown();
  428. assert.position(selection.cursor, 1, 1);
  429. session.selection.moveCursorDown();
  430. assert.position(selection.cursor, 2, 0);
  431. session.selection.moveCursorDown();
  432. assert.position(selection.cursor, 3, 1);
  433. session.selection.moveCursorUp();
  434. assert.position(selection.cursor, 2, 0);
  435. session.selection.moveCursorUp();
  436. assert.position(selection.cursor, 1, 1);
  437. },
  438. "test selectLine": function() {
  439. var session = new EditSession(" text -\n-fold- \n-");
  440. var selection = session.getSelection();
  441. selection.selectLine();
  442. assert.range(selection.getRange(), 0, 0, 1, 0);
  443. selection.clearSelection();
  444. assert.position(selection.getAnchor(), 1, 0);
  445. selection.moveCursorLineEnd();
  446. assert.position(selection.getAnchor(), 1, 9);
  447. selection.moveCursorLineEnd();
  448. assert.position(selection.getAnchor(), 1, 6);
  449. selection.selectLineStart();
  450. assert.range(selection.getRange(), 1, 0, 1, 6);
  451. }
  452. };
  453. });
  454. if (typeof module !== "undefined" && module === ace_require.main) {
  455. ace_require("asyncjs").test.testcase(module.exports).exec();
  456. }