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

[json] formatter should keep empty lines #23260

Closed
donaldpipowitch opened this issue Mar 27, 2017 · 10 comments
Closed

[json] formatter should keep empty lines #23260

donaldpipowitch opened this issue Mar 27, 2017 · 10 comments
Assignees
Labels
feature-request Request for new features or functionality json JSON support issues on-testplan
Milestone

Comments

@donaldpipowitch
Copy link
Contributor

  • VSCode Version: 1.11.0-insider

Steps to Reproduce:

  1. "json.format.enable": true
  2. Place some new lines inside a JSON and press save. New lines are removed.

Use case:

In bigger JSON files - especially ones that allow commenting (e.g. JSON5 or .vscode/settings.json) - it is useful, if we could use new lines, but still benefit from formatting.

@aeschli aeschli added json JSON support issues feature-request Request for new features or functionality labels Mar 27, 2017
@aeschli aeschli added this to the Backlog milestone Mar 27, 2017
@aeschli aeschli changed the title Allow new lines when for "json.format.enable": true [json] formatter should keep empty lines Mar 27, 2017
@francoisbeaussier
Copy link

👍

I ended up adding a bunch of empty comments here and there just to add some spacing

@OneOfOne
Copy link

OneOfOne commented Apr 6, 2018

Related #47361 and #37422.

@IanAshleyEckoh
Copy link

Agreed, the removing of blank lines is unhelpful. At least this could be a setting if there is support for removing the blank lines

@ghost
Copy link

ghost commented Feb 8, 2022

Stumbled upon this as well, however, as I see it there are essentially two main issues here:

  1. formatting removes final/trailing newline
  2. formatting removes arbitrarily added empty lines within structures

Why I think these are 2 separate issues? Simply because JSON files are text files and as such, to ensure compatibility and portability, adding a final newline actually makes is a text file in the first place (a line needs to end with a newline, otherwise it's not really a line of text, just some data).

Now, there are a lot of discussions with pros and cons about this but that is not the issue. What the actual issue is is that if I originally created the JSON file with a trailing newline, I obviously want to have it there - for whatever reason. If my original file did not have one, I may not want it. This is an extremely simple case to handle and would solve almost all issue that got closed as duplicates of this issue.

Now, the second issue (which is the actual issue here) is much more complex to handle, since it involves the parsing context to recognize and handle it, which is completely different from just recognizing a trailing newline state and retain it (or have configuration to add/remove it).

@progonkpa
Copy link

Not ending a file with a new line is a quite big fail imo because,
"Many older tools misbehave if the last line of data in a text file is not terminated with a newline or carriage return / new line."

@broofa
Copy link

broofa commented Apr 28, 2022

'Worth noting that this is particularly frustrating when working with JSONC documents (JSON + comments). For example:

{
  // REFERENCE: https://www.npmjs.com/package/markdown-it
  "html": true, // Enable HTML tags in source

  // This is only for full CommonMark compatibility.
  "breaks": true, // Convert '\n' in paragraphs into <br>

  // Double + single quotes replacement pairs, when typographer enabled,
  // and smartquotes on. Could be either a String or an Array.
  "quotes": "“”‘’"
}

Turns into this when saved:

{
  // REFERENCE: https://www.npmjs.com/package/markdown-it
  "html": true, // Enable HTML tags in source
  // This is only for full CommonMark compatibility.
  "breaks": true, // Convert '\n' in paragraphs into <br>
  // Double + single quotes replacement pairs, when typographer enabled,
  // and smartquotes on. Could be either a String or an Array.
  "quotes": "“”‘’"
}

@bmitc
Copy link

bmitc commented Jul 1, 2022

Not ending a file with a new line is a quite big fail imo because, "Many older tools misbehave if the last line of data in a text file is not terminated with a newline or carriage return / new line."

I found this issue due to all the warnings GitHub places for files without newlines at the end of files. I saw that the JSON formatter is removing the end of file newline. This should probably even be a separate setting, because I think I should be able to format a JSON, including removing whitespace or newlines or something, while still keeping the end of file newline to maintain compliance with tools that expect that, whatever they may be.

@aeschli
Copy link
Contributor

aeschli commented Jul 4, 2022

@bmitc The JSON formatter observes the files.insertFinalNewline setting

@bmitc
Copy link

bmitc commented Jul 6, 2022

@aeschli Thank you for the heads up on that setting!

@aeschli aeschli assigned aiday-mar and unassigned aeschli Jul 7, 2022
@aeschli aeschli modified the milestones: Backlog, July 2022 Jul 7, 2022
@aeschli
Copy link
Contributor

aeschli commented Jul 11, 2022

Implemented by microsoft/node-jsonc-parser#66

@github-actions github-actions bot locked and limited conversation to collaborators Sep 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature-request Request for new features or functionality json JSON support issues on-testplan
Projects
None yet
Development

No branches or pull requests

9 participants