support custom indentation (#5318)

closes #50
This commit is contained in:
Alex Lam S.L
2022-01-28 00:38:11 +00:00
committed by GitHub
parent 3693bde2dd
commit e4a91a89e0
4 changed files with 118 additions and 16 deletions

View File

@@ -101,9 +101,17 @@ function OutputStream(options) {
}
}
function make_indent(value) {
if (typeof value == "number") return new Array(value + 1).join(" ");
if (!/^\s*$/.test(value)) throw new Error("unsupported indentation: " + JSON.stringify("" + value));
return value;
}
var current_col = 0;
var current_line = 1;
var indentation = options.indent_start;
var current_indent = make_indent(options.indent_start);
var full_indent = make_indent(options.indent_level);
var half_indent = full_indent.length + 1 >> 1;
var last;
var line_end = 0;
var line_fixed = true;
@@ -368,14 +376,14 @@ function OutputStream(options) {
var indent = options.beautify ? function(half) {
if (need_newline_indented) print("\n");
print(repeat_string(" ", half ? indentation - (options.indent_level >> 1) : indentation));
print(half ? current_indent.slice(0, -half_indent) : current_indent);
} : noop;
var with_indent = options.beautify ? function(cont) {
var save_indentation = indentation;
indentation += options.indent_level;
var save_indentation = current_indent;
current_indent += full_indent;
cont();
indentation = save_indentation;
current_indent = save_indentation;
} : function(cont) { cont() };
var may_add_newline = options.max_line_len || options.preserve_line ? function() {
@@ -559,7 +567,7 @@ function OutputStream(options) {
reset : reset,
indent : indent,
should_break : options.beautify && options.width ? function() {
return current_col - indentation >= options.width;
return current_col >= options.width;
} : return_false,
has_parens : function() { return last.slice(-1) == "(" },
newline : newline,