From 5fcca19f9354e8492c4be44e2366b0a0764ba250 Mon Sep 17 00:00:00 2001 From: "Pieter van den Hombergh (homberghp)" Date: Mon, 30 Dec 2024 10:35:08 +0100 Subject: [PATCH] solves #7043, formatting error of closing brace in record definition - adds tests to show the error - adds inner and outer record in ui template text for setting java format spaces - solves the error by moving spaces(cs.spaceWithinMethodDeclParens() ? 1 : 0, true); to the correct place. Co-authored-by: Michael Bien --- .../modules/java/source/save/Reformatter.java | 7 +-- .../java/source/save/FormatingTest.java | 59 ++++++++++++++++++- .../modules/java/ui/Bundle.properties | 4 ++ 3 files changed, 64 insertions(+), 6 deletions(-) diff --git a/java/java.source.base/src/org/netbeans/modules/java/source/save/Reformatter.java b/java/java.source.base/src/org/netbeans/modules/java/source/save/Reformatter.java index 0727cbe743be..0e44783168e2 100644 --- a/java/java.source.base/src/org/netbeans/modules/java/source/save/Reformatter.java +++ b/java/java.source.base/src/org/netbeans/modules/java/source/save/Reformatter.java @@ -947,7 +947,7 @@ public Boolean visitClass(ClassTree node, Void p) { } List perms = node.getPermitsClause(); if (perms != null && !perms.isEmpty()) { - wrapToken(cs.wrapExtendsImplementsKeyword(), 1, EXTENDS); + wrapToken(cs.wrapExtendsImplementsKeyword(), 1, EXTENDS); wrapList(cs.wrapExtendsImplementsList(), cs.alignMultilineImplements(), true, COMMA, perms); } } finally { @@ -1352,6 +1352,7 @@ private Boolean scanRecord(ClassTree node, Void p) { if (!recParams.isEmpty()) { spaces(cs.spaceWithinMethodDeclParens() ? 1 : 0, true); wrapList(cs.wrapMethodParams(), cs.alignMultilineMethodParams(), false, COMMA, recParams); + spaces(cs.spaceWithinMethodDeclParens() ? 1 : 0, true); // solves #7403 } accept(RPAREN); List impls = node.getImplementsClause(); @@ -1398,8 +1399,6 @@ private Boolean scanRecord(ClassTree node, Void p) { isFirstMember = false; } } - - spaces(cs.spaceWithinMethodDeclParens() ? 1 : 0, true); } } finally { indent = oldIndent; @@ -5425,7 +5424,7 @@ private void reformatComment() { } /** - * + * * @see for more info on inline tags check documentation here. * @return returns true if has inline tag prefix like "{+@tagname" */ diff --git a/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/save/FormatingTest.java b/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/save/FormatingTest.java index b41613b3e84a..8797b9111b9c 100644 --- a/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/save/FormatingTest.java +++ b/java/java.source.base/test/unit/src/org/netbeans/modules/java/source/save/FormatingTest.java @@ -3525,9 +3525,9 @@ public void testMultipleNestingRecordPattern() throws Exception { public void testAnnotatedRecord() throws Exception { try { - SourceVersion.valueOf("RELEASE_19"); //NOI18N + SourceVersion.valueOf("RELEASE_17"); //NOI18N } catch (IllegalArgumentException ex) { - //OK, no RELEASE_19, skip test + //OK, no RELEASE_17, skip test return; } testFile = new File(getWorkDir(), "Test.java"); @@ -6683,6 +6683,61 @@ public void testRecord4() throws Exception { + "}\n"; reformat(doc, content, golden); } + + // verify closing '}' position during record formatting + public void testRecordClosingBrace7043() throws Exception { + try { + SourceVersion.valueOf("RELEASE_17"); //NOI18N + } catch (IllegalArgumentException ex) { + //OK, no RELEASE_17, skip test + return; + } + sourceLevel = "17"; + JavacParser.DISABLE_SOURCE_LEVEL_DOWNGRADE = true; + 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); + preferences.putBoolean("spaceWithinMethodDeclParens", true); + preferences.putInt("blankLinesAfterClassHeader", 0); + + String content =""" + package test; + record Student(int id,String lastname,String firstname) implements Serializable { + int m(int x){ + return id+x; + } + } // should stay flush to left margin + """; + String golden = """ + package test; + + record Student( int id, String lastname, String firstname ) implements Serializable { + int m( int x ) { + return id + x; + } + } // should stay flush to left margin + """; + reformat(doc, content, golden); + + preferences.putBoolean("spaceWithinMethodDeclParens", false); + golden = """ + package test; + + record Student(int id, String lastname, String firstname) implements Serializable { + int m(int x) { + return id + x; + } + } // should stay flush to left margin + """; + reformat(doc, content, golden); + } + @ServiceProvider(service = CompilerOptionsQueryImplementation.class, position = 100) public static class TestCompilerOptionsQueryImplementation implements CompilerOptionsQueryImplementation { diff --git a/java/java.source/src/org/netbeans/modules/java/ui/Bundle.properties b/java/java.source/src/org/netbeans/modules/java/ui/Bundle.properties index 7589b9380dfe..eb2edc7a97b6 100644 --- a/java/java.source/src/org/netbeans/modules/java/ui/Bundle.properties +++ b/java/java.source/src/org/netbeans/modules/java/ui/Bundle.properties @@ -497,6 +497,10 @@ default\:\ public int add(int a, int b) {\ return a + b;\ }\ +public record ARecord(String name, int age){\ +}\ +}\ +record TopLevelRecord(String name, double value){\ } #do not translate