elastic_tabstops_lite.js 11 KB

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