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

Comments attached to array list elements #702

Open
gagern opened this issue May 29, 2015 · 4 comments
Open

Comments attached to array list elements #702

gagern opened this issue May 29, 2015 · 4 comments

Comments

@gagern
Copy link

gagern commented May 29, 2015

Sometimes there is a lengthy list in code, and one wants to comment on each list item. Once case I recently had was where I wanted to simply include the index for each element, to match that up later on. However, I couldn't get this formatted in a reasonable way.

var lst = [
    /* 0 */ foo,
    /* 1 */ bar(with, many, args),
    /* 2 */ baz
];

got turned into

var lst = [
    /* 0 */
    foo,
    /* 1 */
    bar(with, many, args),
    /* 2 */
    baz
];

while

var lst = [
    foo,                   // 0
    bar(with, many, args), // 1
    baz                    // 2
];

became

var lst = [
    foo, // 0
    bar(with, many, args), // 1
    baz // 2
];

There are many ways how this could be achieved. You could keep short /* … */ comments in line. You could detect aligned // comments in consecutive rows and preserve that. You could offer some mechanism to exempt these lines from beautification, as #384 and #575 requested. Having any of these mechanisms available would in my opinion be important for use cases like this.

@bitwiseman
Copy link
Member

Interesting. Some notes:
This input remains the unchanged the comments are treated as "inline":

var lst = [ /* 0 */ foo, /* 1 */ bar(with, many, args), /* 2 */ baz];

Your second input is definitely an enhancement - aligning all the comments at the end of lines is a quite a bit more complex and lower priority. If you want that addressed, you should add it to #421.

Your first example, looks like bug in the existing behavior. We determine whether a comment is inline based on newlines appearing before before the comment, but we should also look after the comment.

gagern added a commit to CindyJS/CindyJS that referenced this issue Sep 8, 2015
It's annoyingly hard to get these formatted in such a way that js-beautify
doesn't break alignment.
See beautifier/js-beautify#702
@bitwiseman bitwiseman modified the milestones: 2.0.0, v1.6.0 Jan 27, 2016
@bitwiseman
Copy link
Member

The first example in this bug might benefit from the fix proposed in #1069.

@gagern
Copy link
Author

gagern commented Dec 29, 2016

I'm not sure I understand how #1069 would apply here. I thought that was all about the indentation of comments which are on separate lines, while this issue here is looking for ways how the comments could stay on the same line as the actual array elements and still be aligned to a common column.

@bitwiseman
Copy link
Member

@gagern - The reason why #1069 exists is due in part to cases where the engine does not end statements/expressions until after the following comments have been printed. As long as we're changing the set of rules, I want to make sure I had linked in all the comment related issues for examples of what cases needed to be handled.

@bitwiseman bitwiseman modified the milestones: v1.7.x, v2.0.0 Jan 1, 2017
@bitwiseman bitwiseman modified the milestones: v1.9.x, v1.10.x Apr 29, 2019
@bitwiseman bitwiseman modified the milestones: v1.10.x, v1.10.xx Jan 14, 2020
@bitwiseman bitwiseman modified the milestones: v1.10.x, v1.11.x Apr 5, 2020
@bitwiseman bitwiseman modified the milestones: v1.11.x, v1.12.x Aug 13, 2020
@bitwiseman bitwiseman modified the milestones: v1.12.x, 1.13.x Aug 20, 2020
@bitwiseman bitwiseman modified the milestones: v1.13.1, v1.13.x Jan 8, 2021
@bitwiseman bitwiseman modified the milestones: v1.13.6, v1.13.x Jan 26, 2021
@bitwiseman bitwiseman modified the milestones: v1.13.7, v1.13.x Apr 13, 2021
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

2 participants