eclipse-jdt / eclipse-jdt/eclipse.jdt.core

Formatter formats differently when saving; incorrectly breaks line.

Open
#1,020 2 comments 0 reactions 1 assignee Claimed by @jjohnstn View on GitHub
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 12h
Merged PRs (30d)
47

Description

This sort of issue seems to keep coming back; see e.g. [Bug 560889](https://bugs.eclipse.org/bugs/show_bug.cgi?id=560889) and similar (but opposite) [Bug 563487](https://bugs.eclipse.org/bugs/show_bug.cgi?id=563487).

I'm using Eclipse EE 2023-03 on Windows 10 with Java 17. I have custom formatting settings with a line wrapping max line width set to `160`. I'm using tab indention with a display of `2` spaces per tab. In addition in the preferences under **Java > Editor > Save Actions** I have:

`[x]` Perform the selected actions on save
` [x]` Format source code
` (x)` Format edited lines

I enter the following method:

```java
@Override
public void doFoo() {
FooBarThingRepository.save(new FooBarThingDao("xxxxxxxxxxx", "xxxxxx xxx xxx", "xxxxx", "xxxxx", """
xxxxxx xxx xxx

""",

```

The `"""` starts a Java text block.

1. I make changes to the `FooBarThingRepository` line.
2. I do a `Ctrl+Shift+F` to format the code, and it looks fine.
3. Then I do a `Ctrl+S` to safe the file, and Eclipse reformats it to this:

```java
@Override
public void doFoo() {
FooBarThingRepository.save(new FooBarThingDao("xxxxxxxxxxx", "xxxxxx xxx xxx", "xxxxx", "xxxxx"
"""
xxxxxx xxx xxx

""",

```

Eclipse has moved the start of the text block `"""` to another line. This has neither rhyme nor reason.

* First of all, the line is nowhere near the maximum length of `160` I have defined.
* Even if Eclipse is formatting edited lines on saving, it already formatted them when I manually initiated a format via `Ctrl+Shift+F`. Why should it format them again, and format them differently?

I can only guess there is some duplicated-but-diverging code between the entire document format and the format-when-save logic.

I can work around this by using `Ctrl+Z` after saving and then saving again. But once I edit that line again, I have to go through the same dance.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.