This is an automated email from the ASF dual-hosted git repository.
tmysik 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 2dc19077d9 Fix the formatting for the textual operators(`AND`, `OR`,
`XOR`) #4635
new cafc4bbbed Merge pull request #4741 from
junichi11/gh-4635-formatting-for-textual-operators
2dc19077d9 is described below
commit 2dc19077d9336eee4916ca509cad5ed4d79a84a8
Author: Junichi Yamamoto <[email protected]>
AuthorDate: Thu Oct 6 14:25:17 2022 +0900
Fix the formatting for the textual operators(`AND`, `OR`, `XOR`) #4635
- `WHITESPACE_BEFORE_BINARY_OP` and `WHITESPACE_AFTER_BINARY_OP` are added
to `&&` and `||`
- There is the problem if we add them to `AND`, `OR`, and `XOR`
- See: https://bz.apache.org/netbeans/show_bug.cgi?id=240274
- e.g. if they are false, `copy($old,$new) or die("error");` ->
`copy($old,$new) ordie("error");`
- Instead, add `WHITESPACE_AROUND_TEXTUAL_OP` as a new token
---
.../modules/php/editor/indent/FormatToken.java | 1 +
.../modules/php/editor/indent/FormatVisitor.java | 13 ++++--
.../modules/php/editor/indent/TokenFormatter.java | 3 ++
.../data/testfiles/formatting/spaces/gh4635_01.php | 51 ++++++++++++++++++++++
.../formatting/spaces/gh4635_01.php.formatted | 49 +++++++++++++++++++++
.../data/testfiles/formatting/spaces/gh4635_02.php | 51 ++++++++++++++++++++++
.../formatting/spaces/gh4635_02.php.formatted | 49 +++++++++++++++++++++
.../php/editor/indent/PHPFormatterSpacesTest.java | 12 +++++
8 files changed, 226 insertions(+), 3 deletions(-)
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 75a20a2f94..2ea0f02fa9 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
@@ -53,6 +53,7 @@ public class FormatToken {
WHITESPACE_AROUND_INTERSECTION_TYPE_SEPARATOR,
WHITESPACE_AROUND_CONCAT_OP,
WHITESPACE_AROUND_UNARY_OP,
+ WHITESPACE_AROUND_TEXTUAL_OP, // AND, OR, XOR
WHITESPACE_BEFORE_BINARY_OP,
WHITESPACE_AFTER_BINARY_OP,
WHITESPACE_AROUND_TERNARY_OP,
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 69ab21dff3..ab0a7b5159 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
@@ -1895,12 +1895,19 @@ public class FormatVisitor extends DefaultVisitor {
&& lastIndex < ts.index()) {
addFormatToken(formatTokens);
}
- // don't add to AND, OR, and XOR (PHPTokenId.PHP_TEXTUAL_OPERATOR)
- // see https://netbeans.org/bugzilla/show_bug.cgi?id=240274
- if (ts.token().id() == PHPTokenId.PHP_TOKEN || ts.token().id() ==
PHPTokenId.PHP_OPERATOR) {
+ // don't add WHITESPACE_BEFORE_BINARY_OP and
WHITESPACE_AFTER_BINARY_OP to AND, OR, and XOR (PHPTokenId.PHP_TEXTUAL_OPERATOR)
+ // e.g. copy($old,$new) or die("error"); -> copy($old,$new)
ordie("error");
+ // see https://bz.apache.org/netbeans/show_bug.cgi?id=240274
+ if (ts.token().id() == PHPTokenId.PHP_TOKEN
+ || ts.token().id() == PHPTokenId.PHP_OPERATOR) {
formatTokens.add(new FormatToken(whitespaceBefore, ts.offset()));
addFormatToken(formatTokens);
formatTokens.add(new FormatToken(whitespaceAfter, ts.offset() +
ts.token().length()));
+ } else if (ts.token().id() == PHPTokenId.PHP_TEXTUAL_OPERATOR) {
+ // always add whitespace gh-4635
+ formatTokens.add(new
FormatToken(FormatToken.Kind.WHITESPACE_AROUND_TEXTUAL_OP, ts.offset()));
+ addFormatToken(formatTokens);
+ formatTokens.add(new
FormatToken(FormatToken.Kind.WHITESPACE_AROUND_TEXTUAL_OP, ts.offset() +
ts.token().length()));
} else {
ts.movePrevious();
}
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 4cb91610d2..3a93b73014 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
@@ -1083,6 +1083,9 @@ public class TokenFormatter {
case WHITESPACE_AROUND_UNARY_OP:
countSpaces =
docOptions.spaceAroundUnaryOps ? 1 : countSpaces;
break;
+ case WHITESPACE_AROUND_TEXTUAL_OP:
+ countSpaces = 1;
+ break;
case WHITESPACE_BEFORE_BINARY_OP:
if (docOptions.wrapAfterBinOps) {
countSpaces =
docOptions.spaceAroundBinaryOps ? 1 : 0;
diff --git
a/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh4635_01.php
b/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh4635_01.php
new file mode 100644
index 0000000000..4b2e18e722
--- /dev/null
+++ b/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh4635_01.php
@@ -0,0 +1,51 @@
+<?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.
+ */
+
+if ($a AND !$b) { // test
+
+}
+
+if ($a OR !$b) { // test
+
+}
+
+if ($a XOR !$b) { // test
+
+}
+
+if ($a AND $b) {
+
+}
+
+if ($a OR $b) {
+
+}
+
+if ($a XOR $b) {
+
+}
+
+if ($a && !$b) {
+
+}
+
+if ($a || !$b) {
+
+}
diff --git
a/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh4635_01.php.formatted
b/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh4635_01.php.formatted
new file mode 100644
index 0000000000..84c43dd234
--- /dev/null
+++
b/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh4635_01.php.formatted
@@ -0,0 +1,49 @@
+<?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.
+ */
+
+if ($a AND !$b) { // test
+}
+
+if ($a OR !$b) { // test
+}
+
+if ($a XOR !$b) { // test
+}
+
+if ($a AND $b) {
+
+}
+
+if ($a OR $b) {
+
+}
+
+if ($a XOR $b) {
+
+}
+
+if ($a && !$b) {
+
+}
+
+if ($a || !$b) {
+
+}
diff --git
a/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh4635_02.php
b/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh4635_02.php
new file mode 100644
index 0000000000..4b2e18e722
--- /dev/null
+++ b/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh4635_02.php
@@ -0,0 +1,51 @@
+<?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.
+ */
+
+if ($a AND !$b) { // test
+
+}
+
+if ($a OR !$b) { // test
+
+}
+
+if ($a XOR !$b) { // test
+
+}
+
+if ($a AND $b) {
+
+}
+
+if ($a OR $b) {
+
+}
+
+if ($a XOR $b) {
+
+}
+
+if ($a && !$b) {
+
+}
+
+if ($a || !$b) {
+
+}
diff --git
a/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh4635_02.php.formatted
b/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh4635_02.php.formatted
new file mode 100644
index 0000000000..b283e5ad87
--- /dev/null
+++
b/php/php.editor/test/unit/data/testfiles/formatting/spaces/gh4635_02.php.formatted
@@ -0,0 +1,49 @@
+<?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.
+ */
+
+if ($a AND !$b) { // test
+}
+
+if ($a OR !$b) { // test
+}
+
+if ($a XOR !$b) { // test
+}
+
+if ($a AND $b) {
+
+}
+
+if ($a OR $b) {
+
+}
+
+if ($a XOR $b) {
+
+}
+
+if ($a&&!$b) {
+
+}
+
+if ($a||!$b) {
+
+}
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 3d299c407c..7ba212da07 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
@@ -1025,6 +1025,18 @@ public class PHPFormatterSpacesTest extends
PHPFormatterTestBase {
reformatFileContents("testfiles/formatting/spaces/issue240274.php",
options);
}
+ public void testGH4635_01() throws Exception {
+ HashMap<String, Object> options = new
HashMap<>(FmtOptions.getDefaults());
+ options.put(FmtOptions.SPACE_AROUND_BINARY_OPS, true);
+ reformatFileContents("testfiles/formatting/spaces/gh4635_01.php",
options);
+ }
+
+ public void testGH4635_02() throws Exception {
+ HashMap<String, Object> options = new
HashMap<>(FmtOptions.getDefaults());
+ options.put(FmtOptions.SPACE_AROUND_BINARY_OPS, false);
+ reformatFileContents("testfiles/formatting/spaces/gh4635_02.php",
options);
+ }
+
public void testSpacesAroundReturnType01() throws Exception {
HashMap<String, Object> options = new HashMap<String,
Object>(FmtOptions.getDefaults());
reformatFileContents("testfiles/formatting/spaces/spaceAroundReturnType01.php",
options);
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists