whitespace.js 6.6 KB

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