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); + } + }