[Question] Default Java formatter line length
See original GitHub issueI’ve noticed that the default Java formatter (JSON “java.format.enabled”: true) forces lines to be no longer than 80 characters. I can’t seem to find any setting to change this to my own preferred length.
settings.json
{
"editor.formatOnSave": true,
"window.zoomLevel": 0,
"editor.suggestSelection": "first",
"vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
"terminal.integrated.shell.osx": "/bin/bash",
"editor.rulers": [
100
],
"editor.wordWrap": "wordWrapColumn",
"editor.wordWrapColumn": 100,
}
Environment
- Operating System: macOS 10.14.1
- JDK version: openJDK 15.0.1
- Visual Studio Code version: 1.51.1
- Java extension version: 0.70.0
Steps To Reproduce
- Enable Default Java formatting
- Format file
Current Result
Formats all lines in file to be no longer than 80 characters.
Wanted Result
For lines to be formatted to my preferred length
Issue Analytics
- State:
- Created 3 years ago
- Reactions:13
- Comments:5 (1 by maintainers)
Top Results From Across the Web
How to change max line length for formatter provided by ...
After coding, right click in current .java file and choose Format Document, all code will be formatted by the wordWrap setting and the...
Read more >Eclipse : How to change line length for auto formatting of java ...
In this tutorial we learned how to change the maximum line length for auto formatting of java code and comments (using Ctrl+Shift+F). Having...
Read more >Java formatting and linting - Visual Studio Code
Editing formatter settings You can open the editor with the command Java: Open Java Formatter Settings with Preview. In the editor, you can...
Read more >356851 – [formatter] Default line size of Java code ... - Bugs
I find it astonishing this issue is still open after 2 years. +1 for changing to a reasonable default of 120 or more....
Read more >Google Java Style Guide
4 Formatting: 4.1 Braces: 4.2 Block indentation: +2 spaces: 4.3 One statement per line: 4.4 Column limit: 100: 4.5 Line-wrapping: 4.6 Whitespace ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Okay, I found out that this is already possible. There are two commands (I don’t know where exactly they are coming from, but I have the “Java Extension Pack” from Microsoft installed):
I’ve used the “with Preview” one which provides a settings GUI. When first using it you get asked to create a file called
eclipse-formatter.xml
. Choose “yes” and you can change some settings.There’s a settings group called “Wrapping” where you can change the “Maximum line width”. This actually changed how the formatting command formats my Java code.
I’m not sure if all formatter options are included, and since it can be tricky to create a custom formatter file, you can always use a formatter option key-value pair and place it directly in the
java.settings.url
(normally used for compiler options). I’ve documented this at https://github.com/eclipse/eclipse.jdt.ls/wiki/Language-Server-Settings-&-Capabilities#java-formatter-options . All formatter options can be found at https://help.eclipse.org/latest/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fjdt%2Fcore%2Fformatter%2FDefaultCodeFormatterConstants.html .As an example :
.vscode/settings.json
.vscode/settings.prefs
(Note that for lineSplit the default is 120, and for comment.line_length, it is 80, so just included for awareness)
As we do have support, I’ll close this, and just make the documentation a bit more visible.