ext-whitespace.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. ace.define("ace/ext/whitespace",["require","exports","module","ace/lib/lang"], function(require, exports, module) {
  2. "use strict";
  3. var lang = 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, trimEmpty) {
  73. var doc = session.getDocument();
  74. var lines = doc.getAllLines();
  75. var min = trimEmpty ? -1 : 0;
  76. for (var i = 0, l=lines.length; i < l; i++) {
  77. var line = lines[i];
  78. var index = line.search(/\s+$/);
  79. if (index > min)
  80. doc.removeInLine(i, index, line.length);
  81. }
  82. };
  83. exports.convertIndentation = function(session, ch, len) {
  84. var oldCh = session.getTabString()[0];
  85. var oldLen = session.getTabSize();
  86. if (!len) len = oldLen;
  87. if (!ch) ch = oldCh;
  88. var tab = ch == "\t" ? ch: lang.stringRepeat(ch, len);
  89. var doc = session.doc;
  90. var lines = doc.getAllLines();
  91. var cache = {};
  92. var spaceCache = {};
  93. for (var i = 0, l=lines.length; i < l; i++) {
  94. var line = lines[i];
  95. var match = line.match(/^\s*/)[0];
  96. if (match) {
  97. var w = session.$getStringScreenWidth(match)[0];
  98. var tabCount = Math.floor(w/oldLen);
  99. var reminder = w%oldLen;
  100. var toInsert = cache[tabCount] || (cache[tabCount] = lang.stringRepeat(tab, tabCount));
  101. toInsert += spaceCache[reminder] || (spaceCache[reminder] = lang.stringRepeat(" ", reminder));
  102. if (toInsert != match) {
  103. doc.removeInLine(i, 0, match.length);
  104. doc.insertInLine({row: i, column: 0}, toInsert);
  105. }
  106. }
  107. }
  108. session.setTabSize(len);
  109. session.setUseSoftTabs(ch == " ");
  110. };
  111. exports.$parseStringArg = function(text) {
  112. var indent = {};
  113. if (/t/.test(text))
  114. indent.ch = "\t";
  115. else if (/s/.test(text))
  116. indent.ch = " ";
  117. var m = text.match(/\d+/);
  118. if (m)
  119. indent.length = parseInt(m[0], 10);
  120. return indent;
  121. };
  122. exports.$parseArg = function(arg) {
  123. if (!arg)
  124. return {};
  125. if (typeof arg == "string")
  126. return exports.$parseStringArg(arg);
  127. if (typeof arg.text == "string")
  128. return exports.$parseStringArg(arg.text);
  129. return arg;
  130. };
  131. exports.commands = [{
  132. name: "detectIndentation",
  133. exec: function(editor) {
  134. exports.detectIndentation(editor.session);
  135. }
  136. }, {
  137. name: "trimTrailingSpace",
  138. exec: function(editor) {
  139. exports.trimTrailingSpace(editor.session);
  140. }
  141. }, {
  142. name: "convertIndentation",
  143. exec: function(editor, arg) {
  144. var indent = exports.$parseArg(arg);
  145. exports.convertIndentation(editor.session, indent.ch, indent.length);
  146. }
  147. }, {
  148. name: "setIndentation",
  149. exec: function(editor, arg) {
  150. var indent = exports.$parseArg(arg);
  151. indent.length && editor.session.setTabSize(indent.length);
  152. indent.ch && editor.session.setUseSoftTabs(indent.ch == " ");
  153. }
  154. }];
  155. });
  156. (function() {
  157. ace.require(["ace/ext/whitespace"], function() {});
  158. })();