eclipse-jdt / eclipse.jdt.core

Eclipse Public License 2.0
164 stars 130 forks source link

Formatter formats differently when saving; incorrectly breaks line. #1020

Open garretwilson opened 1 year ago

garretwilson commented 1 year ago

This sort of issue seems to keep coming back; see e.g. Bug 560889 and similar (but opposite) Bug 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:

  @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:
  @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.

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.

jjohnstn commented 1 year ago

Hi @garretwilson I recently made a fix in this area with text blocks used as arguments and when I try a sample piece of code which calls a method with multiple strings, I don't see the code change between CTRL+Shift+F and CTRL+S so is possible my recent changes fix this. If you want to test, go to: https://download.eclipse.org/eclipse/downloads/ and click the build starting with I2023 and download the SDK for your platform. You can then try and run some tests to verify it is fixed.

garretwilson commented 1 year ago

Hi, @jjohnstn . Thanks for replying. Since this is not a huge impediment to my work, I'll just wait until the next release before testing it. The biggest thing is knowing that someone cares and looked into it. I appreciate it! I'll circle back and let you know if this bug is still hanging around after the next release.