whitespace.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, 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 lang = require("../lib/lang");
  33. // based on http://www.freehackers.org/Indent_Finder
  34. exports.$detectIndentation = function(lines, fallback) {
  35. var stats = [];
  36. var changes = [];
  37. var tabIndents = 0;
  38. var prevSpaces = 0;
  39. var max = Math.min(lines.length, 1000);
  40. for (var i = 0; i < max; i++) {
  41. var line = lines[i];
  42. // ignore empty and comment lines
  43. if (!/^\s*[^*+\-\s]/.test(line))
  44. continue;
  45. if (line[0] == "\t") {
  46. tabIndents++;
  47. prevSpaces = -Number.MAX_VALUE;
  48. } else {
  49. var spaces = line.match(/^ */)[0].length;
  50. if (spaces && line[spaces] != "\t") {
  51. var diff = spaces - prevSpaces;
  52. if (diff > 0 && !(prevSpaces%diff) && !(spaces%diff))
  53. changes[diff] = (changes[diff] || 0) + 1;
  54. stats[spaces] = (stats[spaces] || 0) + 1;
  55. }
  56. prevSpaces = spaces;
  57. }
  58. // ignore lines ending with backslash
  59. while (i < max && line[line.length - 1] == "\\")
  60. line = lines[i++];
  61. }
  62. function getScore(indent) {
  63. var score = 0;
  64. for (var i = indent; i < stats.length; i += indent)
  65. score += stats[i] || 0;
  66. return score;
  67. }
  68. var changesTotal = changes.reduce(function(a,b){return a+b}, 0);
  69. var first = {score: 0, length: 0};
  70. var spaceIndents = 0;
  71. for (var i = 1; i < 12; i++) {
  72. var score = getScore(i);
  73. if (i == 1) {
  74. spaceIndents = score;
  75. score = stats[1] ? 0.9 : 0.8;
  76. if (!stats.length)
  77. score = 0;
  78. } else
  79. score /= spaceIndents;
  80. if (changes[i])
  81. score += changes[i] / changesTotal;
  82. if (score > first.score)
  83. first = {score: score, length: i};
  84. }
  85. if (first.score && first.score > 1.4)
  86. var tabLength = first.length;
  87. if (tabIndents > spaceIndents + 1) {
  88. if (tabLength == 1 || spaceIndents < tabIndents / 4 || first.score < 1.8)
  89. tabLength = undefined;
  90. return {ch: "\t", length: tabLength};
  91. }
  92. if (spaceIndents > tabIndents + 1)
  93. return {ch: " ", length: tabLength};
  94. };
  95. exports.detectIndentation = function(session) {
  96. var lines = session.getLines(0, 1000);
  97. var indent = exports.$detectIndentation(lines) || {};
  98. if (indent.ch)
  99. session.setUseSoftTabs(indent.ch == " ");
  100. if (indent.length)
  101. session.setTabSize(indent.length);
  102. return indent;
  103. };
  104. exports.trimTrailingSpace = function(session, trimEmpty) {
  105. var doc = session.getDocument();
  106. var lines = doc.getAllLines();
  107. var min = trimEmpty ? -1 : 0;
  108. for (var i = 0, l=lines.length; i < l; i++) {
  109. var line = lines[i];
  110. var index = line.search(/\s+$/);
  111. if (index > min)
  112. doc.removeInLine(i, index, line.length);
  113. }
  114. };
  115. exports.convertIndentation = function(session, ch, len) {
  116. var oldCh = session.getTabString()[0];
  117. var oldLen = session.getTabSize();
  118. if (!len) len = oldLen;
  119. if (!ch) ch = oldCh;
  120. var tab = ch == "\t" ? ch: lang.stringRepeat(ch, len);
  121. var doc = session.doc;
  122. var lines = doc.getAllLines();
  123. var cache = {};
  124. var spaceCache = {};
  125. for (var i = 0, l=lines.length; i < l; i++) {
  126. var line = lines[i];
  127. var match = line.match(/^\s*/)[0];
  128. if (match) {
  129. var w = session.$getStringScreenWidth(match)[0];
  130. var tabCount = Math.floor(w/oldLen);
  131. var reminder = w%oldLen;
  132. var toInsert = cache[tabCount] || (cache[tabCount] = lang.stringRepeat(tab, tabCount));
  133. toInsert += spaceCache[reminder] || (spaceCache[reminder] = lang.stringRepeat(" ", reminder));
  134. if (toInsert != match) {
  135. doc.removeInLine(i, 0, match.length);
  136. doc.insertInLine({row: i, column: 0}, toInsert);
  137. }
  138. }
  139. }
  140. session.setTabSize(len);
  141. session.setUseSoftTabs(ch == " ");
  142. };
  143. exports.$parseStringArg = function(text) {
  144. var indent = {};
  145. if (/t/.test(text))
  146. indent.ch = "\t";
  147. else if (/s/.test(text))
  148. indent.ch = " ";
  149. var m = text.match(/\d+/);
  150. if (m)
  151. indent.length = parseInt(m[0], 10);
  152. return indent;
  153. };
  154. exports.$parseArg = function(arg) {
  155. if (!arg)
  156. return {};
  157. if (typeof arg == "string")
  158. return exports.$parseStringArg(arg);
  159. if (typeof arg.text == "string")
  160. return exports.$parseStringArg(arg.text);
  161. return arg;
  162. };
  163. exports.commands = [{
  164. name: "detectIndentation",
  165. exec: function(editor) {
  166. exports.detectIndentation(editor.session);
  167. // todo show message?
  168. }
  169. }, {
  170. name: "trimTrailingSpace",
  171. exec: function(editor) {
  172. exports.trimTrailingSpace(editor.session);
  173. }
  174. }, {
  175. name: "convertIndentation",
  176. exec: function(editor, arg) {
  177. var indent = exports.$parseArg(arg);
  178. exports.convertIndentation(editor.session, indent.ch, indent.length);
  179. }
  180. }, {
  181. name: "setIndentation",
  182. exec: function(editor, arg) {
  183. var indent = exports.$parseArg(arg);
  184. indent.length && editor.session.setTabSize(indent.length);
  185. indent.ch && editor.session.setUseSoftTabs(indent.ch == " ");
  186. }
  187. }];
  188. });