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 8ab9fe8146 Handle brackets other than string correctly when "]" is 
typed #6706
     new 58f611c7ee Merge pull request #6720 from 
junichi11/php-gh-6706-typing-closing-bracket
8ab9fe8146 is described below

commit 8ab9fe8146560208fb3b8b90a0c6112b495ce2ef
Author: Junichi Yamamoto <junich...@apache.org>
AuthorDate: Sat Nov 18 15:11:34 2023 +0900

    Handle brackets other than string correctly when "]" is typed #6706
    
    - https://github.com/apache/netbeans/issues/6706
    - We can get "[" as PHP_ENCAPSED_AND_WHITESPACE token e.g. `$x = "[$y 
example]";`
    - So, check also token ids
    - Add unit tests
---
 .../typinghooks/PhpTypedTextInterceptor.java       | 23 ++++++++---------
 .../typinghooks/PhpTypedTextInterceptorTest.java   | 30 ++++++++++++++++++++++
 2 files changed, 41 insertions(+), 12 deletions(-)

diff --git 
a/php/php.editor/src/org/netbeans/modules/php/editor/typinghooks/PhpTypedTextInterceptor.java
 
b/php/php.editor/src/org/netbeans/modules/php/editor/typinghooks/PhpTypedTextInterceptor.java
index d6d92c3ee0..c99621066d 100644
--- 
a/php/php.editor/src/org/netbeans/modules/php/editor/typinghooks/PhpTypedTextInterceptor.java
+++ 
b/php/php.editor/src/org/netbeans/modules/php/editor/typinghooks/PhpTypedTextInterceptor.java
@@ -539,13 +539,16 @@ public class PhpTypedTextInterceptor implements 
TypedTextInterceptor {
             }
             token = ts.token();
             while (token != null) {
-                if ((LexUtilities.textEquals(token.text(), '(')) || 
(LexUtilities.textEquals(token.text(), '['))) {
-                    if (LexUtilities.textEquals(token.text(), leftBracket)) {
-                        bracketBalanceWithNewBracket++;
-                    }
-                } else if ((LexUtilities.textEquals(token.text(), ')')) || 
(LexUtilities.textEquals(token.text(), ']'))) {
-                    if (LexUtilities.textEquals(token.text(), bracket)) {
-                        bracketBalanceWithNewBracket--;
+                // GH-6706 we can get "[" as PHP_ENCAPSED_AND_WHITESPACE token 
e.g. $x = "[$y example]"
+                if (token.id() == PHPTokenId.PHP_TOKEN) {
+                    if ((LexUtilities.textEquals(token.text(), '(')) || 
(LexUtilities.textEquals(token.text(), '['))) {
+                        if (LexUtilities.textEquals(token.text(), 
leftBracket)) {
+                            bracketBalanceWithNewBracket++;
+                        }
+                    } else if ((LexUtilities.textEquals(token.text(), ')')) || 
(LexUtilities.textEquals(token.text(), ']'))) {
+                        if (LexUtilities.textEquals(token.text(), bracket)) {
+                            bracketBalanceWithNewBracket--;
+                        }
                     }
                 }
                 if (!ts.moveNext()) {
@@ -553,11 +556,7 @@ public class PhpTypedTextInterceptor implements 
TypedTextInterceptor {
                 }
                 token = ts.token();
             }
-            if (bracketBalanceWithNewBracket == 0) {
-                skipClosingBracket = false;
-            } else {
-                skipClosingBracket = true;
-            }
+            skipClosingBracket = bracketBalanceWithNewBracket != 0;
         }
 
         return skipClosingBracket;
diff --git 
a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/typinghooks/PhpTypedTextInterceptorTest.java
 
b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/typinghooks/PhpTypedTextInterceptorTest.java
index 4f64215ea0..a27b7d66f3 100644
--- 
a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/typinghooks/PhpTypedTextInterceptorTest.java
+++ 
b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/typinghooks/PhpTypedTextInterceptorTest.java
@@ -698,6 +698,36 @@ public class PhpTypedTextInterceptorTest extends 
PhpTypinghooksTestBase {
         insertChar(original, ' ', expected);
     }
 
+    public void testIssueGH6706_01() throws Exception {
+        String original = ""
+                + "$test = \"[$variable test]\";\n"
+                + "$array['key'^];";
+        String expected = ""
+                + "$test = \"[$variable test]\";\n"
+                + "$array['key']^;";
+        insertChar(original, ']', expected);
+    }
+
+    public void testIssueGH6706_02() throws Exception {
+        String original = ""
+                + "$test = \"[test $variable]\";\n"
+                + "$array['key'^];";
+        String expected = ""
+                + "$test = \"[test $variable]\";\n"
+                + "$array['key']^;";
+        insertChar(original, ']', expected);
+    }
+
+    public void testIssueGH6706_03() throws Exception {
+        String original = ""
+                + "$test = \"[$variable]\";\n"
+                + "$array['key'^];";
+        String expected = ""
+                + "$test = \"[$variable]\";\n"
+                + "$array['key']^;";
+        insertChar(original, ']', expected);
+    }
+
 //    Uncomment when CslTestBase.insertChar() will support ambiguous selection 
strings
 //
 //    public void testIssue242358() throws Exception {


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