elastic_tabstops_lite.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2012, 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 ElasticTabstopsLite = function(editor) {
  33. this.$editor = editor;
  34. var self = this;
  35. var changedRows = [];
  36. var recordChanges = false;
  37. this.onAfterExec = function() {
  38. recordChanges = false;
  39. self.processRows(changedRows);
  40. changedRows = [];
  41. };
  42. this.onExec = function() {
  43. recordChanges = true;
  44. };
  45. this.onChange = function(e) {
  46. var range = e.data.range
  47. if (recordChanges) {
  48. if (changedRows.indexOf(range.start.row) == -1)
  49. changedRows.push(range.start.row);
  50. if (range.end.row != range.start.row)
  51. changedRows.push(range.end.row);
  52. }
  53. };
  54. };
  55. (function() {
  56. this.processRows = function(rows) {
  57. this.$inChange = true;
  58. var checkedRows = [];
  59. for (var r = 0, rowCount = rows.length; r < rowCount; r++) {
  60. var row = rows[r];
  61. if (checkedRows.indexOf(row) > -1)
  62. continue;
  63. var cellWidthObj = this.$findCellWidthsForBlock(row);
  64. var cellWidths = this.$setBlockCellWidthsToMax(cellWidthObj.cellWidths);
  65. var rowIndex = cellWidthObj.firstRow;
  66. for (var w = 0, l = cellWidths.length; w < l; w++) {
  67. var widths = cellWidths[w];
  68. checkedRows.push(rowIndex);
  69. this.$adjustRow(rowIndex, widths);
  70. rowIndex++;
  71. }
  72. }
  73. this.$inChange = false;
  74. };
  75. this.$findCellWidthsForBlock = function(row) {
  76. var cellWidths = [], widths;
  77. // starting row and backward
  78. var rowIter = row;
  79. while (rowIter >= 0) {
  80. widths = this.$cellWidthsForRow(rowIter);
  81. if (widths.length == 0)
  82. break;
  83. cellWidths.unshift(widths);
  84. rowIter--;
  85. }
  86. var firstRow = rowIter + 1;
  87. // forward (not including starting row)
  88. rowIter = row;
  89. var numRows = this.$editor.session.getLength();
  90. while (rowIter < numRows - 1) {
  91. rowIter++;
  92. widths = this.$cellWidthsForRow(rowIter);
  93. if (widths.length == 0)
  94. break;
  95. cellWidths.push(widths);
  96. }
  97. return { cellWidths: cellWidths, firstRow: firstRow };
  98. };
  99. this.$cellWidthsForRow = function(row) {
  100. var selectionColumns = this.$selectionColumnsForRow(row);
  101. // todo: support multicursor
  102. var tabs = [-1].concat(this.$tabsForRow(row));
  103. var widths = tabs.map(function(el) { return 0; } ).slice(1);
  104. var line = this.$editor.session.getLine(row);
  105. for (var i = 0, len = tabs.length - 1; i < len; i++) {
  106. var leftEdge = tabs[i]+1;
  107. var rightEdge = tabs[i+1];
  108. var rightmostSelection = this.$rightmostSelectionInCell(selectionColumns, rightEdge);
  109. var cell = line.substring(leftEdge, rightEdge);
  110. widths[i] = Math.max(cell.replace(/\s+$/g,'').length, rightmostSelection - leftEdge);
  111. }
  112. return widths;
  113. };
  114. this.$selectionColumnsForRow = function(row) {
  115. var selections = [], cursor = this.$editor.getCursorPosition();
  116. if (this.$editor.session.getSelection().isEmpty()) {
  117. // todo: support multicursor
  118. if (row == cursor.row)
  119. selections.push(cursor.column);
  120. }
  121. return selections;
  122. };
  123. this.$setBlockCellWidthsToMax = function(cellWidths) {
  124. var startingNewBlock = true, blockStartRow, blockEndRow, maxWidth;
  125. var columnInfo = this.$izip_longest(cellWidths);
  126. for (var c = 0, l = columnInfo.length; c < l; c++) {
  127. var column = columnInfo[c];
  128. if (!column.push) {
  129. console.error(column);
  130. continue;
  131. }
  132. // add an extra None to the end so that the end of the column automatically
  133. // finishes a block
  134. column.push(NaN);
  135. for (var r = 0, s = column.length; r < s; r++) {
  136. var width = column[r];
  137. if (startingNewBlock) {
  138. blockStartRow = r;
  139. maxWidth = 0;
  140. startingNewBlock = false;
  141. }
  142. if (isNaN(width)) {
  143. // block ended
  144. blockEndRow = r;
  145. for (var j = blockStartRow; j < blockEndRow; j++) {
  146. cellWidths[j][c] = maxWidth;
  147. }
  148. startingNewBlock = true;
  149. }
  150. maxWidth = Math.max(maxWidth, width);
  151. }
  152. }
  153. return cellWidths;
  154. };
  155. this.$rightmostSelectionInCell = function(selectionColumns, cellRightEdge) {
  156. var rightmost = 0;
  157. if (selectionColumns.length) {
  158. var lengths = [];
  159. for (var s = 0, length = selectionColumns.length; s < length; s++) {
  160. if (selectionColumns[s] <= cellRightEdge)
  161. lengths.push(s);
  162. else
  163. lengths.push(0);
  164. }
  165. rightmost = Math.max.apply(Math, lengths);
  166. }
  167. return rightmost;
  168. };
  169. this.$tabsForRow = function(row) {
  170. var rowTabs = [], line = this.$editor.session.getLine(row),
  171. re = /\t/g, match;
  172. while ((match = re.exec(line)) != null) {
  173. rowTabs.push(match.index);
  174. }
  175. return rowTabs;
  176. };
  177. this.$adjustRow = function(row, widths) {
  178. var rowTabs = this.$tabsForRow(row);
  179. if (rowTabs.length == 0)
  180. return;
  181. var bias = 0, location = -1;
  182. // this always only contains two elements, so we're safe in the loop below
  183. var expandedSet = this.$izip(widths, rowTabs);
  184. for (var i = 0, l = expandedSet.length; i < l; i++) {
  185. var w = expandedSet[i][0], it = expandedSet[i][1];
  186. location += 1 + w;
  187. it += bias;
  188. var difference = location - it;
  189. if (difference == 0)
  190. continue;
  191. var partialLine = this.$editor.session.getLine(row).substr(0, it );
  192. var strippedPartialLine = partialLine.replace(/\s*$/g, "");
  193. var ispaces = partialLine.length - strippedPartialLine.length;
  194. if (difference > 0) {
  195. // put the spaces after the tab and then delete the tab, so any insertion
  196. // points behave as expected
  197. this.$editor.session.getDocument().insertInLine({row: row, column: it + 1}, Array(difference + 1).join(" ") + "\t");
  198. this.$editor.session.getDocument().removeInLine(row, it, it + 1);
  199. bias += difference;
  200. }
  201. if (difference < 0 && ispaces >= -difference) {
  202. this.$editor.session.getDocument().removeInLine(row, it + difference, it);
  203. bias += difference;
  204. }
  205. }
  206. };
  207. // the is a (naive) Python port--but works for these purposes
  208. this.$izip_longest = function(iterables) {
  209. if (!iterables[0])
  210. return [];
  211. var longest = iterables[0].length;
  212. var iterablesLength = iterables.length;
  213. for (var i = 1; i < iterablesLength; i++) {
  214. var iLength = iterables[i].length;
  215. if (iLength > longest)
  216. longest = iLength;
  217. }
  218. var expandedSet = [];
  219. for (var l = 0; l < longest; l++) {
  220. var set = [];
  221. for (var i = 0; i < iterablesLength; i++) {
  222. if (iterables[i][l] === "")
  223. set.push(NaN);
  224. else
  225. set.push(iterables[i][l]);
  226. }
  227. expandedSet.push(set);
  228. }
  229. return expandedSet;
  230. };
  231. // an even more (naive) Python port
  232. this.$izip = function(widths, tabs) {
  233. // grab the shorter size
  234. var size = widths.length >= tabs.length ? tabs.length : widths.length;
  235. var expandedSet = [];
  236. for (var i = 0; i < size; i++) {
  237. var set = [ widths[i], tabs[i] ];
  238. expandedSet.push(set);
  239. }
  240. return expandedSet;
  241. };
  242. }).call(ElasticTabstopsLite.prototype);
  243. exports.ElasticTabstopsLite = ElasticTabstopsLite;
  244. var Editor = require("../editor").Editor;
  245. require("../config").defineOptions(Editor.prototype, "editor", {
  246. useElasticTabstops: {
  247. set: function(val) {
  248. if (val) {
  249. if (!this.elasticTabstops)
  250. this.elasticTabstops = new ElasticTabstopsLite(this);
  251. this.commands.on("afterExec", this.elasticTabstops.onAfterExec);
  252. this.commands.on("exec", this.elasticTabstops.onExec);
  253. this.on("change", this.elasticTabstops.onChange);
  254. } else if (this.elasticTabstops) {
  255. this.commands.removeListener("afterExec", this.elasticTabstops.onAfterExec);
  256. this.commands.removeListener("exec", this.elasticTabstops.onExec);
  257. this.removeListener("change", this.elasticTabstops.onChange);
  258. }
  259. }
  260. }
  261. });
  262. });