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 an option to preserve indentation on empty lines #1322

Closed
ericmc2 opened this issue Jan 10, 2018 · 11 comments
Closed

Add an option to preserve indentation on empty lines #1322

ericmc2 opened this issue Jan 10, 2018 · 11 comments

Comments

@ericmc2
Copy link

ericmc2 commented Jan 10, 2018

I'd like to request an option for preserving the indentation level on empty lines between indented code.

Use case: The preserved indentation on empty lines makes it easier to insert additional code between two existing lines without having to manually correct the indentation. I'm used to this from various editors and IDEs and I'm really missing this as an option in VSCode (which I was told uses JSBeautify as a formatter and thus cannot provide an option for it).

@HanabishiRecca
Copy link
Contributor

HanabishiRecca commented Mar 17, 2019

Any chances? This behavior is very inconvenient. I don't know at all how you came up with not make it optional.

@bitwiseman
Copy link
Member

@HanabishiRecca
Most editors I know truncate empty lines on save. Keeping the whitespace would be a more involved change from the current behavior (as it is not preserved through to where the output is generated), but making empty lines have indentation matching the current block could be done relatively easily.

This is would involve adding the option, changing the if statement below, and writing tests.

https://github.com/beautify-web/js-beautify/blob/f57eaff4deb0a66123345b1a2333fb343cebd7df/js/src/core/output.js#L163-L166

Probably a 4-8 hours of work, unfortunately I don't have time to work on this currently. Any help you can give would be great!

@HanabishiRecca
Copy link
Contributor

HanabishiRecca commented Mar 18, 2019

@bitwiseman, but save != formatting. For example VS Code has auto-formatting on the fly.

Any help you can give would be great!

Ok, I will try to look for it.

@HanabishiRecca
Copy link
Contributor

@bitwiseman, check https://github.com/HanabishiRecca/js-beautify/commit/3effb435f480fe91fce4dd8cae011d8257475a5b
Tested via VS Code and Python CLI. Can't make a build because don't have linux machine for now.

@bitwiseman
Copy link
Member

@HanabishiRecca
You change looks reasonable to me. Thanks!
We'll need some tests. Please start a PR.

The build should work on Windows and Mac (it does in CI). What error are you seeing?

@HanabishiRecca
Copy link
Contributor

@bitwiseman, started pull request.

The build should work on Windows and Mac (it does in CI). What error are you seeing?

Bash & make requires at least linux subsystem, isn't it?

@HanabishiRecca
Copy link
Contributor

Finally I built it via WSL. Also found and fixed another bug.
#1645

@bitwiseman
Copy link
Member

@HanabishiRecca
Great thanks.
Glad you go this working via WSL. You can also get a minimal bash on windows by installing Git for Windows.

I think option name and value needs to be modified. We're not preserving the whitespace that was on those lines, we're forcing it to the indent level.

So, two ideas - which of these do you think is better:

  1. indent_empty_lines (false|true) - accurately describes the behavior. Probably good enough.
  2. empty_line_whitespace ("none" | "indent") - This would allow us to more easily add a "preserve" setting in the future that would keep the existing whitespace as it was on the line in the input.

Finally, we need some tests. I've added a few smoke tests to your PR take a look and try adding any others you can think of.

@HanabishiRecca
Copy link
Contributor

1st variant with shorter name is good.
2nd is too complicated because generally there is no difference between empty and non-empty lines. If you want to really "preserve" whitespaces, you need to implement another option like "ignore/keep indention" which affects whole code. Doing this to only empty lines doesn't seems to make sense.

@bitwiseman bitwiseman added this to the v1.9.x milestone Mar 22, 2019
@bitwiseman
Copy link
Member

bitwiseman commented Mar 25, 2019

This will be in the v1.9.1.

@bitwiseman bitwiseman modified the milestones: v1.9.x, v1.9.1 Mar 25, 2019
@bitwiseman
Copy link
Member

@HanabishiRecca
microsoft/vscode-html-languageservice#58
We'll see if this goes into the current release or the next one.

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

3 participants