ext-whitespace.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. define("ace/ext/whitespace",["ace_require","exports","module","ace/lib/lang"], function(ace_require, exports, module) {
  2. "use strict";
  3. var lang = ace_require("../lib/lang");
  4. exports.$detectIndentation = function(lines, fallback) {
  5. var stats = [];
  6. var changes = [];
  7. var tabIndents = 0;
  8. var prevSpaces = 0;
  9. var max = Math.min(lines.length, 1000);
  10. for (var i = 0; i < max; i++) {
  11. var line = lines[i];
  12. if (!/^\s*[^*+\-\s]/.test(line))
  13. continue;
  14. if (line[0] == "\t") {
  15. tabIndents++;
  16. prevSpaces = -Number.MAX_VALUE;
  17. } else {
  18. var spaces = line.match(/^ */)[0].length;
  19. if (spaces && line[spaces] != "\t") {
  20. var diff = spaces - prevSpaces;
  21. if (diff > 0 && !(prevSpaces%diff) && !(spaces%diff))
  22. changes[diff] = (changes[diff] || 0) + 1;
  23. stats[spaces] = (stats[spaces] || 0) + 1;
  24. }
  25. prevSpaces = spaces;
  26. }
  27. while (i < max && line[line.length - 1] == "\\")
  28. line = lines[i++];
  29. }
  30. function getScore(indent) {
  31. var score = 0;
  32. for (var i = indent; i < stats.length; i += indent)
  33. score += stats[i] || 0;
  34. return score;
  35. }
  36. var changesTotal = changes.reduce(function(a,b){return a+b;}, 0);
  37. var first = {score: 0, length: 0};
  38. var spaceIndents = 0;
  39. for (var i = 1; i < 12; i++) {
  40. var score = getScore(i);
  41. if (i == 1) {
  42. spaceIndents = score;
  43. score = stats[1] ? 0.9 : 0.8;
  44. if (!stats.length)
  45. score = 0;
  46. } else
  47. score /= spaceIndents;
  48. if (changes[i])
  49. score += changes[i] / changesTotal;
  50. if (score > first.score)
  51. first = {score: score, length: i};
  52. }
  53. if (first.score && first.score > 1.4)
  54. var tabLength = first.length;
  55. if (tabIndents > spaceIndents + 1) {
  56. if (tabLength == 1 || spaceIndents < tabIndents / 4 || first.score < 1.8)
  57. tabLength = undefined;
  58. return {ch: "\t", length: tabLength};
  59. }
  60. if (spaceIndents > tabIndents + 1)
  61. return {ch: " ", length: tabLength};
  62. };
  63. exports.detectIndentation = function(session) {
  64. var lines = session.getLines(0, 1000);
  65. var indent = exports.$detectIndentation(lines) || {};
  66. if (indent.ch)
  67. session.setUseSoftTabs(indent.ch == " ");
  68. if (indent.length)
  69. session.setTabSize(indent.length);
  70. return indent;
  71. };
  72. exports.trimTrailingSpace = function(session, options) {
  73. var doc = session.getDocument();
  74. var lines = doc.getAllLines();
  75. var min = options && options.trimEmpty ? -1 : 0;
  76. var cursors = [], ci = -1;
  77. if (options && options.keepCursorPosition) {
  78. if (session.selection.rangeCount) {
  79. session.selection.rangeList.ranges.forEach(function(x, i, ranges) {
  80. var next = ranges[i + 1];
  81. if (next && next.cursor.row == x.cursor.row)
  82. return;
  83. cursors.push(x.cursor);
  84. });
  85. } else {
  86. cursors.push(session.selection.getCursor());
  87. }
  88. ci = 0;
  89. }
  90. var cursorRow = cursors[ci] && cursors[ci].row;
  91. for (var i = 0, l=lines.length; i < l; i++) {
  92. var line = lines[i];
  93. var index = line.search(/\s+$/);
  94. if (i == cursorRow) {
  95. if (index < cursors[ci].column && index > min)
  96. index = cursors[ci].column;
  97. ci++;
  98. cursorRow = cursors[ci] ? cursors[ci].row : -1;
  99. }
  100. if (index > min)
  101. doc.removeInLine(i, index, line.length);
  102. }
  103. };
  104. exports.convertIndentation = function(session, ch, len) {
  105. var oldCh = session.getTabString()[0];
  106. var oldLen = session.getTabSize();
  107. if (!len) len = oldLen;
  108. if (!ch) ch = oldCh;
  109. var tab = ch == "\t" ? ch: lang.stringRepeat(ch, len);
  110. var doc = session.doc;
  111. var lines = doc.getAllLines();
  112. var cache = {};
  113. var spaceCache = {};
  114. for (var i = 0, l=lines.length; i < l; i++) {
  115. var line = lines[i];
  116. var match = line.match(/^\s*/)[0];
  117. if (match) {
  118. var w = session.$getStringScreenWidth(match)[0];
  119. var tabCount = Math.floor(w/oldLen);
  120. var reminder = w%oldLen;
  121. var toInsert = cache[tabCount] || (cache[tabCount] = lang.stringRepeat(tab, tabCount));
  122. toInsert += spaceCache[reminder] || (spaceCache[reminder] = lang.stringRepeat(" ", reminder));
  123. if (toInsert != match) {
  124. doc.removeInLine(i, 0, match.length);
  125. doc.insertInLine({row: i, column: 0}, toInsert);
  126. }
  127. }
  128. }
  129. session.setTabSize(len);
  130. session.setUseSoftTabs(ch == " ");
  131. };
  132. exports.$parseStringArg = function(text) {
  133. var indent = {};
  134. if (/t/.test(text))
  135. indent.ch = "\t";
  136. else if (/s/.test(text))
  137. indent.ch = " ";
  138. var m = text.match(/\d+/);
  139. if (m)
  140. indent.length = parseInt(m[0], 10);
  141. return indent;
  142. };
  143. exports.$parseArg = function(arg) {
  144. if (!arg)
  145. return {};
  146. if (typeof arg == "string")
  147. return exports.$parseStringArg(arg);
  148. if (typeof arg.text == "string")
  149. return exports.$parseStringArg(arg.text);
  150. return arg;
  151. };
  152. exports.commands = [{
  153. name: "detectIndentation",
  154. description: "Detect indentation from content",
  155. exec: function(editor) {
  156. exports.detectIndentation(editor.session);
  157. }
  158. }, {
  159. name: "trimTrailingSpace",
  160. description: "Trim trailing whitespace",
  161. exec: function(editor, args) {
  162. exports.trimTrailingSpace(editor.session, args);
  163. }
  164. }, {
  165. name: "convertIndentation",
  166. description: "Convert indentation to ...",
  167. exec: function(editor, arg) {
  168. var indent = exports.$parseArg(arg);
  169. exports.convertIndentation(editor.session, indent.ch, indent.length);
  170. }
  171. }, {
  172. name: "setIndentation",
  173. description: "Set indentation",
  174. exec: function(editor, arg) {
  175. var indent = exports.$parseArg(arg);
  176. indent.length && editor.session.setTabSize(indent.length);
  177. indent.ch && editor.session.setUseSoftTabs(indent.ch == " ");
  178. }
  179. }];
  180. }); (function() {
  181. window.ace_require(["ace/ext/whitespace"], function(m) {
  182. if (typeof module == "object" && typeof exports == "object" && module) {
  183. module.exports = m;
  184. }
  185. });
  186. })();