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
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -709,4 +709,52 @@ public class Foo {
)
);
}

/**
* This style should only reorder the imports but it indents the `extends` keyword as well.
* Context is: I'm trying to enforce a custom order of imports, so I don't want this style to do anything else.
*/
@Issue("https://github.com/openrewrite/rewrite/issues/4165")
@Test
void shouldNotIndentExtends() {
rewriteRun(
spec -> spec.parser(JavaParser.fromJavaVersion().styles(
singletonList(
new NamedStyles(
randomId(),
"it.does.more.than.JustOrderImports",
"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 👍

ImportLayoutStyle.builder()
.importAllOthers()
.blankLine()
.importStaticAllOthers()
.build()
)
)
))),
java("class BaseClass {}"),
java(
"""
import static org.junit.jupiter.api.Assertions.*;

import java.io.*;
class Test
extends BaseClass {
}
""",
"""
import java.io.*;

import static org.junit.jupiter.api.Assertions.*;

class Test
extends BaseClass {
}
"""
)
);
}
}