question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Default Java formatter line length

See original GitHub issue

I’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
  1. Enable Default Java formatting
  2. 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:closed
  • Created 3 years ago
  • Reactions:13
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
alinnertcommented, Jun 27, 2021

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):

  • Java: Open Java Formatter Settings
  • Java: Open Java Formatter Settings with Preview

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.

image

0reactions
rgrunbercommented, Sep 21, 2022

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

{
	"java.settings.url": ".vscode/settings.prefs"
}

.vscode/settings.prefs

org.eclipse.jdt.core.formatter.lineSplit=80
org.eclipse.jdt.core.formatter.comment.line_length=80

(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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found