Skip to content

Commit

Permalink
Improve chained methods (fix #151, ref #93, #10)
Browse files Browse the repository at this point in the history
Implement a new option, break_chained_methods:

  False(default): leave the chained methods intact, i.e

      a().b() -> a().b()

      a()     -> a()
        .b()      .b()

  True: always break chained methods with a newline.

      a().b() -> a()
                  .b()

      a()     -> a()
        .b()      .b()

Add a checkbox to turn the feature on/off to the frontpage.
  • Loading branch information
einars committed Oct 5, 2012
1 parent 6934566 commit 595d812
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 46 deletions.
25 changes: 14 additions & 11 deletions beautify.js
Expand Up @@ -82,15 +82,16 @@ function js_beautify(js_source_text, options) {
opt_brace_style = options.brace_style ? options.brace_style : (opt_brace_style ? opt_brace_style : "collapse");


var opt_indent_size = options.indent_size ? options.indent_size : 4;
var opt_indent_char = options.indent_char ? options.indent_char : ' ';
var opt_preserve_newlines = typeof options.preserve_newlines === 'undefined' ? true : options.preserve_newlines;
var opt_max_preserve_newlines = typeof options.max_preserve_newlines === 'undefined' ? false : options.max_preserve_newlines;
var opt_jslint_happy = options.jslint_happy === 'undefined' ? false : options.jslint_happy;
var opt_keep_array_indentation = typeof options.keep_array_indentation === 'undefined' ? false : options.keep_array_indentation;
var opt_space_before_conditional = typeof options.space_before_conditional === 'undefined' ? true : options.space_before_conditional;
var opt_indent_case = typeof options.indent_case === 'undefined' ? false : options.indent_case;
var opt_unescape_strings = typeof options.unescape_strings === 'undefined' ? false : options.unescape_strings;
var opt_indent_size = options.indent_size ? options.indent_size : 4,
opt_indent_char = options.indent_char ? options.indent_char : ' ',
opt_preserve_newlines = typeof options.preserve_newlines === 'undefined' ? true : options.preserve_newlines,
opt_break_chained_methods = typeof options.break_chained_methods === 'undefined' ? false : options.break_chained_methods,
opt_max_preserve_newlines = typeof options.max_preserve_newlines === 'undefined' ? false : options.max_preserve_newlines,
opt_jslint_happy = options.jslint_happy === 'undefined' ? false : options.jslint_happy,
opt_keep_array_indentation = typeof options.keep_array_indentation === 'undefined' ? false : options.keep_array_indentation,
opt_space_before_conditional = typeof options.space_before_conditional === 'undefined' ? true : options.space_before_conditional,
opt_indent_case = typeof options.indent_case === 'undefined' ? false : options.indent_case,
opt_unescape_strings = typeof options.unescape_strings === 'undefined' ? false : options.unescape_strings;

just_added_newline = false;

Expand Down Expand Up @@ -808,8 +809,10 @@ function js_beautify(js_source_text, options) {
if (is_special_word(last_text)) {
print_single_space();
} else if (last_text === ')') {
flags.chain_extra_indentation = 1;
print_newline(true /* ignore_repeated */, false /* reset_statement_flags */);
if (opt_break_chained_methods || wanted_newline) {
flags.chain_extra_indentation = 1;
print_newline(true /* ignore_repeated */, false /* reset_statement_flags */);
}
}

print_token();
Expand Down
51 changes: 21 additions & 30 deletions index.html
Expand Up @@ -148,6 +148,7 @@
$('#detect-packers').attr('checked', $.cookie('detect-packers') !== 'off');
$('#preserve-newlines').attr('checked', $.cookie('preserve-newlines') !== 'off');
$('#keep-array-indentation').attr('checked', $.cookie('keep-array-indentation') === 'on');
$('#break-chained-methods').attr('checked', $.cookie('break-chained-methods') === 'on');
$('#indent-scripts').val(any($.cookie('indent-scripts'), 'normal'));
$('#space-before-conditional').attr('checked', $.cookie('space-before-conditional') !== 'off');
$('#unescape-strings').attr('checked', $.cookie('unescape-strings') === 'on');
Expand All @@ -160,6 +161,7 @@
$.cookie('detect-packers', $('#detect-packers').attr('checked') ? 'on' : 'off', opts);
$.cookie('preserve-newlines', $('#preserve-newlines').attr('checked') ? 'on' : 'off', opts);
$.cookie('keep-array-indentation', $('#keep-array-indentation').attr('checked') ? 'on' : 'off', opts);
$.cookie('break-chained-methods', $('#break-chained-methods').attr('checked') ? 'on' : 'off', opts);
$.cookie('space-before-conditional', $('#space-before-conditional').attr('checked') ? 'on' : 'off', opts);
$.cookie('unescape-strings', $('#unescape-strings').attr('checked') ? 'on' : 'off', opts);
$.cookie('indent-scripts', $('#indent-scripts').val(), opts);
Expand Down Expand Up @@ -206,41 +208,29 @@
}


function beautify() {
function beautify()
{
if (the.beautify_in_progress) return;

store_settings_to_cookie();

the.beautify_in_progress = true;

var source;
if (the.editor) {
source = the.editor.getValue();
} else {
source = $('#source').val();
}
var indent_size = $('#tabsize').val();
var indent_char = indent_size == 1 ? '\t' : ' ';
var preserve_newlines = $('#preserve-newlines').attr('checked');
var keep_array_indentation = $('#keep-array-indentation').attr('checked');
var indent_scripts = $('#indent-scripts').val();
var brace_style = $('#brace-style').val();
var space_before_conditional = $('#space-before-conditional').attr('checked');
var unescape_strings = $('#unescape-strings').attr('checked');


var opts = {
indent_size: indent_size,
indent_char: indent_char,
preserve_newlines:preserve_newlines,
brace_style: brace_style,
keep_array_indentation:keep_array_indentation,
space_after_anon_function:true,
space_before_conditional: space_before_conditional,
unescape_strings: unescape_strings,
indent_scripts:indent_scripts};

var output;
var source = the.editor ? the.editor.getValue() : $('#source').val(),
output,
opts = {};

opts.indent_size = $('#tabsize').val();
opts.indent_char = opts.indent_size == 1 ? '\t' : ' ';
opts.preserve_newlines = $('#preserve-newlines').attr('checked');
opts.keep_array_indentation = $('#keep-array-indentation').attr('checked');
opts.break_chained_methods = $('#break-chained-methods').attr('checked');
opts.indent_scripts = $('#indent-scripts').val();
opts.brace_style = $('#brace-style').val();
opts.space_before_conditional = $('#space-before-conditional').attr('checked');
opts.unescape_strings = $('#unescape-strings').attr('checked');
opts.space_after_anon_function = true;

if (looks_like_html(source)) {
output = style_html(source, opts);
} else {
Expand Down Expand Up @@ -338,9 +328,10 @@
<input class="checkbox" type="checkbox" id="preserve-newlines"><label for="preserve-newlines"> Preserve empty lines?</label><br>
<input class="checkbox" type="checkbox" id="detect-packers"><label for="detect-packers"> Detect packers and obfuscators?</label><br>
<input class="checkbox" type="checkbox" id="keep-array-indentation"><label for="keep-array-indentation"> Keep array indentation?</label><br>
<input class="checkbox" type="checkbox" id="break-chained-methods"><label for="break-chained-methods"> Break lines on chained methods?</label><br>
<input class="checkbox" type="checkbox" id="space-before-conditional"><label for="space-before-conditional"> Space before conditional: "if(x)" / "if (x)"</label><br>
<input class="checkbox" type="checkbox" id="unescape-strings"><label for="unescape-strings"> Unescape printable chars encoded as \xNN or \uNNNN?</label>
<br><a href="?without-codemirror" class="turn-off-codemirror">Turn off bells and whistles?</a>
<br><a href="?without-codemirror" class="turn-off-codemirror">Use a simple textarea for code input?</a>


</td></tr>
Expand Down
6 changes: 4 additions & 2 deletions python/jsbeautifier/__init__.py
Expand Up @@ -43,6 +43,7 @@ def __init__(self):
self.keep_function_indentation = False
self.eval_code = False
self.unescape_strings = False
self.break_chained_methods = False



Expand Down Expand Up @@ -1138,8 +1139,9 @@ def handle_dot(self, token_text):
if self.is_special_word(self.last_text):
self.append(' ')
elif self.last_text == ')':
self.flags.chain_extra_indentation = 1;
self.append_newline(True, False)
if self.opts.break_chained_methods or self.wanted_newline:
self.flags.chain_extra_indentation = 1;
self.append_newline(True, False)
self.append(token_text)

def handle_unknown(self, token_text):
Expand Down
4 changes: 3 additions & 1 deletion python/jsbeautifier/tests/testjsbeautifier.py
Expand Up @@ -148,7 +148,7 @@ def test_beautifier(self):
bt("for(var a=1,b=2)", "for (var a = 1, b = 2)");
bt("for(var a=1,b=2,c=3)", "for (var a = 1, b = 2, c = 3)");
bt("for(var a=1,b=2,c=3;d<3;d++)", "for (var a = 1, b = 2, c = 3; d < 3; d++)");
bt("function x(){(a||b).c()}", "function x() {\n (a || b)\n .c()\n}");
bt("function x(){(a||b).c()}", "function x() {\n (a || b).c()\n}");
bt("function x(){return - 1}", "function x() {\n return -1\n}");
bt("function x(){return ! a}", "function x() {\n return !a\n}");

Expand Down Expand Up @@ -465,6 +465,7 @@ def test_beautifier(self):
bt('if (foo) // comment\n(bar());');
bt('if (foo) // comment\n/asdf/;');

self.options.break_chained_methods = True
bt('foo.bar().baz().cucumber(fat)', 'foo.bar()\n .baz()\n .cucumber(fat)');
bt('foo.bar().baz().cucumber(fat); foo.bar().baz().cucumber(fat)', 'foo.bar()\n .baz()\n .cucumber(fat);\nfoo.bar()\n .baz()\n .cucumber(fat)');
bt('foo.bar().baz().cucumber(fat)\n foo.bar().baz().cucumber(fat)', 'foo.bar()\n .baz()\n .cucumber(fat)\nfoo.bar()\n .baz()\n .cucumber(fat)');
Expand Down Expand Up @@ -500,6 +501,7 @@ def setUpClass(cls):
options.keep_array_indentation = False
options.brace_style = 'collapse'
options.indent_level = 0
options.break_chained_methods = False

cls.options = options
cls.wrapregex = re.compile('^(.+)$', re.MULTILINE)
Expand Down
6 changes: 4 additions & 2 deletions tests/beautify-tests.js
Expand Up @@ -14,7 +14,8 @@ var opts = {
jslint_happy: false,
keep_array_indentation: false,
brace_style: 'collapse',
space_before_conditional: true
space_before_conditional: true,
break_chained_methods: false
};

function test_beautifier(input)
Expand Down Expand Up @@ -191,7 +192,7 @@ function run_beautifier_tests(test_obj)
bt("for(var a=1,b=2)", "for (var a = 1, b = 2)");
bt("for(var a=1,b=2,c=3)", "for (var a = 1, b = 2, c = 3)");
bt("for(var a=1,b=2,c=3;d<3;d++)", "for (var a = 1, b = 2, c = 3; d < 3; d++)");
bt("function x(){(a||b).c()}", "function x() {\n (a || b)\n .c()\n}");
bt("function x(){(a||b).c()}", "function x() {\n (a || b).c()\n}");
bt("function x(){return - 1}", "function x() {\n return -1\n}");
bt("function x(){return ! a}", "function x() {\n return !a\n}");

Expand Down Expand Up @@ -522,6 +523,7 @@ function run_beautifier_tests(test_obj)
bt('if(foo) // comment\n/asdf/;');


opts.break_chained_methods = true;
bt('foo.bar().baz().cucumber(fat)', 'foo.bar()\n .baz()\n .cucumber(fat)');
bt('foo.bar().baz().cucumber(fat); foo.bar().baz().cucumber(fat)', 'foo.bar()\n .baz()\n .cucumber(fat);\nfoo.bar()\n .baz()\n .cucumber(fat)');
bt('foo.bar().baz().cucumber(fat)\n foo.bar().baz().cucumber(fat)', 'foo.bar()\n .baz()\n .cucumber(fat)\nfoo.bar()\n .baz()\n .cucumber(fat)');
Expand Down

0 comments on commit 595d812

Please sign in to comment.