edit_session_test.js 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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. var lang = ace_require("./lib/lang");
  37. var EditSession = ace_require("./edit_session").EditSession;
  38. var Editor = ace_require("./editor").Editor;
  39. var UndoManager = ace_require("./undomanager").UndoManager;
  40. var MockRenderer = ace_require("./test/mockrenderer").MockRenderer;
  41. var Range = ace_require("./range").Range;
  42. var assert = ace_require("./test/assertions");
  43. var JavaScriptMode = ace_require("./mode/javascript").Mode;
  44. function createFoldTestSession() {
  45. var lines = [
  46. "function foo(items) {",
  47. " for (var i=0; i<items.length; i++) {",
  48. " alert(items[i] + \"juhu\");",
  49. " } // Real Tab.",
  50. "}"
  51. ];
  52. var session = new EditSession(lines.join("\n"));
  53. session.setUndoManager(new UndoManager());
  54. session.addFold("args...", new Range(0, 13, 0, 18));
  55. session.addFold("foo...", new Range(1, 10, 2, 10));
  56. session.addFold("bar...", new Range(2, 20, 2, 25));
  57. return session;
  58. }
  59. function assertArray(a, b) {
  60. assert.equal(a+"", b+"");
  61. assert.ok(a.length == b.length);
  62. for (var i = 0; i < a.length; i++) {
  63. assert.equal(a[i], b[i]);
  64. }
  65. }
  66. module.exports = {
  67. "test: find matching opening bracket in Text mode" : function() {
  68. var session = new EditSession(["(()(", "())))"]);
  69. assert.position(session.findMatchingBracket({row: 0, column: 3}), 0, 1);
  70. assert.position(session.findMatchingBracket({row: 1, column: 2}), 1, 0);
  71. assert.position(session.findMatchingBracket({row: 1, column: 3}), 0, 3);
  72. assert.position(session.findMatchingBracket({row: 1, column: 4}), 0, 0);
  73. assert.equal(session.findMatchingBracket({row: 1, column: 5}), null);
  74. },
  75. "test: find matching closing bracket in Text mode" : function() {
  76. var session = new EditSession(["(()(", "())))"]);
  77. assert.position(session.findMatchingBracket({row: 1, column: 1}), 1, 1);
  78. assert.position(session.findMatchingBracket({row: 1, column: 1}), 1, 1);
  79. assert.position(session.findMatchingBracket({row: 0, column: 4}), 1, 2);
  80. assert.position(session.findMatchingBracket({row: 0, column: 2}), 0, 2);
  81. assert.position(session.findMatchingBracket({row: 0, column: 1}), 1, 3);
  82. assert.equal(session.findMatchingBracket({row: 0, column: 0}), null);
  83. },
  84. "test: find matching opening bracket in JavaScript mode" : function() {
  85. var lines = [
  86. "function foo() {",
  87. " var str = \"{ foo()\";",
  88. " if (debug) {",
  89. " // write str (a string) to the console",
  90. " console.log(str);",
  91. " }",
  92. " str += \" bar() }\";",
  93. "}"
  94. ];
  95. var session = new EditSession(lines.join("\n"), new JavaScriptMode());
  96. assert.position(session.findMatchingBracket({row: 0, column: 14}), 0, 12);
  97. assert.position(session.findMatchingBracket({row: 7, column: 1}), 0, 15);
  98. assert.position(session.findMatchingBracket({row: 6, column: 20}), 1, 15);
  99. assert.position(session.findMatchingBracket({row: 1, column: 22}), 1, 20);
  100. assert.position(session.findMatchingBracket({row: 3, column: 31}), 3, 21);
  101. assert.position(session.findMatchingBracket({row: 4, column: 24}), 4, 19);
  102. assert.equal(session.findMatchingBracket({row: 0, column: 1}), null);
  103. },
  104. "test: find matching closing bracket in JavaScript mode" : function() {
  105. var lines = [
  106. "function foo() {",
  107. " var str = \"{ foo()\";",
  108. " if (debug) {",
  109. " // write str (a string) to the console",
  110. " console.log(str);",
  111. " }",
  112. " str += \" bar() }\";",
  113. "}"
  114. ];
  115. var session = new EditSession(lines.join("\n"), new JavaScriptMode());
  116. assert.position(session.findMatchingBracket({row: 0, column: 13}), 0, 13);
  117. assert.position(session.findMatchingBracket({row: 0, column: 16}), 7, 0);
  118. assert.position(session.findMatchingBracket({row: 1, column: 16}), 6, 19);
  119. assert.position(session.findMatchingBracket({row: 1, column: 21}), 1, 21);
  120. assert.position(session.findMatchingBracket({row: 3, column: 22}), 3, 30);
  121. assert.position(session.findMatchingBracket({row: 4, column: 20}), 4, 23);
  122. },
  123. "test: handle unbalanced brackets in JavaScript mode" : function() {
  124. var lines = [
  125. "function foo() {",
  126. " var str = \"{ foo()\";",
  127. " if (debug) {",
  128. " // write str a string) to the console",
  129. " console.log(str);",
  130. " ",
  131. " str += \" bar() \";",
  132. "}"
  133. ];
  134. var session = new EditSession(lines.join("\n"), new JavaScriptMode());
  135. assert.equal(session.findMatchingBracket({row: 0, column: 16}), null);
  136. assert.equal(session.findMatchingBracket({row: 3, column: 30}), null);
  137. assert.equal(session.findMatchingBracket({row: 1, column: 16}), null);
  138. },
  139. "test: match different bracket types" : function() {
  140. var session = new EditSession(["({[", ")]}"]);
  141. assert.position(session.findMatchingBracket({row: 0, column: 1}), 1, 0);
  142. assert.position(session.findMatchingBracket({row: 0, column: 2}), 1, 2);
  143. assert.position(session.findMatchingBracket({row: 0, column: 3}), 1, 1);
  144. assert.position(session.findMatchingBracket({row: 1, column: 1}), 0, 0);
  145. assert.position(session.findMatchingBracket({row: 1, column: 2}), 0, 2);
  146. assert.position(session.findMatchingBracket({row: 1, column: 3}), 0, 1);
  147. },
  148. "test: move lines down" : function() {
  149. var session = new EditSession(["a1", "a2", "a3", "a4"]);
  150. session.moveLinesDown(0, 1);
  151. assert.equal(session.getValue(), ["a3", "a1", "a2", "a4"].join("\n"));
  152. session.moveLinesDown(1, 2);
  153. assert.equal(session.getValue(), ["a3", "a4", "a1", "a2"].join("\n"));
  154. session.moveLinesDown(2, 3);
  155. assert.equal(session.getValue(), ["a3", "a4", "a1", "a2"].join("\n"));
  156. session.moveLinesDown(2, 2);
  157. assert.equal(session.getValue(), ["a3", "a4", "a2", "a1"].join("\n"));
  158. },
  159. "test: move lines up" : function() {
  160. var session = new EditSession(["a1", "a2", "a3", "a4"]);
  161. session.moveLinesUp(2, 3);
  162. assert.equal(session.getValue(), ["a1", "a3", "a4", "a2"].join("\n"));
  163. session.moveLinesUp(1, 2);
  164. assert.equal(session.getValue(), ["a3", "a4", "a1", "a2"].join("\n"));
  165. session.moveLinesUp(0, 1);
  166. assert.equal(session.getValue(), ["a3", "a4", "a1", "a2"].join("\n"));
  167. session.moveLinesUp(2, 2);
  168. assert.equal(session.getValue(), ["a3", "a1", "a4", "a2"].join("\n"));
  169. },
  170. "test: duplicate lines" : function() {
  171. var session = new EditSession(["1", "2", "3", "4"]);
  172. session.duplicateLines(1, 2);
  173. assert.equal(session.getValue(), ["1", "2", "3", "2", "3", "4"].join("\n"));
  174. },
  175. "test: duplicate last line" : function() {
  176. var session = new EditSession(["1", "2", "3"]);
  177. session.duplicateLines(2, 2);
  178. assert.equal(session.getValue(), ["1", "2", "3", "3"].join("\n"));
  179. },
  180. "test: duplicate first line" : function() {
  181. var session = new EditSession(["1", "2", "3"]);
  182. session.duplicateLines(0, 0);
  183. assert.equal(session.getValue(), ["1", "1", "2", "3"].join("\n"));
  184. },
  185. "test: getScreenLastRowColumn": function() {
  186. var session = new EditSession([
  187. "juhu",
  188. "12\t\t34",
  189. "ぁぁa"
  190. ]);
  191. assert.equal(session.getScreenLastRowColumn(0), 4);
  192. assert.equal(session.getScreenLastRowColumn(1), 10);
  193. assert.equal(session.getScreenLastRowColumn(2), 5);
  194. },
  195. "test: convert document to screen coordinates" : function() {
  196. var session = new EditSession("01234\t567890\t1234");
  197. session.setTabSize(4);
  198. assert.equal(session.documentToScreenColumn(0, 0), 0);
  199. assert.equal(session.documentToScreenColumn(0, 4), 4);
  200. assert.equal(session.documentToScreenColumn(0, 5), 5);
  201. assert.equal(session.documentToScreenColumn(0, 6), 8);
  202. assert.equal(session.documentToScreenColumn(0, 12), 14);
  203. assert.equal(session.documentToScreenColumn(0, 13), 16);
  204. session.setTabSize(2);
  205. assert.equal(session.documentToScreenColumn(0, 0), 0);
  206. assert.equal(session.documentToScreenColumn(0, 4), 4);
  207. assert.equal(session.documentToScreenColumn(0, 5), 5);
  208. assert.equal(session.documentToScreenColumn(0, 6), 6);
  209. assert.equal(session.documentToScreenColumn(0, 7), 7);
  210. assert.equal(session.documentToScreenColumn(0, 12), 12);
  211. assert.equal(session.documentToScreenColumn(0, 13), 14);
  212. },
  213. "test: convert document to screen coordinates with leading tabs": function() {
  214. var session = new EditSession("\t\t123");
  215. session.setTabSize(4);
  216. assert.equal(session.documentToScreenColumn(0, 0), 0);
  217. assert.equal(session.documentToScreenColumn(0, 1), 4);
  218. assert.equal(session.documentToScreenColumn(0, 2), 8);
  219. assert.equal(session.documentToScreenColumn(0, 3), 9);
  220. },
  221. "test: documentToScreen without soft wrap": function() {
  222. var session = new EditSession([
  223. "juhu",
  224. "12\t\t34",
  225. "ぁぁa"
  226. ]);
  227. assert.position(session.documentToScreenPosition(0, 3), 0, 3);
  228. assert.position(session.documentToScreenPosition(1, 3), 1, 4);
  229. assert.position(session.documentToScreenPosition(1, 4), 1, 8);
  230. assert.position(session.documentToScreenPosition(2, 2), 2, 4);
  231. },
  232. "test: documentToScreen with soft wrap": function() {
  233. var session = new EditSession(["foo bar foo bar"]);
  234. session.setUseWrapMode(true);
  235. session.setWrapLimitRange(12, 12);
  236. session.adjustWrapLimit(80);
  237. session.setOption("wrapMethod", "text");
  238. assert.position(session.documentToScreenPosition(0, 11), 0, 11);
  239. assert.position(session.documentToScreenPosition(0, 12), 1, 0);
  240. },
  241. "test: documentToScreen with soft wrap and multibyte characters": function() {
  242. var session = new EditSession(["ぁぁa"]);
  243. session.setUseWrapMode(true);
  244. session.setWrapLimitRange(2, 2);
  245. session.adjustWrapLimit(80);
  246. assert.position(session.documentToScreenPosition(0, 1), 1, 0);
  247. assert.position(session.documentToScreenPosition(0, 2), 2, 0);
  248. assert.position(session.documentToScreenPosition(0, 4), 2, 1);
  249. },
  250. "test: documentToScreen should clip position to the document boundaries": function() {
  251. var session = new EditSession("foo bar\njuhu kinners");
  252. assert.position(session.documentToScreenPosition(-1, 4), 0, 0);
  253. assert.position(session.documentToScreenPosition(3, 0), 1, 12);
  254. },
  255. "test: convert screen to document coordinates" : function() {
  256. var session = new EditSession("01234\t567890\t1234");
  257. session.setTabSize(4);
  258. assert.equal(session.screenToDocumentColumn(0, 0), 0);
  259. assert.equal(session.screenToDocumentColumn(0, 4), 4);
  260. assert.equal(session.screenToDocumentColumn(0, 5), 5);
  261. assert.equal(session.screenToDocumentColumn(0, 6), 5);
  262. assert.equal(session.screenToDocumentColumn(0, 7), 5);
  263. assert.equal(session.screenToDocumentColumn(0, 8), 6);
  264. assert.equal(session.screenToDocumentColumn(0, 9), 7);
  265. assert.equal(session.screenToDocumentColumn(0, 15), 12);
  266. assert.equal(session.screenToDocumentColumn(0, 19), 16);
  267. session.setTabSize(2);
  268. assert.equal(session.screenToDocumentColumn(0, 0), 0);
  269. assert.equal(session.screenToDocumentColumn(0, 4), 4);
  270. assert.equal(session.screenToDocumentColumn(0, 5), 5);
  271. assert.equal(session.screenToDocumentColumn(0, 6), 6);
  272. assert.equal(session.screenToDocumentColumn(0, 12), 12);
  273. assert.equal(session.screenToDocumentColumn(0, 13), 12);
  274. assert.equal(session.screenToDocumentColumn(0, 14), 13);
  275. },
  276. "test: screenToDocument with soft wrap": function() {
  277. var session = new EditSession(["foo bar foo bar"]);
  278. session.setUseWrapMode(true);
  279. session.setWrapLimitRange(12, 12);
  280. session.adjustWrapLimit(80);
  281. assert.position(session.screenToDocumentPosition(1, 0), 0, 12);
  282. assert.position(session.screenToDocumentPosition(0, 11), 0, 11);
  283. // Check if the position is clamped the right way.
  284. assert.position(session.screenToDocumentPosition(0, 12), 0, 11);
  285. assert.position(session.screenToDocumentPosition(0, 20), 0, 11);
  286. },
  287. "test: screenToDocument with soft wrap and multi byte characters": function() {
  288. var session = new EditSession(["ぁ a"]);
  289. session.setUseWrapMode(true);
  290. session.adjustWrapLimit(80);
  291. assert.position(session.screenToDocumentPosition(0, 1), 0, 0);
  292. assert.position(session.screenToDocumentPosition(0, 2), 0, 1);
  293. assert.position(session.screenToDocumentPosition(0, 3), 0, 2);
  294. assert.position(session.screenToDocumentPosition(0, 4), 0, 3);
  295. assert.position(session.screenToDocumentPosition(0, 5), 0, 3);
  296. },
  297. "test: screenToDocument should clip position to the document boundaries": function() {
  298. var session = new EditSession("foo bar\njuhu kinners");
  299. assert.position(session.screenToDocumentPosition(-1, 4), 0, 0);
  300. assert.position(session.screenToDocumentPosition(0, -1), 0, 0);
  301. assert.position(session.screenToDocumentPosition(0, 30), 0, 7);
  302. assert.position(session.screenToDocumentPosition(2, 4), 1, 12);
  303. assert.position(session.screenToDocumentPosition(1, 30), 1, 12);
  304. assert.position(session.screenToDocumentPosition(20, 50), 1, 12);
  305. assert.position(session.screenToDocumentPosition(20, 5), 1, 12);
  306. // and the same for folded rows
  307. session.addFold("...", new Range(0,1,1,3));
  308. assert.position(session.screenToDocumentPosition(1, 2), 1, 12);
  309. // for wrapped rows
  310. session.setUseWrapMode(true);
  311. session.setWrapLimitRange(5,5);
  312. assert.position(session.screenToDocumentPosition(4, 1), 1, 12);
  313. },
  314. "test: wrapLine split function" : function() {
  315. function computeAndAssert(line, assertEqual, wrapLimit, tabSize) {
  316. wrapLimit = wrapLimit || 12;
  317. tabSize = tabSize || 4;
  318. line = lang.stringTrimRight(line);
  319. var tokens = EditSession.prototype.$getDisplayTokens(line);
  320. var splits = EditSession.prototype.$computeWrapSplits(tokens, wrapLimit, tabSize);
  321. assert.ok(splits.length == assertEqual.length);
  322. for (var i = 0; i < splits.length; i++) {
  323. assert.ok(splits[i] == assertEqual[i]);
  324. }
  325. }
  326. EditSession.prototype.$wrapAsCode = true;
  327. EditSession.prototype.$indentedSoftWrap = false;
  328. // Basic splitting.
  329. computeAndAssert("foo bar foo bar", [ 12 ]);
  330. computeAndAssert("foo bar f bar", [ 12 ]);
  331. computeAndAssert("foo bar f r", [ 12 ]); // 14 if we enable
  332. computeAndAssert("foo bar foo bar foo bara foo", [12, 25]);
  333. // Don't split if there is only whitespaces/tabs at the end of the line.
  334. computeAndAssert("foo foo foo \t \t", [ ]);
  335. // If there is no space to split, force split.
  336. computeAndAssert("foooooooooooooo", [ 12 ]);
  337. computeAndAssert("fooooooooooooooooooooooooooo", [12, 24]);
  338. computeAndAssert("foo bar fooooooooooobooooooo", [8, 20]);
  339. // Basic splitting + tabs.
  340. computeAndAssert("foo \t\tbar", [ 6 ]);
  341. computeAndAssert("foo \t \tbar", [ 7 ]);
  342. // Ignore spaces/tabs at beginning of split.
  343. computeAndAssert("foo \t \t \t \t bar", [ 7 ]); // 14
  344. // Test wrapping for asian characters.
  345. computeAndAssert("ぁぁ", [1], 2);
  346. computeAndAssert(" ぁぁ", [1, 2], 2);
  347. computeAndAssert(" ぁ\tぁ", [1, 3], 2);
  348. computeAndAssert(" ぁぁ\tぁ", [2, 4], 4);
  349. computeAndAssert("ぁぁ ぁぁ\tぁ", [3, 6], 6);
  350. // Test wrapping for punctuation.
  351. computeAndAssert(" ab.c;ef++", [2, 4, 6, 8], 2);
  352. computeAndAssert(" ab.c;ef++", [3, 5, 8], 3);
  353. computeAndAssert(" a.b", [1, 2, 3], 1);
  354. computeAndAssert("#>>", [1, 2], 1);
  355. // Test wrapping for punctuation in
  356. EditSession.prototype.$wrapAsCode = false;
  357. computeAndAssert("ab cde, Juhu kinners", [3, 8, 13, 19], 6);
  358. // test indented wrapping
  359. EditSession.prototype.$indentedSoftWrap = true;
  360. computeAndAssert("foo bar foo bar foo bara foo", [12, 25]);
  361. computeAndAssert("fooooooooooooooooooooooooooo", [12, 24]);
  362. computeAndAssert("\t\tfoo bar fooooooooooobooooooo", [6, 10, 16, 22, 28]);
  363. computeAndAssert("\t\t\tfoo bar fooooooooooobooooooo", [3, 7, 11, 17, 23, 29]);
  364. computeAndAssert("\tfoo \t \t \t \t bar", [6, 12]); // 14
  365. },
  366. "test get longest line" : function() {
  367. var session = new EditSession(["12"]);
  368. session.setTabSize(4);
  369. assert.equal(session.getScreenWidth(), 2);
  370. session.doc.insertMergedLines({row: 0, column: Infinity}, ['', '']);
  371. session.doc.insertFullLines(1, ["123"]);
  372. assert.equal(session.getScreenWidth(), 3);
  373. session.doc.insertMergedLines({row: 0, column: Infinity}, ['', '']);
  374. session.doc.insertFullLines(1, ["\t\t"]);
  375. assert.equal(session.getScreenWidth(), 8);
  376. session.setTabSize(2);
  377. assert.equal(session.getScreenWidth(), 4);
  378. },
  379. "test getDisplayString": function() {
  380. var session = new EditSession(["12"]);
  381. session.setTabSize(4);
  382. assert.equal(session.$getDisplayTokens("\t").length, 4);
  383. assert.equal(session.$getDisplayTokens("abc").length, 3);
  384. assert.equal(session.$getDisplayTokens("abc\t").length, 4);
  385. },
  386. "test issue 83": function() {
  387. var session = new EditSession("");
  388. var editor = new Editor(new MockRenderer(), session);
  389. var document = session.getDocument();
  390. session.setUseWrapMode(true);
  391. document.insertFullLines(0, ["a", "b"]);
  392. document.insertFullLines(2, ["c", "d"]);
  393. document.removeFullLines(1, 2);
  394. },
  395. "test wrapMode init has to create wrapData array": function() {
  396. var session = new EditSession("foo bar\nfoo bar");
  397. var editor = new Editor(new MockRenderer(), session);
  398. session.setUseWrapMode(true);
  399. session.setWrapLimitRange(3, 3);
  400. session.adjustWrapLimit(80);
  401. // Test if wrapData is there and was computed.
  402. assert.equal(session.$wrapData.length, 2);
  403. assert.equal(session.$wrapData[0].length, 1);
  404. assert.equal(session.$wrapData[1].length, 1);
  405. },
  406. "test first line blank with wrap": function() {
  407. var session = new EditSession("\nfoo");
  408. session.setUseWrapMode(true);
  409. assert.equal(session.doc.getValue(), ["", "foo"].join("\n"));
  410. },
  411. "test first line blank with wrap 2" : function() {
  412. var session = new EditSession("");
  413. session.setUseWrapMode(true);
  414. session.setValue("\nfoo");
  415. assert.equal(session.doc.getValue(), ["", "foo"].join("\n"));
  416. },
  417. "test fold getFoldDisplayLine": function() {
  418. var session = createFoldTestSession();
  419. function assertDisplayLine(foldLine, str) {
  420. var line = session.getLine(foldLine.end.row);
  421. var displayLine =
  422. session.getFoldDisplayLine(foldLine, foldLine.end.row, line.length);
  423. assert.equal(displayLine, str);
  424. }
  425. assertDisplayLine(session.$foldData[0], "function foo(args...) {");
  426. assertDisplayLine(session.$foldData[1], " for (vfoo...ert(items[bar...\"juhu\");");
  427. },
  428. "test foldLine idxToPosition": function() {
  429. var session = createFoldTestSession();
  430. function assertIdx2Pos(foldLineIdx, idx, row, column) {
  431. var foldLine = session.$foldData[foldLineIdx];
  432. assert.position(foldLine.idxToPosition(idx), row, column);
  433. }
  434. // "function foo(items) {",
  435. // " for (var i=0; i<items.length; i++) {",
  436. // " alert(items[i] + \"juhu\");",
  437. // " } // Real Tab.",
  438. // "}"
  439. assertIdx2Pos(0, 12, 0, 12);
  440. assertIdx2Pos(0, 13, 0, 13);
  441. assertIdx2Pos(0, 14, 0, 13);
  442. assertIdx2Pos(0, 19, 0, 13);
  443. assertIdx2Pos(0, 20, 0, 18);
  444. assertIdx2Pos(1, 10, 1, 10);
  445. assertIdx2Pos(1, 11, 1, 10);
  446. assertIdx2Pos(1, 15, 1, 10);
  447. assertIdx2Pos(1, 16, 2, 10);
  448. assertIdx2Pos(1, 26, 2, 20);
  449. assertIdx2Pos(1, 27, 2, 20);
  450. assertIdx2Pos(1, 32, 2, 25);
  451. },
  452. "test fold documentToScreen": function() {
  453. var session = createFoldTestSession();
  454. function assertDoc2Screen(docRow, docCol, screenRow, screenCol) {
  455. assert.position(
  456. session.documentToScreenPosition(docRow, docCol),
  457. screenRow, screenCol
  458. );
  459. }
  460. // One fold ending in the same row.
  461. assertDoc2Screen(0, 0, 0, 0);
  462. assertDoc2Screen(0, 13, 0, 13);
  463. assertDoc2Screen(0, 14, 0, 13);
  464. assertDoc2Screen(0, 17, 0, 13);
  465. assertDoc2Screen(0, 18, 0, 20);
  466. // Fold ending on some other row.
  467. assertDoc2Screen(1, 0, 1, 0);
  468. assertDoc2Screen(1, 10, 1, 10);
  469. assertDoc2Screen(1, 11, 1, 10);
  470. assertDoc2Screen(1, 99, 1, 10);
  471. assertDoc2Screen(2, 0, 1, 10);
  472. assertDoc2Screen(2, 9, 1, 10);
  473. assertDoc2Screen(2, 10, 1, 16);
  474. assertDoc2Screen(2, 11, 1, 17);
  475. // Fold in the same row with fold over more then one row in the same row.
  476. assertDoc2Screen(2, 19, 1, 25);
  477. assertDoc2Screen(2, 20, 1, 26);
  478. assertDoc2Screen(2, 21, 1, 26);
  479. assertDoc2Screen(2, 24, 1, 26);
  480. assertDoc2Screen(2, 25, 1, 32);
  481. assertDoc2Screen(2, 26, 1, 33);
  482. assertDoc2Screen(2, 99, 1, 40);
  483. // Test one position after the folds. Should be all like normal.
  484. assertDoc2Screen(3, 0, 2, 0);
  485. },
  486. "test fold screenToDocument": function() {
  487. var session = createFoldTestSession();
  488. function assertScreen2Doc(docRow, docCol, screenRow, screenCol) {
  489. assert.position(
  490. session.screenToDocumentPosition(screenRow, screenCol),
  491. docRow, docCol
  492. );
  493. }
  494. // One fold ending in the same row.
  495. assertScreen2Doc(0, 0, 0, 0);
  496. assertScreen2Doc(0, 13, 0, 13);
  497. assertScreen2Doc(0, 13, 0, 14);
  498. assertScreen2Doc(0, 18, 0, 20);
  499. assertScreen2Doc(0, 19, 0, 21);
  500. // Fold ending on some other row.
  501. assertScreen2Doc(1, 0, 1, 0);
  502. assertScreen2Doc(1, 10, 1, 10);
  503. assertScreen2Doc(1, 10, 1, 11);
  504. assertScreen2Doc(1, 10, 1, 15);
  505. assertScreen2Doc(2, 10, 1, 16);
  506. assertScreen2Doc(2, 11, 1, 17);
  507. // Fold in the same row with fold over more then one row in the same row.
  508. assertScreen2Doc(2, 19, 1, 25);
  509. assertScreen2Doc(2, 20, 1, 26);
  510. assertScreen2Doc(2, 20, 1, 27);
  511. assertScreen2Doc(2, 20, 1, 31);
  512. assertScreen2Doc(2, 25, 1, 32);
  513. assertScreen2Doc(2, 26, 1, 33);
  514. assertScreen2Doc(2, 33, 1, 99);
  515. // Test one position after the folds. Should be all like normal.
  516. assertScreen2Doc(3, 0, 2, 0);
  517. },
  518. "test getFoldsInRange()": function() {
  519. var session = createFoldTestSession();
  520. var foldLines = session.$foldData;
  521. var folds = foldLines[0].folds.concat(foldLines[1].folds);
  522. function test(startRow, startColumn, endColumn, endRow, folds) {
  523. var r = new Range(startRow, startColumn, endColumn, endRow);
  524. var retFolds = session.getFoldsInRange(r);
  525. assert.ok(retFolds.length == folds.length);
  526. for (var i = 0; i < retFolds.length; i++) {
  527. assert.equal(retFolds[i].range + "", folds[i].range + "");
  528. }
  529. }
  530. test(0, 0, 0, 13, [ ]);
  531. test(0, 0, 0, 14, [ folds[0] ]);
  532. test(0, 0, 0, 18, [ folds[0] ]);
  533. test(0, 0, 1, 10, [ folds[0] ]);
  534. test(0, 0, 1, 11, [ folds[0], folds[1] ]);
  535. test(0, 18, 1, 11, [ folds[1] ]);
  536. test(2, 0, 2, 13, [ folds[1] ]);
  537. test(2, 10, 2, 20, [ ]);
  538. test(2, 10, 2, 11, [ ]);
  539. test(2, 19, 2, 20, [ ]);
  540. },
  541. "test fold one-line text insert": function() {
  542. // These are mostly test for the FoldLine.addRemoveChars function.
  543. var session = createFoldTestSession();
  544. var undoManager = session.getUndoManager();
  545. var foldLines = session.$foldData;
  546. function insert(row, column, text) {
  547. session.insert({row: row, column: column}, text);
  548. // Force the session to store all changes made to the document NOW
  549. // on the undoManager's queue. Otherwise we can't undo in separate
  550. // steps later.
  551. session.$syncInformUndoManager();
  552. }
  553. var foldLine, fold, folds;
  554. // First line.
  555. foldLine = session.$foldData[0];
  556. fold = foldLine.folds[0];
  557. insert(0, 0, "0");
  558. assert.range(foldLine.range, 0, 14, 0, 19);
  559. assert.range(fold.range, 0, 14, 0, 19);
  560. insert(0, 14, "1");
  561. assert.range(foldLine.range, 0, 15, 0, 20);
  562. assert.range(fold.range, 0, 15, 0, 20);
  563. insert(0, 20, "2");
  564. assert.range(foldLine.range, 0, 15, 0, 20);
  565. assert.range(fold.range, 0, 15, 0, 20);
  566. // Second line.
  567. foldLine = session.$foldData[1];
  568. folds = foldLine.folds;
  569. insert(1, 0, "3");
  570. assert.range(foldLine.range, 1, 11, 2, 25);
  571. assert.range(folds[0].range, 1, 11, 2, 10);
  572. assert.range(folds[1].range, 2, 20, 2, 25);
  573. insert(1, 11, "4");
  574. assert.range(foldLine.range, 1, 12, 2, 25);
  575. assert.range(folds[0].range, 1, 12, 2, 10);
  576. assert.range(folds[1].range, 2, 20, 2, 25);
  577. insert(2, 10, "5");
  578. assert.range(foldLine.range, 1, 12, 2, 26);
  579. assert.range(folds[0].range, 1, 12, 2, 10);
  580. assert.range(folds[1].range, 2, 21, 2, 26);
  581. insert(2, 21, "6");
  582. assert.range(foldLine.range, 1, 12, 2, 27);
  583. assert.range(folds[0].range, 1, 12, 2, 10);
  584. assert.range(folds[1].range, 2, 22, 2, 27);
  585. insert(2, 27, "7");
  586. assert.range(foldLine.range, 1, 12, 2, 27);
  587. assert.range(folds[0].range, 1, 12, 2, 10);
  588. assert.range(folds[1].range, 2, 22, 2, 27);
  589. // UNDO = REMOVE
  590. undoManager.undo(); // 6
  591. assert.range(foldLine.range, 1, 12, 2, 27);
  592. assert.range(folds[0].range, 1, 12, 2, 10);
  593. assert.range(folds[1].range, 2, 22, 2, 27);
  594. undoManager.undo(); // 5
  595. assert.range(foldLine.range, 1, 12, 2, 26);
  596. assert.range(folds[0].range, 1, 12, 2, 10);
  597. assert.range(folds[1].range, 2, 21, 2, 26);
  598. undoManager.undo(); // 4
  599. assert.range(foldLine.range, 1, 12, 2, 25);
  600. assert.range(folds[0].range, 1, 12, 2, 10);
  601. assert.range(folds[1].range, 2, 20, 2, 25);
  602. undoManager.undo(); // 3
  603. assert.range(foldLine.range, 1, 11, 2, 25);
  604. assert.range(folds[0].range, 1, 11, 2, 10);
  605. assert.range(folds[1].range, 2, 20, 2, 25);
  606. undoManager.undo(); // Beginning first line.
  607. assert.equal(foldLines.length, 2);
  608. assert.range(foldLines[0].range, 0, 15, 0, 20);
  609. assert.range(foldLines[1].range, 1, 10, 2, 25);
  610. foldLine = session.$foldData[0];
  611. fold = foldLine.folds[0];
  612. undoManager.undo(); // 2
  613. assert.range(foldLine.range, 0, 15, 0, 20);
  614. assert.range(fold.range, 0, 15, 0, 20);
  615. undoManager.undo(); // 1
  616. assert.range(foldLine.range, 0, 14, 0, 19);
  617. assert.range(fold.range, 0, 14, 0, 19);
  618. undoManager.undo(); // 0
  619. assert.range(foldLine.range, 0, 13, 0, 18);
  620. assert.range(fold.range, 0, 13, 0, 18);
  621. },
  622. "test fold multi-line insert/remove": function() {
  623. var session = createFoldTestSession(),
  624. undoManager = session.getUndoManager(),
  625. foldLines = session.$foldData;
  626. function insert(row, column, text) {
  627. session.insert({row: row, column: column}, text);
  628. // Force the session to store all changes made to the document NOW
  629. // on the undoManager's queue. Otherwise we can't undo in separate
  630. // steps later.
  631. session.$syncInformUndoManager();
  632. }
  633. var foldLines = session.$foldData;
  634. insert(0, 0, "\nfo0");
  635. assert.equal(foldLines.length, 2);
  636. assert.range(foldLines[0].range, 1, 16, 1, 21);
  637. assert.range(foldLines[1].range, 2, 10, 3, 25);
  638. insert(2, 0, "\nba1");
  639. assert.equal(foldLines.length, 2);
  640. assert.range(foldLines[0].range, 1, 16, 1, 21);
  641. assert.range(foldLines[1].range, 3, 13, 4, 25);
  642. insert(3, 10, "\nfo2");
  643. assert.equal(foldLines.length, 2);
  644. assert.range(foldLines[0].range, 1, 16, 1, 21);
  645. assert.range(foldLines[1].range, 4, 6, 5, 25);
  646. insert(5, 10, "\nba3");
  647. assert.equal(foldLines.length, 3);
  648. assert.range(foldLines[0].range, 1, 16, 1, 21);
  649. assert.range(foldLines[1].range, 4, 6, 5, 10);
  650. assert.range(foldLines[2].range, 6, 13, 6, 18);
  651. insert(6, 18, "\nfo4");
  652. assert.equal(foldLines.length, 3);
  653. assert.range(foldLines[0].range, 1, 16, 1, 21);
  654. assert.range(foldLines[1].range, 4, 6, 5, 10);
  655. assert.range(foldLines[2].range, 6, 13, 6, 18);
  656. undoManager.undo(); // 3
  657. assert.equal(foldLines.length, 3);
  658. assert.range(foldLines[0].range, 1, 16, 1, 21);
  659. assert.range(foldLines[1].range, 4, 6, 5, 10);
  660. assert.range(foldLines[2].range, 6, 13, 6, 18);
  661. undoManager.undo(); // 2
  662. assert.equal(foldLines.length, 2);
  663. assert.range(foldLines[0].range, 1, 16, 1, 21);
  664. assert.range(foldLines[1].range, 4, 6, 5, 25);
  665. undoManager.undo(); // 1
  666. assert.equal(foldLines.length, 2);
  667. assert.range(foldLines[0].range, 1, 16, 1, 21);
  668. assert.range(foldLines[1].range, 3, 13, 4, 25);
  669. undoManager.undo(); // 0
  670. assert.equal(foldLines.length, 2);
  671. assert.range(foldLines[0].range, 1, 16, 1, 21);
  672. assert.range(foldLines[1].range, 2, 10, 3, 25);
  673. undoManager.undo(); // Beginning
  674. assert.equal(foldLines.length, 2);
  675. assert.range(foldLines[0].range, 0, 13, 0, 18);
  676. assert.range(foldLines[1].range, 1, 10, 2, 25);
  677. // TODO: Add test for inseration inside of folds.
  678. },
  679. "test fold wrap data compution": function() {
  680. function assertWrap(line0, line1, line2) {
  681. line0 && assertArray(wrapData[0], line0);
  682. line1 && assertArray(wrapData[1], line1);
  683. line2 && assertArray(wrapData[2], line2);
  684. }
  685. function removeFoldAssertWrap(docRow, docColumn, line0, line1, line2) {
  686. session.removeFold(session.getFoldAt(docRow, docColumn));
  687. assertWrap(line0, line1, line2);
  688. }
  689. var lines = [
  690. "foo bar foo bar",
  691. "foo bar foo bar",
  692. "foo bar foo bar"
  693. ];
  694. var session = new EditSession(lines.join("\n"));
  695. session.setUseWrapMode(true);
  696. session.$wrapLimit = 7;
  697. session.$updateWrapData(0, 2);
  698. var wrapData = session.$wrapData;
  699. // Do a simple assertion without folds to check basic functionallity.
  700. assertWrap([8], [8], [8]);
  701. // --- Do in line folding ---
  702. // Adding a fold. The split position is inside of the fold. As placeholder
  703. // are not splitable, the split should be before the split.
  704. session.addFold("woot", new Range(0, 4, 0, 15));
  705. assertWrap([4], [8], [8]);
  706. // Remove the fold again which should reset the wrapData.
  707. removeFoldAssertWrap(0, 4, [8], [8], [8]);
  708. session.addFold("woot", new Range(0, 6, 0, 9));
  709. assertWrap([6, 13], [8], [8]);
  710. removeFoldAssertWrap(0, 6, [8], [8], [8]);
  711. // The fold fits into the wrap limit - no split expected.
  712. session.addFold("woot", new Range(0, 3, 0, 15));
  713. assertWrap([], [8], [8]);
  714. removeFoldAssertWrap(0, 4, [8], [8], [8]);
  715. // Fold after split position should be all fine.
  716. session.addFold("woot", new Range(0, 8, 0, 15));
  717. assertWrap([8], [8], [8]);
  718. removeFoldAssertWrap(0, 8, [8], [8], [8]);
  719. // Fold's placeholder is far too long for wrapSplit.
  720. session.addFold("woot0123456789", new Range(0, 8, 0, 15));
  721. assertWrap([8], [8], [8]);
  722. removeFoldAssertWrap(0, 8, [8], [8], [8]);
  723. // Fold's placeholder is far too long for wrapSplit
  724. // + content at the end of the line
  725. session.addFold("woot0123456789", new Range(0, 6, 0, 8));
  726. assertWrap([6, 20], [8], [8]);
  727. removeFoldAssertWrap(0, 8, [8], [8], [8]);
  728. session.addFold("woot0123456789", new Range(0, 6, 0, 8));
  729. session.addFold("woot0123456789", new Range(0, 8, 0, 10));
  730. assertWrap([6, 20, 34], [8], [8]);
  731. session.removeFold(session.getFoldAt(0, 7));
  732. removeFoldAssertWrap(0, 8, [8], [8], [8]);
  733. session.addFold("woot0123456789", new Range(0, 7, 0, 9));
  734. session.addFold("woot0123456789", new Range(0, 13, 0, 15));
  735. assertWrap([7, 21, 25], [8], [8]);
  736. session.removeFold(session.getFoldAt(0, 7));
  737. removeFoldAssertWrap(0, 14, [8], [8], [8]);
  738. // --- Do some multiline folding ---
  739. // Add a fold over two lines. Note, that the wrapData[1] stays the
  740. // same. This is an implementation detail and expected behavior.
  741. session.addFold("woot", new Range(0, 8, 1, 15));
  742. assertWrap([8], [8 /* See comments */], [8]);
  743. removeFoldAssertWrap(0, 8, [8], [8], [8]);
  744. session.addFold("woot", new Range(0, 9, 1, 11));
  745. assertWrap([8, 14], [8 /* See comments */], [8]);
  746. removeFoldAssertWrap(0, 9, [8], [8], [8]);
  747. session.addFold("woot", new Range(0, 9, 1, 15));
  748. assertWrap([8], [8 /* See comments */], [8]);
  749. removeFoldAssertWrap(0, 9, [8], [8], [8]);
  750. return session;
  751. },
  752. "test delete fold with wrap enabled": function() {
  753. var session = new EditSession("");
  754. session.setValue([
  755. "This is some placeholder text that will be folded inline.",
  756. "This is some placeholder text that will be folded inline.",
  757. "More text.",
  758. "<p>The cursor in this paragraph text will be offset by 1 row.<p>",
  759. "<p>Everything after this will be offset as well due to the folds in the row before too.</p>"
  760. ].join("\n"));
  761. session.addFold('...', new Range(0, 8, 0, 42));
  762. session.addFold('...', new Range(1, 8, 1, 42));
  763. session.addFold('...', new Range(3, 7, 3, 51));
  764. session.setOption("wrap", 40);
  765. session.remove(new Range(0,0, 2, 5));
  766. // needed because adjustWrapLimit is called async from renderer
  767. session.adjustWrapLimit(80);
  768. assert.equal(session.$wrapData + "", [[], [], [40, 76]] + "");
  769. },
  770. "test add fold": function() {
  771. var session = createFoldTestSession();
  772. var fold;
  773. function addFold(placeholder, range) {
  774. fold = session.addFold(placeholder, range);
  775. }
  776. addFold("foo", new Range(0, 13, 0, 17));
  777. addFold("foo", new Range(0, 14, 0, 18));
  778. addFold("foo", new Range(0, 13, 0, 18));
  779. assert.equal(session.$foldData[0].folds.length, 1);
  780. addFold("f", new Range(0, 13, 0, 18));
  781. addFold("foo", new Range(0, 18, 0, 21));
  782. assert.equal(session.$foldData[0].folds.length, 2);
  783. session.removeFold(fold);
  784. addFold("foo", new Range(0, 18, 0, 22));
  785. addFold("foo", new Range(0, 18, 0, 19));
  786. addFold("foo", new Range(0, 22, 1, 10));
  787. session.unfold();
  788. addFold("x", new Range(1, 9, 3, 15));
  789. addFold("x", new Range(2, 9, 2, 15));
  790. addFold("x", new Range(2, 10, 2, 18));
  791. addFold("x", new Range(2, 10, 2, 18));
  792. addFold("x", new Range(2, 25, 2, 27));
  793. addFold("x", new Range(2, 28, 2, 30));
  794. addFold("x", new Range(2, 7, 2, 29));
  795. var folds = session.getFoldsInRange(new Range(0,0,100,100));
  796. assert.equal(folds.length, 1);
  797. session.expandFolds(folds);
  798. folds = session.getFoldsInRange(new Range(0,0,100,100));
  799. assert.equal(folds.length, 1);
  800. session.expandFolds(folds);
  801. folds = session.getFoldsInRange(new Range(0,0,100,100));
  802. assert.equal(folds.length, 2);
  803. session.expandFolds(folds);
  804. folds = session.getFoldsInRange(new Range(0,0,100,100));
  805. assert.equal(folds.length, 0);
  806. },
  807. "test add subfolds": function() {
  808. var session = createFoldTestSession();
  809. var fold, oldFold;
  810. var foldData = session.$foldData;
  811. oldFold = foldData[0].folds[0];
  812. fold = session.addFold("fold0", new Range(0, 10, 0, 21));
  813. assert.equal(foldData[0].folds.length, 1);
  814. assert.equal(fold.subFolds.length, 1);
  815. assert.equal(fold.subFolds[0], oldFold);
  816. session.expandFold(fold);
  817. assert.equal(foldData[0].folds.length, 1);
  818. assert.equal(foldData[0].folds[0], oldFold);
  819. assert.equal(fold.subFolds.length, 0);
  820. fold = session.addFold("fold0", new Range(0, 13, 2, 10));
  821. assert.equal(foldData.length, 1);
  822. assert.equal(fold.subFolds.length, 2);
  823. assert.equal(fold.subFolds[0], oldFold);
  824. session.expandFold(fold);
  825. assert.equal(foldData.length, 2);
  826. assert.equal(foldData[0].folds.length, 1);
  827. assert.equal(foldData[0].folds[0], oldFold);
  828. assert.equal(fold.subFolds.length, 0);
  829. session.unfold(null, true);
  830. fold = session.addFold("fold0", new Range(0, 0, 0, 21));
  831. session.addFold("fold0", new Range(0, 1, 0, 5));
  832. session.addFold("fold0", new Range(0, 6, 0, 8));
  833. assert.equal(fold.subFolds.length, 2);
  834. },
  835. "test row cache": function() {
  836. var session = createFoldTestSession();
  837. session.screenToDocumentPosition(2,3);
  838. assertArray(session.$docRowCache, [1,3]);
  839. assertArray(session.$screenRowCache, [1,2]);
  840. session.screenToDocumentPosition(5,3);
  841. assertArray(session.$docRowCache, [1,3,4]);
  842. assertArray(session.$screenRowCache, [1,2,3]);
  843. session.screenToDocumentPosition(0,3);
  844. assertArray(session.$docRowCache, [1,3,4]);
  845. assertArray(session.$screenRowCache, [1,2,3]);
  846. var pos = session.screenToDocumentPosition(0,0);
  847. assert.equal(pos.row, 0);
  848. assertArray(session.$docRowCache, [1,3,4]);
  849. assertArray(session.$screenRowCache, [1,2,3]);
  850. session.screenToDocumentPosition(1,0);
  851. assertArray(session.$docRowCache, [1,3,4]);
  852. assertArray(session.$screenRowCache, [1,2,3]);
  853. session.$resetRowCache();
  854. assertArray(session.$docRowCache, []);
  855. assertArray(session.$screenRowCache, []);
  856. session.screenToDocumentPosition(1,3);
  857. assertArray(session.$docRowCache, [1]);
  858. assertArray(session.$screenRowCache, [1]);
  859. session.screenToDocumentPosition(5,3);
  860. assertArray(session.$docRowCache, [1,3,4]);
  861. assertArray(session.$screenRowCache, [1,2,3]);
  862. session = new EditSession(new Array(30).join("\n"));
  863. session.documentToScreenPosition(2,0);
  864. session.documentToScreenPosition(2,0);
  865. assertArray(session.$docRowCache, [1,2]);
  866. assertArray(session.$screenRowCache, [1,2]);
  867. },
  868. "test annotations": function() {
  869. var session = new EditSession([]),
  870. annotation = {row: 0, type: 'info', text: "This is a test."};
  871. session.clearAnnotations();
  872. assertArray(session.getAnnotations(), []);
  873. session.setAnnotations([annotation]);
  874. assertArray(session.getAnnotations(), [annotation]);
  875. },
  876. "test: mode loading" : function(next) {
  877. if (!ace_require.undef) {
  878. console.log("Skipping test: This test only runs in the browser");
  879. next();
  880. return;
  881. }
  882. var session = new EditSession([]);
  883. session.setMode("ace/mode/javascript");
  884. assert.equal(session.$modeid, "ace/mode/javascript");
  885. session.on("changeMode", function() {
  886. assert.equal(session.$modeid, "ace/mode/javascript");
  887. });
  888. session.setMode("ace/mode/sh", function(mode) {
  889. assert.ok(!mode);
  890. });
  891. setTimeout(function() {
  892. session.setMode("ace/mode/javascript", function(mode) {
  893. session.setMode("ace/mode/javascript");
  894. assert.equal(session.$modeid, "ace/mode/javascript");
  895. next();
  896. });
  897. }, 0);
  898. }
  899. };
  900. });
  901. if (typeof module !== "undefined" && module === ace_require.main) {
  902. ace_require("asyncjs").test.testcase(module.exports).exec();
  903. }