Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test case to illustrate issue with unsolicited indentation. #4548

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

timo-a
Copy link

@timo-a timo-a commented Oct 3, 2024

What's changed?

This PR adds a test to illustrate a bug.

What's your motivation?

I want to enforce a custom order of imports on new PRs through a custom style.
As I report in "observation 2" of #4165 custom styles also change indentation. I don't think they should do that.

Anything in particular you'd like reviewers to focus on?

Anyone you would like to review specifically?

Have you considered any alternatives or workarounds?

Any additional context

I've marked this PR ready for review in he hope of triggering a build, but it seems a manual approval is necessary.
I won't be available to work on a fix.

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

"custom style",
null,
emptySet(),
singletonList(
Copy link
Contributor

@Laurens-W Laurens-W Oct 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @timo-a, I think in your case you also want to influence the tabs and indents like we do here in our IntelliJ style.
Specifically the 4th parameter of the TabsAndIndentsStyle here influences the continuation indent

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pointer! I expected that not defining a TabsAndIndentsStyle in the first place would leave every indent as it is but apparently not. When I set the 4th parameter continuationIndent to

  • 4 the test works, but then every continuation is indented by a fixed value which is not what I want
  • null the apparent default value of 8 is used
  • 0 there is no indentation at all
  • -1 the token is lifted to the line before
  • -2 or less the original indentation is kept.

So, with some configuration, namely setting continuationIndent to -2 it can be done already. Unfortunately this behavior is not documented at all and I worry that it will be "fixed" in the future, so I modified my test to confirm it. Please merge it in and do consider adding javadoc to at least continuationIndent.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timtebeek is currently unavailable until Tuesday, Let's wait for his response on whether this is expected behavior and if it is we can merge the PR as regression test 👍

@knutwannheden
Copy link
Contributor

knutwannheden commented Oct 4, 2024

Adding something like this to the TabsAndIndentsVisitor should fix this specific case, although the same should then probably also be done for JContainer and JRightPadded and by extension then possibly also for some other formatter visitors:

    @Override
    public <T> JLeftPadded<T> visitLeftPadded(@Nullable JLeftPadded<T> left, JLeftPadded.Location loc, P p) {
        if (getCursor().getNearestMessage("stop") != null) {
            return left;
        }
        return super.visitLeftPadded(left, loc, p);
    }

Background: While the stopping mechanism works, it short-circuits any calls to visit() and thus prevents the visitor from descending down the tree. However, the spaces (which is what the formatter changes in the end) can also be directory contained by JRightPadded, JLeftPadded, and JContainer and there is nothing stopping them from being visited.

@timtebeek timtebeek marked this pull request as draft October 5, 2024 20:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

3 participants