ext-elastic_tabstops_lite.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. ace.define("ace/ext/elastic_tabstops_lite",[], function(require, exports, module) {
  2. "use strict";
  3. var ElasticTabstopsLite = function(editor) {
  4. this.$editor = editor;
  5. var self = this;
  6. var changedRows = [];
  7. var recordChanges = false;
  8. this.onAfterExec = function() {
  9. recordChanges = false;
  10. self.processRows(changedRows);
  11. changedRows = [];
  12. };
  13. this.onExec = function() {
  14. recordChanges = true;
  15. };
  16. this.onChange = function(delta) {
  17. if (recordChanges) {
  18. if (changedRows.indexOf(delta.start.row) == -1)
  19. changedRows.push(delta.start.row);
  20. if (delta.end.row != delta.start.row)
  21. changedRows.push(delta.end.row);
  22. }
  23. };
  24. };
  25. (function() {
  26. this.processRows = function(rows) {
  27. this.$inChange = true;
  28. var checkedRows = [];
  29. for (var r = 0, rowCount = rows.length; r < rowCount; r++) {
  30. var row = rows[r];
  31. if (checkedRows.indexOf(row) > -1)
  32. continue;
  33. var cellWidthObj = this.$findCellWidthsForBlock(row);
  34. var cellWidths = this.$setBlockCellWidthsToMax(cellWidthObj.cellWidths);
  35. var rowIndex = cellWidthObj.firstRow;
  36. for (var w = 0, l = cellWidths.length; w < l; w++) {
  37. var widths = cellWidths[w];
  38. checkedRows.push(rowIndex);
  39. this.$adjustRow(rowIndex, widths);
  40. rowIndex++;
  41. }
  42. }
  43. this.$inChange = false;
  44. };
  45. this.$findCellWidthsForBlock = function(row) {
  46. var cellWidths = [], widths;
  47. var rowIter = row;
  48. while (rowIter >= 0) {
  49. widths = this.$cellWidthsForRow(rowIter);
  50. if (widths.length == 0)
  51. break;
  52. cellWidths.unshift(widths);
  53. rowIter--;
  54. }
  55. var firstRow = rowIter + 1;
  56. rowIter = row;
  57. var numRows = this.$editor.session.getLength();
  58. while (rowIter < numRows - 1) {
  59. rowIter++;
  60. widths = this.$cellWidthsForRow(rowIter);
  61. if (widths.length == 0)
  62. break;
  63. cellWidths.push(widths);
  64. }
  65. return { cellWidths: cellWidths, firstRow: firstRow };
  66. };
  67. this.$cellWidthsForRow = function(row) {
  68. var selectionColumns = this.$selectionColumnsForRow(row);
  69. var tabs = [-1].concat(this.$tabsForRow(row));
  70. var widths = tabs.map(function(el) { return 0; } ).slice(1);
  71. var line = this.$editor.session.getLine(row);
  72. for (var i = 0, len = tabs.length - 1; i < len; i++) {
  73. var leftEdge = tabs[i]+1;
  74. var rightEdge = tabs[i+1];
  75. var rightmostSelection = this.$rightmostSelectionInCell(selectionColumns, rightEdge);
  76. var cell = line.substring(leftEdge, rightEdge);
  77. widths[i] = Math.max(cell.replace(/\s+$/g,'').length, rightmostSelection - leftEdge);
  78. }
  79. return widths;
  80. };
  81. this.$selectionColumnsForRow = function(row) {
  82. var selections = [], cursor = this.$editor.getCursorPosition();
  83. if (this.$editor.session.getSelection().isEmpty()) {
  84. if (row == cursor.row)
  85. selections.push(cursor.column);
  86. }
  87. return selections;
  88. };
  89. this.$setBlockCellWidthsToMax = function(cellWidths) {
  90. var startingNewBlock = true, blockStartRow, blockEndRow, maxWidth;
  91. var columnInfo = this.$izip_longest(cellWidths);
  92. for (var c = 0, l = columnInfo.length; c < l; c++) {
  93. var column = columnInfo[c];
  94. if (!column.push) {
  95. console.error(column);
  96. continue;
  97. }
  98. column.push(NaN);
  99. for (var r = 0, s = column.length; r < s; r++) {
  100. var width = column[r];
  101. if (startingNewBlock) {
  102. blockStartRow = r;
  103. maxWidth = 0;
  104. startingNewBlock = false;
  105. }
  106. if (isNaN(width)) {
  107. blockEndRow = r;
  108. for (var j = blockStartRow; j < blockEndRow; j++) {
  109. cellWidths[j][c] = maxWidth;
  110. }
  111. startingNewBlock = true;
  112. }
  113. maxWidth = Math.max(maxWidth, width);
  114. }
  115. }
  116. return cellWidths;
  117. };
  118. this.$rightmostSelectionInCell = function(selectionColumns, cellRightEdge) {
  119. var rightmost = 0;
  120. if (selectionColumns.length) {
  121. var lengths = [];
  122. for (var s = 0, length = selectionColumns.length; s < length; s++) {
  123. if (selectionColumns[s] <= cellRightEdge)
  124. lengths.push(s);
  125. else
  126. lengths.push(0);
  127. }
  128. rightmost = Math.max.apply(Math, lengths);
  129. }
  130. return rightmost;
  131. };
  132. this.$tabsForRow = function(row) {
  133. var rowTabs = [], line = this.$editor.session.getLine(row),
  134. re = /\t/g, match;
  135. while ((match = re.exec(line)) != null) {
  136. rowTabs.push(match.index);
  137. }
  138. return rowTabs;
  139. };
  140. this.$adjustRow = function(row, widths) {
  141. var rowTabs = this.$tabsForRow(row);
  142. if (rowTabs.length == 0)
  143. return;
  144. var bias = 0, location = -1;
  145. var expandedSet = this.$izip(widths, rowTabs);
  146. for (var i = 0, l = expandedSet.length; i < l; i++) {
  147. var w = expandedSet[i][0], it = expandedSet[i][1];
  148. location += 1 + w;
  149. it += bias;
  150. var difference = location - it;
  151. if (difference == 0)
  152. continue;
  153. var partialLine = this.$editor.session.getLine(row).substr(0, it );
  154. var strippedPartialLine = partialLine.replace(/\s*$/g, "");
  155. var ispaces = partialLine.length - strippedPartialLine.length;
  156. if (difference > 0) {
  157. this.$editor.session.getDocument().insertInLine({row: row, column: it + 1}, Array(difference + 1).join(" ") + "\t");
  158. this.$editor.session.getDocument().removeInLine(row, it, it + 1);
  159. bias += difference;
  160. }
  161. if (difference < 0 && ispaces >= -difference) {
  162. this.$editor.session.getDocument().removeInLine(row, it + difference, it);
  163. bias += difference;
  164. }
  165. }
  166. };
  167. this.$izip_longest = function(iterables) {
  168. if (!iterables[0])
  169. return [];
  170. var longest = iterables[0].length;
  171. var iterablesLength = iterables.length;
  172. for (var i = 1; i < iterablesLength; i++) {
  173. var iLength = iterables[i].length;
  174. if (iLength > longest)
  175. longest = iLength;
  176. }
  177. var expandedSet = [];
  178. for (var l = 0; l < longest; l++) {
  179. var set = [];
  180. for (var i = 0; i < iterablesLength; i++) {
  181. if (iterables[i][l] === "")
  182. set.push(NaN);
  183. else
  184. set.push(iterables[i][l]);
  185. }
  186. expandedSet.push(set);
  187. }
  188. return expandedSet;
  189. };
  190. this.$izip = function(widths, tabs) {
  191. var size = widths.length >= tabs.length ? tabs.length : widths.length;
  192. var expandedSet = [];
  193. for (var i = 0; i < size; i++) {
  194. var set = [ widths[i], tabs[i] ];
  195. expandedSet.push(set);
  196. }
  197. return expandedSet;
  198. };
  199. }).call(ElasticTabstopsLite.prototype);
  200. exports.ElasticTabstopsLite = ElasticTabstopsLite;
  201. var Editor = require("../editor").Editor;
  202. require("../config").defineOptions(Editor.prototype, "editor", {
  203. useElasticTabstops: {
  204. set: function(val) {
  205. if (val) {
  206. if (!this.elasticTabstops)
  207. this.elasticTabstops = new ElasticTabstopsLite(this);
  208. this.commands.on("afterExec", this.elasticTabstops.onAfterExec);
  209. this.commands.on("exec", this.elasticTabstops.onExec);
  210. this.on("change", this.elasticTabstops.onChange);
  211. } else if (this.elasticTabstops) {
  212. this.commands.removeListener("afterExec", this.elasticTabstops.onAfterExec);
  213. this.commands.removeListener("exec", this.elasticTabstops.onExec);
  214. this.removeListener("change", this.elasticTabstops.onChange);
  215. }
  216. }
  217. }
  218. });
  219. });
  220. (function() {
  221. ace.require(["ace/ext/elastic_tabstops_lite"], function(m) {
  222. if (typeof module == "object" && typeof exports == "object" && module) {
  223. module.exports = m;
  224. }
  225. });
  226. })();