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 2022/02/10 08:43:44 UTC

[netbeans] branch master updated: [NETBEANS-6438] Add code folding for PHP attributes

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 2924661  [NETBEANS-6438] Add code folding for PHP attributes
     new b4811aa  Merge pull request #3578 from junichi11/netbeans-6438-folding-for-attributes
2924661 is described below

commit 2924661a0b237420cf381649baa6ef7769889910
Author: Junichi Yamamoto <ju...@apache.org>
AuthorDate: Wed Feb 9 14:05:26 2022 +0900

    [NETBEANS-6438] Add code folding for PHP attributes
    
    - https://issues.apache.org/jira/browse/NETBEANS-6438
    - Add code folding for attributes
    - Add `CancelSupport.getDefault().isCancelled()` to check whether the task is canceled
---
 .../modules/php/editor/csl/FoldingScanner.java     |  60 +++++++++++
 .../modules/php/editor/csl/PHPFoldingProvider.java |   4 +-
 .../data/testfiles/attributeSyntax_01.php.folds    | 110 +++++++++++++++++++++
 .../modules/php/editor/csl/FoldingTest.java        |   5 +
 4 files changed, 178 insertions(+), 1 deletion(-)

diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/csl/FoldingScanner.java b/php/php.editor/src/org/netbeans/modules/php/editor/csl/FoldingScanner.java
index 67dc6f7..f82fa74 100644
--- a/php/php.editor/src/org/netbeans/modules/php/editor/csl/FoldingScanner.java
+++ b/php/php.editor/src/org/netbeans/modules/php/editor/csl/FoldingScanner.java
@@ -33,6 +33,7 @@ import org.netbeans.api.lexer.TokenSequence;
 import org.netbeans.editor.BaseDocument;
 import org.netbeans.modules.csl.api.OffsetRange;
 import org.netbeans.modules.csl.spi.ParserResult;
+import org.netbeans.modules.csl.spi.support.CancelSupport;
 import org.netbeans.modules.parsing.api.Source;
 import org.netbeans.modules.php.editor.lexer.LexUtilities;
 import org.netbeans.modules.php.editor.lexer.PHPTokenId;
@@ -50,6 +51,7 @@ import org.netbeans.modules.php.editor.parser.api.Utils;
 import org.netbeans.modules.php.editor.parser.astnodes.ASTError;
 import org.netbeans.modules.php.editor.parser.astnodes.ASTNode;
 import org.netbeans.modules.php.editor.parser.astnodes.ArrayCreation;
+import org.netbeans.modules.php.editor.parser.astnodes.Attribute;
 import org.netbeans.modules.php.editor.parser.astnodes.CatchClause;
 import org.netbeans.modules.php.editor.parser.astnodes.Comment;
 import org.netbeans.modules.php.editor.parser.astnodes.DoStatement;
@@ -130,6 +132,13 @@ public final class FoldingScanner {
             new FoldTemplate(0, 0, "...") // NOI18N
     );
 
+    @NbBundle.Messages("FT_Attributes=Attributes")
+    public static final FoldType TYPE_ATTRIBUTES = FoldType.MEMBER.derive(
+            "attribute", // NOI18N
+            Bundle.FT_Attributes(),
+            new FoldTemplate(0, 0, "#[...]") // NOI18N
+    );
+
     private static final String LAST_CORRECT_FOLDING_PROPERTY = "LAST_CORRECT_FOLDING_PROPERY"; //NOI18N
     private static final boolean FOLD_PHPTAG = !Boolean.getBoolean("nb.php.editor.doNotFoldPhptag"); // NOI18N NETBEANS-5480
 
@@ -352,6 +361,7 @@ public final class FoldingScanner {
     }
 
     private class FoldingVisitor extends DefaultVisitor {
+
         private final Map<String, List<OffsetRange>> folds;
 
         public FoldingVisitor(final Map<String, List<OffsetRange>> folds) {
@@ -360,6 +370,9 @@ public final class FoldingScanner {
 
         @Override
         public void visit(IfStatement node) {
+            if (CancelSupport.getDefault().isCancelled()) {
+                return;
+            }
             super.visit(node);
             if (node.getTrueStatement() != null) {
                 addFold(node.getTrueStatement());
@@ -371,6 +384,9 @@ public final class FoldingScanner {
 
         @Override
         public void visit(UseTraitStatement node) {
+            if (CancelSupport.getDefault().isCancelled()) {
+                return;
+            }
             super.visit(node);
             if (node.getBody() != null) {
                 addFold(node.getBody());
@@ -379,6 +395,9 @@ public final class FoldingScanner {
 
         @Override
         public void visit(ForEachStatement node) {
+            if (CancelSupport.getDefault().isCancelled()) {
+                return;
+            }
             super.visit(node);
             if (node.getStatement() != null) {
                 addFold(node.getStatement());
@@ -387,6 +406,9 @@ public final class FoldingScanner {
 
         @Override
         public void visit(ForStatement node) {
+            if (CancelSupport.getDefault().isCancelled()) {
+                return;
+            }
             super.visit(node);
             if (node.getBody() != null) {
                 addFold(node.getBody());
@@ -395,6 +417,9 @@ public final class FoldingScanner {
 
         @Override
         public void visit(WhileStatement node) {
+            if (CancelSupport.getDefault().isCancelled()) {
+                return;
+            }
             super.visit(node);
             if (node.getBody() != null) {
                 addFold(node.getBody());
@@ -403,6 +428,9 @@ public final class FoldingScanner {
 
         @Override
         public void visit(DoStatement node) {
+            if (CancelSupport.getDefault().isCancelled()) {
+                return;
+            }
             super.visit(node);
             if (node.getBody() != null) {
                 addFold(node.getBody());
@@ -411,6 +439,9 @@ public final class FoldingScanner {
 
         @Override
         public void visit(SwitchStatement node) {
+            if (CancelSupport.getDefault().isCancelled()) {
+                return;
+            }
             super.visit(node);
             if (node.getBody() != null) {
                 addFold(node.getBody());
@@ -419,6 +450,9 @@ public final class FoldingScanner {
 
         @Override
         public void visit(SwitchCase node) {
+            if (CancelSupport.getDefault().isCancelled()) {
+                return;
+            }
             super.visit(node);
             List<Statement> actions = node.getActions();
             if (!actions.isEmpty()) {
@@ -437,6 +471,9 @@ public final class FoldingScanner {
 
         @Override
         public void visit(MatchExpression node) {
+            if (CancelSupport.getDefault().isCancelled()) {
+                return;
+            }
             // NETBEANS-4443 PHP 8.0
             super.visit(node);
             addFold(node.getBlockRange());
@@ -444,6 +481,9 @@ public final class FoldingScanner {
 
         @Override
         public void visit(TryStatement node) {
+            if (CancelSupport.getDefault().isCancelled()) {
+                return;
+            }
             super.visit(node);
             if (node.getBody() != null) {
                 addFold(node.getBody());
@@ -452,6 +492,9 @@ public final class FoldingScanner {
 
         @Override
         public void visit(CatchClause node) {
+            if (CancelSupport.getDefault().isCancelled()) {
+                return;
+            }
             super.visit(node);
             if (node.getBody() != null) {
                 addFold(node.getBody());
@@ -460,6 +503,9 @@ public final class FoldingScanner {
 
         @Override
         public void visit(FinallyClause node) {
+            if (CancelSupport.getDefault().isCancelled()) {
+                return;
+            }
             super.visit(node);
             if (node.getBody() != null) {
                 addFold(node.getBody());
@@ -468,6 +514,9 @@ public final class FoldingScanner {
 
         @Override
         public void visit(ArrayCreation node) {
+            if (CancelSupport.getDefault().isCancelled()) {
+                return;
+            }
             super.visit(node);
             ArrayCreation.Type type = node.getType();
             if (type == ArrayCreation.Type.NEW) {
@@ -477,6 +526,17 @@ public final class FoldingScanner {
             }
         }
 
+        @Override
+        public void visit(Attribute node) {
+            if (CancelSupport.getDefault().isCancelled()) {
+                return;
+            }
+            if (node != null) {
+                addFold(node, TYPE_ATTRIBUTES);
+            }
+            super.visit(node);
+        }
+
         private void addFold(final ASTNode node) {
             if (!(node instanceof ASTError) && !(node instanceof EmptyStatement)) {
                 addFold(createOffsetRange(node));
diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/csl/PHPFoldingProvider.java b/php/php.editor/src/org/netbeans/modules/php/editor/csl/PHPFoldingProvider.java
index 0da604d..a404dc8 100644
--- a/php/php.editor/src/org/netbeans/modules/php/editor/csl/PHPFoldingProvider.java
+++ b/php/php.editor/src/org/netbeans/modules/php/editor/csl/PHPFoldingProvider.java
@@ -31,7 +31,8 @@ import org.netbeans.spi.editor.fold.FoldTypeProvider;
  */
 @MimeRegistration(mimeType = FileUtils.PHP_MIME_TYPE, service = FoldTypeProvider.class, position = 1000)
 public class PHPFoldingProvider implements FoldTypeProvider {
-    private static final Collection<FoldType> TYPES = new ArrayList<>(8);
+
+    private static final Collection<FoldType> TYPES = new ArrayList<>(9);
 
     static {
         TYPES.add(FoldingScanner.TYPE_CLASS);
@@ -42,6 +43,7 @@ public class PHPFoldingProvider implements FoldTypeProvider {
         TYPES.add(FoldingScanner.TYPE_ARRAY);
         TYPES.add(FoldingScanner.TYPE_USE);
         TYPES.add(FoldingScanner.TYPE_PHPTAG);
+        TYPES.add(FoldingScanner.TYPE_ATTRIBUTES);
     }
 
     @Override
diff --git a/php/php.editor/test/unit/data/testfiles/attributeSyntax_01.php.folds b/php/php.editor/test/unit/data/testfiles/attributeSyntax_01.php.folds
new file mode 100644
index 0000000..e69d645
--- /dev/null
+++ b/php/php.editor/test/unit/data/testfiles/attributeSyntax_01.php.folds
@@ -0,0 +1,110 @@
+  <?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.
+-  */
+  
++ #[Class1(1, self::CONSTANT)]
+  class AttributeSyntax
++ {
++     #[Class1(2)]
++     #[Class2(2)]
+|     public const CONSTANT = 'constant';
+| 
++     #[Class1(3)]
+|     public int|string $field;
+| 
++     #[Class1(4), Class2(4)] // group
+|     public $staticField;
+| 
++     #[Class1(5)]
++     public function method(#[Class1(6)] $param1, #[Class1('foo', 'bar', 7)] int $pram2) {}
+| 
++     #[Class1(8)]
++     public static function staticMethod(#[Class1\Test(9)] int|string $param1): bool|int {
+|         return false;
+-     }
+- }
+  
++ #[ChildClass1(1)]
++ class AttributeSyntaxChild extends AttributeSyntax {
+- }
+  
++ #[Trait1(1)]
++ #[Trait2(1)]
+  trait TestTrait
++ {
++     #[Trait1(2)]
+|     public $field;
+| 
++     #[Trait1(3)]
+|     public $staticField;
+| 
++     #[Trait1(4)]
++     public function method(#[Trait1(5)] $param1) {}
+| 
++     #[Trait1(6)]
++     #[Trait2(6)]
++     public static function staticMethod(#[Trait1] int|string $param1): bool|int {
+|         return false;
+-     }
+- }
+  
++ $anon = new #[Anon1(1)] class () {};
+  
++ $anon2 = new #[Anon2(1)] class ($test) {
++     #[Anon2(2)]
+|     public const CONSTANT = 'constant';
+| 
++     #[Anon2(3)]
+|     public string $field = 'test';
+| 
++     #[Anon2(4)]
+|     public static int $staticField = 1;
+| 
++     #[Anon2(5)]
++     public function __construct(#[Anon2(6)] $param1) {
+-     }
+| 
++     #[Anon2(7)] #[Anon2()]
++     public function method(#[Anon2(8)] $param1): int|string {
+|         return 1;
+-     }
+| 
++     #[Anon2]
++     private static function staticMethod(#[Anon2(9)] int|bool $pram1): int|string {
+|         return 1;
+-     }
+- };
+  
++ #[
+|     Func1(1),
+|     Func1(2),
+- ] //group
++ function func1() {}
+  
++ #[Func2(1)]
++ function func2(#[Func2(2)] int|string $param): void {}
+  
++ $labmda1 = #[Lambda1(1)] function() {};
++ $labmda2 = #[Lambda2(1)] #[Lambda2()] function(): void {};
++ $labmda3 = #[Lambda3(1)] static function(): void {};
+  
++ $arrow1 = #[Arrow1(1)] fn() => 100;
++ $arrow2 = #[Arrow2(1), Arrow2(2, "test")] fn(): int|string => 100;
++ $arrow3 = #[Arrow3(1)] static fn(): int|string => 100;
+  
diff --git a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/csl/FoldingTest.java b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/csl/FoldingTest.java
index 674b6ff..ae07324 100644
--- a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/csl/FoldingTest.java
+++ b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/csl/FoldingTest.java
@@ -106,4 +106,9 @@ public class FoldingTest extends PHPTestBase {
         checkFolds("testfiles/parser/foldingMatch_03.php");
     }
 
+    // NETBEANS-6438
+    public void testAttributes_01() throws Exception {
+        checkFolds("testfiles/parser/php80/attributeSyntax_01.php");
+    }
+
 }

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