Skip to content

Commit

Permalink
Merge pull request apache#7423 from junichi11/php-gh-7190-formatting-…
Browse files Browse the repository at this point in the history
…for-typed-fields-alignment

Fix formatting for typed field and const alignment apache#7190
  • Loading branch information
junichi11 authored Jun 5, 2024
2 parents fb457ad + f586da1 commit 66d637f
Show file tree
Hide file tree
Showing 14 changed files with 674 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<FormatToken> formatTokens;
private final TokenSequence<PHPTokenId> ts;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

class GH7190_01 {

private const ?string C1 = '';
private const ?Test C2 = null;
private const Test1|Test2|null C3 = null;
private const array|(Test2&Test3) CONST4 = [];
private const CONSTANT5 = [];
}

class GH7190_02 {

private const ?string CONST1 = '';
public const Test CONST2 = null;
private const ?int CONST3 = null;
protected const ?array CONST4 = [];
private const CONSTANT5 = [];
const array | ( Test2 & Test3 ) CONST6 = [];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

class GH7190_01 {

private const ?string C1 = '';
private const ?Test C2 = null;
private const Test1|Test2|null C3 = null;
private const array|(Test2&Test3) CONST4 = [];
private const CONSTANT5 = [];
}

class GH7190_02 {

private const ?string CONST1 = '';
public const Test CONST2 = null;
private const ?int CONST3 = null;
protected const ?array CONST4 = [];
private const CONSTANT5 = [];
const array|(Test2&Test3) CONST6 = [];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

class GH7190_01 {

private const ?string C1 = '';
private const ?Test C2 = null;
private const Test1|Test2|null C3 = null;
private const array|(Test2&Test3) CONST4 = [];
private const CONSTANT5 = [];
}

class GH7190_02 {

private const ?string CONST1 = '';
public const Test CONST2 = null;
private const ?int CONST3 = null;
protected const ?array CONST4 = [];
private const CONSTANT5 = [];
const array|(Test2&Test3) CONST6 = [];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

class GH7190_01 {

private const ?string C1 = '';
private const ?Test C2 = null;
private const Test1|Test2|null C3 = null;
private const array|(Test2&Test3) CONST4 = [];
private const CONSTANT5 = [];
}

class GH7190_02 {

private const ?string CONST1 = '';
public const Test CONST2 = null;
private const ?int CONST3 = null;
protected const ?array CONST4 = [];
private const CONSTANT5 = [];
const array|(Test2&Test3) CONST6 = [];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

class GH7190_01 {

private const ?string C1 = '';
private const ?Test C2 = null;
private const Test1|Test2|null C3 = null;
private const array|(Test2&Test3) CONST4 = [];
private const CONSTANT5 = [];
}

class GH7190_02 {

private const ?string CONST1 = '';
public const Test CONST2 = null;
private const ?int CONST3 = null;
protected const ?array CONST4 = [];
private const CONSTANT5 = [];
const array|(Test2&Test3) CONST6 = [];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

class GH7190_01 {

private const ?string C1 = '';
private const ?Test C2 = null;
private const Test1|Test2|null C3 = null;
private const array|(Test2&Test3) CONST4 = [];
private const CONSTANT5 = [];
}

class GH7190_02 {

private const ?string CONST1 = '';
public const Test CONST2 = null;
private const ?int CONST3 = null;
protected const ?array CONST4 = [];
private const CONSTANT5 = [];
const array|(Test2&Test3) CONST6 = [];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

class GH7190_01 {

private ?string $t1 = '';
private ?Test $t2 = null;
private Test1|Test2|null $t3 = null;
private array|(Test2&Test3) $test4 = [];
private $test5 = [];
}

class GH7190_02 {

private ?string $test1 = '';
private Test $test2 = null;
private ?int $test3 = null;
private ?array $test4 = [];
}

class GH7190_03 {

private ?string $t1 = '';
private ?Test $t2;
private ?Test $t3 = null;
private readonly Test1&Test2 $t4;
private array |( Test2 & Test3 ) $test5 = [];
}

class GH7190_04 {

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 = [];
}
Loading

0 comments on commit 66d637f

Please sign in to comment.