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

Add "collapse-one-line" option for non-collapse brace styles #487

Closed
Sevin777 opened this issue Jul 2, 2014 · 10 comments
Closed

Add "collapse-one-line" option for non-collapse brace styles #487

Sevin777 opened this issue Jul 2, 2014 · 10 comments

Comments

@Sevin777
Copy link

Sevin777 commented Jul 2, 2014

Would it be possible to add an option that makes brackets stay on the same line after a conditional statement IF there is only one line of code after the conditional?

Example:

if(a==1) { a++; }

right now this gets turned into:

if (a == 1) {
      a++;
}

or (depending on options):

if (a == 1)
{
    a++;
}

Using brackets for a single line after a conditional is good practice as it prevents ambiguity and stupid mistakes for what is going to execute after the conditional. However, reading code that requires either 3 or 4 lines (depending on current options) for a single line conditional and single line result is tedious as it takes up far too much space.

@fabien-h
Copy link

fabien-h commented Sep 5, 2014

a === 1 && a++;

:)

@bitwiseman bitwiseman changed the title New Option? Add "collapse-one-line" option for braces Sep 10, 2014
@bitwiseman bitwiseman added this to the Future milestone Sep 10, 2014
@bitwiseman
Copy link
Member

Interesting idea, but it isn't high on our list of requests.

@laggingreflex
Copy link

+1 Not just for if-else, but it'd be useful for small objects

UserDB.findOne({username:'xyz'}, function(err, user){...

@bitwiseman
Copy link
Member

Similar to #315

@bitwiseman bitwiseman modified the milestones: 1.6.1, Future Jan 29, 2016
@bitwiseman
Copy link
Member

This some forms of this may be covered by 1.6.0, but it has not been fully tested.

@laggingreflex
Copy link

hmm just pulled the latest but these are still the same:

if (a == 1) {
  a++;
}

a = {
  a: a
};

UserDB.findOne({
  username: 'xyz'
}, function(err, user) {});

import {
  fs
} from 'fs';

@bitwiseman
Copy link
Member

If you set `brace_style=collapse-preserve-inline" and provide the inline versions as inputs these work:

if (a == 1) { a++; }

a = { a: a };

UserDB.findOne({ username: 'xyz' }, function(err, user) {});

import { fs } from 'fs';

@Sevin777
Copy link
Author

Sevin777 commented Feb 2, 2016

Thank you for working on this. Unfortunately, the result does not work for me because it's implemented as a new brace-style collapse-preserve-inline that is really a subset of an existing brace style collapse.

I use the end-expand brace-style (which is the most common for formatting JavaScript). Ideally, there should be a separate option for preserve-inline, which is compatible with with all of the existing brace-styles (except none, which does not mess with braces). Another option would be to add brace styles expand-preserve-inline and end-expand-preserve-inline.

The current solution only works for a very small subset of users who actually prefer the collapse brace style.

Also, there is another issue in that the new collapse-preserve-inline style messes up conditional line breaks:

// input sample (want to keep this formatting)
if (a == 1) {a++;}
else if (a == 2) {
    a++;
    b++;
}


// using collapse-preserve-inline.
// this is very undesirable... now my control statements are on the same line
if (a == 1) { a++; } else if (a == 2) {
    a++;
    b++;
}

If the above ideas were implemented (and the bug fixed), then we would likely have a working solution.

The 'perfect' solution would require a lot of work (and is likely not going to happen):
The way that Visual Studio handles C# formatting is a great example of how the formatting should work ( C# syntax is almost identical to JavaScript in terms of new line formatting). It allows very advanced configuration for when new lines are created, and more importantly, you can override the default formatting by editing the code (such as putting braces inline when they would normally be in a new line) and the next time you auto format the document it does not override your inline braces. I think part of the reason this works is because it auto formats sections when a closing brace is typed (try this demo for my implementation of this with JS Beautify - type a closing brace and it will auto format the contents between braces). It would be great if JS beautify could work this way... but I understand it would require a lot of work and this is an open source project.

image

@bitwiseman
Copy link
Member

Thank you for following up and continuing to provide feedback.

When I was implementing the preserve-inline, I considered that people my want expand with preserved inline blocks and then thought "Nah, why would anyone do that?" 😄 And as usual, I was wrong.

What you describe as a bug in if-else is technically correct behavior for "collapse" (else should be on the same line as the closing brace). 😢 Sorry.

What you're talking about for perfect solution would require full parsing of the javascript (see #200). And you're also talking about the difference between formatting in the editor vs formatting of a whole file. And yes much more work to implement, and then the combinatorial problem of testing the interactions between all those options. There are other projects that are attempting to provide this level of granularity, but it isn't a goal of this project.

I'll reopen this, but we are again back to low priority. I would welcome a pull request that added `expand-preserve-inline' and 'end-expand-preserve-inline' (or even one that moved the preserve inline to a separate setting), assuming it included tests and the python port.

Just for reference, the main change for preserving inline is here:
55f111e#diff-655cdbdfd52b6516fe4d5d2e53a7af50R797

It might not be that hard to add the functionality you're suggesting.

@bitwiseman bitwiseman reopened this Feb 2, 2016
@bitwiseman bitwiseman modified the milestones: v2.0.0, v1.6.1 Feb 2, 2016
@bitwiseman bitwiseman changed the title Add "collapse-one-line" option for braces Add "collapse-one-line" option for non-collapse brace styles Feb 2, 2016
@bitwiseman bitwiseman modified the milestones: v1.6.6, v2.0.0 Jan 1, 2017
@bitwiseman
Copy link
Member

@sevin7676 - please see 1.6.6 and #1052 . I think this issue is addressed. Please reopen if it is not.

0-wiz-0 pushed a commit to NetBSD/pkgsrc-wip that referenced this issue Apr 14, 2017
pkgsrc changes:
 - Use ALTERNATIVES in order to permit multi-pkgs to coexists and adjust
   PLIST and introduce Makefile post-install target accordingly

Changes:
* CSS: Preserve Newlines ([#537](beautifier/js-beautify#537))

Reverted #1117 - Preserve newlines broken

* On beautify, new line before next CSS selector ([#1142](beautifier/js-beautify#1142))

Added `preserver_newlines` to css beautifier

* Fixed html formatting issue with attribute wrap (Thanks, @HookyQR!)
* Fixed python package publishing

* Wrong HTML beautification starting with v1.6.5 ([#1115](beautifier/js-beautify#1115))
* Ignore linebreak when meet handlebar ([#1104](beautifier/js-beautify#1104))
* Lines are not un-indented correctly when attributes are wrapped ([#1103](beautifier/js-beautify#1103))
* force-aligned is not aligned when indenting with tabs ([#1102](beautifier/js-beautify#1102))
* Python package fails to publish  ([#1101](beautifier/js-beautify#1101))
* Explaination of 'operator_position' is absent from README.md ([#1047](beautifier/js-beautify#1047))

* Fixed a batch of comment and semicolon-less code bugs

* Incorrect indentation after loop with comment ([#1090](beautifier/js-beautify#1090))
* Extra newline is inserted after beautifying code with anonymous function ([#1085](beautifier/js-beautify#1085))
* end brace with next comment line make bad indent ([#1043](beautifier/js-beautify#1043))
* Javascript comment in last line doesn't beautify well ([#964](beautifier/js-beautify#964))
* indent doesn't work with comment (jsdoc) ([#913](beautifier/js-beautify#913))
* Wrong indentation, when new line between chained methods ([#892](beautifier/js-beautify#892))
* Comments in a non-semicolon style have extra indent ([#815](beautifier/js-beautify#815))
* [bug] Incorrect indentation due to commented line(s) following a function call with a function argument. ([#713](beautifier/js-beautify#713))
* Wrong indent formatting ([#569](beautifier/js-beautify#569))

Added `content_unformatted` option (Thanks @arai-a)

* HTML pre code indentation ([#928](beautifier/js-beautify#928))
* Beautify script/style tags but ignore their inner JS/CSS content ([#906](beautifier/js-beautify#906))

* Added support for editorconfig from stdin
* Added js-beautify to cdnjs
* Fixed CRLF to LF for HTML and CSS on windows
* Added inheritance/overriding to config format (Thanks @DaniGuardiola and @HookyQR)
* Added `force-align` to `wrap-attributes` (Thanks @LukinoS)
* Added `force-expand-multiline` to `wrap-attributes` (Thanks @tobias-zucali)
* Added `preserve-inline` as independent brace setting (Thanks @Coburn37)
* Fixed handlebars with angle-braces (Thanks @mmsqe)

* Wrong indentation for comment after nested unbraced control constructs ([#1079](beautifier/js-beautify#1079))
* Should prefer breaking the line after operator ? instead of before operator < ([#1073](beautifier/js-beautify#1073))
* New option "force-expand-multiline" for "wrap_attributes" ([#1070](beautifier/js-beautify#1070))
* Breaks if html file starts with comment ([#1068](beautifier/js-beautify#1068))
* collapse-preserve-inline restricts users to collapse brace_style ([#1057](beautifier/js-beautify#1057))
* Parsing failure on numbers with "e" ([#1054](beautifier/js-beautify#1054))
* Issue with Browser Instructions ([#1053](beautifier/js-beautify#1053))
* Add preserve inline function for expand style braces ([#1052](beautifier/js-beautify#1052))
* Update years in LICENSE ([#1038](beautifier/js-beautify#1038))
* JS. Switch with template literals. Unexpected indentation. ([#1030](beautifier/js-beautify#1030))
* The object with spread object formatted not correctly ([#1023](beautifier/js-beautify#1023))
* Bad output generator function in class ([#1013](beautifier/js-beautify#1013))
* Support editorconfig for stdin ([#1012](beautifier/js-beautify#1012))
* Publish to cdnjs ([#992](beautifier/js-beautify#992))
* breaks if handlebars comments contain handlebars tags ([#930](beautifier/js-beautify#930))
* Using jsbeautifyrc is broken ([#929](beautifier/js-beautify#929))
* Option to put HTML attributes on their own lines, aligned ([#916](beautifier/js-beautify#916))
* Erroneously changes CRLF to LF on Windows in HTML and CSS ([#899](beautifier/js-beautify#899))
* Weird space in {get } vs { normal } ([#888](beautifier/js-beautify#888))
* Bad for-of formatting with constant Array ([#875](beautifier/js-beautify#875))
* Problems with filter property in css and scss ([#755](beautifier/js-beautify#755))
* Add "collapse-one-line" option for non-collapse brace styles  ([#487](beautifier/js-beautify#487))
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

4 participants