This is an automated email from the ASF dual-hosted git repository.

junichi11 pushed a commit to branch php-nb21-features
in repository https://gitbox.apache.org/repos/asf/netbeans.git


The following commit(s) were added to refs/heads/php-nb21-features by this push:
     new 76d2feab8a Fix the formatting for a method invocation with a ternary 
or null-coalescing operator #5380
     new d3ebc8be92 Merge pull request #6724 from 
junichi11/php-gh-5380-ternary-formatting
76d2feab8a is described below

commit 76d2feab8adca311184866f81723d7e1b8566576
Author: Junichi Yamamoto <junich...@apache.org>
AuthorDate: Sun Nov 19 11:12:45 2023 +0900

    Fix the formatting for a method invocation with a ternary or 
null-coalescing operator #5380
    
    - https://github.com/apache/netbeans/issues/5380
    - Add whitespaces after `UNBREAKABLE_SEQUENCE_END` token
    - Check whether there is a whitespace token before an indent token
    - Add unit tests
---
 .../modules/php/editor/indent/FormatVisitor.java   | 11 ++++++++++
 .../formatting/broken/issue197074_04.php.formatted |  6 +++---
 .../data/testfiles/formatting/spaces/gh5380_01.php | 23 +++++++++++++++++++++
 .../spaces/gh5380_01.php.testGH5380_01a.formatted  | 24 ++++++++++++++++++++++
 .../spaces/gh5380_01.php.testGH5380_01b.formatted  | 24 ++++++++++++++++++++++
 .../data/testfiles/formatting/spaces/gh5380_02.php | 23 +++++++++++++++++++++
 .../spaces/gh5380_02.php.testGH5380_02a.formatted  | 24 ++++++++++++++++++++++
 .../spaces/gh5380_02.php.testGH5380_02b.formatted  | 24 ++++++++++++++++++++++
 .../php/editor/indent/PHPFormatterSpacesTest.java  | 23 +++++++++++++++++++++
 9 files changed, 179 insertions(+), 3 deletions(-)

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 2913ec7a66..caa0d307c9 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
@@ -3133,6 +3133,17 @@ public class FormatVisitor extends DefaultVisitor {
             removedWS = formatTokens.remove(formatTokens.size() - 1);
             index--;
             lastToken = formatTokens.get(index);
+        } else if (lastToken.getId() == FormatToken.Kind.INDENT) {
+            // GH-5380 there are whitespaces before ")" in the method 
invocation with ternary or null-coalescing operator
+            // e.g. var_dump($a ? 1 : 2  ); var_dump($a ?? null    );
+            if (index - 1 > 0) {
+                FormatToken possibleWSToken = formatTokens.get(index -1);
+                if (possibleWSToken.isWhitespace()) {
+                    removedWS = formatTokens.remove(index - 1);
+                    index -= 2;
+                    lastToken = formatTokens.get(index);
+                }
+            }
         }
 
         if (lastToken.getId() == FormatToken.Kind.WHITESPACE_AFTER_COMMA) {
diff --git 
a/php/php.editor/test/unit/data/testfiles/formatting/broken/issue197074_04.php.formatted
 
b/php/php.editor/test/unit/data/testfiles/formatting/broken/issue197074_04.php.formatted
index 49243bbd16..85edae3adc 100644
--- 
a/php/php.editor/test/unit/data/testfiles/formatting/broken/issue197074_04.php.formatted
+++ 
b/php/php.editor/test/unit/data/testfiles/formatting/broken/issue197074_04.php.formatted
@@ -72,10 +72,10 @@ if ($request->isMethod('post') && 
$form->bindAndValid($request)) {
 
     $module_action = $this->getPage()->getModuleAction();
     if ($module_action == 'main/editCompanyAddress')
-        $
-            this->redirect 
($this->getHelper()->link('main/editMyCompany')->getHref());
+        $t
+            his->redirect 
($this->getHelper()->link('main/editMyCompany')->getHref());
 
 }
-$this->forms['CompanyAddresses '] = $form;
+$this->forms['CompanyAddresses  '] = $form;
 
 }
diff --git 
a/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh5380_01.php 
b/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh5380_01.php
new file mode 100644
index 0000000000..5961bc2eb3
--- /dev/null
+++ b/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh5380_01.php
@@ -0,0 +1,23 @@
+<?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.
+ */
+
+var_dump(   $test   );
+var_dump(     $test ? 1 : 2     );
+var_dump(     $test ?? null     );
diff --git 
a/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh5380_01.php.testGH5380_01a.formatted
 
b/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh5380_01.php.testGH5380_01a.formatted
new file mode 100644
index 0000000000..4ec40c6bdb
--- /dev/null
+++ 
b/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh5380_01.php.testGH5380_01a.formatted
@@ -0,0 +1,24 @@
+<?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.
+ */
+
+var_dump($test);
+var_dump($test ? 1 : 2);
+var_dump($test ?? null);
diff --git 
a/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh5380_01.php.testGH5380_01b.formatted
 
b/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh5380_01.php.testGH5380_01b.formatted
new file mode 100644
index 0000000000..2b625a0212
--- /dev/null
+++ 
b/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh5380_01.php.testGH5380_01b.formatted
@@ -0,0 +1,24 @@
+<?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.
+ */
+
+var_dump( $test );
+var_dump( $test ? 1 : 2 );
+var_dump( $test ?? null );
diff --git 
a/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh5380_02.php 
b/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh5380_02.php
new file mode 100644
index 0000000000..b05a612f2a
--- /dev/null
+++ b/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh5380_02.php
@@ -0,0 +1,23 @@
+<?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.
+ */
+
+var_dump($test);
+var_dump($test ? 1 : 2);
+var_dump($test ?? null);
diff --git 
a/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh5380_02.php.testGH5380_02a.formatted
 
b/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh5380_02.php.testGH5380_02a.formatted
new file mode 100644
index 0000000000..4ec40c6bdb
--- /dev/null
+++ 
b/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh5380_02.php.testGH5380_02a.formatted
@@ -0,0 +1,24 @@
+<?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.
+ */
+
+var_dump($test);
+var_dump($test ? 1 : 2);
+var_dump($test ?? null);
diff --git 
a/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh5380_02.php.testGH5380_02b.formatted
 
b/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh5380_02.php.testGH5380_02b.formatted
new file mode 100644
index 0000000000..2b625a0212
--- /dev/null
+++ 
b/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh5380_02.php.testGH5380_02b.formatted
@@ -0,0 +1,24 @@
+<?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.
+ */
+
+var_dump( $test );
+var_dump( $test ? 1 : 2 );
+var_dump( $test ?? null );
diff --git 
a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/indent/PHPFormatterSpacesTest.java
 
b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/indent/PHPFormatterSpacesTest.java
index 12d88c6e6d..872ee7f86c 100644
--- 
a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/indent/PHPFormatterSpacesTest.java
+++ 
b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/indent/PHPFormatterSpacesTest.java
@@ -1705,4 +1705,27 @@ public class PHPFormatterSpacesTest extends 
PHPFormatterTestBase {
         
reformatFileContents("testfiles/formatting/spaces/php81/spaceWithinMethodCallParensWithFirstClassCallable_02b.php",
 options);
     }
 
+    public void testGH5380_01a() throws Exception {
+        HashMap<String, Object> options = new 
HashMap<>(FmtOptions.getDefaults());
+        options.put(FmtOptions.SPACE_WITHIN_METHOD_CALL_PARENS, false);
+        reformatFileContents("testfiles/formatting/spaces/gh5380_01.php", 
options,  false, true);
+    }
+
+    public void testGH5380_01b() throws Exception {
+        HashMap<String, Object> options = new 
HashMap<>(FmtOptions.getDefaults());
+        options.put(FmtOptions.SPACE_WITHIN_METHOD_CALL_PARENS, true);
+        reformatFileContents("testfiles/formatting/spaces/gh5380_01.php", 
options,  false, true);
+    }
+
+    public void testGH5380_02a() throws Exception {
+        HashMap<String, Object> options = new 
HashMap<>(FmtOptions.getDefaults());
+        options.put(FmtOptions.SPACE_WITHIN_METHOD_CALL_PARENS, false);
+        reformatFileContents("testfiles/formatting/spaces/gh5380_02.php", 
options,  false, true);
+    }
+
+    public void testGH5380_02b() throws Exception {
+        HashMap<String, Object> options = new 
HashMap<>(FmtOptions.getDefaults());
+        options.put(FmtOptions.SPACE_WITHIN_METHOD_CALL_PARENS, true);
+        reformatFileContents("testfiles/formatting/spaces/gh5380_02.php", 
options,  false, true);
+    }
 }


---------------------------------------------------------------------
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

Reply via email to