EADS-3749 encountering issues with the displaying of code in Developer Studio when code folding is enabled
authorSubhashini Sooriarachchi <subslk@netbeans.org>
Mon, 12 Aug 2013 11:26:54 +0530
changeset 464e10b2e8563fc
parent 463 f9493e34684d
child 465 fed38113b171
EADS-3749 encountering issues with the displaying of code in Developer Studio when code folding is enabled
PLSQL/Execution/test/unit/data/expected/fndbasdr.sql.structure
PLSQL/Execution/test/unit/src/org/netbeans/modules/plsql/execution/PlsqlExecutableBlocksMakerTest.java
PLSQL/Folding/nbproject/project.xml
PLSQL/Folding/src/org/netbeans/modules/plsql/fold/FoldAdapter.java
PLSQL/Folding/src/org/netbeans/modules/plsql/fold/FoldSearchObject.java
PLSQL/Folding/src/org/netbeans/modules/plsql/fold/NewPlsqlFoldManager.java
PLSQL/Folding/src/org/netbeans/modules/plsql/fold/PlsqlFoldManagerFactory.java
PLSQL/Folding/test/unit/src/org/netbeans/modules/plsql/fold/FoldSearchObjectTest.java
PLSQL/Lexer/src/org/netbeans/modules/plsql/lexer/PlsqlBlockFactory.java
PLSQL/Lexer/test/unit/data/TestComments.apy
PLSQL/Lexer/test/unit/data/expected/Dictionary.apy.structure
PLSQL/Lexer/test/unit/data/expected/FndEvent.apy.structure
PLSQL/Lexer/test/unit/data/expected/IdentityPayInfo.apy.structure
PLSQL/Lexer/test/unit/data/expected/ScorecardInputValue.apy.structure
PLSQL/Lexer/test/unit/data/expected/TestComments.apy.structure
PLSQL/Lexer/test/unit/data/expected/Trainer.apy.structure
PLSQL/Lexer/test/unit/data/expected/sys_IalObjectSlave.apy.structure
PLSQL/Lexer/test/unit/data/expected/test2.apy.structure
PLSQL/Lexer/test/unit/data/expected/txtser.ins.structure
PLSQL/Lexer/test/unit/src/org/netbeans/modules/plsql/lexer/PlsqlBlockFactoryTest.java
     1.1 --- a/PLSQL/Execution/test/unit/data/expected/fndbasdr.sql.structure	Mon Aug 12 10:50:10 2013 +0530
     1.2 +++ b/PLSQL/Execution/test/unit/data/expected/fndbasdr.sql.structure	Mon Aug 12 11:26:54 2013 +0530
     1.3 @@ -27,7 +27,6 @@
     1.4  COMMENT
     1.5  BEGINEND
     1.6  COMMENT
     1.7 -COMMENT
     1.8  BEGINEND
     1.9  COMMENT
    1.10  BEGINEND
    1.11 @@ -35,7 +34,6 @@
    1.12  COMMENT
    1.13  BEGINEND
    1.14  COMMENT
    1.15 -COMMENT
    1.16  BEGINEND
    1.17  COMMENT
    1.18  BEGINEND
     2.1 --- a/PLSQL/Execution/test/unit/src/org/netbeans/modules/plsql/execution/PlsqlExecutableBlocksMakerTest.java	Mon Aug 12 10:50:10 2013 +0530
     2.2 +++ b/PLSQL/Execution/test/unit/src/org/netbeans/modules/plsql/execution/PlsqlExecutableBlocksMakerTest.java	Mon Aug 12 11:26:54 2013 +0530
     2.3 @@ -99,7 +99,7 @@
     2.4              PlsqlExecutableBlocksMaker instance = new PlsqlExecutableBlocksMaker(doc);
     2.5              List<PlsqlExecutableObject> makeExceutableObjects = instance.makeExceutableObjects();
     2.6              
     2.7 -            int size = makeExceutableObjects.size();           
     2.8 +            int size = makeExceutableObjects.size(); 
     2.9              assertEquals(12, size);
    2.10              processExecutableBLocksBlocks(plsqlFileName,makeExceutableObjects);
    2.11              
    2.12 @@ -220,7 +220,7 @@
    2.13              List<PlsqlExecutableObject> makeExceutableObjects = instance.makeExceutableObjects();
    2.14              
    2.15              int size = makeExceutableObjects.size();           
    2.16 -            assertEquals(83, size);
    2.17 +            assertEquals(81, size);
    2.18              processExecutableBLocksBlocks(plsqlFileName,makeExceutableObjects);
    2.19              
    2.20          } catch (BadLocationException ex) {
     3.1 --- a/PLSQL/Folding/nbproject/project.xml	Mon Aug 12 10:50:10 2013 +0530
     3.2 +++ b/PLSQL/Folding/nbproject/project.xml	Mon Aug 12 11:26:54 2013 +0530
     3.3 @@ -102,6 +102,10 @@
     3.4                          <code-name-base>org.netbeans.libs.junit4</code-name-base>
     3.5                          <compile-dependency/>
     3.6                      </test-dependency>
     3.7 +                    <test-dependency>
     3.8 +                        <code-name-base>org.netbeans.modules.nbjunit</code-name-base>
     3.9 +                        <compile-dependency/>
    3.10 +                    </test-dependency>
    3.11                  </test-type>
    3.12              </test-dependencies>
    3.13              <public-packages/>
     4.1 --- a/PLSQL/Folding/src/org/netbeans/modules/plsql/fold/FoldAdapter.java	Mon Aug 12 10:50:10 2013 +0530
     4.2 +++ b/PLSQL/Folding/src/org/netbeans/modules/plsql/fold/FoldAdapter.java	Mon Aug 12 11:26:54 2013 +0530
     4.3 @@ -5,6 +5,7 @@
     4.4  package org.netbeans.modules.plsql.fold;
     4.5  
     4.6  import org.netbeans.api.editor.fold.Fold;
     4.7 +import org.netbeans.api.editor.fold.FoldType;
     4.8  
     4.9  /**
    4.10   *
    4.11 @@ -25,4 +26,8 @@
    4.12     int getEndOffset() {
    4.13        return fold.getEndOffset();
    4.14     }
    4.15 +   
    4.16 +   FoldType getFoldType(){
    4.17 +        return fold.getType();   
    4.18 +   }
    4.19  }
     5.1 --- a/PLSQL/Folding/src/org/netbeans/modules/plsql/fold/FoldSearchObject.java	Mon Aug 12 10:50:10 2013 +0530
     5.2 +++ b/PLSQL/Folding/src/org/netbeans/modules/plsql/fold/FoldSearchObject.java	Mon Aug 12 11:26:54 2013 +0530
     5.3 @@ -5,6 +5,7 @@
     5.4  package org.netbeans.modules.plsql.fold;
     5.5  
     5.6  import java.io.Serializable;
     5.7 +import org.netbeans.api.editor.fold.FoldType;
     5.8  
     5.9  /**
    5.10   *
    5.11 @@ -15,15 +16,17 @@
    5.12     private static final long serialVersionUID = 1L;
    5.13     private final int startOffset;
    5.14     private final int endOffset;
    5.15 +   private FoldType foldType;
    5.16     private FoldAdapter fold;
    5.17  
    5.18 -   FoldSearchObject(int startOffset, int endOffset) {
    5.19 +   FoldSearchObject(int startOffset, int endOffset, FoldType foldType) {
    5.20        this.startOffset = startOffset;
    5.21        this.endOffset = endOffset;
    5.22 +      this.foldType = foldType;
    5.23     }
    5.24  
    5.25     FoldSearchObject(FoldAdapter fold) {
    5.26 -      this(-1, -1);
    5.27 +      this(-1, -1,null);
    5.28        this.fold = fold;
    5.29     }
    5.30  
    5.31 @@ -40,7 +43,14 @@
    5.32        }
    5.33        return endOffset;
    5.34     }
    5.35 -
    5.36 +   
    5.37 +   public FoldType getFoldType(){
    5.38 +       if (foldType == null) {
    5.39 +         return fold.getFoldType();
    5.40 +      }
    5.41 +      return foldType;
    5.42 +   }
    5.43 +   
    5.44     @Override
    5.45     public int hashCode() {
    5.46        int hash = 7;
     6.1 --- a/PLSQL/Folding/src/org/netbeans/modules/plsql/fold/NewPlsqlFoldManager.java	Mon Aug 12 10:50:10 2013 +0530
     6.2 +++ b/PLSQL/Folding/src/org/netbeans/modules/plsql/fold/NewPlsqlFoldManager.java	Mon Aug 12 11:26:54 2013 +0530
     6.3 @@ -42,12 +42,15 @@
     6.4  package org.netbeans.modules.plsql.fold;
     6.5  
     6.6  import java.util.ArrayList;
     6.7 +import java.util.Collections;
     6.8 +import java.util.Comparator;
     6.9  import java.util.List;
    6.10  import java.util.Observable;
    6.11  import java.util.Observer;
    6.12  import java.util.logging.Level;
    6.13  import java.util.logging.Logger;
    6.14  import javax.swing.event.DocumentEvent;
    6.15 +import javax.swing.text.AbstractDocument;
    6.16  import javax.swing.text.BadLocationException;
    6.17  import javax.swing.text.Document;
    6.18  import org.netbeans.api.editor.fold.Fold;
    6.19 @@ -61,6 +64,7 @@
    6.20  import org.netbeans.spi.editor.fold.FoldHierarchyTransaction;
    6.21  import org.netbeans.spi.editor.fold.FoldManager;
    6.22  import org.netbeans.spi.editor.fold.FoldOperation;
    6.23 +import org.openide.util.Exceptions;
    6.24  import org.openide.util.Lookup;
    6.25  import org.openide.util.RequestProcessor;
    6.26  
    6.27 @@ -70,281 +74,472 @@
    6.28   */
    6.29  public class NewPlsqlFoldManager implements FoldManager, Runnable, Observer {
    6.30  
    6.31 -   private static final Logger LOG = Logger.getLogger(NewPlsqlFoldManager.class.getName());
    6.32 -   private static final RequestProcessor RP = new RequestProcessor(NewPlsqlFoldManager.class.getName(), 1, false, false);
    6.33 -   private static final int TASK_DELAY = 1000;
    6.34 -   private final RequestProcessor.Task task = RP.create(this);
    6.35 -   private FoldOperation operation;
    6.36 -   private Document doc;
    6.37 -   // Note: FoldSearchObject need to be in a List, otherwise contains doesn't work. Seems HashSet keeps internal list of hash.
    6.38 -   private final List<FoldSearchObject> foldSearchObjects = new ArrayList<FoldSearchObject>();
    6.39 -   private List<Fold> removedFoldList = new ArrayList<Fold>(3);
    6.40 -   private boolean initial = true;
    6.41 -   private PlsqlBlockFactory blockFactory;
    6.42 +    private static final Logger LOG = Logger.getLogger(NewPlsqlFoldManager.class.getName());
    6.43 +    private static final RequestProcessor RP = new RequestProcessor(NewPlsqlFoldManager.class.getName(), 1, false, false);
    6.44 +    private static final int TASK_DELAY = 100;
    6.45 +    private final RequestProcessor.Task task = RP.create(this);
    6.46 +    private FoldOperation operation;
    6.47 +    private Document doc;
    6.48 +    // Note: FoldSearchObject need to be in a List, otherwise contains doesn't work. Seems HashSet keeps internal list of hash.
    6.49 +    private final List<FoldSearchObject> foldSearchObjects = new ArrayList<FoldSearchObject>();
    6.50 +    private List<Fold> removedFoldList = new ArrayList<Fold>(3);
    6.51 +    private boolean initial = true;
    6.52 +    private PlsqlBlockFactory blockFactory;
    6.53  
    6.54 -   @Override
    6.55 -   public void init(FoldOperation operation) {
    6.56 -      this.operation = operation;
    6.57 -      if (LOG.isLoggable(Level.FINE)) {
    6.58 -         LOG.log(Level.FINE, "Initialized: {0}", System.identityHashCode(this));
    6.59 -      }
    6.60 -   }
    6.61 +    @Override
    6.62 +    public void init(FoldOperation operation) {
    6.63 +        this.operation = operation;
    6.64 +        if (LOG.isLoggable(Level.FINE)) {
    6.65 +            LOG.log(Level.FINE, "Initialized: {0}", System.identityHashCode(this));
    6.66 +        }
    6.67 +    }
    6.68  
    6.69 -   private FoldOperation getOperation() {
    6.70 -      return operation;
    6.71 -   }
    6.72 +    private FoldOperation getOperation() {
    6.73 +        return operation;
    6.74 +    }
    6.75  
    6.76 -   @Override
    6.77 -   public void initFolds(FoldHierarchyTransaction transaction) {
    6.78 -      doc = getOperation().getHierarchy().getComponent().getDocument();
    6.79 -      blockFactory = getBlockFactory();
    6.80 -      if (blockFactory != null) {
    6.81 -         blockFactory.addObserver(this);
    6.82 -      }
    6.83 -      task.schedule(TASK_DELAY);
    6.84 -   }
    6.85 +    @Override
    6.86 +    public void initFolds(FoldHierarchyTransaction transaction) {
    6.87 +        doc = getOperation().getHierarchy().getComponent().getDocument();
    6.88 +        blockFactory = getBlockFactory();
    6.89 +        if (blockFactory != null) {
    6.90 +            blockFactory.addObserver(this);
    6.91 +        }
    6.92 +        task.schedule(TASK_DELAY);
    6.93 +    }
    6.94  
    6.95 -   @Override
    6.96 -   public void insertUpdate(DocumentEvent evt, FoldHierarchyTransaction transaction) {
    6.97 -      if (LOG.isLoggable(Level.FINER)) {
    6.98 -         LOG.log(Level.FINER, "insertUpdate: {0}", System.identityHashCode(this));
    6.99 -      }
   6.100 -      processRemovedFolds(transaction);
   6.101 -   }
   6.102 +    @Override
   6.103 +    public void insertUpdate(DocumentEvent evt, FoldHierarchyTransaction transaction) {
   6.104 +        if (LOG.isLoggable(Level.FINER)) {
   6.105 +            LOG.log(Level.FINER, "insertUpdate: {0}", System.identityHashCode(this));
   6.106 +        }
   6.107 +        processRemovedFolds(transaction);
   6.108 +    }
   6.109  
   6.110 -   @Override
   6.111 -   public void removeUpdate(DocumentEvent evt, FoldHierarchyTransaction transaction) {
   6.112 -      if (LOG.isLoggable(Level.FINER)) {
   6.113 -         LOG.log(Level.FINER, "removeUpdate: {0}", System.identityHashCode(this));
   6.114 -      }
   6.115 -      processRemovedFolds(transaction);
   6.116 -   }
   6.117 +    @Override
   6.118 +    public void removeUpdate(DocumentEvent evt, FoldHierarchyTransaction transaction) {
   6.119 +        if (LOG.isLoggable(Level.FINER)) {
   6.120 +            LOG.log(Level.FINER, "removeUpdate: {0}", System.identityHashCode(this));
   6.121 +        }
   6.122 +        processRemovedFolds(transaction);
   6.123 +    }
   6.124  
   6.125 -   @Override
   6.126 -   public void changedUpdate(DocumentEvent evt, FoldHierarchyTransaction transaction) {
   6.127 -   }
   6.128 +    @Override
   6.129 +    public void changedUpdate(DocumentEvent evt, FoldHierarchyTransaction transaction) {
   6.130 +        if (LOG.isLoggable(Level.FINER)) {
   6.131 +            LOG.log(Level.FINER, "changeUpdate: {0}", System.identityHashCode(this));
   6.132 +        }
   6.133 +        processRemovedFolds(transaction);
   6.134 +    }
   6.135  
   6.136 -   @Override
   6.137 -   public void removeEmptyNotify(Fold emptyFold) {
   6.138 -      if (LOG.isLoggable(Level.FINER)) {
   6.139 -         LOG.log(Level.FINER, "removeEmptyNotify: {0}", System.identityHashCode(this));
   6.140 -      }
   6.141 -      removeFoldNotify(emptyFold);
   6.142 -   }
   6.143 +    @Override
   6.144 +    public void removeEmptyNotify(Fold emptyFold) {
   6.145 +        if (LOG.isLoggable(Level.FINER)) {
   6.146 +            LOG.log(Level.FINER, "removeEmptyNotify: {0}", System.identityHashCode(this));
   6.147 +        }
   6.148 +        removeFoldNotify(emptyFold);
   6.149 +    }
   6.150  
   6.151 -   @Override
   6.152 -   public void removeDamagedNotify(Fold damagedFold) {
   6.153 -      if (LOG.isLoggable(Level.FINER)) {
   6.154 -         LOG.log(Level.FINER, "removeDamagedNotify: {0}", System.identityHashCode(this));
   6.155 -      }
   6.156 -      removeFoldNotify(damagedFold);
   6.157 -   }
   6.158 +    @Override
   6.159 +    public void removeDamagedNotify(Fold damagedFold) {
   6.160 +        if (LOG.isLoggable(Level.FINER)) {
   6.161 +            LOG.log(Level.FINER, "removeDamagedNotify: {0}", System.identityHashCode(this));
   6.162 +        }
   6.163 +        removeFoldNotify(damagedFold);
   6.164 +    }
   6.165  
   6.166 -   @Override
   6.167 -   public void expandNotify(Fold expandedFold) {
   6.168 -   }
   6.169 +    @Override
   6.170 +    public void expandNotify(Fold expandedFold) {
   6.171 +    }
   6.172  
   6.173 -   @Override
   6.174 -   public void release() {
   6.175 -      if (LOG.isLoggable(Level.FINE)) {
   6.176 -         LOG.log(Level.FINE, "release: {0}", System.identityHashCode(this));
   6.177 -      }
   6.178 -   }
   6.179 +    @Override
   6.180 +    public void release() {
   6.181 +        if (LOG.isLoggable(Level.FINE)) {
   6.182 +            LOG.log(Level.FINE, "release: {0}", System.identityHashCode(this));
   6.183 +        }
   6.184 +    }
   6.185  
   6.186 -   @Override
   6.187 -   public void update(Observable o, Object arg) {
   6.188 -      if (LOG.isLoggable(Level.FINER)) {
   6.189 -         LOG.log(Level.FINER, "update: {0}", System.identityHashCode(this));
   6.190 -      }
   6.191 -      task.schedule(TASK_DELAY);
   6.192 -   }
   6.193 +    @Override
   6.194 +    public void update(Observable o, Object arg) {
   6.195 +        if (LOG.isLoggable(Level.FINER)) {
   6.196 +            LOG.log(Level.FINER, "update: {0}", System.identityHashCode(this));
   6.197 +        }
   6.198 +        task.schedule(TASK_DELAY);
   6.199 +    }
   6.200  
   6.201 -   @Override
   6.202 -   public void run() {
   6.203 -      if (operation.isReleased()) {
   6.204 -         if (LOG.isLoggable(Level.FINE)) {
   6.205 -            LOG.log(Level.FINE, "Update skipped, already released: {0}", System.identityHashCode(this));
   6.206 -         }
   6.207 -         return;
   6.208 -      }
   6.209 -      ((BaseDocument) doc).readLock();
   6.210 -      try {
   6.211 +    @Override
   6.212 +    public void run() {
   6.213 +        if (operation.isReleased()) {
   6.214 +            if (LOG.isLoggable(Level.FINE)) {
   6.215 +                LOG.log(Level.FINE, "Update skipped, already released: {0}", System.identityHashCode(this));
   6.216 +            }
   6.217 +            return;
   6.218 +        }
   6.219 +        ((BaseDocument) doc).readLock();
   6.220 +        try {
   6.221 +            if (blockFactory != null) {
   6.222 +                FoldHierarchy hierarchy = getOperation().getHierarchy();
   6.223  
   6.224 -         if (blockFactory != null) {
   6.225 -            FoldHierarchy hierarchy = getOperation().getHierarchy();
   6.226 -            hierarchy.lock();
   6.227 +                hierarchy.lock();
   6.228 +                try {
   6.229 +                    if (operation.isReleased()) {
   6.230 +                        if (LOG.isLoggable(Level.FINE)) {
   6.231 +                            LOG.log(Level.FINE, "Update skipped, already released: {0}", System.identityHashCode(this));
   6.232 +                        }
   6.233 +                        return;
   6.234 +                    }
   6.235 +                    if (LOG.isLoggable(Level.FINE)) {
   6.236 +                        LOG.log(Level.FINE, "Updating: {0}", System.identityHashCode(this));
   6.237 +                        LOG.log(Level.FINE, "blockFactory.getBlockHierarchy().size(): {0}", blockFactory.getBlockHierarchy().size());
   6.238 +                        LOG.log(Level.FINE, "blockFactory.getNewBlocks().size(): {0}", blockFactory.getNewBlocks().size());
   6.239 +                        LOG.log(Level.FINE, "blockFactory.getRemovedBlocks().size(): {0}", blockFactory.getRemovedBlocks().size());
   6.240 +                    }
   6.241 +                    FoldHierarchyTransaction transaction = getOperation().openTransaction();
   6.242 +                    try {
   6.243 +
   6.244 +                        final Fold root = hierarchy.getRootFold();
   6.245 +
   6.246 +                        if (initial) {
   6.247 +                            List<PlsqlBlock> blocks = blockFactory.getBlockHierarchy();
   6.248 +                            updateFolds(blocks, transaction, null);
   6.249 +                            //Add custom fold blocks
   6.250 +                            updateFolds(blockFactory.getCustomFolds(), transaction, null);
   6.251 +                            initial = false;
   6.252 +                        } else {
   6.253 +                            final List<FoldSearchObject> collapsedFolds = new ArrayList<FoldSearchObject>();
   6.254 +                            getCollapsedFolds(root, collapsedFolds);
   6.255 +                            final List<PlsqlBlock> oldBlocks = blockFactory.getRemovedBlocks();
   6.256 +
   6.257 +                            //Remove non existing blocks   
   6.258 +                            if (!oldBlocks.isEmpty()) {
   6.259 +                                final int childCount = root.getFoldCount();
   6.260 +                                for (int i = (childCount - 1); i >= 0; i--) {
   6.261 +                                    final Fold child = root.getFold(i);
   6.262 +                                    removeWithChildren(child, oldBlocks, transaction);
   6.263 +                                }
   6.264 +                            }
   6.265 +                            //Add new blocks to the hierarchy
   6.266 +                            List<PlsqlBlock> blocks = blockFactory.getNewBlocks();
   6.267 +                            updateFolds(blocks, transaction, collapsedFolds);
   6.268 +                            //Add custom fold blocks
   6.269 +                            updateFolds(blockFactory.getCustomFolds(), transaction, collapsedFolds);
   6.270 +                        }
   6.271 +                    } catch (BadLocationException ex) {
   6.272 +                        Exceptions.printStackTrace(ex);
   6.273 +                    } finally {
   6.274 +                        transaction.commit();
   6.275 +                    }
   6.276 +                } finally {
   6.277 +                    hierarchy.unlock();
   6.278 +                }
   6.279 +            }
   6.280 +        } finally {
   6.281 +            ((BaseDocument) doc).readUnlock();
   6.282 +        }
   6.283 +    }
   6.284 +
   6.285 +    private void getCollapsedFolds(final Fold parent, final List<FoldSearchObject> foldInfoLst) {
   6.286 +        if (parent.isCollapsed()) {
   6.287 +            final FoldSearchObject tempInfo = new FoldSearchObject(parent.getStartOffset(), parent.getEndOffset(), parent.getType());
   6.288 +            foldInfoLst.add(tempInfo);
   6.289 +        }
   6.290 +        final int count = parent.getFoldCount();
   6.291 +        for (int i = 0; i < count; i++) {
   6.292 +            final Fold temp = parent.getFold(i);
   6.293 +            getCollapsedFolds(temp, foldInfoLst);
   6.294 +        }
   6.295 +    }
   6.296 +
   6.297 +    /**
   6.298 +     * Remove fold with its children
   6.299 +     *
   6.300 +     * @param fold
   6.301 +     * @param blockHier
   6.302 +     * @param transaction
   6.303 +     * @return true if removed all the children
   6.304 +     */
   6.305 +    private void removeWithChildren(final Fold fold, final List<PlsqlBlock> blockHier, final FoldHierarchyTransaction transaction) {
   6.306 +
   6.307 +        final int childCount = fold.getFoldCount();
   6.308 +        for (int i = (childCount - 1); i >= 0; i--) {
   6.309 +            final Fold child = fold.getFold(i);
   6.310 +            removeWithChildren(child, blockHier, transaction);
   6.311 +        }
   6.312 +
   6.313 +        //If a custom fold remove
   6.314 +        if (fold.getType() == PlsqlFoldTypes.CUSTOM || checkExists(fold, blockHier)) {
   6.315 +            operation.removeFromHierarchy(fold, transaction);
   6.316 +            foldSearchObjects.remove(new FoldSearchObject(new FoldAdapter(fold)));
   6.317 +        }
   6.318 +    }
   6.319 +
   6.320 +    /**
   6.321 +     * Method that will check whether given fold exists in block hier
   6.322 +     *
   6.323 +     * @param fold
   6.324 +     * @param blockHier
   6.325 +     * @return
   6.326 +     */
   6.327 +    private boolean checkExists(final Fold fold, final List<PlsqlBlock> blockHier) {
   6.328 +        final Comparator<PlsqlBlock> comparator = new Comparator<PlsqlBlock>() {
   6.329 +            @Override
   6.330 +            public int compare(final PlsqlBlock o1, final PlsqlBlock o2) {
   6.331 +                Integer o1pos, o2pos;
   6.332 +                if (o1.getStartOffset() > -1 && o2.getStartOffset() > -1) {
   6.333 +                    o1pos = Integer.valueOf(o1.getStartOffset());
   6.334 +                    o2pos = Integer.valueOf(o2.getStartOffset());
   6.335 +                } else {
   6.336 +                    o1pos = Integer.valueOf(o1.getEndOffset());
   6.337 +                    o2pos = Integer.valueOf(o2.getEndOffset());
   6.338 +                }
   6.339 +                return o1pos.compareTo(o2pos);
   6.340 +            }
   6.341 +        };
   6.342 +        return checkExists(fold, blockHier, comparator);
   6.343 +    }
   6.344 +
   6.345 +    /**
   6.346 +     * Method that will check whether given fold exists in block hier
   6.347 +     *
   6.348 +     * @param fold
   6.349 +     * @param blockHier
   6.350 +     * @param comparator
   6.351 +     * @return
   6.352 +     */
   6.353 +    private boolean checkExists(final Fold fold, final List<PlsqlBlock> blockHier, final Comparator<PlsqlBlock> comparator) {
   6.354 +        final int size = blockHier.size();
   6.355 +        Collections.sort(blockHier, comparator);
   6.356 +        if (size == 0) {
   6.357 +            return false;
   6.358 +        }
   6.359 +        //make sure that the fold isn't before the first block or after the last block in the hierarchy.
   6.360 +        if (fold.getStartOffset() > blockHier.get(size - 1).getEndOffset()
   6.361 +                || fold.getEndOffset() < blockHier.get(0).getStartOffset()) {
   6.362 +            return false;
   6.363 +        }
   6.364 +        for (int i = 0; i < size; i++) {
   6.365 +            final PlsqlBlock block = blockHier.get(i);
   6.366 +            if (block.getStartOffset() <= fold.getStartOffset() && block.getEndOffset() >= fold.getEndOffset()) {
   6.367 +                return true;
   6.368 +            }
   6.369 +            if (block.getPreviousStart() <= fold.getStartOffset() && block.getPreviousEnd() >= fold.getEndOffset()) {
   6.370 +                return true;
   6.371 +            }
   6.372 +            if ((block.getEndOffset() == fold.getEndOffset() || fold.getEndOffset() == block.getPreviousEnd()
   6.373 +                    || block.getStartOffset() == fold.getStartOffset() || block.getPreviousStart() == fold.getStartOffset())
   6.374 +                    && (getFoldType(block.getType()).equals(fold.getType()))
   6.375 +                    && (getFoldDescription(block).equals(fold.getDescription()))) {
   6.376 +                return true;
   6.377 +            }
   6.378 +
   6.379 +            if (checkExists(fold, block.getChildBlocks(), comparator)) {
   6.380 +                return true;
   6.381 +            }
   6.382 +        }
   6.383 +
   6.384 +        return false;
   6.385 +    }
   6.386 +
   6.387 +    private FoldType getFoldType(final PlsqlBlockType blockType) {
   6.388 +        switch (blockType) {
   6.389 +            case VIEW:
   6.390 +                return PlsqlFoldTypes.VIEW;
   6.391 +            case TABLE_COMMENT:
   6.392 +                return PlsqlFoldTypes.TABLECOMMENT;
   6.393 +            case COLUMN_COMMENT:
   6.394 +                return PlsqlFoldTypes.COLUMNCOMMENT;
   6.395 +            case COMMENT:
   6.396 +                return PlsqlFoldTypes.COMMENT;
   6.397 +            case PACKAGE:
   6.398 +                return PlsqlFoldTypes.PACKAGE;
   6.399 +            case PACKAGE_BODY:
   6.400 +                return PlsqlFoldTypes.PACKAGEBODY;
   6.401 +            case PROCEDURE_IMPL:
   6.402 +                return PlsqlFoldTypes.PROCEDUREIMPL;
   6.403 +            case FUNCTION_IMPL:
   6.404 +                return PlsqlFoldTypes.FUNCTIONIMPL;
   6.405 +            case PROCEDURE_DEF:
   6.406 +                return PlsqlFoldTypes.PROCEDUREDEF;
   6.407 +            case FUNCTION_DEF:
   6.408 +                return PlsqlFoldTypes.FUNCTIONDEF;
   6.409 +            case DECLARE_END:
   6.410 +                return PlsqlFoldTypes.DECLAREEND;
   6.411 +            case BEGIN_END:
   6.412 +                return PlsqlFoldTypes.BEGINEND;
   6.413 +            case TRIGGER:
   6.414 +                return PlsqlFoldTypes.TRIGGER;
   6.415 +            case IF:
   6.416 +                return PlsqlFoldTypes.IF;
   6.417 +            case CASE:
   6.418 +                return PlsqlFoldTypes.CASE;
   6.419 +            case WHILE_LOOP:
   6.420 +                return PlsqlFoldTypes.WHILELOOP;
   6.421 +            case FOR_LOOP:
   6.422 +                return PlsqlFoldTypes.FORLOOP;
   6.423 +            case LOOP:
   6.424 +                return PlsqlFoldTypes.LOOP;
   6.425 +            case CUSTOM_FOLD:
   6.426 +                return PlsqlFoldTypes.CUSTOM;
   6.427 +            case STATEMENT:
   6.428 +                return PlsqlFoldTypes.STATEMENT;
   6.429 +            case CURSOR:
   6.430 +                return PlsqlFoldTypes.CURSOR;
   6.431 +            case JAVA_SOURCE:
   6.432 +                return PlsqlFoldTypes.JAVASOURCE;
   6.433 +            default:
   6.434 +                return null;
   6.435 +        }
   6.436 +    }
   6.437 +
   6.438 +    /**
   6.439 +     * Method that will return the fold description given the Plsql block Used
   6.440 +     * when changing the descriptions only.
   6.441 +     *
   6.442 +     * @param block
   6.443 +     * @return
   6.444 +     */
   6.445 +    private String getFoldDescription(final PlsqlBlock block) {
   6.446 +        switch (block.getType()) {
   6.447 +            case VIEW:
   6.448 +                return block.getPrefix() + "VIEW " + block.getName();
   6.449 +            case TABLE_COMMENT:
   6.450 +                return "COMMENT ON TABLE " + block.getName();
   6.451 +            case COLUMN_COMMENT:
   6.452 +                return "COLUMN COMMENTS ON TABLE " + block.getName();
   6.453 +            case COMMENT:
   6.454 +                return block.getName();
   6.455 +            case PACKAGE:
   6.456 +                return block.getPrefix() + "PACKAGE " + block.getName();
   6.457 +            case PACKAGE_BODY:
   6.458 +                return block.getPrefix() + "PACKAGE BODY " + block.getName();
   6.459 +            case PROCEDURE_IMPL:
   6.460 +                return block.getPrefix() + "PROCEDURE IMPLEMENTATION " + block.getName();
   6.461 +            case FUNCTION_IMPL:
   6.462 +                return block.getPrefix() + "FUNCTION IMPLEMENTATION " + block.getName();
   6.463 +            case PROCEDURE_DEF:
   6.464 +                return "PROCEDURE DEFINITION " + block.getName();
   6.465 +            case FUNCTION_DEF:
   6.466 +                return "FUNCTION DEFINITION " + block.getName();
   6.467 +            case DECLARE_END:
   6.468 +                return "DECLARE BLOCK";
   6.469 +            case BEGIN_END:
   6.470 +                return "BEGIN BLOCK";
   6.471 +            case TRIGGER:
   6.472 +                return block.getPrefix() + "TRIGGER " + block.getName();
   6.473 +            case IF:
   6.474 +                return block.getName();
   6.475 +            case CASE:
   6.476 +                return block.getName();
   6.477 +            case WHILE_LOOP:
   6.478 +                return "WHILE " + block.getName();
   6.479 +            case FOR_LOOP:
   6.480 +                return "FOR " + block.getName();
   6.481 +            case LOOP:
   6.482 +                return "LOOP ";
   6.483 +            case CUSTOM_FOLD:
   6.484 +                return block.getName();
   6.485 +            case STATEMENT:
   6.486 +                return block.getPrefix() + block.getName();
   6.487 +            case CURSOR:
   6.488 +                return "CURSOR " + block.getName();
   6.489 +            case JAVA_SOURCE:
   6.490 +                return block.getPrefix() + "JAVA SOURCE";
   6.491 +            default:
   6.492 +                return "";
   6.493 +        }
   6.494 +    }
   6.495 +
   6.496 +    private void removeFoldNotify(Fold removedFold) {
   6.497 +        removedFoldList.add(removedFold);
   6.498 +    }
   6.499 +
   6.500 +    private void processRemovedFolds(FoldHierarchyTransaction transaction) {
   6.501 +        for (Fold removedFold : removedFoldList) {
   6.502 +            operation.removeFromHierarchy(removedFold, transaction);
   6.503 +            if (LOG.isLoggable(Level.FINE)) {
   6.504 +                LOG.log(Level.FINE, "Fold={0} removed={1}", new Object[]{removedFold, true});
   6.505 +            }
   6.506 +        }
   6.507 +        removedFoldList.clear();
   6.508 +    }
   6.509 +
   6.510 +    private Document getDocument() {
   6.511 +        Object obj = null;
   6.512 +        for (int i = 0; i < 10; i++) {
   6.513 +            obj = getOperation().getHierarchy().getComponent().getDocument();
   6.514 +            if (obj instanceof AbstractDocument) {
   6.515 +                return (Document) obj;
   6.516 +            }
   6.517              try {
   6.518 -               if (operation.isReleased()) {
   6.519 -                  if (LOG.isLoggable(Level.FINE)) {
   6.520 -                     LOG.log(Level.FINE, "Update skipped, already released: {0}", System.identityHashCode(this));
   6.521 -                  }
   6.522 -                  return;
   6.523 -               }
   6.524 -               if (LOG.isLoggable(Level.FINE)) {
   6.525 -                  LOG.log(Level.FINE, "Updating: {0}", System.identityHashCode(this));
   6.526 -                  LOG.log(Level.FINE, "blockFactory.getBlockHierarchy().size(): {0}", blockFactory.getBlockHierarchy().size());
   6.527 -                  LOG.log(Level.FINE, "blockFactory.getNewBlocks().size(): {0}", blockFactory.getNewBlocks().size());
   6.528 -                  LOG.log(Level.FINE, "blockFactory.getRemovedBlocks().size(): {0}", blockFactory.getRemovedBlocks().size());
   6.529 -               }
   6.530 -               FoldHierarchyTransaction transaction = getOperation().openTransaction();
   6.531 -               try {
   6.532 -                  //Add new blocks to the hierarchy
   6.533 -                  List<PlsqlBlock> blocks = blockFactory.getNewBlocks();
   6.534 -                  if (initial) {
   6.535 -                     blocks = blockFactory.getBlockHierarchy();
   6.536 -                     initial = false;
   6.537 -                  }
   6.538 +                Thread.currentThread().sleep(i);
   6.539 +            } catch (InterruptedException e) {
   6.540 +            }
   6.541 +        }
   6.542 +        throw new IllegalStateException("[PLSQLFolding] PLSQLFoldManager.getDocument() NOT returned AbstractDocument, but " + obj.getClass() + "!. This is caused by not yet resolved issue #49497."); //NOI18N
   6.543 +    }
   6.544  
   6.545 -                  updateFolds(blocks, transaction);
   6.546 -                  //Add custom fold blocks
   6.547 -                  updateFolds(blockFactory.getCustomFolds(), transaction);
   6.548 -               } finally {
   6.549 -                  transaction.commit();
   6.550 -               }
   6.551 -            } finally {
   6.552 -               hierarchy.unlock();
   6.553 +    private void updateFolds(final List<PlsqlBlock> blockHier, final FoldHierarchyTransaction transaction, final List<FoldSearchObject> collapsedFolds) throws BadLocationException {
   6.554 +        final int count = blockHier.size();
   6.555 +        Document doc = getDocument();
   6.556 +        for (int i = 0; i < count; i++) {
   6.557 +            final PlsqlBlock block = blockHier.get(i);
   6.558 +            FoldType foldType = null;
   6.559 +            final PlsqlBlockType type = block.getType();
   6.560 +            String description = "";
   6.561 +
   6.562 +            if (!(type == PlsqlBlockType.COMMENT && doc.getText(block.getStartOffset(), block.getEndOffset() - block.getStartOffset()).indexOf("\n") == -1)) { // check for single line comments
   6.563 +                foldType = getFoldType(type);
   6.564 +                description = getFoldDescription(block);
   6.565 +
   6.566 +                if (doc.getEndPosition().getOffset() >= block.getEndOffset()) {                    
   6.567 +                    operation.addToHierarchy(foldType, description, isCollapsed(block, foldType, collapsedFolds),
   6.568 +                            block.getStartOffset(), block.getEndOffset(), 0, 0, null, transaction);
   6.569 +                    //check for any child folds and add them also
   6.570 +                    updateFolds(block.getChildBlocks(), transaction, collapsedFolds);
   6.571 +                }
   6.572              }
   6.573 -         }
   6.574 -      } finally {
   6.575 -         ((BaseDocument) doc).readUnlock();
   6.576 -      }
   6.577 -   }
   6.578 +        }
   6.579 +    }
   6.580  
   6.581 -   private void removeFoldNotify(Fold removedFold) {
   6.582 -      removedFoldList.add(removedFold);
   6.583 -   }
   6.584 +    /**
   6.585 +     * Method that will select and return the corresponding fold to parent from
   6.586 +     * oldRoot fold hierarchy
   6.587 +     *
   6.588 +     * @param parent
   6.589 +     * @param foldInfoLst
   6.590 +     * @return
   6.591 +     */
   6.592 +    private boolean isCollapsed(final PlsqlBlock block, final FoldType foldType, final List<FoldSearchObject> foldInfoLst) {
   6.593 +        if (OptionsUtilities.isPlSqlExpandFolds()) {
   6.594 +            return false;
   6.595 +        }
   6.596 +        if (foldInfoLst == null) {
   6.597 +            return foldType == PlsqlFoldTypes.COMMENT;
   6.598 +        }
   6.599 +        final int size = foldInfoLst.size();
   6.600 +        for (int i = 0; i < size; i++) {
   6.601 +            final FoldSearchObject temp = foldInfoLst.get(i);
   6.602  
   6.603 -   private void processRemovedFolds(FoldHierarchyTransaction transaction) {
   6.604 -      for (Fold removedFold : removedFoldList) {
   6.605 -         boolean remove = foldSearchObjects.remove(new FoldSearchObject(new FoldAdapter(removedFold)));
   6.606 -         if (LOG.isLoggable(Level.FINE)) {
   6.607 -            LOG.log(Level.FINE, "Fold={0} removed={1}", new Object[]{removedFold, remove});
   6.608 -         }
   6.609 -      }
   6.610 -      removedFoldList.clear();
   6.611 -   }
   6.612 +            if ((temp.getFoldType() == foldType)
   6.613 +                    && (temp.getStartOffset() == block.getPreviousStart())
   6.614 +                    && (temp.getEndOffset() == block.getPreviousEnd())) {
   6.615 +                return true;
   6.616 +            }
   6.617 +        }
   6.618  
   6.619 -   private void updateFolds(List<PlsqlBlock> blocks, FoldHierarchyTransaction transaction) {
   6.620 -      for (PlsqlBlock block : blocks) {
   6.621 +        return false;
   6.622 +    }
   6.623  
   6.624 -         FoldType foldType = null;
   6.625 -         final PlsqlBlockType type = block.getType();
   6.626 -         String description = null;
   6.627 -         try {
   6.628 -            if (!(type == PlsqlBlockType.COMMENT && doc.getText(block.getStartOffset(), block.getEndOffset() - block.getStartOffset()).indexOf("\n") == -1)) { // check for single line comments
   6.629 -               if (type == PlsqlBlockType.VIEW) {
   6.630 -                  foldType = PlsqlFoldTypes.VIEW;
   6.631 -                  description = block.getPrefix() + "VIEW " + block.getName();
   6.632 -               } else if (type == PlsqlBlockType.TABLE_COMMENT) {
   6.633 -                  foldType = PlsqlFoldTypes.TABLECOMMENT;
   6.634 -                  description = "COMMENT ON TABLE " + block.getName();
   6.635 -               } else if (type == PlsqlBlockType.COLUMN_COMMENT) {
   6.636 -                  foldType = PlsqlFoldTypes.COLUMNCOMMENT;
   6.637 -                  description = "COLUMN COMMENTS ON TABLE " + block.getName();
   6.638 -               } else if (type == PlsqlBlockType.COMMENT) {
   6.639 -                  foldType = PlsqlFoldTypes.COMMENT;
   6.640 -                  description = block.getName();
   6.641 -               } else if (type == PlsqlBlockType.PACKAGE) {
   6.642 -                  foldType = PlsqlFoldTypes.PACKAGE;
   6.643 -                  description = block.getPrefix() + "PACKAGE " + block.getName();
   6.644 -               } else if (type == PlsqlBlockType.PACKAGE_BODY) {
   6.645 -                  foldType = PlsqlFoldTypes.PACKAGEBODY;
   6.646 -                  description = block.getPrefix() + "PACKAGE BODY " + block.getName();
   6.647 -               } else if (type == PlsqlBlockType.PROCEDURE_IMPL) {
   6.648 -                  foldType = PlsqlFoldTypes.PROCEDUREIMPL;
   6.649 -                  description = block.getPrefix() + "PROCEDURE IMPLEMENTATION " + block.getName();
   6.650 -               } else if (type == PlsqlBlockType.FUNCTION_IMPL) {
   6.651 -                  foldType = PlsqlFoldTypes.FUNCTIONIMPL;
   6.652 -                  description = block.getPrefix() + "FUNCTION IMPLEMENTATION " + block.getName();
   6.653 -               } else if (type == PlsqlBlockType.PROCEDURE_DEF) {
   6.654 -                  foldType = PlsqlFoldTypes.PROCEDUREDEF;
   6.655 -                  description = "PROCEDURE DEFINITION " + block.getName();
   6.656 -               } else if (type == PlsqlBlockType.FUNCTION_DEF) {
   6.657 -                  foldType = PlsqlFoldTypes.FUNCTIONDEF;
   6.658 -                  description = "FUNCTION DEFINITION " + block.getName();
   6.659 -               } else if (type == PlsqlBlockType.DECLARE_END) {
   6.660 -                  foldType = PlsqlFoldTypes.DECLAREEND;
   6.661 -                  description = "DECLARE BLOCK";
   6.662 -               } else if (type == PlsqlBlockType.BEGIN_END) {
   6.663 -                  foldType = PlsqlFoldTypes.BEGINEND;
   6.664 -                  description = "BEGIN BLOCK";
   6.665 -               } else if (type == PlsqlBlockType.TRIGGER) {
   6.666 -                  foldType = PlsqlFoldTypes.TRIGGER;
   6.667 -                  description = block.getPrefix() + "TRIGGER " + block.getName();
   6.668 -               } else if (type == PlsqlBlockType.IF) {
   6.669 -                  foldType = PlsqlFoldTypes.IF;
   6.670 -                  description = block.getName();
   6.671 -               } else if (type == PlsqlBlockType.CASE) {
   6.672 -                  foldType = PlsqlFoldTypes.CASE;
   6.673 -                  description = block.getName();
   6.674 -               } else if (type == PlsqlBlockType.WHILE_LOOP) {
   6.675 -                  foldType = PlsqlFoldTypes.WHILELOOP;
   6.676 -                  description = "WHILE " + block.getName();
   6.677 -               } else if (type == PlsqlBlockType.FOR_LOOP) {
   6.678 -                  foldType = PlsqlFoldTypes.FORLOOP;
   6.679 -                  description = "FOR " + block.getName();
   6.680 -               } else if (type == PlsqlBlockType.LOOP) {
   6.681 -                  foldType = PlsqlFoldTypes.LOOP;
   6.682 -                  description = "LOOP ";
   6.683 -               } else if (type == PlsqlBlockType.CUSTOM_FOLD) {
   6.684 -                  foldType = PlsqlFoldTypes.CUSTOM;
   6.685 -                  description = block.getName();
   6.686 -               } else if (type == PlsqlBlockType.STATEMENT) {
   6.687 -                  foldType = PlsqlFoldTypes.STATEMENT;
   6.688 -                  description = block.getPrefix() + block.getName();
   6.689 -               } else if (type == PlsqlBlockType.CURSOR) {
   6.690 -                  foldType = PlsqlFoldTypes.CURSOR;
   6.691 -                  description = "CURSOR " + block.getName();
   6.692 -               } else if (type == PlsqlBlockType.JAVA_SOURCE) {
   6.693 -                  foldType = PlsqlFoldTypes.JAVASOURCE;
   6.694 -                  description = block.getPrefix() + "JAVA SOURCE";
   6.695 -               }
   6.696 -
   6.697 -               final FoldSearchObject foldSearchObject = new FoldSearchObject(block.getStartOffset(), block.getEndOffset());
   6.698 -               if (!foldSearchObjects.contains(foldSearchObject)) {
   6.699 -                  try {
   6.700 -                     final Fold fold = operation.addToHierarchy(foldType, description, isCollapsed(foldType), block.getStartOffset(), block.getEndOffset(), 0, 0, null, transaction);
   6.701 -                     foldSearchObjects.add(new FoldSearchObject(new FoldAdapter(fold)));
   6.702 -                  } catch (BadLocationException ex) {
   6.703 -                     if (LOG.isLoggable(Level.FINE)) {
   6.704 -                        LOG.log(Level.FINE, "Ignore BadLocationException", ex);
   6.705 -                     }
   6.706 -                  }
   6.707 -               }
   6.708 -               updateFolds(block.getChildBlocks(), transaction);
   6.709 -            }
   6.710 -         } catch (BadLocationException ex) {
   6.711 -            if (LOG.isLoggable(Level.FINE)) {
   6.712 -               LOG.log(Level.FINE, "Ignore BadLocationException", ex);
   6.713 -            }
   6.714 -         }
   6.715 -      }
   6.716 -   }
   6.717 -
   6.718 -   /**
   6.719 -    * Method that will select and return the corresponding fold to parent from oldRoot fold hierarchy
   6.720 -    *
   6.721 -    * @param parent
   6.722 -    * @param foldInfoLst
   6.723 -    * @return
   6.724 -    */
   6.725 -   private boolean isCollapsed(final FoldType foldType) {
   6.726 -      if (OptionsUtilities.isPlSqlExpandFolds()) {
   6.727 -         return false;
   6.728 -      }
   6.729 -      return foldType == PlsqlFoldTypes.COMMENT;
   6.730 -   }
   6.731 -
   6.732 -   /**
   6.733 -    * Method that will return the relevant block factory
   6.734 -    *
   6.735 -    * @return
   6.736 -    */
   6.737 -   private PlsqlBlockFactory getBlockFactory() {
   6.738 -      final Object obj = doc.getProperty(Document.StreamDescriptionProperty);
   6.739 -      if (obj instanceof Lookup.Provider) {
   6.740 -         return ((Lookup.Provider) obj).getLookup().lookup(PlsqlBlockFactory.class);
   6.741 -      }
   6.742 -      return null;
   6.743 -   }
   6.744 +    /**
   6.745 +     * Method that will return the relevant block factory
   6.746 +     *
   6.747 +     * @return
   6.748 +     */
   6.749 +    private PlsqlBlockFactory getBlockFactory() {
   6.750 +        final Object obj = doc.getProperty(Document.StreamDescriptionProperty);
   6.751 +        if (obj instanceof Lookup.Provider) {
   6.752 +            return ((Lookup.Provider) obj).getLookup().lookup(PlsqlBlockFactory.class);
   6.753 +        }
   6.754 +        return null;
   6.755 +    }
   6.756  }
     7.1 --- a/PLSQL/Folding/src/org/netbeans/modules/plsql/fold/PlsqlFoldManagerFactory.java	Mon Aug 12 10:50:10 2013 +0530
     7.2 +++ b/PLSQL/Folding/src/org/netbeans/modules/plsql/fold/PlsqlFoldManagerFactory.java	Mon Aug 12 11:26:54 2013 +0530
     7.3 @@ -53,7 +53,7 @@
     7.4  
     7.5     @Override
     7.6     public FoldManager createFoldManager() {
     7.7 -      final String property = System.getProperty("plsql.fold.manager", "old");
     7.8 +      final String property = System.getProperty("plsql.fold.manager", "new");
     7.9        if ("custom".equals(property)) {
    7.10           return new CustomFoldManager();
    7.11        }
     8.1 --- a/PLSQL/Folding/test/unit/src/org/netbeans/modules/plsql/fold/FoldSearchObjectTest.java	Mon Aug 12 10:50:10 2013 +0530
     8.2 +++ b/PLSQL/Folding/test/unit/src/org/netbeans/modules/plsql/fold/FoldSearchObjectTest.java	Mon Aug 12 11:26:54 2013 +0530
     8.3 @@ -7,6 +7,7 @@
     8.4  import org.junit.Test;
     8.5  import static org.junit.Assert.*;
     8.6  import static org.mockito.Mockito.*;
     8.7 +import org.netbeans.api.editor.fold.FoldType;
     8.8  
     8.9  /**
    8.10   *
    8.11 @@ -18,11 +19,13 @@
    8.12     public void shouldBeEqualWithAndWithoutBackingFoldObject() {
    8.13        int startOffset = 100;
    8.14        int endOffset = 200;
    8.15 -      FoldSearchObject searchObject1 = new FoldSearchObject(startOffset, endOffset);
    8.16 +      FoldType foldType = PlsqlFoldTypes.COMMENT;
    8.17 +      FoldSearchObject searchObject1 = new FoldSearchObject(startOffset, endOffset, foldType);
    8.18        FoldAdapter fold = mock(FoldAdapter.class);
    8.19        FoldSearchObject searchObject2 = new FoldSearchObject(fold);
    8.20        when(fold.getStartOffset()).thenReturn(startOffset);
    8.21        when(fold.getEndOffset()).thenReturn(endOffset);
    8.22 +      when(fold.getFoldType()).thenReturn(foldType);
    8.23        assertEquals(searchObject1, searchObject2);
    8.24     }
    8.25  }
    8.26 \ No newline at end of file
     9.1 --- a/PLSQL/Lexer/src/org/netbeans/modules/plsql/lexer/PlsqlBlockFactory.java	Mon Aug 12 10:50:10 2013 +0530
     9.2 +++ b/PLSQL/Lexer/src/org/netbeans/modules/plsql/lexer/PlsqlBlockFactory.java	Mon Aug 12 11:26:54 2013 +0530
     9.3 @@ -2524,6 +2524,10 @@
     9.4          ts.moveNext();
     9.5  
     9.6          //Calculate end offset
     9.7 +        if(commentEnd.id() == PlsqlTokenId.WHITESPACE ){
     9.8 +            ts.movePrevious();
     9.9 +            commentEnd = ts.token();
    9.10 +        }
    9.11          int endOffset = commentEnd.offset(tokenHierarchy) + commentEnd.length();
    9.12  
    9.13          return new PlsqlBlock(commentBegin.offset(tokenHierarchy),
    9.14 @@ -3870,7 +3874,7 @@
    9.15          Token<PlsqlTokenId> tmp = ts.token();
    9.16          LOG.log(Level.FINE, "getNextNonWhitespaceForComments, tmp.id()={0}, tmp.text()={1}", new Object[]{tmp.id(), tmp.text().toString()});
    9.17          while (moveNext) {
    9.18 -            if (tmp.id() == PlsqlTokenId.WHITESPACE && "\n".equals(tmp.text())) {
    9.19 +            if (tmp.id() == PlsqlTokenId.WHITESPACE && ("\n".equals(tmp.text()) || tmp.text().toString().contains("\n "))) {
    9.20                  moveNext = ts.moveNext();
    9.21                  tmp = ts.token();
    9.22              } else {
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/PLSQL/Lexer/test/unit/data/TestComments.apy	Mon Aug 12 11:26:54 2013 +0530
    10.3 @@ -0,0 +1,58 @@
    10.4 +-----------------------------------------------------------------------------
    10.5 +--
    10.6 +--  Logical unit: <<<LU: Put your LU name here>>>
    10.7 +--
    10.8 +--  IFS Developer Studio Template Version 2.5
    10.9 +--
   10.10 +--  Date    Sign    History
   10.11 +--  ------  ------  ---------------------------------------------------------
   10.12 +-----------------------------------------------------------------------------
   10.13 +
   10.14 +DEFINE MODULE        = <<<MODULE:   Put your module name here>>>
   10.15 +DEFINE LU            = <<<LU:       Put your lu name here>>>
   10.16 +DEFINE PKG           = <<<PKG:      Put your PACKAGE name here>>>
   10.17 +
   10.18 +PROMPT Creating &PKG SPECIFICATION
   10.19 +
   10.20 +CREATE OR REPLACE PACKAGE BODY &PKG IS
   10.21 +
   10.22 +-----------------------------------------------------------------------------
   10.23 +-------------------- LU SPECIFIC IMPLEMENTATION METHODS ---------------------
   10.24 +-----------------------------------------------------------------------------
   10.25 +
   10.26 +
   10.27 +-----------------------------------------------------------------------------
   10.28 +-------------------- LU SPECIFIC PRIVATE METHODS ----------------------------
   10.29 +-----------------------------------------------------------------------------
   10.30 +
   10.31 +
   10.32 +-----------------------------------------------------------------------------
   10.33 +-------------------- LU SPECIFIC PROTECTED METHODS --------------------------
   10.34 +-----------------------------------------------------------------------------
   10.35 +
   10.36 +
   10.37 +-----------------------------------------------------------------------------
   10.38 +-------------------- LU SPECIFIC PUBLIC METHODS -----------------------------
   10.39 +-----------------------------------------------------------------------------
   10.40 +
   10.41 +PROCEDURE Delete___ (
   10.42 +   objid_  IN VARCHAR2,
   10.43 +   remrec_ IN &VIEW%ROWTYPE )
   10.44 +
   10.45 +IS
   10.46 +        key_ VARCHAR2(2000);
   10.47 +        key_ VARCHAR2(2000);
   10.48 +   type_ intface_header_tab.procedure_name%TYPE;
   10.49 +BEGIN
   10.50 +   temp =: NULL;
   10.51 +   --
   10.52 +   -- Call to Intface_Util_API.Register_Delete_ removed.
   10.53 +   -- Logic is now handled in Intface_Util_API.Drop_IC_Table_
   10.54 +   --
   10.55 +   
   10.56 +   key_ := remrec_.intface_name || '^' || remrec_.column_name || '^';
   10.57 +   Reference_SYS.Do_Cascade_Delete(lu_name_, key_);
   10.58 +   DELETE           
   10.59 +      FROM  intface_detail_tab
   10.60 +      WHERE &OBJID = objid_;   
   10.61 +END Delete___;
    11.1 --- a/PLSQL/Lexer/test/unit/data/expected/Dictionary.apy.structure	Mon Aug 12 10:50:10 2013 +0530
    11.2 +++ b/PLSQL/Lexer/test/unit/data/expected/Dictionary.apy.structure	Mon Aug 12 11:26:54 2013 +0530
    11.3 @@ -10,25 +10,6 @@
    11.4  FUNCTION_IMPL
    11.5  CURSOR
    11.6  COMMENT
    11.7 -COMMENT
    11.8 -COMMENT
    11.9 -COMMENT
   11.10 -COMMENT
   11.11 -COMMENT
   11.12 -COMMENT
   11.13 -COMMENT
   11.14 -COMMENT
   11.15 -COMMENT
   11.16 -COMMENT
   11.17 -COMMENT
   11.18 -COMMENT
   11.19 -COMMENT
   11.20 -COMMENT
   11.21 -COMMENT
   11.22 -COMMENT
   11.23 -COMMENT
   11.24 -COMMENT
   11.25 -COMMENT
   11.26  CURSOR
   11.27  COMMENT
   11.28  CURSOR
   11.29 @@ -63,7 +44,6 @@
   11.30  COMMENT
   11.31  FOR_LOOP
   11.32  COMMENT
   11.33 -COMMENT
   11.34  IF
   11.35  BEGIN_END
   11.36  COMMENT
   11.37 @@ -73,7 +53,6 @@
   11.38  COMMENT
   11.39  FOR_LOOP
   11.40  COMMENT
   11.41 -COMMENT
   11.42  IF
   11.43  BEGIN_END
   11.44  COMMENT
   11.45 @@ -146,21 +125,16 @@
   11.46  IF
   11.47  STATEMENT
   11.48  COMMENT
   11.49 -COMMENT
   11.50  STATEMENT
   11.51  COMMENT
   11.52 -COMMENT
   11.53 -COMMENT
   11.54  STATEMENT
   11.55  COMMENT
   11.56  STATEMENT
   11.57  PROCEDURE_IMPL
   11.58  COMMENT
   11.59 -COMMENT
   11.60  IF
   11.61  IF
   11.62  COMMENT
   11.63 -COMMENT
   11.64  IF
   11.65  STATEMENT
   11.66  IF
   11.67 @@ -170,8 +144,6 @@
   11.68  IF
   11.69  STATEMENT
   11.70  COMMENT
   11.71 -COMMENT
   11.72 -COMMENT
   11.73  IF
   11.74  IF
   11.75  IF
   11.76 @@ -181,11 +153,8 @@
   11.77  CURSOR
   11.78  CURSOR
   11.79  COMMENT
   11.80 -COMMENT
   11.81 -COMMENT
   11.82  IF
   11.83  COMMENT
   11.84 -COMMENT
   11.85  IF
   11.86  COMMENT
   11.87  STATEMENT
   11.88 @@ -223,7 +192,6 @@
   11.89  IF
   11.90  IF
   11.91  COMMENT
   11.92 -COMMENT
   11.93  IF
   11.94  COMMENT
   11.95  COMMENT
   11.96 @@ -238,17 +206,12 @@
   11.97  CURSOR
   11.98  CURSOR
   11.99  COMMENT
  11.100 -COMMENT
  11.101 -COMMENT
  11.102  IF
  11.103  COMMENT
  11.104 -COMMENT
  11.105  IF
  11.106  COMMENT
  11.107  STATEMENT
  11.108  COMMENT
  11.109 -COMMENT
  11.110 -COMMENT
  11.111  IF
  11.112  FOR_LOOP
  11.113  COMMENT
  11.114 @@ -265,17 +228,12 @@
  11.115  CURSOR
  11.116  CURSOR
  11.117  COMMENT
  11.118 -COMMENT
  11.119 -COMMENT
  11.120  IF
  11.121  COMMENT
  11.122 -COMMENT
  11.123  IF
  11.124  COMMENT
  11.125  STATEMENT
  11.126  COMMENT
  11.127 -COMMENT
  11.128 -COMMENT
  11.129  IF
  11.130  FOR_LOOP
  11.131  IF
  11.132 @@ -287,13 +245,9 @@
  11.133  FOR_LOOP
  11.134  COMMENT
  11.135  COMMENT
  11.136 -COMMENT
  11.137 -COMMENT
  11.138  IF
  11.139  IF
  11.140  COMMENT
  11.141 -COMMENT
  11.142 -COMMENT
  11.143  IF
  11.144  COMMENT
  11.145  FOR_LOOP
  11.146 @@ -307,25 +261,14 @@
  11.147  STATEMENT
  11.148  COMMENT
  11.149  COMMENT
  11.150 -COMMENT
  11.151  IF
  11.152  IF
  11.153  COMMENT
  11.154  COMMENT
  11.155 -COMMENT
  11.156 -COMMENT
  11.157  STATEMENT
  11.158  COMMENT
  11.159 -COMMENT
  11.160 -COMMENT
  11.161  IF
  11.162  COMMENT
  11.163 -COMMENT
  11.164 -COMMENT
  11.165 -COMMENT
  11.166 -COMMENT
  11.167 -COMMENT
  11.168 -COMMENT
  11.169  IF
  11.170  IF
  11.171  COMMENT
  11.172 @@ -342,8 +285,6 @@
  11.173  IF
  11.174  IF
  11.175  COMMENT
  11.176 -COMMENT
  11.177 -COMMENT
  11.178  IF
  11.179  COMMENT
  11.180  COMMENT
  11.181 @@ -370,12 +311,6 @@
  11.182  COMMENT
  11.183  COMMENT
  11.184  COMMENT
  11.185 -COMMENT
  11.186 -COMMENT
  11.187 -COMMENT
  11.188 -COMMENT
  11.189 -COMMENT
  11.190 -COMMENT
  11.191  IF
  11.192  COMMENT
  11.193  COMMENT
  11.194 @@ -391,54 +326,30 @@
  11.195  IF
  11.196  IF
  11.197  COMMENT
  11.198 -COMMENT
  11.199 -COMMENT
  11.200  IF
  11.201  COMMENT
  11.202 -COMMENT
  11.203 -COMMENT
  11.204  STATEMENT
  11.205  STATEMENT
  11.206  STATEMENT
  11.207  STATEMENT
  11.208  STATEMENT
  11.209  COMMENT
  11.210 -COMMENT
  11.211 -COMMENT
  11.212 -COMMENT
  11.213 -COMMENT
  11.214  STATEMENT
  11.215  COMMENT
  11.216 -COMMENT
  11.217 -COMMENT
  11.218 -COMMENT
  11.219 +STATEMENT
  11.220  COMMENT
  11.221  STATEMENT
  11.222  COMMENT
  11.223  COMMENT
  11.224  COMMENT
  11.225  COMMENT
  11.226 -STATEMENT
  11.227 -COMMENT
  11.228 -COMMENT
  11.229 -COMMENT
  11.230 -COMMENT
  11.231 -COMMENT
  11.232 -COMMENT
  11.233  PROCEDURE_IMPL
  11.234  CURSOR
  11.235  COMMENT
  11.236  COMMENT
  11.237 -COMMENT
  11.238 -COMMENT
  11.239 -COMMENT
  11.240 -COMMENT
  11.241 -COMMENT
  11.242  FOR_LOOP
  11.243  IF
  11.244  COMMENT
  11.245 -COMMENT
  11.246 -COMMENT
  11.247  PROCEDURE_IMPL
  11.248  PROCEDURE_IMPL
  11.249  PROCEDURE_IMPL
    12.1 --- a/PLSQL/Lexer/test/unit/data/expected/FndEvent.apy.structure	Mon Aug 12 10:50:10 2013 +0530
    12.2 +++ b/PLSQL/Lexer/test/unit/data/expected/FndEvent.apy.structure	Mon Aug 12 11:26:54 2013 +0530
    12.3 @@ -215,10 +215,6 @@
    12.4  IF
    12.5  COMMENT
    12.6  COMMENT
    12.7 -COMMENT
    12.8 -COMMENT
    12.9 -COMMENT
   12.10 -COMMENT
   12.11  FOR_LOOP
   12.12  COMMENT
   12.13  COMMENT
    13.1 --- a/PLSQL/Lexer/test/unit/data/expected/IdentityPayInfo.apy.structure	Mon Aug 12 10:50:10 2013 +0530
    13.2 +++ b/PLSQL/Lexer/test/unit/data/expected/IdentityPayInfo.apy.structure	Mon Aug 12 11:26:54 2013 +0530
    13.3 @@ -225,7 +225,6 @@
    13.4  COMMENT
    13.5  IF
    13.6  COMMENT
    13.7 -COMMENT
    13.8  IF
    13.9  COMMENT
   13.10  IF
   13.11 @@ -281,7 +280,6 @@
   13.12  IF
   13.13  IF
   13.14  COMMENT
   13.15 -COMMENT
   13.16  IF
   13.17  PROCEDURE_IMPL
   13.18  CURSOR
   13.19 @@ -292,7 +290,6 @@
   13.20  FOR_LOOP
   13.21  STATEMENT
   13.22  COMMENT
   13.23 -COMMENT
   13.24  IF
   13.25  PROCEDURE_IMPL
   13.26  CURSOR
    14.1 --- a/PLSQL/Lexer/test/unit/data/expected/ScorecardInputValue.apy.structure	Mon Aug 12 10:50:10 2013 +0530
    14.2 +++ b/PLSQL/Lexer/test/unit/data/expected/ScorecardInputValue.apy.structure	Mon Aug 12 11:26:54 2013 +0530
    14.3 @@ -704,8 +704,6 @@
    14.4  PROCEDURE_IMPL
    14.5  CURSOR
    14.6  COMMENT
    14.7 -COMMENT
    14.8 -COMMENT
    14.9  FUNCTION_IMPL
   14.10  CURSOR
   14.11  PROCEDURE_IMPL
   14.12 @@ -716,11 +714,9 @@
   14.13  IF
   14.14  IF
   14.15  COMMENT
   14.16 -COMMENT
   14.17  FOR_LOOP
   14.18  LOOP
   14.19  COMMENT
   14.20 -COMMENT
   14.21  IF
   14.22  IF
   14.23  IF
   14.24 @@ -822,7 +818,6 @@
   14.25  IF
   14.26  COMMENT
   14.27  COMMENT
   14.28 -COMMENT
   14.29  FOR_LOOP
   14.30  COMMENT
   14.31  IF
   14.32 @@ -863,14 +858,9 @@
   14.33  CURSOR
   14.34  CURSOR
   14.35  COMMENT
   14.36 -COMMENT
   14.37 -COMMENT
   14.38 -COMMENT
   14.39 -COMMENT
   14.40  IF
   14.41  COMMENT
   14.42  COMMENT
   14.43 -COMMENT
   14.44  IF
   14.45  IF
   14.46  IF
   14.47 @@ -896,9 +886,6 @@
   14.48  COMMENT
   14.49  COMMENT
   14.50  COMMENT
   14.51 -COMMENT
   14.52 -COMMENT
   14.53 -COMMENT
   14.54  IF
   14.55  COMMENT
   14.56  COMMENT
   14.57 @@ -909,9 +896,6 @@
   14.58  COMMENT
   14.59  COMMENT
   14.60  COMMENT
   14.61 -COMMENT
   14.62 -COMMENT
   14.63 -COMMENT
   14.64  IF
   14.65  COMMENT
   14.66  COMMENT
    15.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    15.2 +++ b/PLSQL/Lexer/test/unit/data/expected/TestComments.apy.structure	Mon Aug 12 11:26:54 2013 +0530
    15.3 @@ -0,0 +1,8 @@
    15.4 +COMMENT
    15.5 +COMMENT
    15.6 +COMMENT
    15.7 +COMMENT
    15.8 +COMMENT
    15.9 +PROCEDURE_IMPL
   15.10 +COMMENT
   15.11 +STATEMENT
    16.1 --- a/PLSQL/Lexer/test/unit/data/expected/Trainer.apy.structure	Mon Aug 12 10:50:10 2013 +0530
    16.2 +++ b/PLSQL/Lexer/test/unit/data/expected/Trainer.apy.structure	Mon Aug 12 11:26:54 2013 +0530
    16.3 @@ -89,8 +89,6 @@
    16.4  STATEMENT
    16.5  COMMENT
    16.6  COMMENT
    16.7 -COMMENT
    16.8 -COMMENT
    16.9  PROCEDURE_IMPL
   16.10  PROCEDURE_IMPL
   16.11  IF
    17.1 --- a/PLSQL/Lexer/test/unit/data/expected/sys_IalObjectSlave.apy.structure	Mon Aug 12 10:50:10 2013 +0530
    17.2 +++ b/PLSQL/Lexer/test/unit/data/expected/sys_IalObjectSlave.apy.structure	Mon Aug 12 11:26:54 2013 +0530
    17.3 @@ -23,7 +23,6 @@
    17.4  COMMENT
    17.5  PROCEDURE_IMPL
    17.6  COMMENT
    17.7 -COMMENT
    17.8  PROCEDURE_IMPL
    17.9  CURSOR
   17.10  IF
    18.1 --- a/PLSQL/Lexer/test/unit/data/expected/test2.apy.structure	Mon Aug 12 10:50:10 2013 +0530
    18.2 +++ b/PLSQL/Lexer/test/unit/data/expected/test2.apy.structure	Mon Aug 12 11:26:54 2013 +0530
    18.3 @@ -41,13 +41,11 @@
    18.4  IF
    18.5  COMMENT
    18.6  COMMENT
    18.7 -COMMENT
    18.8  IF
    18.9  COMMENT
   18.10  IF
   18.11  COMMENT
   18.12  COMMENT
   18.13 -COMMENT
   18.14  IF
   18.15  IF
   18.16  IF
   18.17 @@ -116,21 +114,18 @@
   18.18  IF
   18.19  IF
   18.20  COMMENT
   18.21 +IF
   18.22  COMMENT
   18.23  IF
   18.24 +IF
   18.25 +IF
   18.26 +IF
   18.27 +IF
   18.28 +IF
   18.29 +IF
   18.30  COMMENT
   18.31  COMMENT
   18.32  IF
   18.33 -IF
   18.34 -IF
   18.35 -IF
   18.36 -IF
   18.37 -IF
   18.38 -IF
   18.39 -COMMENT
   18.40 -COMMENT
   18.41 -IF
   18.42 -COMMENT
   18.43  COMMENT
   18.44  COMMENT
   18.45  FOR_LOOP
    19.1 --- a/PLSQL/Lexer/test/unit/data/expected/txtser.ins.structure	Mon Aug 12 10:50:10 2013 +0530
    19.2 +++ b/PLSQL/Lexer/test/unit/data/expected/txtser.ins.structure	Mon Aug 12 11:26:54 2013 +0530
    19.3 @@ -8,6 +8,4 @@
    19.4  COMMENT
    19.5  IF
    19.6  COMMENT
    19.7 -COMMENT
    19.8 -COMMENT
    19.9  IF
    20.1 --- a/PLSQL/Lexer/test/unit/src/org/netbeans/modules/plsql/lexer/PlsqlBlockFactoryTest.java	Mon Aug 12 10:50:10 2013 +0530
    20.2 +++ b/PLSQL/Lexer/test/unit/src/org/netbeans/modules/plsql/lexer/PlsqlBlockFactoryTest.java	Mon Aug 12 11:26:54 2013 +0530
    20.3 @@ -568,6 +568,28 @@
    20.4          }
    20.5      }
    20.6      
    20.7 +    @Test
    20.8 +    public void testForComments() throws IOException, BadLocationException {
    20.9 +        System.out.println("Test for Comment blocks ");
   20.10 +        final String plsqlFileName = "TestComments.apy";
   20.11 +        FileObject fileObject = fs.getRoot().createData(plsqlFileName);
   20.12 +        assertNotNull(fileObject);
   20.13 +        try {
   20.14 +            PlsqlBlockFactory blockFac = loadAsTmpFile(fileObject, plsqlFileName);
   20.15 +            assertNotNull(blockFac);
   20.16 +
   20.17 +            List<PlsqlBlock> lstBlockFac = blockFac.getBlockHierarchy();
   20.18 +            //printHierarchy(lstBlockFac, "");
   20.19 +            //assertEquals(2, lstBlockFac.size());
   20.20 +            processBlocks(plsqlFileName, lstBlockFac);
   20.21 +
   20.22 +        } finally {
   20.23 +            if (fileObject != null) {
   20.24 +                fileObject.delete();
   20.25 +            }
   20.26 +        }
   20.27 +    }
   20.28 +    
   20.29     
   20.30      private PlsqlBlockFactory loadAsTmpFile(FileObject fileObj, String fileName) throws IOException, BadLocationException {
   20.31          InputStream inputStream = new FileInputStream(new File(getDataDir(), fileName));