apache / netbeans

Apache NetBeans
https://netbeans.apache.org/
Apache License 2.0
2.59k stars 834 forks source link

NetBeans doesn't recognize annotation on function return type with source formatting #7535

Open PavelTurk opened 6 days ago

PavelTurk commented 6 days ago

Apache NetBeans version

Apache NetBeans 22

What happened

NB formats code in a wrong way when annotation is used for function return type.

Language / Project Type / NetBeans Component

Java Maven Project using Java Modules (JPMS)

How to reproduce

Create NewClass:

import org.jetbrains.annotations.NotNull;

public class NewClass {

  public @NotNull String doItNow() {
    return null;
  }
}

And press ALT + SHIFT + F to format the code. You will get this result:

import org.jetbrains.annotations.NotNull;

public class NewClass {

    public @NotNull
    String doItNow() {
        return null;
    }
}

The same happens for class fields:

private final @Nullable List<String> foos;

Did this work correctly in an earlier version?

No / Don't know

Operating System

Ubuntu 20.04.3 LTS

JDK

Java: 21.0.2; OpenJDK 64-Bit Server VM 21.0.2+13-58

Apache NetBeans packaging

Apache NetBeans binary zip

Anything else

No response

Are you willing to submit a pull request?

No

neilcsmith-net commented 6 days ago

Try Editor / Formatting / Language: Java / Category: Wrapping and change the value of Annotations from Always to Never. Does this work for you? Annotations is one area I find the automatic formatting settings to be a problem by default, partly because they're not contextual.

PavelTurk commented 6 days ago

@neilcsmith-net I tried you suggestion. This is source code:

import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class NewClass {

  private @Nullable List<String> foos;

  public @NotNull String doItNow() {
    return null;
  }
}

And this is the result code:

import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class NewClass {

    private @Nullable List<String> foos;

    public @NotNull
    String doItNow() {
        return null;
    }
}

As you see it worked for field, but not for method

neilcsmith-net commented 6 days ago

Yes, can reproduce. Seems to be OK for method and method parameter annotations, but not on the return type. That definitely seems to be a bug. Then there's an open question about what the default should be.

PavelTurk commented 6 days ago

@neilcsmith-net Could you say if there is an easy way to format only code indents? I have a code where one indent is equal to 2 spaces and I want to make it 4 spaces. Is there a way to do only it?