eclipse-jdt / eclipse.jdt.core

Eclipse Public License 2.0
164 stars 130 forks source link

[Formatter][Unnamed patterns/variables] leading space added to conditional statements following an unnamed variable #3070

Closed bwRavencl closed 3 weeks ago

bwRavencl commented 4 weeks ago

Description

This issue first appeared with Eclipse >= 4.32 and is also present in 4.33.

The formatter seems to add a superfluous space when a conditional statement (if or while) follows directly after a try-catch block with an unnamed variable.

It can be observed with the Eclipse [built-in] formatter config and custom configs.

Example

Input:

class Example {
    private void foo() {
        var a = false;

        try {
        } catch (Exception _) { // <- the unnamed variable triggers the issue
        }

        if (a) {  // <-  no leading space before the variable name
        }
    }
}

Output (after formatter is applied):

class Example {
    private void foo() {
        var a = false;

        try {
        } catch (Exception _) {
        }

        if (  a) {  // <- an additional space gets added before the variable
        }
    }
}
carstenartur commented 3 weeks ago

Maybe a similar problem like what was fixed for the NaiveASTFlattener (but for some reasons did not make it into the distribution)? Didn't check, though...

https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/181069

srikanth-sankaran commented 3 weeks ago

Maybe a similar problem like what was fixed for the NaiveASTFlattener (but for some reasons did not make it into the distribution)? Didn't check, though...

https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/181069

It is very unlikely to be related to that. I think this originates from 's use as unnamed variable - a very recent feature - earlier the scanner would classify as an identifier. Not now. I think some code is not adapted to deal with the new token type.

I'll investigate