-
Notifications
You must be signed in to change notification settings - Fork 931
Update Reformatter to honour case wrap settings with switch expressions (GH7326). #9484
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3416,6 +3416,80 @@ public void testSwitchCaseAllPatternsWithReturnValue() throws Exception { | |
| reformat(doc, content, golden); | ||
| } | ||
|
|
||
| public void testSwitchCaseExprWrapping() throws Exception { | ||
| try { | ||
| SourceVersion.valueOf("RELEASE_17"); //NOI18N | ||
| } catch (IllegalArgumentException ex) { | ||
| //OK, no RELEASE_17, skip test | ||
| return; | ||
| } | ||
| testFile = new File(getWorkDir(), "Test.java"); | ||
| TestUtilities.copyStringToFile(testFile, ""); | ||
| FileObject testSourceFO = FileUtil.toFileObject(testFile); | ||
| DataObject testSourceDO = DataObject.find(testSourceFO); | ||
| EditorCookie ec = (EditorCookie) testSourceDO.getCookie(EditorCookie.class); | ||
| final Document doc = ec.openDocument(); | ||
| doc.putProperty(Language.class, JavaTokenId.language()); | ||
| Preferences preferences = MimeLookup.getLookup(JavaTokenId.language().mimeType()).lookup(Preferences.class); | ||
|
|
||
| String content | ||
| = """ | ||
| package p; | ||
| public class Test { | ||
| String choose(String str) { | ||
| return switch (str) { | ||
| Object str = "pattern matching switch"; | ||
| IntStream iso = IntStream.of(1, 2); | ||
| case null -> "case with null formatting"; | ||
|
Comment on lines
+3441
to
+3445
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if intended but this code is not valid (several compiler errors, matching error and also syntax (e.g We had issues before where tests using invalid code started failing for the wildest reasons (example: compiler suddenly thought it was a compact-java file and generated an additional wrapping class). Unless this is intended for the purpose of the test it might be better to use valid code. also would be good to add a case which uses curly braces+yield, e.g String choose(String str) {
IntStream iso = IntStream.of(1, 2);
return switch (str) {
case null -> "case with null formatting";
case String string when (string.length() == iso.filter(i -> i / 2 == 0).count() || string.length() == 0) ->
"case with pattern matching + condition + lambda expression formatting";
case String s when s.length() == 1 -> "case with pattern matching + condition formatting";
case String s when s.length() == 2 -> {
yield "case with pattern matching + condition formatting";
}
default -> "default formatting";
};
}
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, partly bad copy/paste after converting to multiline. Will fix and add your example. Need to check the existing test code is valid too. |
||
| case String string when (string.length() == iso.filter(i -> i / 2 == 0).count() || string.length() == 0) -> | ||
| "case with pattern matching + condition + lambda expression formatting"; | ||
| case String s when s.length() == 1 -> | ||
| "case with pattern matching + condition formatting"; | ||
| case (String s) -> | ||
| "case with pattern matching + condition formatting"; | ||
| case CharSequence s -> "case with pattern matching formatting"; | ||
| case default -> "default formatting"; | ||
| }; | ||
| } | ||
| } | ||
| """; | ||
|
|
||
| String golden | ||
| = """ | ||
| package p; | ||
| public class Test { | ||
| String choose(String str) { | ||
| return switch (str) { | ||
| Object str = "pattern matching switch"; | ||
| IntStream iso = IntStream.of(1, 2); | ||
| case null -> | ||
| "case with null formatting"; | ||
| case String string when (string.length() == iso.filter(i -> i / 2 == 0).count() || string.length() == 0) -> | ||
| "case with pattern matching + condition + lambda expression formatting"; | ||
| case String s when s.length() == 1 -> | ||
| "case with pattern matching + condition formatting"; | ||
| case (String s) -> | ||
| "case with pattern matching + condition formatting"; | ||
| case CharSequence s -> | ||
| "case with pattern matching formatting"; | ||
| case default -> | ||
| "default formatting"; | ||
| }; | ||
| } | ||
| } | ||
| """; | ||
| reformat(doc, content, golden); | ||
| preferences.put("wrapCaseStatements", CodeStyle.WrapStyle.WRAP_NEVER.name()); | ||
| reformat(doc, content, content); | ||
|
|
||
| } | ||
|
|
||
| public void testNormalRecordPattern() throws Exception { | ||
| try { | ||
| SourceVersion.valueOf("RELEASE_19"); //NOI18N | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we add the missing
t? Because I once used to think the java format[t]er had no tests since i couldn't find it :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can rename, but if we do should look at doing so in a way that navigate to test works.