Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to preserve existing var and object alignment (like preserve_array_indentation) #396

Open
QuentinFchx opened this issue Jan 31, 2014 · 18 comments

Comments

@QuentinFchx
Copy link

Would it be possible/acceptable to add an option that converts

var short_var = "value";
var very_long_var = "value";

var obj = {
    "short_prop": "value",
    "very_long_property": "value" 
}

to

var short_var     = "value";
var very_long_var = "value";

var obj = {
    "short_prop"        : "value",
    "very_long_property": "value" 
}
@einars
Copy link
Contributor

einars commented Jan 31, 2014

Theoretically — yes, it wouldn't be against the beautifier's spirit,
but the practical implementation would be quite horrible and hackish, the parser isn't really suited for observing the code in the ways needed for this.

@bitwiseman
Copy link
Member

See #200.

@QuentinFchx
Copy link
Author

Thanks.
Instead, could we just add an option to prevent the beautifier from removing those extra whitespaces ?
We are already using that "alignment rule" in our project and I'd like to use the beautifier and keep the alignment.
Is it too specific to be implemented ?

@bitwiseman
Copy link
Member

Interesting.

Yes, that seems possible - we have an option to preserve array indentation the serves a similar purpose. The code path for this feature would be similar to that. (I've edited the title for this.)

However, it is lower priority for us than several other highly requested features and fixes. You are welcome to code it up and submit a PR, but I will warn you we're asking people to submit tests as well and we're trying to minimize api changes, especially on the command-line. So there might need to be some additional discussion.

Worst case if we don't accept your PR is still that you have a patch that you can use locally. 😄

@jramoyo
Copy link

jramoyo commented Sep 25, 2014

+1

@bitwiseman bitwiseman added this to the Future milestone Sep 25, 2014
@Nikita240
Copy link

@bitwiseman
Copy link
Member

I have no idea what bearing the features of PHP_Beautify has have on this project. 😄

@Nikita240
Copy link

@bitwiseman It demonstrates a precedent for the feature

@bitwiseman
Copy link
Member

Sorry, I was mostly teasing you. 😄
I already agreed this is a valid and potentially useful feature, just lower priority and high difficulty than other currently open issues.

@Nikita240
Copy link

I'll try to find some free time to make a PR then :)

@bitwiseman
Copy link
Member

A little unsolicited advice: I'd try implementing this for just the object literal case first. It's relatively cleanly recognized start and end, during which you can easily track the lines where fields are declared and the field lengths. Do a PR for that, we can discuss and work out any rough edges, and the go back and do that var case next. If you get stuck, drop me a line or an intermediate branch to look at.

@ryanoasis
Copy link

Excuse my ignorance but is there any current way to preserve object formatting like:

var obj = { a: 123, b: "xyz" };

whoops looks like I should be looking at #315 and #370

@chiluap
Copy link

chiluap commented Dec 15, 2016

eslint can do this for you with

Fix all auto-fixable problems

and the configuration:

"key-spacing": [2, {
        "afterColon": true,
        "align": "value",
        "beforeColon": false
    }]

@clawconduce
Copy link

This should link to #365 and #603

@bitwiseman
Copy link
Member

@clawconduce - Agreed, thanks.

@creativecag
Copy link

creativecag commented May 25, 2017

+1. A solution that either handles the spacing or at least ignores the spacing inside objects. I don't even mind doing the spacing myself (my editor can do that) so long as JSB doesn't then revert my changes when I save.

@clawconduce
Copy link

See #1209 , it's not pretty, but it's at least a start, and could be mergeable with some love.

@clawconduce
Copy link

I gave this a quick go in #1209, but I don't think I'll have the time to properly wrap it up.

Among the harder things to figure out are when to ignore this logic - I maintain some files where the keys are function names and the values are the functions themselves - so the objects are hundreds of lines and I don't want those indented. Maybe if a function appears in the object, this formatting doesn't happen? Maybe it doesn't happen if the object covers a certain number of lines?

Anyways, hope someone can pick this up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants