Keeping New lines #96

Closed
wisec opened this Issue Feb 14, 2012 · 13 comments

Projects

None yet

7 participants

@wisec

Would it be possible to keep original newlines by simply adding:
preserve_max_newlines:1
to beautify options?

By modifying n_newlines condition it's "probably" easy to preserve the nl.
What do you think?

--- beautify.js 2012-02-14 15:37:23.000000000 +0100
+++ beautify.js 2012-02-14 15:55:37.000000000 +0100
@@ -345,7 +345,7 @@ function js_beautify(js_source_text, opt
             }

             if (opt_preserve_newlines) {
-                if (n_newlines > 1) {
+                if (n_newlines > 0) {
                     for (i = 0; i < n_newlines; i += 1) {
                         print_newline(i === 0);
                         just_added_newline = true;

Of course if the patch is wrong i'd like to understand why.

Thanks

@einars

Hmm, what would be achieved by keeping the newlines? This would effectively forbid the beautifier to make any meaningful changes, as e.g

var a =
1;

would not be restored to any beautiful form. The preserve_newlines options were implemented to keep the blank lines between the blocks of code intact, but I don't see the use in preserving all existing nl's, but please correct me if i'm wrong, maybe I misunderstood something.

@wisec

broadly speaking I'd agree, but if you consider unfixed issues like:
b++
c=++o

which become:
b++c=++o

or similar, I'd say this feature is worth exposing..unless it breaks something else of course :)

That way you wouldn't break original newlines (and original js) but you would still beautify it.

Looking forward to your reply

@wisec

I mean, newlines are definitely metachars as well, so unless there's the chance to be perfect in js sintax, I'd prefer to have the option to be conservative on newlines.
I'm not saying to be always like that, just to have an option to be like that :)

@nightwing

+1 on this

usually while typing i have newlines the way i want but some spaces are missing
it would be nice if editor could do this

var a=1
if(a==1)
 b+=x

var a = 1
if (a == 1)
    b += x
@einars

Yes, now I see the point in that.

@Chiel92

Is this already implemented in the python version? It would be cool if

if(true)
 bar();

wouldnt be converted to

if(true) bar();

Some of my colleagues dislike it when I use jsbeautifier, for the above reason. :(

@wisec

@hickop +1 :)

@bitwiseman

This is at least partially addressed by my recent changes to newline preservation.

But I see where you're headed with this - having an option where there would still be some formatting but newlines always kept.

@bitwiseman

I'm going to close this. If the new behavior is insufficient, please reopen.

@bitwiseman bitwiseman closed this May 26, 2013
@jonschlinkert

I know this issue was created with js in mind, but it would be great to have a fix for HTML as well.

@bitwiseman

Please file a new issue.

@bitwiseman bitwiseman reopened this Jun 5, 2013
@bitwiseman bitwiseman closed this Jun 5, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment