Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -3047,7 +3047,14 @@ public Boolean visitCase(CaseTree node, Void p) {
}
}
else if (caseBody != null) {
newline();
switch (cs.wrapCaseStatements()) {
case WRAP_ALWAYS -> {
newline();
}
default -> {
spaces(1, true);
}
}
scan(caseBody, p);
spaces(cs.spaceBeforeSemi() ? 1 : 0);
accept(SEMICOLON);
Expand Down
Comment thread
neilcsmith-net marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,21 @@
*
* @author Dusan Balek
*/
public class FormatingTest extends NbTestCase {
public class ReformatterTest extends NbTestCase {

File testFile = null;
private String sourceLevel = "1.8";
private static final List<String> EXTRA_OPTIONS = new ArrayList<>();

/** Creates a new instance of FormatingTest */
public FormatingTest(String name) {
public ReformatterTest(String name) {
super(name);
}

public static NbTestSuite suite() {
NbTestSuite suite = new NbTestSuite();
suite.addTestSuite(FormatingTest.class);
// suite.addTest(new FormatingTest("testLabelled"));
suite.addTestSuite(ReformatterTest.class);
// suite.addTest(new ReformatterTest("testLabelled"));
return suite;
}

Expand Down Expand Up @@ -2891,6 +2891,74 @@ public void testSwitchCaseDefault() 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) {
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";
};
}
}
""";

String golden
= """
package p;

public class Test {

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";
};
}
}
""";
reformat(doc, content, golden);
preferences.put("wrapCaseStatements", CodeStyle.WrapStyle.WRAP_NEVER.name());
reformat(doc, content, content);

}

public void testSwitchCaseNullAndDefault() throws Exception {
try {
SourceVersion.valueOf("RELEASE_17"); //NOI18N
Expand Down
Loading