selection.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  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. define(function(require, exports, module) {
  31. "use strict";
  32. var oop = require("./lib/oop");
  33. var lang = require("./lib/lang");
  34. var EventEmitter = require("./lib/event_emitter").EventEmitter;
  35. var Range = require("./range").Range;
  36. /**
  37. * Contains the cursor position and the text selection of an edit session.
  38. *
  39. * The row/columns used in the selection are in document coordinates representing the coordinates as they appear in the document before applying soft wrap and folding.
  40. * @class Selection
  41. **/
  42. /**
  43. * Emitted when the cursor position changes.
  44. * @event changeCursor
  45. *
  46. **/
  47. /**
  48. * Emitted when the cursor selection changes.
  49. *
  50. * @event changeSelection
  51. **/
  52. /**
  53. * Creates a new `Selection` object.
  54. * @param {EditSession} session The session to use
  55. *
  56. * @constructor
  57. **/
  58. var Selection = function(session) {
  59. this.session = session;
  60. this.doc = session.getDocument();
  61. this.clearSelection();
  62. this.lead = this.selectionLead = this.doc.createAnchor(0, 0);
  63. this.anchor = this.selectionAnchor = this.doc.createAnchor(0, 0);
  64. var self = this;
  65. this.lead.on("change", function(e) {
  66. self._emit("changeCursor");
  67. if (!self.$isEmpty)
  68. self._emit("changeSelection");
  69. if (!self.$keepDesiredColumnOnChange && e.old.column != e.value.column)
  70. self.$desiredColumn = null;
  71. });
  72. this.selectionAnchor.on("change", function() {
  73. if (!self.$isEmpty)
  74. self._emit("changeSelection");
  75. });
  76. };
  77. (function() {
  78. oop.implement(this, EventEmitter);
  79. /**
  80. *
  81. * Returns `true` if the selection is empty.
  82. * @returns {Boolean}
  83. **/
  84. this.isEmpty = function() {
  85. return (this.$isEmpty || (
  86. this.anchor.row == this.lead.row &&
  87. this.anchor.column == this.lead.column
  88. ));
  89. };
  90. /**
  91. * Returns `true` if the selection is a multi-line.
  92. * @returns {Boolean}
  93. **/
  94. this.isMultiLine = function() {
  95. if (this.isEmpty()) {
  96. return false;
  97. }
  98. return this.getRange().isMultiLine();
  99. };
  100. /**
  101. * Returns an object containing the `row` and `column` current position of the cursor.
  102. * @returns {Object}
  103. **/
  104. this.getCursor = function() {
  105. return this.lead.getPosition();
  106. };
  107. /**
  108. * Sets the row and column position of the anchor. This function also emits the `'changeSelection'` event.
  109. * @param {Number} row The new row
  110. * @param {Number} column The new column
  111. *
  112. *
  113. **/
  114. this.setSelectionAnchor = function(row, column) {
  115. this.anchor.setPosition(row, column);
  116. if (this.$isEmpty) {
  117. this.$isEmpty = false;
  118. this._emit("changeSelection");
  119. }
  120. };
  121. /**
  122. * Returns an object containing the `row` and `column` of the calling selection anchor.
  123. *
  124. * @returns {Object}
  125. * @related Anchor.getPosition
  126. **/
  127. this.getSelectionAnchor = function() {
  128. if (this.$isEmpty)
  129. return this.getSelectionLead();
  130. else
  131. return this.anchor.getPosition();
  132. };
  133. /**
  134. *
  135. * Returns an object containing the `row` and `column` of the calling selection lead.
  136. * @returns {Object}
  137. **/
  138. this.getSelectionLead = function() {
  139. return this.lead.getPosition();
  140. };
  141. /**
  142. * Shifts the selection up (or down, if [[Selection.isBackwards `isBackwards()`]] is true) the given number of columns.
  143. * @param {Number} columns The number of columns to shift by
  144. *
  145. *
  146. *
  147. **/
  148. this.shiftSelection = function(columns) {
  149. if (this.$isEmpty) {
  150. this.moveCursorTo(this.lead.row, this.lead.column + columns);
  151. return;
  152. }
  153. var anchor = this.getSelectionAnchor();
  154. var lead = this.getSelectionLead();
  155. var isBackwards = this.isBackwards();
  156. if (!isBackwards || anchor.column !== 0)
  157. this.setSelectionAnchor(anchor.row, anchor.column + columns);
  158. if (isBackwards || lead.column !== 0) {
  159. this.$moveSelection(function() {
  160. this.moveCursorTo(lead.row, lead.column + columns);
  161. });
  162. }
  163. };
  164. /**
  165. * Returns `true` if the selection is going backwards in the document.
  166. * @returns {Boolean}
  167. **/
  168. this.isBackwards = function() {
  169. var anchor = this.anchor;
  170. var lead = this.lead;
  171. return (anchor.row > lead.row || (anchor.row == lead.row && anchor.column > lead.column));
  172. };
  173. /**
  174. * [Returns the [[Range]] for the selected text.]{: #Selection.getRange}
  175. * @returns {Range}
  176. **/
  177. this.getRange = function() {
  178. var anchor = this.anchor;
  179. var lead = this.lead;
  180. if (this.isEmpty())
  181. return Range.fromPoints(lead, lead);
  182. if (this.isBackwards()) {
  183. return Range.fromPoints(lead, anchor);
  184. }
  185. else {
  186. return Range.fromPoints(anchor, lead);
  187. }
  188. };
  189. /**
  190. * [Empties the selection (by de-selecting it). This function also emits the `'changeSelection'` event.]{: #Selection.clearSelection}
  191. **/
  192. this.clearSelection = function() {
  193. if (!this.$isEmpty) {
  194. this.$isEmpty = true;
  195. this._emit("changeSelection");
  196. }
  197. };
  198. /**
  199. * Selects all the text in the document.
  200. **/
  201. this.selectAll = function() {
  202. var lastRow = this.doc.getLength() - 1;
  203. this.setSelectionAnchor(0, 0);
  204. this.moveCursorTo(lastRow, this.doc.getLine(lastRow).length);
  205. };
  206. /**
  207. * Sets the selection to the provided range.
  208. * @param {Range} range The range of text to select
  209. * @param {Boolean} reverse Indicates if the range should go backwards (`true`) or not
  210. *
  211. *
  212. * @method setSelectionRange
  213. * @alias setRange
  214. **/
  215. this.setRange =
  216. this.setSelectionRange = function(range, reverse) {
  217. if (reverse) {
  218. this.setSelectionAnchor(range.end.row, range.end.column);
  219. this.selectTo(range.start.row, range.start.column);
  220. } else {
  221. this.setSelectionAnchor(range.start.row, range.start.column);
  222. this.selectTo(range.end.row, range.end.column);
  223. }
  224. if (this.getRange().isEmpty())
  225. this.$isEmpty = true;
  226. this.$desiredColumn = null;
  227. };
  228. this.$moveSelection = function(mover) {
  229. var lead = this.lead;
  230. if (this.$isEmpty)
  231. this.setSelectionAnchor(lead.row, lead.column);
  232. mover.call(this);
  233. };
  234. /**
  235. * Moves the selection cursor to the indicated row and column.
  236. * @param {Number} row The row to select to
  237. * @param {Number} column The column to select to
  238. *
  239. *
  240. *
  241. **/
  242. this.selectTo = function(row, column) {
  243. this.$moveSelection(function() {
  244. this.moveCursorTo(row, column);
  245. });
  246. };
  247. /**
  248. * Moves the selection cursor to the row and column indicated by `pos`.
  249. * @param {Object} pos An object containing the row and column
  250. *
  251. *
  252. *
  253. **/
  254. this.selectToPosition = function(pos) {
  255. this.$moveSelection(function() {
  256. this.moveCursorToPosition(pos);
  257. });
  258. };
  259. /**
  260. * Moves the selection cursor to the indicated row and column.
  261. * @param {Number} row The row to select to
  262. * @param {Number} column The column to select to
  263. *
  264. **/
  265. this.moveTo = function(row, column) {
  266. this.clearSelection();
  267. this.moveCursorTo(row, column);
  268. };
  269. /**
  270. * Moves the selection cursor to the row and column indicated by `pos`.
  271. * @param {Object} pos An object containing the row and column
  272. **/
  273. this.moveToPosition = function(pos) {
  274. this.clearSelection();
  275. this.moveCursorToPosition(pos);
  276. };
  277. /**
  278. *
  279. * Moves the selection up one row.
  280. **/
  281. this.selectUp = function() {
  282. this.$moveSelection(this.moveCursorUp);
  283. };
  284. /**
  285. *
  286. * Moves the selection down one row.
  287. **/
  288. this.selectDown = function() {
  289. this.$moveSelection(this.moveCursorDown);
  290. };
  291. /**
  292. *
  293. *
  294. * Moves the selection right one column.
  295. **/
  296. this.selectRight = function() {
  297. this.$moveSelection(this.moveCursorRight);
  298. };
  299. /**
  300. *
  301. * Moves the selection left one column.
  302. **/
  303. this.selectLeft = function() {
  304. this.$moveSelection(this.moveCursorLeft);
  305. };
  306. /**
  307. *
  308. * Moves the selection to the beginning of the current line.
  309. **/
  310. this.selectLineStart = function() {
  311. this.$moveSelection(this.moveCursorLineStart);
  312. };
  313. /**
  314. *
  315. * Moves the selection to the end of the current line.
  316. **/
  317. this.selectLineEnd = function() {
  318. this.$moveSelection(this.moveCursorLineEnd);
  319. };
  320. /**
  321. *
  322. * Moves the selection to the end of the file.
  323. **/
  324. this.selectFileEnd = function() {
  325. this.$moveSelection(this.moveCursorFileEnd);
  326. };
  327. /**
  328. *
  329. * Moves the selection to the start of the file.
  330. **/
  331. this.selectFileStart = function() {
  332. this.$moveSelection(this.moveCursorFileStart);
  333. };
  334. /**
  335. *
  336. * Moves the selection to the first word on the right.
  337. **/
  338. this.selectWordRight = function() {
  339. this.$moveSelection(this.moveCursorWordRight);
  340. };
  341. /**
  342. *
  343. * Moves the selection to the first word on the left.
  344. **/
  345. this.selectWordLeft = function() {
  346. this.$moveSelection(this.moveCursorWordLeft);
  347. };
  348. /**
  349. * Moves the selection to highlight the entire word.
  350. * @related EditSession.getWordRange
  351. **/
  352. this.getWordRange = function(row, column) {
  353. if (typeof column == "undefined") {
  354. var cursor = row || this.lead;
  355. row = cursor.row;
  356. column = cursor.column;
  357. }
  358. return this.session.getWordRange(row, column);
  359. };
  360. /**
  361. *
  362. * Selects an entire word boundary.
  363. **/
  364. this.selectWord = function() {
  365. this.setSelectionRange(this.getWordRange());
  366. };
  367. /**
  368. * Selects a word, including its right whitespace.
  369. * @related EditSession.getAWordRange
  370. **/
  371. this.selectAWord = function() {
  372. var cursor = this.getCursor();
  373. var range = this.session.getAWordRange(cursor.row, cursor.column);
  374. this.setSelectionRange(range);
  375. };
  376. this.getLineRange = function(row, excludeLastChar) {
  377. var rowStart = typeof row == "number" ? row : this.lead.row;
  378. var rowEnd;
  379. var foldLine = this.session.getFoldLine(rowStart);
  380. if (foldLine) {
  381. rowStart = foldLine.start.row;
  382. rowEnd = foldLine.end.row;
  383. } else {
  384. rowEnd = rowStart;
  385. }
  386. if (excludeLastChar === true)
  387. return new Range(rowStart, 0, rowEnd, this.session.getLine(rowEnd).length);
  388. else
  389. return new Range(rowStart, 0, rowEnd + 1, 0);
  390. };
  391. /**
  392. * Selects the entire line.
  393. **/
  394. this.selectLine = function() {
  395. this.setSelectionRange(this.getLineRange());
  396. };
  397. /**
  398. *
  399. * Moves the cursor up one row.
  400. **/
  401. this.moveCursorUp = function() {
  402. this.moveCursorBy(-1, 0);
  403. };
  404. /**
  405. *
  406. * Moves the cursor down one row.
  407. **/
  408. this.moveCursorDown = function() {
  409. this.moveCursorBy(1, 0);
  410. };
  411. /**
  412. *
  413. * Moves the cursor left one column.
  414. **/
  415. this.moveCursorLeft = function() {
  416. var cursor = this.lead.getPosition(),
  417. fold;
  418. if (fold = this.session.getFoldAt(cursor.row, cursor.column, -1)) {
  419. this.moveCursorTo(fold.start.row, fold.start.column);
  420. } else if (cursor.column === 0) {
  421. // cursor is a line (start
  422. if (cursor.row > 0) {
  423. this.moveCursorTo(cursor.row - 1, this.doc.getLine(cursor.row - 1).length);
  424. }
  425. }
  426. else {
  427. var tabSize = this.session.getTabSize();
  428. if (this.session.isTabStop(cursor) && this.doc.getLine(cursor.row).slice(cursor.column-tabSize, cursor.column).split(" ").length-1 == tabSize)
  429. this.moveCursorBy(0, -tabSize);
  430. else
  431. this.moveCursorBy(0, -1);
  432. }
  433. };
  434. /**
  435. *
  436. * Moves the cursor right one column.
  437. **/
  438. this.moveCursorRight = function() {
  439. var cursor = this.lead.getPosition(),
  440. fold;
  441. if (fold = this.session.getFoldAt(cursor.row, cursor.column, 1)) {
  442. this.moveCursorTo(fold.end.row, fold.end.column);
  443. }
  444. else if (this.lead.column == this.doc.getLine(this.lead.row).length) {
  445. if (this.lead.row < this.doc.getLength() - 1) {
  446. this.moveCursorTo(this.lead.row + 1, 0);
  447. }
  448. }
  449. else {
  450. var tabSize = this.session.getTabSize();
  451. var cursor = this.lead;
  452. if (this.session.isTabStop(cursor) && this.doc.getLine(cursor.row).slice(cursor.column, cursor.column+tabSize).split(" ").length-1 == tabSize)
  453. this.moveCursorBy(0, tabSize);
  454. else
  455. this.moveCursorBy(0, 1);
  456. }
  457. };
  458. /**
  459. *
  460. * Moves the cursor to the start of the line.
  461. **/
  462. this.moveCursorLineStart = function() {
  463. var row = this.lead.row;
  464. var column = this.lead.column;
  465. var screenRow = this.session.documentToScreenRow(row, column);
  466. // Determ the doc-position of the first character at the screen line.
  467. var firstColumnPosition = this.session.screenToDocumentPosition(screenRow, 0);
  468. // Determ the line
  469. var beforeCursor = this.session.getDisplayLine(
  470. row, null, firstColumnPosition.row,
  471. firstColumnPosition.column
  472. );
  473. var leadingSpace = beforeCursor.match(/^\s*/);
  474. // TODO find better way for emacs mode to override selection behaviors
  475. if (leadingSpace[0].length != column && !this.session.$useEmacsStyleLineStart)
  476. firstColumnPosition.column += leadingSpace[0].length;
  477. this.moveCursorToPosition(firstColumnPosition);
  478. };
  479. /**
  480. *
  481. * Moves the cursor to the end of the line.
  482. **/
  483. this.moveCursorLineEnd = function() {
  484. var lead = this.lead;
  485. var lineEnd = this.session.getDocumentLastRowColumnPosition(lead.row, lead.column);
  486. if (this.lead.column == lineEnd.column) {
  487. var line = this.session.getLine(lineEnd.row);
  488. if (lineEnd.column == line.length) {
  489. var textEnd = line.search(/\s+$/);
  490. if (textEnd > 0)
  491. lineEnd.column = textEnd;
  492. }
  493. }
  494. this.moveCursorTo(lineEnd.row, lineEnd.column);
  495. };
  496. /**
  497. *
  498. * Moves the cursor to the end of the file.
  499. **/
  500. this.moveCursorFileEnd = function() {
  501. var row = this.doc.getLength() - 1;
  502. var column = this.doc.getLine(row).length;
  503. this.moveCursorTo(row, column);
  504. };
  505. /**
  506. *
  507. * Moves the cursor to the start of the file.
  508. **/
  509. this.moveCursorFileStart = function() {
  510. this.moveCursorTo(0, 0);
  511. };
  512. /**
  513. *
  514. * Moves the cursor to the word on the right.
  515. **/
  516. this.moveCursorLongWordRight = function() {
  517. var row = this.lead.row;
  518. var column = this.lead.column;
  519. var line = this.doc.getLine(row);
  520. var rightOfCursor = line.substring(column);
  521. var match;
  522. this.session.nonTokenRe.lastIndex = 0;
  523. this.session.tokenRe.lastIndex = 0;
  524. // skip folds
  525. var fold = this.session.getFoldAt(row, column, 1);
  526. if (fold) {
  527. this.moveCursorTo(fold.end.row, fold.end.column);
  528. return;
  529. }
  530. // first skip space
  531. if (match = this.session.nonTokenRe.exec(rightOfCursor)) {
  532. column += this.session.nonTokenRe.lastIndex;
  533. this.session.nonTokenRe.lastIndex = 0;
  534. rightOfCursor = line.substring(column);
  535. }
  536. // if at line end proceed with next line
  537. if (column >= line.length) {
  538. this.moveCursorTo(row, line.length);
  539. this.moveCursorRight();
  540. if (row < this.doc.getLength() - 1)
  541. this.moveCursorWordRight();
  542. return;
  543. }
  544. // advance to the end of the next token
  545. if (match = this.session.tokenRe.exec(rightOfCursor)) {
  546. column += this.session.tokenRe.lastIndex;
  547. this.session.tokenRe.lastIndex = 0;
  548. }
  549. this.moveCursorTo(row, column);
  550. };
  551. /**
  552. *
  553. * Moves the cursor to the word on the left.
  554. **/
  555. this.moveCursorLongWordLeft = function() {
  556. var row = this.lead.row;
  557. var column = this.lead.column;
  558. // skip folds
  559. var fold;
  560. if (fold = this.session.getFoldAt(row, column, -1)) {
  561. this.moveCursorTo(fold.start.row, fold.start.column);
  562. return;
  563. }
  564. var str = this.session.getFoldStringAt(row, column, -1);
  565. if (str == null) {
  566. str = this.doc.getLine(row).substring(0, column);
  567. }
  568. var leftOfCursor = lang.stringReverse(str);
  569. var match;
  570. this.session.nonTokenRe.lastIndex = 0;
  571. this.session.tokenRe.lastIndex = 0;
  572. // skip whitespace
  573. if (match = this.session.nonTokenRe.exec(leftOfCursor)) {
  574. column -= this.session.nonTokenRe.lastIndex;
  575. leftOfCursor = leftOfCursor.slice(this.session.nonTokenRe.lastIndex);
  576. this.session.nonTokenRe.lastIndex = 0;
  577. }
  578. // if at begin of the line proceed in line above
  579. if (column <= 0) {
  580. this.moveCursorTo(row, 0);
  581. this.moveCursorLeft();
  582. if (row > 0)
  583. this.moveCursorWordLeft();
  584. return;
  585. }
  586. // move to the begin of the word
  587. if (match = this.session.tokenRe.exec(leftOfCursor)) {
  588. column -= this.session.tokenRe.lastIndex;
  589. this.session.tokenRe.lastIndex = 0;
  590. }
  591. this.moveCursorTo(row, column);
  592. };
  593. this.$shortWordEndIndex = function(rightOfCursor) {
  594. var match, index = 0, ch;
  595. var whitespaceRe = /\s/;
  596. var tokenRe = this.session.tokenRe;
  597. tokenRe.lastIndex = 0;
  598. if (match = this.session.tokenRe.exec(rightOfCursor)) {
  599. index = this.session.tokenRe.lastIndex;
  600. } else {
  601. while ((ch = rightOfCursor[index]) && whitespaceRe.test(ch))
  602. index ++;
  603. if (index < 1) {
  604. tokenRe.lastIndex = 0;
  605. while ((ch = rightOfCursor[index]) && !tokenRe.test(ch)) {
  606. tokenRe.lastIndex = 0;
  607. index ++;
  608. if (whitespaceRe.test(ch)) {
  609. if (index > 2) {
  610. index--;
  611. break;
  612. } else {
  613. while ((ch = rightOfCursor[index]) && whitespaceRe.test(ch))
  614. index ++;
  615. if (index > 2)
  616. break;
  617. }
  618. }
  619. }
  620. }
  621. }
  622. tokenRe.lastIndex = 0;
  623. return index;
  624. };
  625. this.moveCursorShortWordRight = function() {
  626. var row = this.lead.row;
  627. var column = this.lead.column;
  628. var line = this.doc.getLine(row);
  629. var rightOfCursor = line.substring(column);
  630. var fold = this.session.getFoldAt(row, column, 1);
  631. if (fold)
  632. return this.moveCursorTo(fold.end.row, fold.end.column);
  633. if (column == line.length) {
  634. var l = this.doc.getLength();
  635. do {
  636. row++;
  637. rightOfCursor = this.doc.getLine(row);
  638. } while (row < l && /^\s*$/.test(rightOfCursor));
  639. if (!/^\s+/.test(rightOfCursor))
  640. rightOfCursor = "";
  641. column = 0;
  642. }
  643. var index = this.$shortWordEndIndex(rightOfCursor);
  644. this.moveCursorTo(row, column + index);
  645. };
  646. this.moveCursorShortWordLeft = function() {
  647. var row = this.lead.row;
  648. var column = this.lead.column;
  649. var fold;
  650. if (fold = this.session.getFoldAt(row, column, -1))
  651. return this.moveCursorTo(fold.start.row, fold.start.column);
  652. var line = this.session.getLine(row).substring(0, column);
  653. if (column === 0) {
  654. do {
  655. row--;
  656. line = this.doc.getLine(row);
  657. } while (row > 0 && /^\s*$/.test(line));
  658. column = line.length;
  659. if (!/\s+$/.test(line))
  660. line = "";
  661. }
  662. var leftOfCursor = lang.stringReverse(line);
  663. var index = this.$shortWordEndIndex(leftOfCursor);
  664. return this.moveCursorTo(row, column - index);
  665. };
  666. this.moveCursorWordRight = function() {
  667. if (this.session.$selectLongWords)
  668. this.moveCursorLongWordRight();
  669. else
  670. this.moveCursorShortWordRight();
  671. };
  672. this.moveCursorWordLeft = function() {
  673. if (this.session.$selectLongWords)
  674. this.moveCursorLongWordLeft();
  675. else
  676. this.moveCursorShortWordLeft();
  677. };
  678. /**
  679. * Moves the cursor to position indicated by the parameters. Negative numbers move the cursor backwards in the document.
  680. * @param {Number} rows The number of rows to move by
  681. * @param {Number} chars The number of characters to move by
  682. *
  683. *
  684. * @related EditSession.documentToScreenPosition
  685. **/
  686. this.moveCursorBy = function(rows, chars) {
  687. var screenPos = this.session.documentToScreenPosition(
  688. this.lead.row,
  689. this.lead.column
  690. );
  691. if (chars === 0) {
  692. if (this.$desiredColumn)
  693. screenPos.column = this.$desiredColumn;
  694. else
  695. this.$desiredColumn = screenPos.column;
  696. }
  697. var docPos = this.session.screenToDocumentPosition(screenPos.row + rows, screenPos.column);
  698. if (rows !== 0 && chars === 0 && docPos.row === this.lead.row && docPos.column === this.lead.column) {
  699. if (this.session.lineWidgets && this.session.lineWidgets[docPos.row]) {
  700. if (docPos.row > 0 || rows > 0)
  701. docPos.row++;
  702. }
  703. }
  704. // move the cursor and update the desired column
  705. this.moveCursorTo(docPos.row, docPos.column + chars, chars === 0);
  706. };
  707. /**
  708. * Moves the selection to the position indicated by its `row` and `column`.
  709. * @param {Object} position The position to move to
  710. *
  711. *
  712. **/
  713. this.moveCursorToPosition = function(position) {
  714. this.moveCursorTo(position.row, position.column);
  715. };
  716. /**
  717. * Moves the cursor to the row and column provided. [If `preventUpdateDesiredColumn` is `true`, then the cursor stays in the same column position as its original point.]{: #preventUpdateBoolDesc}
  718. * @param {Number} row The row to move to
  719. * @param {Number} column The column to move to
  720. * @param {Boolean} keepDesiredColumn [If `true`, the cursor move does not respect the previous column]{: #preventUpdateBool}
  721. *
  722. *
  723. **/
  724. this.moveCursorTo = function(row, column, keepDesiredColumn) {
  725. // Ensure the row/column is not inside of a fold.
  726. var fold = this.session.getFoldAt(row, column, 1);
  727. if (fold) {
  728. row = fold.start.row;
  729. column = fold.start.column;
  730. }
  731. this.$keepDesiredColumnOnChange = true;
  732. this.lead.setPosition(row, column);
  733. this.$keepDesiredColumnOnChange = false;
  734. if (!keepDesiredColumn)
  735. this.$desiredColumn = null;
  736. };
  737. /**
  738. * Moves the cursor to the screen position indicated by row and column. {:preventUpdateBoolDesc}
  739. * @param {Number} row The row to move to
  740. * @param {Number} column The column to move to
  741. * @param {Boolean} keepDesiredColumn {:preventUpdateBool}
  742. *
  743. *
  744. **/
  745. this.moveCursorToScreen = function(row, column, keepDesiredColumn) {
  746. var pos = this.session.screenToDocumentPosition(row, column);
  747. this.moveCursorTo(pos.row, pos.column, keepDesiredColumn);
  748. };
  749. // remove listeners from document
  750. this.detach = function() {
  751. this.lead.detach();
  752. this.anchor.detach();
  753. this.session = this.doc = null;
  754. };
  755. this.fromOrientedRange = function(range) {
  756. this.setSelectionRange(range, range.cursor == range.start);
  757. this.$desiredColumn = range.desiredColumn || this.$desiredColumn;
  758. };
  759. this.toOrientedRange = function(range) {
  760. var r = this.getRange();
  761. if (range) {
  762. range.start.column = r.start.column;
  763. range.start.row = r.start.row;
  764. range.end.column = r.end.column;
  765. range.end.row = r.end.row;
  766. } else {
  767. range = r;
  768. }
  769. range.cursor = this.isBackwards() ? range.start : range.end;
  770. range.desiredColumn = this.$desiredColumn;
  771. return range;
  772. };
  773. /**
  774. * Saves the current cursor position and calls `func` that can change the cursor
  775. * postion. The result is the range of the starting and eventual cursor position.
  776. * Will reset the cursor position.
  777. * @param {Function} The callback that should change the cursor position
  778. * @returns {Range}
  779. *
  780. **/
  781. this.getRangeOfMovements = function(func) {
  782. var start = this.getCursor();
  783. try {
  784. func.call(null, this);
  785. var end = this.getCursor();
  786. return Range.fromPoints(start,end);
  787. } catch(e) {
  788. return Range.fromPoints(start,start);
  789. } finally {
  790. this.moveCursorToPosition(start);
  791. }
  792. };
  793. this.toJSON = function() {
  794. if (this.rangeCount) {
  795. var data = this.ranges.map(function(r) {
  796. var r1 = r.clone();
  797. r1.isBackwards = r.cursor == r.start;
  798. return r1;
  799. });
  800. } else {
  801. var data = this.getRange();
  802. data.isBackwards = this.isBackwards();
  803. }
  804. return data;
  805. };
  806. this.fromJSON = function(data) {
  807. if (data.start == undefined) {
  808. if (this.rangeList) {
  809. this.toSingleRange(data[0]);
  810. for (var i = data.length; i--; ) {
  811. var r = Range.fromPoints(data[i].start, data[i].end);
  812. if (data[i].isBackwards)
  813. r.cursor = r.start;
  814. this.addRange(r, true);
  815. }
  816. return;
  817. } else
  818. data = data[0];
  819. }
  820. if (this.rangeList)
  821. this.toSingleRange(data);
  822. this.setSelectionRange(data, data.isBackwards);
  823. };
  824. this.isEqual = function(data) {
  825. if ((data.length || this.rangeCount) && data.length != this.rangeCount)
  826. return false;
  827. if (!data.length || !this.ranges)
  828. return this.getRange().isEqual(data);
  829. for (var i = this.ranges.length; i--; ) {
  830. if (!this.ranges[i].isEqual(data[i]))
  831. return false;
  832. }
  833. return true;
  834. };
  835. }).call(Selection.prototype);
  836. exports.Selection = Selection;
  837. });