Improved support for comment folding
authoraxelcl <axelcl>
Wed, 26 Oct 2005 21:39:54 +0000 (21:39 +0000)
committeraxelcl <axelcl>
Wed, 26 Oct 2005 21:39:54 +0000 (21:39 +0000)
net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/folding/DefaultJavaFoldingPreferenceBlock.java
net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/folding/DefaultJavaFoldingStructureProvider.java
net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/folding/FoldingMessages.properties

index b873cc6..2ed9f83 100644 (file)
@@ -32,42 +32,53 @@ import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Label;
 
-
 /**
  * Java default folding preferences.
- * 
+ *
  * @since 3.0
  */
 public class DefaultJavaFoldingPreferenceBlock implements IJavaFoldingPreferenceBlock {
 
        private IPreferenceStore fStore;
+
        private OverlayPreferenceStore fOverlayStore;
+
        private OverlayKey[] fKeys;
-       private Map fCheckBoxes= new HashMap();
-       private SelectionListener fCheckBoxListener= new SelectionListener() {
+
+       private Map fCheckBoxes = new HashMap();
+
+       private SelectionListener fCheckBoxListener = new SelectionListener() {
                public void widgetDefaultSelected(SelectionEvent e) {
                }
+
                public void widgetSelected(SelectionEvent e) {
-                       Button button= (Button) e.widget;
+                       Button button = (Button) e.widget;
                        fOverlayStore.setValue((String) fCheckBoxes.get(button), button.getSelection());
                }
        };
-       
 
        public DefaultJavaFoldingPreferenceBlock() {
-               fStore= PHPeclipsePlugin.getDefault().getPreferenceStore();
-               fKeys= createKeys();
-               fOverlayStore= new OverlayPreferenceStore(fStore, fKeys);
+               fStore = PHPeclipsePlugin.getDefault().getPreferenceStore();
+               fKeys = createKeys();
+               fOverlayStore = new OverlayPreferenceStore(fStore, fKeys);
        }
-       
+
        private OverlayKey[] createKeys() {
-               ArrayList overlayKeys= new ArrayList();
+               ArrayList overlayKeys = new ArrayList();
+
+               overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN,
+                               PreferenceConstants.EDITOR_FOLDING_JAVADOC));
+               overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN,
+                               PreferenceConstants.EDITOR_FOLDING_HEADERS));
+               // overlayKeys.add(new
+               // OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN,
+               // PreferenceConstants.EDITOR_FOLDING_INNERTYPES));
+               overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN,
+                               PreferenceConstants.EDITOR_FOLDING_METHODS));
+               // overlayKeys.add(new
+               // OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN,
+               // PreferenceConstants.EDITOR_FOLDING_IMPORTS));
 
-               overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_FOLDING_JAVADOC));
-//             overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_FOLDING_INNERTYPES));
-               overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_FOLDING_METHODS));
-//             overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_FOLDING_IMPORTS));
-               
                return (OverlayKey[]) overlayKeys.toArray(new OverlayKey[overlayKeys.size()]);
        }
 
@@ -77,64 +88,71 @@ public class DefaultJavaFoldingPreferenceBlock implements IJavaFoldingPreference
        public Control createControl(Composite composite) {
                fOverlayStore.load();
                fOverlayStore.start();
-               
-               Composite inner= new Composite(composite, SWT.NONE);
-               GridLayout layout= new GridLayout(1, true);
-               layout.verticalSpacing= 3;
-               layout.marginWidth= 0;
+
+               Composite inner = new Composite(composite, SWT.NONE);
+               GridLayout layout = new GridLayout(1, true);
+               layout.verticalSpacing = 3;
+               layout.marginWidth = 0;
                inner.setLayout(layout);
-               
-               Label label= new Label(inner, SWT.LEFT);
+
+               Label label = new Label(inner, SWT.LEFT);
                label.setText(FoldingMessages.getString("DefaultJavaFoldingPreferenceBlock.title")); //$NON-NLS-1$
-               
-//             addCheckBox(inner, FoldingMessages.getString("DefaultJavaFoldingPreferenceBlock.comments"), PreferenceConstants.EDITOR_FOLDING_JAVADOC, 0); //$NON-NLS-1$
-//             addCheckBox(inner, FoldingMessages.getString("DefaultJavaFoldingPreferenceBlock.innerTypes"), PreferenceConstants.EDITOR_FOLDING_INNERTYPES, 0); //$NON-NLS-1$
-               addCheckBox(inner, FoldingMessages.getString("DefaultJavaFoldingPreferenceBlock.methods"), PreferenceConstants.EDITOR_FOLDING_METHODS, 0); //$NON-NLS-1$
-//             addCheckBox(inner, FoldingMessages.getString("DefaultJavaFoldingPreferenceBlock.imports"), PreferenceConstants.EDITOR_FOLDING_IMPORTS, 0); //$NON-NLS-1$
-               
+
+               addCheckBox(inner,
+                               FoldingMessages.getString("DefaultJavaFoldingPreferenceBlock.comments"), PreferenceConstants.EDITOR_FOLDING_JAVADOC, 0); //$NON-NLS-1$
+               addCheckBox(inner,
+                               FoldingMessages.getString("DefaultJavaFoldingPreferenceBlock.headers"), PreferenceConstants.EDITOR_FOLDING_HEADERS, 0);//$NON-NLS-1$
+               // addCheckBox(inner,
+               // FoldingMessages.getString("DefaultJavaFoldingPreferenceBlock.innerTypes"),
+               // PreferenceConstants.EDITOR_FOLDING_INNERTYPES, 0); //$NON-NLS-1$
+               addCheckBox(inner,
+                               FoldingMessages.getString("DefaultJavaFoldingPreferenceBlock.methods"), PreferenceConstants.EDITOR_FOLDING_METHODS, 0); //$NON-NLS-1$
+               // addCheckBox(inner,
+               // FoldingMessages.getString("DefaultJavaFoldingPreferenceBlock.imports"),
+               // PreferenceConstants.EDITOR_FOLDING_IMPORTS, 0); //$NON-NLS-1$
+
                return inner;
        }
-       
-       private Button addCheckBox(Composite parent, String label, String key, int indentation) {               
-               Button checkBox= new Button(parent, SWT.CHECK);
+
+       private Button addCheckBox(Composite parent, String label, String key, int indentation) {
+               Button checkBox = new Button(parent, SWT.CHECK);
                checkBox.setText(label);
-               
-               GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
-               gd.horizontalIndent= indentation;
-               gd.horizontalSpan= 1;
-               gd.grabExcessVerticalSpace= false;
+
+               GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
+               gd.horizontalIndent = indentation;
+               gd.horizontalSpan = 1;
+               gd.grabExcessVerticalSpace = false;
                checkBox.setLayoutData(gd);
                checkBox.addSelectionListener(fCheckBoxListener);
-               
+
                fCheckBoxes.put(checkBox, key);
-               
+
                return checkBox;
        }
-       
+
        private void initializeFields() {
-               Iterator it= fCheckBoxes.keySet().iterator();
+               Iterator it = fCheckBoxes.keySet().iterator();
                while (it.hasNext()) {
-                       Button b= (Button) it.next();
-                       String key= (String) fCheckBoxes.get(b);
+                       Button b = (Button) it.next();
+                       String key = (String) fCheckBoxes.get(b);
                        b.setSelection(fOverlayStore.getBoolean(key));
                }
        }
-       
+
        /*
         * @see net.sourceforge.phpdt.internal.ui.text.folding.AbstractJavaFoldingPreferences#performOk()
         */
        public void performOk() {
                fOverlayStore.propagate();
        }
-       
-       
+
        /*
         * @see net.sourceforge.phpdt.internal.ui.text.folding.AbstractJavaFoldingPreferences#initialize()
         */
        public void initialize() {
                initializeFields();
        }
-       
+
        /*
         * @see net.sourceforge.phpdt.internal.ui.text.folding.AbstractJavaFoldingPreferences#performDefaults()
         */
@@ -142,7 +160,7 @@ public class DefaultJavaFoldingPreferenceBlock implements IJavaFoldingPreference
                fOverlayStore.loadDefaults();
                initializeFields();
        }
-       
+
        /*
         * @see net.sourceforge.phpdt.internal.ui.text.folding.AbstractJavaFoldingPreferences#dispose()
         */
index a15d39c..77a19ac 100644 (file)
@@ -37,6 +37,7 @@ import net.sourceforge.phpdt.core.ToolFactory;
 import net.sourceforge.phpdt.core.compiler.IScanner;
 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
+import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
 import net.sourceforge.phpdt.internal.ui.text.DocumentCharacterIterator;
 import net.sourceforge.phpdt.ui.IWorkingCopyManager;
 import net.sourceforge.phpdt.ui.PreferenceConstants;
@@ -384,7 +385,7 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
 
        private boolean fCollapseJavadoc = false;
 
-//     private boolean fCollapseImportContainer = true;
+       // private boolean fCollapseImportContainer = true;
 
        private boolean fCollapseInnerTypes = true;
 
@@ -484,7 +485,7 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                                                ICompilationUnit unit = (ICompilationUnit) fInput;
                                                synchronized (unit) {
                                                        try {
-//                                                             unit.reconcile(ICompilationUnit.NO_AST, false, null, null);
+                                                               // unit.reconcile(ICompilationUnit.NO_AST, false, null, null);
                                                                unit.reconcile();
                                                        } catch (JavaModelException x) {
                                                        }
@@ -517,7 +518,8 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
        private void initializePreferences() {
                IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
                fCollapseInnerTypes = store.getBoolean(PreferenceConstants.EDITOR_FOLDING_INNERTYPES);
-//             fCollapseImportContainer = store.getBoolean(PreferenceConstants.EDITOR_FOLDING_IMPORTS);
+               // fCollapseImportContainer =
+               // store.getBoolean(PreferenceConstants.EDITOR_FOLDING_IMPORTS);
                fCollapseJavadoc = store.getBoolean(PreferenceConstants.EDITOR_FOLDING_JAVADOC);
                fCollapseMethods = store.getBoolean(PreferenceConstants.EDITOR_FOLDING_METHODS);
                fCollapseHeaderComments = store.getBoolean(PreferenceConstants.EDITOR_FOLDING_HEADERS);
@@ -525,7 +527,7 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
 
        private Map computeAdditions(IParent parent) {
                Map map = new LinkedHashMap(); // use a linked map to maintain ordering of
-                                                                                                                                               // comments
+               // comments
                try {
                        computeAdditions(parent.getChildren(), map);
                } catch (JavaModelException x) {
@@ -553,10 +555,10 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                boolean collapse = false;
                switch (element.getElementType()) {
 
-//             case IJavaElement.IMPORT_CONTAINER:
-//                     collapse = fAllowCollapsing && fCollapseImportContainer;
-//                     createProjection = true;
-//                     break;
+               // case IJavaElement.IMPORT_CONTAINER:
+               // collapse = fAllowCollapsing && fCollapseImportContainer;
+               // createProjection = true;
+               // break;
                case IJavaElement.TYPE:
                        collapse = fAllowCollapsing && fCollapseInnerTypes && isInnerType((IType) element);
                        createProjection = true;
@@ -637,28 +639,30 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                                        }
                                }
 
-                               IScanner scanner = ToolFactory.createScanner(true, false, false, false);
-                               scanner.setSource(contents.toCharArray());
                                final int shift = range.getOffset();
                                int start = shift;
-                               while (true) {
+                               if (element instanceof IType) {
+                                       Scanner scanner = ToolFactory.createScanner(true, false, false, false);
+                                       scanner.setSource(contents.toCharArray());
+                                       scanner.setPHPMode(true);
 
                                        int token = scanner.getNextToken();
-                                       start = shift + scanner.getCurrentTokenStartPosition();
+                                       while (token != ITerminalSymbols.TokenNameEOF) {
 
-                                       switch (token) {
-                                       case ITerminalSymbols.TokenNameCOMMENT_PHPDOC:
-                                       case ITerminalSymbols.TokenNameCOMMENT_BLOCK: {
-                                               int end = shift + scanner.getCurrentTokenEndPosition() + 1;
-                                               regions.add(new Region(start, end - start));
-                                       }
-                                       case ITerminalSymbols.TokenNameCOMMENT_LINE:
-                                               continue;
-                                       }
+                                               token = scanner.getNextToken();
+                                               start = shift + scanner.getCurrentTokenStartPosition();
 
-                                       break;
+                                               switch (token) {
+                                               case ITerminalSymbols.TokenNameCOMMENT_PHPDOC:
+                                               case ITerminalSymbols.TokenNameCOMMENT_BLOCK: {
+                                                       int end = shift + scanner.getCurrentTokenEndPosition() + 1;
+                                                       regions.add(new Region(start, end - start));
+                                               }
+                                               case ITerminalSymbols.TokenNameCOMMENT_LINE:
+                                                       continue;
+                                               }
+                                       }
                                }
-
                                regions.add(new Region(start, shift + range.getLength() - start));
 
                                if (regions.size() > 0) {
@@ -666,6 +670,7 @@ public class DefaultJavaFoldingStructureProvider implements IProjectionListener,
                                        regions.toArray(result);
                                        return result;
                                }
+
                        }
                } catch (JavaModelException e) {
                } catch (InvalidInputException e) {
index 103e630..b89b15e 100644 (file)
@@ -1,10 +1,10 @@
 ###############################################################################
 # Copyright (c) 2000, 2004 IBM Corporation and others.
-# All rights reserved. This program and the accompanying materials 
+# All rights reserved. This program and the accompanying materials
 # are made available under the terms of the Common Public License v1.0
 # which accompanies this distribution, and is available at
 # http://www.eclipse.org/legal/cpl-v10.html
-# 
+#
 # Contributors:
 #     IBM Corporation - initial API and implementation
 ###############################################################################
@@ -12,6 +12,7 @@
 
 DefaultJavaFoldingPreferenceBlock.title= Initially fold these region types:
 DefaultJavaFoldingPreferenceBlock.comments= &Comments
+DefaultJavaFoldingPreferenceBlock.headers= &Header Comments
 DefaultJavaFoldingPreferenceBlock.innerTypes= Inner &types
 DefaultJavaFoldingPreferenceBlock.methods= &Methods
 DefaultJavaFoldingPreferenceBlock.imports= &Imports