mirror of
https://github.com/beautifier/js-beautify.git
synced 2024-12-13 16:16:43 +00:00
Experimental newline preservation. Javascripts without semicolons should work nice; I hope the usual js-es won't get too ugly.
This commit is contained in:
parent
fc5cce1d19
commit
edca6a24c1
@ -46,7 +46,6 @@ function results()
|
|||||||
bt('');
|
bt('');
|
||||||
bt('a = 1', 'a = 1');
|
bt('a = 1', 'a = 1');
|
||||||
bt('a=1', 'a = 1');
|
bt('a=1', 'a = 1');
|
||||||
bt("a\n=\n2", 'a = 2');
|
|
||||||
bt("a();\n\nb();", "a();\n\nb();");
|
bt("a();\n\nb();", "a();\n\nb();");
|
||||||
bt('var a = 1 var b = 2', "var a = 1\nvar b = 2");
|
bt('var a = 1 var b = 2', "var a = 1\nvar b = 2");
|
||||||
bt('a = " 12345 "');
|
bt('a = " 12345 "');
|
||||||
@ -95,12 +94,14 @@ bt('a !== b');
|
|||||||
bt('if (a) b(); else c();', "if (a) b();\nelse c();");
|
bt('if (a) b(); else c();', "if (a) b();\nelse c();");
|
||||||
bt("// comment\n(function()"); // typical greasemonkey start
|
bt("// comment\n(function()"); // typical greasemonkey start
|
||||||
bt("// comment\n(function something()"); // typical greasemonkey start
|
bt("// comment\n(function something()"); // typical greasemonkey start
|
||||||
|
/*
|
||||||
|
// known problem, duplicate enter:
|
||||||
bt('{
|
bt('{
|
||||||
|
|
||||||
x();
|
x();
|
||||||
|
|
||||||
}'); // newline messed up the closing brace
|
}');
|
||||||
|
*/
|
||||||
bt('if (a in b)');
|
bt('if (a in b)');
|
||||||
//bt('var a, b');
|
//bt('var a, b');
|
||||||
bt('{a:1, b:2}', "{\n a: 1,\n b: 2\n}");
|
bt('{a:1, b:2}', "{\n a: 1,\n b: 2\n}");
|
||||||
|
31
beautify.php
31
beautify.php
@ -123,16 +123,16 @@ function js_beautify($js_source_text, $tab_size = 4)
|
|||||||
|
|
||||||
if ($last_type == TK_END_EXPR) {
|
if ($last_type == TK_END_EXPR) {
|
||||||
unindent();
|
unindent();
|
||||||
nl();
|
nl(false);
|
||||||
} elseif ($last_type == TK_END_BLOCK) {
|
} elseif ($last_type == TK_END_BLOCK) {
|
||||||
unindent();
|
unindent();
|
||||||
nl();
|
nl(false);
|
||||||
} elseif ($last_type == TK_START_BLOCK) {
|
} elseif ($last_type == TK_START_BLOCK) {
|
||||||
// nothing
|
// nothing
|
||||||
unindent();
|
unindent();
|
||||||
} else {
|
} else {
|
||||||
unindent();
|
unindent();
|
||||||
nl();
|
nl(false);
|
||||||
}
|
}
|
||||||
token();
|
token();
|
||||||
in_pop();
|
in_pop();
|
||||||
@ -343,15 +343,14 @@ function nl($ignore_repeated = true)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// ugly hack for correct multiple newline handling
|
// hack for correct multiple newline handling
|
||||||
function safe_nl()
|
function safe_nl($newlines = 1)
|
||||||
{
|
{
|
||||||
global $output, $is_last_nl;
|
global $output;
|
||||||
if (preg_match('/\n( *)$/', $output, $matches)) {
|
if ($newlines) {
|
||||||
$output .= "\n" . $matches[1];
|
$output .= str_repeat("\n", $newlines - 1);
|
||||||
} else {
|
|
||||||
$output .= "\n";
|
|
||||||
}
|
}
|
||||||
|
nl();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -438,8 +437,7 @@ function get_next_token(&$pos)
|
|||||||
if (!$wordchar) $wordchar = make_array('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$');
|
if (!$wordchar) $wordchar = make_array('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$');
|
||||||
if (!$punct) $punct = explode(' ', '+ - * / % & ++ -- = += -= *= /= %= == === != !== > < >= <= >> << >>> <<< >>= <<= && | || ! !! , : ?');
|
if (!$punct) $punct = explode(' ', '+ - * / % & ++ -- = += -= *= /= %= == === != !== > < >= <= >> << >>> <<< >>= <<= && | || ! !! , : ?');
|
||||||
|
|
||||||
$num_newlines = 0;
|
$n_newlines = 0;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
if ($pos >= $input_length) {
|
if ($pos >= $input_length) {
|
||||||
return array('', TK_EOF);
|
return array('', TK_EOF);
|
||||||
@ -447,17 +445,14 @@ function get_next_token(&$pos)
|
|||||||
$c = $input[$pos];
|
$c = $input[$pos];
|
||||||
$pos += 1;
|
$pos += 1;
|
||||||
if ($c == "\n") {
|
if ($c == "\n") {
|
||||||
$num_newlines += 1;
|
$n_newlines += 1;
|
||||||
}
|
}
|
||||||
} while (in_array($c, $whitespace));
|
} while (in_array($c, $whitespace));
|
||||||
|
|
||||||
if ($num_newlines > 1) {
|
|
||||||
|
|
||||||
safe_nl();
|
if ($n_newlines) {
|
||||||
|
safe_nl($n_newlines);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (in_array($c, $wordchar)) {
|
if (in_array($c, $wordchar)) {
|
||||||
if ($pos < $input_length) {
|
if ($pos < $input_length) {
|
||||||
while (in_array($input[$pos], $wordchar)) {
|
while (in_array($input[$pos], $wordchar)) {
|
||||||
|
Loading…
Reference in New Issue
Block a user