This is an automated email from the ASF dual-hosted git repository. junichi11 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/netbeans.git
The following commit(s) were added to refs/heads/master by this push: new 52b4131 [NETBEANS-5073] Add space after data type new cbadb54 Merge pull request #2565 from KacerCZ/netbeans-5073-space-after-type 52b4131 is described below commit 52b413122eeb4e23c55fcc18e46c1f674bca037c Author: Tomas Prochazka <ka...@razdva.cz> AuthorDate: Sat Nov 28 00:49:15 2020 +0100 [NETBEANS-5073] Add space after data type https://issues.apache.org/jira/browse/NETBEANS-5073 Fixes formatting of PHP code by adding space between data type and parameter or property. Source ```php class Foo { public string$name; public function isValid(float$length): bool {} } ``` is formatted as ```php class Foo { public string $name; public function isValid(float $length) {} } ``` --- .../modules/php/editor/indent/FormatToken.java | 1 + .../modules/php/editor/indent/FormatVisitor.java | 15 ++++++++ .../modules/php/editor/indent/TokenFormatter.java | 3 ++ .../data/testfiles/formatting/spaceAfterType.php | 34 ++++++++++++++++++ .../formatting/spaceAfterType.php.formatted | 40 ++++++++++++++++++++++ .../php/editor/indent/PHPFormatterTest.java | 5 +++ 6 files changed, 98 insertions(+) diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/indent/FormatToken.java b/php/php.editor/src/org/netbeans/modules/php/editor/indent/FormatToken.java index 3abd72a..16b5c60 100644 --- a/php/php.editor/src/org/netbeans/modules/php/editor/indent/FormatToken.java +++ b/php/php.editor/src/org/netbeans/modules/php/editor/indent/FormatToken.java @@ -158,6 +158,7 @@ public class FormatToken { WHITESPACE_AFTER_NULLABLE_TYPE_PREFIX, WHITESPACE_BEFORE_MULTI_CATCH_SEPARATOR, WHITESPACE_AFTER_MULTI_CATCH_SEPARATOR, + WHITESPACE_AFTER_TYPE, LINE_COMMENT, COMMENT, COMMENT_START, 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 9c8d46c..eb408c3 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 @@ -1261,6 +1261,17 @@ public class FormatVisitor extends DefaultVisitor { } @Override + public void visit(FormalParameter node) { + Expression parameterType = node.getParameterType(); + scan(parameterType); + if (parameterType != null) { + formatTokens.add(new FormatToken(FormatToken.Kind.WHITESPACE_AFTER_TYPE, parameterType.getEndOffset())); + } + scan(node.getParameterName()); + scan(node.getDefaultValue()); + } + + @Override public void visit(FunctionInvocation node) { if (path.size() > 1 && path.get(1) instanceof MethodInvocation) { while (ts.moveNext() && ts.token().id() != PHPTokenId.PHP_OBJECT_OPERATOR @@ -1592,6 +1603,10 @@ public class FormatVisitor extends DefaultVisitor { @Override public void visit(SingleFieldDeclaration node) { + Expression fieldType = node.getFieldType(); + if (fieldType != null) { + formatTokens.add(new FormatToken(FormatToken.Kind.WHITESPACE_AFTER_TYPE, fieldType.getEndOffset())); + } Variable name = node.getName(); scan(name); if (node.getValue() != null) { diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/indent/TokenFormatter.java b/php/php.editor/src/org/netbeans/modules/php/editor/indent/TokenFormatter.java index ca4b942..7e5e9b0 100644 --- a/php/php.editor/src/org/netbeans/modules/php/editor/indent/TokenFormatter.java +++ b/php/php.editor/src/org/netbeans/modules/php/editor/indent/TokenFormatter.java @@ -1718,6 +1718,9 @@ public class TokenFormatter { case WHITESPACE_AFTER_MULTI_CATCH_SEPARATOR: countSpaces = 1; break; + case WHITESPACE_AFTER_TYPE: + countSpaces = 1; + break; default: //no-op } diff --git a/php/php.editor/test/unit/data/testfiles/formatting/spaceAfterType.php b/php/php.editor/test/unit/data/testfiles/formatting/spaceAfterType.php new file mode 100644 index 0000000..f2e250d --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/formatting/spaceAfterType.php @@ -0,0 +1,34 @@ +<?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. + */ +function test(string$param) { +} + +$lambda = function (int$count) { + +}; + +fn(\Countable$collection) => $collection->count(); + +class Foo { + public string$name; + public function isValid(float$length): bool { + + } +} diff --git a/php/php.editor/test/unit/data/testfiles/formatting/spaceAfterType.php.formatted b/php/php.editor/test/unit/data/testfiles/formatting/spaceAfterType.php.formatted new file mode 100644 index 0000000..5839fb0 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/formatting/spaceAfterType.php.formatted @@ -0,0 +1,40 @@ +<?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. + */ + +function test(string $param) { + +} + +$lambda = function (int $count) { + +}; + +fn(\Countable $collection) => $collection->count(); + +class Foo { + + public string $name; + + public function isValid(float $length): bool { + + } + +} diff --git a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/indent/PHPFormatterTest.java b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/indent/PHPFormatterTest.java index 9238b4f..b8ee304 100644 --- a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/indent/PHPFormatterTest.java +++ b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/indent/PHPFormatterTest.java @@ -888,4 +888,9 @@ public class PHPFormatterTest extends PHPFormatterTestBase { reformatFileContents("testfiles/formatting/netbeans4970.php", options); } + public void testSpaceAfterType() throws Exception { + HashMap<String, Object> options = new HashMap<>(FmtOptions.getDefaults()); + reformatFileContents("testfiles/formatting/spaceAfterType.php", options); + } + } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org For additional commands, e-mail: commits-h...@netbeans.apache.org For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists