From f586da1b03743c847cdc63fb37307acbc912ec2f Mon Sep 17 00:00:00 2001 From: Junichi Yamamoto Date: Fri, 31 May 2024 06:47:38 +0900 Subject: [PATCH] Fix formatting for typed field and const alignment #7190 - https://github.com/apache/netbeans/issues/7190 - Add a field type name length - Fix const name length - Add unit tests Example: ```php class GH7190 { private const ?string C1 = ''; public const ?string CCCC2 = ''; const ?string CCCC3 = ''; private ?Test $t2; private ?Test $t3 = null; private readonly Test1&Test2 $t4; private array|(Test2&Test3) $test5 = []; } ``` Before: ```php class GH7190 { private const ?string C1 = ''; public const ?string CCCC2 = ''; const ?string CCCC3 = ''; private ?Test $t2; private ?Test $t3 = null; private readonly Test1&Test2 $t4; private array|(Test2&Test3) $test5 = []; } ``` After: ```php class GH7190 { private const ?string C1 = ''; public const ?string CCCC2 = ''; const ?string CCCC3 = ''; private ?Test $t2; private ?Test $t3 = null; private readonly Test1&Test2 $t4; private array|(Test2&Test3) $test5 = []; } ``` --- .../php/editor/indent/FormatVisitor.java | 46 ++++++++++++++- ...oupAlignmentAssignmentTypedConstants01.php | 38 ++++++++++++ ...nmentAssignmentTypedConstants01a.formatted | 39 +++++++++++++ ...nmentAssignmentTypedConstants01b.formatted | 39 +++++++++++++ ...oupAlignmentAssignmentTypedConstants02.php | 39 +++++++++++++ ...nmentAssignmentTypedConstants02a.formatted | 39 +++++++++++++ ...nmentAssignmentTypedConstants02b.formatted | 39 +++++++++++++ .../groupAlignmentAssignmentTypedFields01.php | 57 ++++++++++++++++++ ...lignmentAssignmentTypedFields01a.formatted | 58 +++++++++++++++++++ ...lignmentAssignmentTypedFields01b.formatted | 58 +++++++++++++++++++ .../groupAlignmentAssignmentTypedFields02.php | 58 +++++++++++++++++++ ...lignmentAssignmentTypedFields02a.formatted | 58 +++++++++++++++++++ ...lignmentAssignmentTypedFields02b.formatted | 58 +++++++++++++++++++ .../indent/PHPFormatterAlignmentTest.java | 50 ++++++++++++++++ 14 files changed, 674 insertions(+), 2 deletions(-) create mode 100644 php/php.editor/test/unit/data/testfiles/formatting/alignment/groupAlignmentAssignmentTypedConstants01.php create mode 100644 php/php.editor/test/unit/data/testfiles/formatting/alignment/groupAlignmentAssignmentTypedConstants01.php.testGroupAlignmentAssignmentTypedConstants01a.formatted create mode 100644 php/php.editor/test/unit/data/testfiles/formatting/alignment/groupAlignmentAssignmentTypedConstants01.php.testGroupAlignmentAssignmentTypedConstants01b.formatted create mode 100644 php/php.editor/test/unit/data/testfiles/formatting/alignment/groupAlignmentAssignmentTypedConstants02.php create mode 100644 php/php.editor/test/unit/data/testfiles/formatting/alignment/groupAlignmentAssignmentTypedConstants02.php.testGroupAlignmentAssignmentTypedConstants02a.formatted create mode 100644 php/php.editor/test/unit/data/testfiles/formatting/alignment/groupAlignmentAssignmentTypedConstants02.php.testGroupAlignmentAssignmentTypedConstants02b.formatted create mode 100644 php/php.editor/test/unit/data/testfiles/formatting/alignment/groupAlignmentAssignmentTypedFields01.php create mode 100644 php/php.editor/test/unit/data/testfiles/formatting/alignment/groupAlignmentAssignmentTypedFields01.php.testGroupAlignmentAssignmentTypedFields01a.formatted create mode 100644 php/php.editor/test/unit/data/testfiles/formatting/alignment/groupAlignmentAssignmentTypedFields01.php.testGroupAlignmentAssignmentTypedFields01b.formatted create mode 100644 php/php.editor/test/unit/data/testfiles/formatting/alignment/groupAlignmentAssignmentTypedFields02.php create mode 100644 php/php.editor/test/unit/data/testfiles/formatting/alignment/groupAlignmentAssignmentTypedFields02.php.testGroupAlignmentAssignmentTypedFields02a.formatted create mode 100644 php/php.editor/test/unit/data/testfiles/formatting/alignment/groupAlignmentAssignmentTypedFields02.php.testGroupAlignmentAssignmentTypedFields02b.formatted diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/indent/FormatVisitor.java b/php/php.editor/src/org/netbeans/modules/php/editor/indent/FormatVisitor.java index b811e01ee062..509b8030d356 100644 --- a/php/php.editor/src/org/netbeans/modules/php/editor/indent/FormatVisitor.java +++ b/php/php.editor/src/org/netbeans/modules/php/editor/indent/FormatVisitor.java @@ -52,6 +52,7 @@ import org.netbeans.modules.php.editor.parser.astnodes.Attribute; import org.netbeans.modules.php.editor.parser.astnodes.AttributeDeclaration; import org.netbeans.modules.php.editor.parser.astnodes.Block; +import org.netbeans.modules.php.editor.parser.astnodes.BodyDeclaration.Modifier; import org.netbeans.modules.php.editor.parser.astnodes.CaseDeclaration; import org.netbeans.modules.php.editor.parser.astnodes.CastExpression; import org.netbeans.modules.php.editor.parser.astnodes.CatchClause; @@ -120,6 +121,8 @@ public class FormatVisitor extends DefaultVisitor { private static final Logger LOGGER = Logger.getLogger(FormatVisitor.class.getName()); + private static final int SPACE_LENGTH = 1; + private static final int CONST_AND_SPACE_LENGTH = "const ".length(); // NOI18N private final BaseDocument document; private final List formatTokens; private final TokenSequence ts; @@ -1097,7 +1100,9 @@ public void visit(ConstantDeclaration node) { } if (ts.token().id() == PHPTokenId.PHP_TOKEN || ts.token().id() == PHPTokenId.PHP_OPERATOR) { - handleGroupAlignment(node.getNames().get(0)); + // GH-7190 + int nodeLength = computeConstNodeNameLength(node); + handleGroupAlignment(nodeLength); addFormatToken(formatTokens); } else { ts.movePrevious(); @@ -1136,6 +1141,21 @@ public void visit(ConstantDeclaration node) { } } + private int computeConstNodeNameLength(ConstantDeclaration node) { + int modifierLength = 0; + if (!Modifier.isImplicitPublic(node.getModifier())) { + modifierLength = node.getModifierString().length() + SPACE_LENGTH; + } + Expression constType = node.getConstType(); + int constTypeLength = getTypeLength(constType); + if (constTypeLength != 0) { + constTypeLength += SPACE_LENGTH; + } + Identifier constName = node.getNames().get(0); + int constNameLength = constName.getEndOffset() - constName.getStartOffset(); + return modifierLength + CONST_AND_SPACE_LENGTH + constTypeLength + constNameLength; + } + @Override public void visit(CaseDeclaration node) { Block block = (Block) path.get(1); @@ -2175,7 +2195,12 @@ public void visit(SingleFieldDeclaration node) { assert (parent instanceof FieldsDeclaration); FieldsDeclaration fieldsDeclaration = (FieldsDeclaration) path.get(1); if (ts.token().id() == PHPTokenId.PHP_OPERATOR && TokenUtilities.textEquals("=", ts.token().text())) { //NOI18N - int realNodeLength = fieldsDeclaration.getModifierString().length() + " ".length() + name.getEndOffset() - name.getStartOffset(); //NOI18N + // GH-7190 + int fieldTypeLength = getTypeLength(fieldType); + if (fieldTypeLength != 0) { + fieldTypeLength += SPACE_LENGTH; + } + int realNodeLength = fieldsDeclaration.getModifierString().length() + SPACE_LENGTH + fieldTypeLength + name.getEndOffset() - name.getStartOffset(); handleGroupAlignment(realNodeLength); addFormatToken(formatTokens); } else { @@ -3329,6 +3354,23 @@ private boolean moveNext() { return value; } + private int getTypeLength(@NullAllowed Expression expression) { + int typeLength = 0; + if (expression != null) { + int start = expression.getStartOffset(); + int end = expression.getEndOffset(); + try { + String typeString = document.getText(start, end - start); + // e.g. `private array | int | null $a = 1; + typeLength = typeString.replace(" ", "").length(); // NOI18N + } catch (BadLocationException ex) { + LOGGER.log(Level.WARNING, "Invalid offset: {0}", ex.offsetRequested()); // NOI18N + typeLength = expression.getEndOffset() - expression.getStartOffset() + SPACE_LENGTH; + } + } + return typeLength; + } + /** * * @param node and identifier that is before the operator that is aligned in diff --git a/php/php.editor/test/unit/data/testfiles/formatting/alignment/groupAlignmentAssignmentTypedConstants01.php b/php/php.editor/test/unit/data/testfiles/formatting/alignment/groupAlignmentAssignmentTypedConstants01.php new file mode 100644 index 000000000000..7c2ffa24ee9e --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/formatting/alignment/groupAlignmentAssignmentTypedConstants01.php @@ -0,0 +1,38 @@ + options = new HashMap<>(FmtOptions.getDefaults()); + options.put(FmtOptions.GROUP_ALIGNMENT_ASSIGNMENT, true); + reformatFileContents(getTestFilePath("groupAlignmentAssignmentTypedFields01.php"), options, false, true); + } + + public void testGroupAlignmentAssignmentTypedFields01b() throws Exception { + HashMap options = new HashMap<>(FmtOptions.getDefaults()); + options.put(FmtOptions.GROUP_ALIGNMENT_ASSIGNMENT, false); + reformatFileContents(getTestFilePath("groupAlignmentAssignmentTypedFields01.php"), options, false, true); + } + + public void testGroupAlignmentAssignmentTypedFields02a() throws Exception { + HashMap options = new HashMap<>(FmtOptions.getDefaults()); + options.put(FmtOptions.GROUP_ALIGNMENT_ASSIGNMENT, true); + reformatFileContents(getTestFilePath("groupAlignmentAssignmentTypedFields02.php"), options, false, true); + } + + public void testGroupAlignmentAssignmentTypedFields02b() throws Exception { + HashMap options = new HashMap<>(FmtOptions.getDefaults()); + options.put(FmtOptions.GROUP_ALIGNMENT_ASSIGNMENT, false); + reformatFileContents(getTestFilePath("groupAlignmentAssignmentTypedFields02.php"), options, false, true); + } + + public void testGroupAlignmentAssignmentTypedConstants01a() throws Exception { + HashMap options = new HashMap<>(FmtOptions.getDefaults()); + options.put(FmtOptions.GROUP_ALIGNMENT_ASSIGNMENT, true); + reformatFileContents(getTestFilePath("groupAlignmentAssignmentTypedConstants01.php"), options, false, true); + } + + public void testGroupAlignmentAssignmentTypedConstants01b() throws Exception { + HashMap options = new HashMap<>(FmtOptions.getDefaults()); + options.put(FmtOptions.GROUP_ALIGNMENT_ASSIGNMENT, false); + reformatFileContents(getTestFilePath("groupAlignmentAssignmentTypedConstants01.php"), options, false, true); + } + + public void testGroupAlignmentAssignmentTypedConstants02a() throws Exception { + HashMap options = new HashMap<>(FmtOptions.getDefaults()); + options.put(FmtOptions.GROUP_ALIGNMENT_ASSIGNMENT, true); + reformatFileContents(getTestFilePath("groupAlignmentAssignmentTypedConstants02.php"), options, false, true); + } + + public void testGroupAlignmentAssignmentTypedConstants02b() throws Exception { + HashMap options = new HashMap<>(FmtOptions.getDefaults()); + options.put(FmtOptions.GROUP_ALIGNMENT_ASSIGNMENT, false); + reformatFileContents(getTestFilePath("groupAlignmentAssignmentTypedConstants02.php"), options, false, true); + } + }