You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@netbeans.apache.org by tm...@apache.org on 2019/08/11 16:03:46 UTC
[netbeans] branch master updated: [NETBEANS-1047] Don't add
whitespace to a wrong place when textual operators are used
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 858cd27 [NETBEANS-1047] Don't add whitespace to a wrong place when textual operators are used
new 16aad9d Merge pull request #1418 from junichi11/netbeans-1047
858cd27 is described below
commit 858cd2795f35cd905fe95d95ff0f9a80ba3589de
Author: Junichi Yamamoto <ju...@apache.org>
AuthorDate: Sun Aug 11 11:38:50 2019 +0900
[NETBEANS-1047] Don't add whitespace to a wrong place when textual operators are used
---
.../modules/php/editor/indent/FormatVisitor.java | 5 +++-
.../modules/php/editor/lexer/LexUtilities.java | 12 +++++++++
.../modules/php/editor/lexer/PHPTokenId.java | 4 +--
.../data/testfiles/formatting/netbeans1047.php | 27 +++++++++++++++++++
.../formatting/netbeans1047.php.formatted | 31 ++++++++++++++++++++++
.../php/editor/indent/PHPFormatterTest.java | 4 +++
6 files changed, 80 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 86dda8f..151733b 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
@@ -1326,10 +1326,12 @@ public class FormatVisitor extends DefaultVisitor {
}
while (ts.moveNext() && ts.offset() < node.getRight().getStartOffset()
- && ts.token().id() != PHPTokenId.PHP_TOKEN && ts.token().id() != PHPTokenId.PHP_OPERATOR
+ && ts.token().id() != PHPTokenId.PHP_TOKEN && !LexUtilities.isPHPOperator(ts.token().id())
&& 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) {
formatTokens.add(new FormatToken(whitespaceBefore, ts.offset()));
addFormatToken(formatTokens);
@@ -2558,4 +2560,5 @@ public class FormatVisitor extends DefaultVisitor {
private static boolean isAnonymousClass(ASTNode astNode) {
return astNode instanceof ClassInstanceCreation && ((ClassInstanceCreation) astNode).isAnonymous();
}
+
}
diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/lexer/LexUtilities.java b/php/php.editor/src/org/netbeans/modules/php/editor/lexer/LexUtilities.java
index cf75fc1..0f4af77 100644
--- a/php/php.editor/src/org/netbeans/modules/php/editor/lexer/LexUtilities.java
+++ b/php/php.editor/src/org/netbeans/modules/php/editor/lexer/LexUtilities.java
@@ -634,4 +634,16 @@ public final class LexUtilities {
return start;
}
+
+ /**
+ * Check whether the token id is an operator(PHP_OPERATOR or
+ * PHP_TEXTUAL_OPERATOR). PHP_TEXTUAL_OPERATOR is "AND", "OR", or "XOR".
+ *
+ * @param id the token id
+ * @return {@code true} the token id is an operator, otherwise {@code false}
+ */
+ public static boolean isPHPOperator(PHPTokenId id) {
+ return id == PHPTokenId.PHP_OPERATOR || id == PHPTokenId.PHP_TEXTUAL_OPERATOR;
+ }
+
}
diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/lexer/PHPTokenId.java b/php/php.editor/src/org/netbeans/modules/php/editor/lexer/PHPTokenId.java
index 61ba1aa..d7a546b 100644
--- a/php/php.editor/src/org/netbeans/modules/php/editor/lexer/PHPTokenId.java
+++ b/php/php.editor/src/org/netbeans/modules/php/editor/lexer/PHPTokenId.java
@@ -156,8 +156,8 @@ public enum PHPTokenId implements TokenId {
PHP__LINE__(null, "constant"), //NOI18N
PHP__DIR__(null, "constant"), //NOI18N
PHP__NAMESPACE__(null, "constant"), //NOI18N
- PHP_OPERATOR(null, "operator"), //NOI18N
- PHP_TEXTUAL_OPERATOR(null, "operator"), //NOI18N
+ PHP_OPERATOR(null, "operator"), //NOI18N e.g. ||, &&
+ PHP_TEXTUAL_OPERATOR(null, "operator"), //NOI18N e.g. OR, AND
PHP_PARENT(null, "keyword"), //NOI18N
PHP__CLASS__(null, "constant"), //NOI18N
PHP__TRAIT__(null, "constant"), //NOI18N
diff --git a/php/php.editor/test/unit/data/testfiles/formatting/netbeans1047.php b/php/php.editor/test/unit/data/testfiles/formatting/netbeans1047.php
new file mode 100644
index 0000000..0a07eff
--- /dev/null
+++ b/php/php.editor/test/unit/data/testfiles/formatting/netbeans1047.php
@@ -0,0 +1,27 @@
+<?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.
+ */
+
+$test = false;
+if ($test OR (!$test)) {
+}
+if ($test AND (!$test)) {
+}
+if ($test XOR (!$test)) {
+}
diff --git a/php/php.editor/test/unit/data/testfiles/formatting/netbeans1047.php.formatted b/php/php.editor/test/unit/data/testfiles/formatting/netbeans1047.php.formatted
new file mode 100644
index 0000000..6299bdd
--- /dev/null
+++ b/php/php.editor/test/unit/data/testfiles/formatting/netbeans1047.php.formatted
@@ -0,0 +1,31 @@
+<?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.
+ */
+
+$test = false;
+if ($test OR (!$test)) {
+
+}
+if ($test AND (!$test)) {
+
+}
+if ($test XOR (!$test)) {
+
+}
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 3fd7bfb..6090679 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
@@ -824,4 +824,8 @@ public class PHPFormatterTest extends PHPFormatterTestBase {
reformatFileContents("testfiles/formatting/arrowFunctions02.php", options);
}
+ public void testNetBeans1047() throws Exception {
+ HashMap<String, Object> options = new HashMap<>(FmtOptions.getDefaults());
+ reformatFileContents("testfiles/formatting/netbeans1047.php", options);
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@netbeans.apache.org
For additional commands, e-mail: commits-help@netbeans.apache.org
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists