d9ecee6680c17fca1648410e97980c7a478ede837858ea54ce51951fcd9efa93e6112f1cb862c694e0e33e91969feab4da98fd4e1498a1aa71fcec999976dc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*jshint node:true */
  2. /*
  3. The MIT License (MIT)
  4. Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
  5. Permission is hereby granted, free of charge, to any person
  6. obtaining a copy of this software and associated documentation files
  7. (the "Software"), to deal in the Software without restriction,
  8. including without limitation the rights to use, copy, modify, merge,
  9. publish, distribute, sublicense, and/or sell copies of the Software,
  10. and to permit persons to whom the Software is furnished to do so,
  11. subject to the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  18. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  19. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22. */
  23. 'use strict';
  24. function OutputLine(parent) {
  25. this.__parent = parent;
  26. this.__character_count = 0;
  27. // use indent_count as a marker for this.__lines that have preserved indentation
  28. this.__indent_count = -1;
  29. this.__alignment_count = 0;
  30. this.__wrap_point_index = 0;
  31. this.__wrap_point_character_count = 0;
  32. this.__wrap_point_indent_count = -1;
  33. this.__wrap_point_alignment_count = 0;
  34. this.__items = [];
  35. }
  36. OutputLine.prototype.clone_empty = function() {
  37. var line = new OutputLine(this.__parent);
  38. line.set_indent(this.__indent_count, this.__alignment_count);
  39. return line;
  40. };
  41. OutputLine.prototype.item = function(index) {
  42. if (index < 0) {
  43. return this.__items[this.__items.length + index];
  44. } else {
  45. return this.__items[index];
  46. }
  47. };
  48. OutputLine.prototype.has_match = function(pattern) {
  49. for (var lastCheckedOutput = this.__items.length - 1; lastCheckedOutput >= 0; lastCheckedOutput--) {
  50. if (this.__items[lastCheckedOutput].match(pattern)) {
  51. return true;
  52. }
  53. }
  54. return false;
  55. };
  56. OutputLine.prototype.set_indent = function(indent, alignment) {
  57. if (this.is_empty()) {
  58. this.__indent_count = indent || 0;
  59. this.__alignment_count = alignment || 0;
  60. this.__character_count = this.__parent.get_indent_size(this.__indent_count, this.__alignment_count);
  61. }
  62. };
  63. OutputLine.prototype._set_wrap_point = function() {
  64. if (this.__parent.wrap_line_length) {
  65. this.__wrap_point_index = this.__items.length;
  66. this.__wrap_point_character_count = this.__character_count;
  67. this.__wrap_point_indent_count = this.__parent.next_line.__indent_count;
  68. this.__wrap_point_alignment_count = this.__parent.next_line.__alignment_count;
  69. }
  70. };
  71. OutputLine.prototype._should_wrap = function() {
  72. return this.__wrap_point_index &&
  73. this.__character_count > this.__parent.wrap_line_length &&
  74. this.__wrap_point_character_count > this.__parent.next_line.__character_count;
  75. };
  76. OutputLine.prototype._allow_wrap = function() {
  77. if (this._should_wrap()) {
  78. this.__parent.add_new_line();
  79. var next = this.__parent.current_line;
  80. next.set_indent(this.__wrap_point_indent_count, this.__wrap_point_alignment_count);
  81. next.__items = this.__items.slice(this.__wrap_point_index);
  82. this.__items = this.__items.slice(0, this.__wrap_point_index);
  83. next.__character_count += this.__character_count - this.__wrap_point_character_count;
  84. this.__character_count = this.__wrap_point_character_count;
  85. if (next.__items[0] === " ") {
  86. next.__items.splice(0, 1);
  87. next.__character_count -= 1;
  88. }
  89. return true;
  90. }
  91. return false;
  92. };
  93. OutputLine.prototype.is_empty = function() {
  94. return this.__items.length === 0;
  95. };
  96. OutputLine.prototype.last = function() {
  97. if (!this.is_empty()) {
  98. return this.__items[this.__items.length - 1];
  99. } else {
  100. return null;
  101. }
  102. };
  103. OutputLine.prototype.push = function(item) {
  104. this.__items.push(item);
  105. var last_newline_index = item.lastIndexOf('\n');
  106. if (last_newline_index !== -1) {
  107. this.__character_count = item.length - last_newline_index;
  108. } else {
  109. this.__character_count += item.length;
  110. }
  111. };
  112. OutputLine.prototype.pop = function() {
  113. var item = null;
  114. if (!this.is_empty()) {
  115. item = this.__items.pop();
  116. this.__character_count -= item.length;
  117. }
  118. return item;
  119. };
  120. OutputLine.prototype._remove_indent = function() {
  121. if (this.__indent_count > 0) {
  122. this.__indent_count -= 1;
  123. this.__character_count -= this.__parent.indent_size;
  124. }
  125. };
  126. OutputLine.prototype._remove_wrap_indent = function() {
  127. if (this.__wrap_point_indent_count > 0) {
  128. this.__wrap_point_indent_count -= 1;
  129. }
  130. };
  131. OutputLine.prototype.trim = function() {
  132. while (this.last() === ' ') {
  133. this.__items.pop();
  134. this.__character_count -= 1;
  135. }
  136. };
  137. OutputLine.prototype.toString = function() {
  138. var result = '';
  139. if (this.is_empty()) {
  140. if (this.__parent.indent_empty_lines) {
  141. result = this.__parent.get_indent_string(this.__indent_count);
  142. }
  143. } else {
  144. result = this.__parent.get_indent_string(this.__indent_count, this.__alignment_count);
  145. result += this.__items.join('');
  146. }
  147. return result;
  148. };
  149. function IndentStringCache(options, baseIndentString) {
  150. this.__cache = [''];
  151. this.__indent_size = options.indent_size;
  152. this.__indent_string = options.indent_char;
  153. if (!options.indent_with_tabs) {
  154. this.__indent_string = new Array(options.indent_size + 1).join(options.indent_char);
  155. }
  156. // Set to null to continue support for auto detection of base indent
  157. baseIndentString = baseIndentString || '';
  158. if (options.indent_level > 0) {
  159. baseIndentString = new Array(options.indent_level + 1).join(this.__indent_string);
  160. }
  161. this.__base_string = baseIndentString;
  162. this.__base_string_length = baseIndentString.length;
  163. }
  164. IndentStringCache.prototype.get_indent_size = function(indent, column) {
  165. var result = this.__base_string_length;
  166. column = column || 0;
  167. if (indent < 0) {
  168. result = 0;
  169. }
  170. result += indent * this.__indent_size;
  171. result += column;
  172. return result;
  173. };
  174. IndentStringCache.prototype.get_indent_string = function(indent_level, column) {
  175. var result = this.__base_string;
  176. column = column || 0;
  177. if (indent_level < 0) {
  178. indent_level = 0;
  179. result = '';
  180. }
  181. column += indent_level * this.__indent_size;
  182. this.__ensure_cache(column);
  183. result += this.__cache[column];
  184. return result;
  185. };
  186. IndentStringCache.prototype.__ensure_cache = function(column) {
  187. while (column >= this.__cache.length) {
  188. this.__add_column();
  189. }
  190. };
  191. IndentStringCache.prototype.__add_column = function() {
  192. var column = this.__cache.length;
  193. var indent = 0;
  194. var result = '';
  195. if (this.__indent_size && column >= this.__indent_size) {
  196. indent = Math.floor(column / this.__indent_size);
  197. column -= indent * this.__indent_size;
  198. result = new Array(indent + 1).join(this.__indent_string);
  199. }
  200. if (column) {
  201. result += new Array(column + 1).join(' ');
  202. }
  203. this.__cache.push(result);
  204. };
  205. function Output(options, baseIndentString) {
  206. this.__indent_cache = new IndentStringCache(options, baseIndentString);
  207. this.raw = false;
  208. this._end_with_newline = options.end_with_newline;
  209. this.indent_size = options.indent_size;
  210. this.wrap_line_length = options.wrap_line_length;
  211. this.indent_empty_lines = options.indent_empty_lines;
  212. this.__lines = [];
  213. this.previous_line = null;
  214. this.current_line = null;
  215. this.next_line = new OutputLine(this);
  216. this.space_before_token = false;
  217. this.non_breaking_space = false;
  218. this.previous_token_wrapped = false;
  219. // initialize
  220. this.__add_outputline();
  221. }
  222. Output.prototype.__add_outputline = function() {
  223. this.previous_line = this.current_line;
  224. this.current_line = this.next_line.clone_empty();
  225. this.__lines.push(this.current_line);
  226. };
  227. Output.prototype.get_line_number = function() {
  228. return this.__lines.length;
  229. };
  230. Output.prototype.get_indent_string = function(indent, column) {
  231. return this.__indent_cache.get_indent_string(indent, column);
  232. };
  233. Output.prototype.get_indent_size = function(indent, column) {
  234. return this.__indent_cache.get_indent_size(indent, column);
  235. };
  236. Output.prototype.is_empty = function() {
  237. return !this.previous_line && this.current_line.is_empty();
  238. };
  239. Output.prototype.add_new_line = function(force_newline) {
  240. // never newline at the start of file
  241. // otherwise, newline only if we didn't just add one or we're forced
  242. if (this.is_empty() ||
  243. (!force_newline && this.just_added_newline())) {
  244. return false;
  245. }
  246. // if raw output is enabled, don't print additional newlines,
  247. // but still return True as though you had
  248. if (!this.raw) {
  249. this.__add_outputline();
  250. }
  251. return true;
  252. };
  253. Output.prototype.get_code = function(eol) {
  254. this.trim(true);
  255. // handle some edge cases where the last tokens
  256. // has text that ends with newline(s)
  257. var last_item = this.current_line.pop();
  258. if (last_item) {
  259. if (last_item[last_item.length - 1] === '\n') {
  260. last_item = last_item.replace(/\n+$/g, '');
  261. }
  262. this.current_line.push(last_item);
  263. }
  264. if (this._end_with_newline) {
  265. this.__add_outputline();
  266. }
  267. var sweet_code = this.__lines.join('\n');
  268. if (eol !== '\n') {
  269. sweet_code = sweet_code.replace(/[\n]/g, eol);
  270. }
  271. return sweet_code;
  272. };
  273. Output.prototype.set_wrap_point = function() {
  274. this.current_line._set_wrap_point();
  275. };
  276. Output.prototype.set_indent = function(indent, alignment) {
  277. indent = indent || 0;
  278. alignment = alignment || 0;
  279. // Next line stores alignment values
  280. this.next_line.set_indent(indent, alignment);
  281. // Never indent your first output indent at the start of the file
  282. if (this.__lines.length > 1) {
  283. this.current_line.set_indent(indent, alignment);
  284. return true;
  285. }
  286. this.current_line.set_indent();
  287. return false;
  288. };
  289. Output.prototype.add_raw_token = function(token) {
  290. for (var x = 0; x < token.newlines; x++) {
  291. this.__add_outputline();
  292. }
  293. this.current_line.set_indent(-1);
  294. this.current_line.push(token.whitespace_before);
  295. this.current_line.push(token.text);
  296. this.space_before_token = false;
  297. this.non_breaking_space = false;
  298. this.previous_token_wrapped = false;
  299. };
  300. Output.prototype.add_token = function(printable_token) {
  301. this.__add_space_before_token();
  302. this.current_line.push(printable_token);
  303. this.space_before_token = false;
  304. this.non_breaking_space = false;
  305. this.previous_token_wrapped = this.current_line._allow_wrap();
  306. };
  307. Output.prototype.__add_space_before_token = function() {
  308. if (this.space_before_token && !this.just_added_newline()) {
  309. if (!this.non_breaking_space) {
  310. this.set_wrap_point();
  311. }
  312. this.current_line.push(' ');
  313. }
  314. };
  315. Output.prototype.remove_indent = function(index) {
  316. var output_length = this.__lines.length;
  317. while (index < output_length) {
  318. this.__lines[index]._remove_indent();
  319. index++;
  320. }
  321. this.current_line._remove_wrap_indent();
  322. };
  323. Output.prototype.trim = function(eat_newlines) {
  324. eat_newlines = (eat_newlines === undefined) ? false : eat_newlines;
  325. this.current_line.trim();
  326. while (eat_newlines && this.__lines.length > 1 &&
  327. this.current_line.is_empty()) {
  328. this.__lines.pop();
  329. this.current_line = this.__lines[this.__lines.length - 1];
  330. this.current_line.trim();
  331. }
  332. this.previous_line = this.__lines.length > 1 ?
  333. this.__lines[this.__lines.length - 2] : null;
  334. };
  335. Output.prototype.just_added_newline = function() {
  336. return this.current_line.is_empty();
  337. };
  338. Output.prototype.just_added_blankline = function() {
  339. return this.is_empty() ||
  340. (this.current_line.is_empty() && this.previous_line.is_empty());
  341. };
  342. Output.prototype.ensure_empty_line_above = function(starts_with, ends_with) {
  343. var index = this.__lines.length - 2;
  344. while (index >= 0) {
  345. var potentialEmptyLine = this.__lines[index];
  346. if (potentialEmptyLine.is_empty()) {
  347. break;
  348. } else if (potentialEmptyLine.item(0).indexOf(starts_with) !== 0 &&
  349. potentialEmptyLine.item(-1) !== ends_with) {
  350. this.__lines.splice(index + 1, 0, new OutputLine(this));
  351. this.previous_line = this.__lines[this.__lines.length - 2];
  352. break;
  353. }
  354. index--;
  355. }
  356. };
  357. module.exports.Output = Output;