-
Notifications
You must be signed in to change notification settings - Fork 29.3k
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
Comments
"json.format.enable": true
👍 I ended up adding a bunch of empty comments here and there just to add some spacing |
Agreed, the removing of blank lines is unhelpful. At least this could be a setting if there is support for removing the blank lines |
Stumbled upon this as well, however, as I see it there are essentially two main issues here:
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). |
Not ending a file with a new line is a quite big fail imo because, |
'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": "“”‘’"
} |
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. |
@bmitc The JSON formatter observes the |
@aeschli Thank you for the heads up on that setting! |
Implemented by microsoft/node-jsonc-parser#66 |
Steps to Reproduce:
"json.format.enable": true
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.The text was updated successfully, but these errors were encountered: