Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

simplify keep_array_indentation, fix #155

  • Loading branch information...
commit ce35bad1d571b0d1f83e3f2dcd086fe7225028a5 1 parent de159f4
@einars einars authored
View
26 beautify.js
@@ -228,7 +228,6 @@ function js_beautify(js_source_text, options) {
in_case: false, // we're on the exact line with "case 0:"
case_body: false, // the indented case-action block
eat_next_space: false,
- indentation_baseline: -1,
indentation_level: (flags ? flags.indentation_level + ((flags.var_line && flags.var_line_reindented) ? 1 : 0) : 0),
ternary_depth: 0
};
@@ -307,19 +306,6 @@ function js_beautify(js_source_text, options) {
if (keep_whitespace) {
- //
- // slight mess to allow nice preservation of array indentation and reindent that correctly
- // first time when we get to the arrays:
- // var a = [
- // ....'something'
- // we make note of whitespace_count = 4 into flags.indentation_baseline
- // so we know that 4 whitespaces in original source match indent_level of reindented source
- //
- // and afterwards, when we get to
- // 'something,
- // .......'something else'
- // we know that this should be indented to indent_level + (7 - indentation_baseline) spaces
- //
var whitespace_count = 0;
while (in_array(c, whitespace)) {
@@ -347,18 +333,10 @@ function js_beautify(js_source_text, options) {
parser_pos += 1;
}
- if (flags.indentation_baseline === -1) {
- flags.indentation_baseline = whitespace_count;
- }
if (just_added_newline) {
- for (i = 0; i < flags.indentation_level + 1; i += 1) {
- output.push(indent_string);
- }
- if (flags.indentation_baseline !== -1) {
- for (i = 0; i < whitespace_count - flags.indentation_baseline; i++) {
- output.push(' ');
- }
+ for (i = 0; i < whitespace_count; i++) {
+ output.push(' ');

This actually replaces any tabs used for the array indentation with spaces. Changing this to:

output.push(indent_string);

And changing:

if (c === '\t') {
whitespace_count += 4;

to:

if (c === '\t') {
whitespace_count += 1;

fixes it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
}
}
View
25 python/jsbeautifier/__init__.py
@@ -86,7 +86,6 @@ def __init__(self, mode):
self.in_case_statement = False
self.case_body = False
self.eat_next_space = False
- self.indentation_baseline = -1
self.indentation_level = 0
self.ternary_depth = 0
@@ -387,18 +386,6 @@ def get_next_token(self):
keep_whitespace = self.opts.keep_array_indentation and self.is_array(self.flags.mode)
if keep_whitespace:
- # slight mess to allow nice preservation of array indentation and reindent that correctly
- # first time when we get to the arrays:
- # var a = [
- # ....'something'
- # we make note of whitespace_count = 4 into flags.indentation_baseline
- # so we know that 4 whitespaces in original source match indent_level of reindented source
- #
- # and afterwards, when we get to
- # 'something,
- # .......'something else'
- # we know that this should be indented to indent_level + (7 - indentation_baseline) spaces
-
whitespace_count = 0
while c in self.whitespace:
if c == '\n':
@@ -419,17 +406,9 @@ def get_next_token(self):
c = self.input[parser_pos]
parser_pos += 1
- if self.flags.indentation_baseline == -1:
-
- self.flags.indentation_baseline = whitespace_count
-
if self.just_added_newline:
- for i in range(self.flags.indentation_level + 1):
- self.output.append(self.indent_string)
-
- if self.flags.indentation_baseline != -1:
- for i in range(whitespace_count - self.flags.indentation_baseline):
- self.output.append(' ')
+ for i in range(whitespace_count):
+ self.output.append(' ')
else: # not keep_whitespace
while c in self.whitespace:
View
8 python/jsbeautifier/tests/testjsbeautifier.py
@@ -342,12 +342,14 @@ def test_beautifier(self):
self.options.keep_array_indentation = True;
- test_fragment('var a = [\n// comment:\n{\n foo:bar\n}\n];', 'var a = [\n // comment:\n{\n foo: bar\n}\n];')
+ bt("a = ['a', 'b', 'c',\n 'd', 'e', 'f']");
+ bt("a = ['a', 'b', 'c',\n 'd', 'e', 'f',\n 'g', 'h', 'i']");
+ bt("a = ['a', 'b', 'c',\n 'd', 'e', 'f',\n 'g', 'h', 'i']");
bt('var x = [{}\n]', 'var x = [{}\n]');
bt('var x = [{foo:bar}\n]', 'var x = [{\n foo: bar\n}\n]');
- bt("a = ['something',\n'completely',\n'different'];\nif (x);", "a = ['something',\n 'completely',\n 'different'];\nif (x);");
+ bt("a = ['something',\n'completely',\n'different'];\nif (x);");
bt("a = ['a','b','c']", "a = ['a', 'b', 'c']");
bt("a = ['a', 'b','c']", "a = ['a', 'b', 'c']");
@@ -491,7 +493,7 @@ def bt(self, input, expectation=None):
expectation = expectation or input
self.decodesto(input, expectation)
if self.options.indent_size == 4 and input:
- wrapped_input = '{\n%s\nfoo=bar;}' % input
+ wrapped_input = '{\n%s\nfoo=bar;}' % self.wrap(input)
wrapped_expect = '{\n%s\n foo = bar;\n}' % self.wrap(expectation)
self.decodesto(wrapped_input, wrapped_expect)
View
8 tests/beautify-tests.js
@@ -53,7 +53,7 @@ function bt(input, expectation)
// }
if (opts.indent_size === 4 && input) {
- wrapped_input = '{\n' + input + '\nfoo=bar;}';
+ wrapped_input = '{\n' + input.replace(/^(.+)$/mg, ' $1') + '\n foo = bar;\n}';
wrapped_expectation = '{\n' + expectation.replace(/^(.+)$/mg, ' $1') + '\n foo = bar;\n}';
test_fragment(wrapped_input, wrapped_expectation);
}
@@ -384,12 +384,14 @@ function run_beautifier_tests(test_obj)
opts.keep_array_indentation = true;
+ bt("a = ['a', 'b', 'c',\n 'd', 'e', 'f']");
+ bt("a = ['a', 'b', 'c',\n 'd', 'e', 'f',\n 'g', 'h', 'i']");
+ bt("a = ['a', 'b', 'c',\n 'd', 'e', 'f',\n 'g', 'h', 'i']");
- test_fragment('var a = [\n// comment:\n{\n foo:bar\n}\n];', 'var a = [\n // comment:\n{\n foo: bar\n}\n];');
bt('var x = [{}\n]', 'var x = [{}\n]');
bt('var x = [{foo:bar}\n]', 'var x = [{\n foo: bar\n}\n]');
- bt("a = ['something',\n'completely',\n'different'];\nif (x);", "a = ['something',\n 'completely',\n 'different'];\nif (x);");
+ bt("a = ['something',\n'completely',\n'different'];\nif (x);");
bt("a = ['a','b','c']", "a = ['a', 'b', 'c']");
bt("a = ['a', 'b','c']", "a = ['a', 'b', 'c']");
@mcongrove

This actually replaces any tabs used for the array indentation with spaces. Changing this to:

output.push(indent_string);

And changing:

if (c === '\t') {
whitespace_count += 4;

to:

if (c === '\t') {
whitespace_count += 1;

fixes it.

Please sign in to comment.
Something went wrong with that request. Please try again.