Changeset 95

Show
Ignore:
Timestamp:
07/05/08 14:01:30 (6 months ago)
Author:
mb0
Message:

added initial folding as already advertised in the preferences. (header comments, all comments, imports, members)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • axdt/trunk/org.axdt.as3/src/org/axdt/as3/imp/foldingUpdater/AS3FoldingUpdater.java

    r92 r95  
    55import java.util.List; 
    66 
     7import org.eclipse.imp.parser.IParseController; 
     8import org.eclipse.imp.parser.ISourcePositionLocator; 
    79import org.eclipse.imp.services.base.FolderBase; 
     10import org.eclipse.jface.preference.IPreferenceStore; 
     11import org.eclipse.jface.text.Position; 
     12import org.eclipse.jface.text.source.Annotation; 
     13import org.eclipse.jface.text.source.projection.ProjectionAnnotation; 
     14import org.eclipse.jface.text.source.projection.ProjectionAnnotationModel; 
    815 
    916import org.axdt.as3.imp.parser.Ast.*; 
     17import org.axdt.as3.preferences.AS3Preferences; 
     18 
    1019import lpg.runtime.*; 
    1120 
    1221/** 
    13  * This file provides a skeletal implementation of the language-dependent 
    14  * aspects of a source-text folder. This implementation is generated from a 
    15  * template that is parameterized with respect to the name of the language, the 
    16  * package containing the language-specific types for AST nodes and 
    17  * AbstractVisitors, and the name of the folder package and class. 
    18  *  
    19  * @author suttons@us.ibm.com 
     22 * @author mb0 
    2023 */ 
    2124public class AS3FoldingUpdater extends FolderBase { 
    22         IPrsStream prsStream; 
    2325 
    24         public void makeAnnotationWithOffsets(int first_offset, int last_offset) { 
    25                 super.makeAnnotation(first_offset, last_offset - first_offset + 1); 
     26        protected boolean useFolding; 
     27        protected boolean foldComments, foldHeaders, foldImports, foldMembers; 
     28        protected List<Annotation> autoFolds; 
     29        protected int packageStart; 
     30 
     31        public AS3FoldingUpdater() { 
     32                IPreferenceStore store = AS3Preferences.getInstance().getStore(); 
     33                useFolding = store.getBoolean(AS3Preferences.USE_FOLDING); 
     34                foldComments = store.getBoolean(AS3Preferences.FOLD_COMMENTS); 
     35                foldHeaders = store.getBoolean(AS3Preferences.FOLD_HEADERS); 
     36                foldImports = store.getBoolean(AS3Preferences.FOLD_IMPORTS); 
     37                foldMembers = store.getBoolean(AS3Preferences.FOLD_MEMBERS); 
     38                autoFolds = new ArrayList<Annotation>(); 
    2639        } 
    2740 
    28         // 
    29         // Use this version of makeAnnotation when you have a range of 
    30         // tokens to fold. 
    31         // 
    32         private void makeAnnotation(IToken first_token, IToken last_token) { 
    33                 if (last_token.getEndLine() > first_token.getLine()) { 
    34                        IToken next_token = prsStream.getIToken(prsStream.getNext(last_token.getTokenIndex())); 
    35                         IToken[] adjuncts = next_token.getPrecedingAdjuncts()
    36                         IToken gate_token = adjuncts.length == 0 ? next_token : adjuncts[0]; 
    37                         makeAnnotationWithOffsets(first_token.getStartOffset(), 
    38                                        gate_token.getLine() > last_token.getEndLine() ? prsStream.getLexStream() 
    39                                                        .getLineOffset(gate_token.getLine() - 1) : last_token.getEndOffset()); 
     41        protected void makeCommentAnnotations(IToken[] comments) { 
     42               if (comments == null || comments.length < 1) return; 
     43               IPrsStream prsStream = comments[0].getPrsStream(); 
     44               IToken previous_token = prsStream.getIToken(comments[0].getTokenIndex()); 
     45               int next = prsStream.getNext(previous_token.getTokenIndex()); 
     46                IToken next_token = prsStream.getIToken(next); 
     47                for (int k = 0; k < comments.length; k++) { 
     48                        Adjunct comment = (Adjunct) comments[k]
     49                        if (comment.getEndLine() > comment.getLine()) { 
     50                               IToken gate_token = k + 1 < comments.length ? comments[k + 1] : next_token; 
     51                                makeCommentAnnotation(comment, gate_token); 
     52                        } 
    4053                } 
    4154        } 
    4255 
    43         private void makeAnnotation(ASTNode n) { 
    44                 makeAnnotation(n.getLeftIToken(), n.getRightIToken()); 
     56        protected void makeCommentAnnotation(Adjunct comment, IToken gate) { 
     57                int start = comment.getStartOffset(); 
     58                int end = comment.getEndOffset(); 
     59                if (gate.getLine() > comment.getEndLine()) { 
     60                        ILexStream lexStream = comment.getPrsStream().getLexStream(); 
     61                        end = lexStream.getLineOffset(gate.getLine() - 1); 
     62                } 
     63                makeAnnotation(start, end - start + 1); 
     64                if (foldComments) autoFoldLast(); 
     65                else if (foldHeaders && packageStart > end) autoFoldLast(); 
    4566        } 
    4667 
    4768        private void makeAdjunctAnnotations(ASTNode theAST) { 
    48                 ILexStream lexStream = prsStream.getLexStream(); 
    49                 ArrayList adjuncts = (ArrayList) prsStream.getAdjuncts(); 
     69                IPrsStream prsStream = theAST.getLeftIToken().getPrsStream(); 
     70                ArrayList adjuncts = prsStream.getAdjuncts(); 
    5071                for (int i = 0; i < adjuncts.size();) { 
    5172                        Adjunct adjunct = (Adjunct) adjuncts.get(i); 
    52  
    53                         IToken previous_token = prsStream.getIToken(adjunct.getTokenIndex()), next_token = prsStream 
    54                                         .getIToken(prsStream.getNext(previous_token.getTokenIndex())), comments[] = previous_token 
    55                                         .getFollowingAdjuncts(); 
    56  
    57                         for (int k = 0; k < comments.length; k++) { 
    58                                 Adjunct comment = (Adjunct) comments[k]; 
    59                                 if (comment.getEndLine() > comment.getLine()) { 
    60                                         IToken gate_token = k + 1 < comments.length ? comments[k + 1] : next_token; 
    61                                         makeAnnotationWithOffsets(comment.getStartOffset(), 
    62                                                         gate_token.getLine() > comment.getEndLine() ? lexStream 
    63                                                                         .getLineOffset(gate_token.getLine() - 1) : comment 
    64                                                                         .getEndOffset()); 
    65                                 } 
    66                         } 
    67  
     73                        IToken previous_token = prsStream.getIToken(adjunct.getTokenIndex()); 
     74                        IToken[] comments = previous_token.getFollowingAdjuncts(); 
     75                        makeCommentAnnotations(comments); 
    6876                        i += comments.length; 
    6977                } 
    7078        } 
    7179 
    72         /* 
     80        public void sendVisitorToAST(HashMap newAnnotations, List annotations, Object ast) { 
     81                ASTNode theAST = (ASTNode) ast; 
     82                theAST.accept(new FoldingVisitor()); 
     83                makeAdjunctAnnotations(theAST); 
     84        } 
     85 
     86        @Override 
     87        public synchronized void updateFoldingStructure(IParseController controller, 
     88                        ProjectionAnnotationModel model) { 
     89                if (!useFolding) return; 
     90                super.updateFoldingStructure(controller, model); 
     91                if (autoFolds != null) { 
     92                        for (Annotation anno:autoFolds) { 
     93                                model.collapse(anno); 
     94                        } 
     95                        autoFolds = null; 
     96                } 
     97        } 
     98 
     99        protected void autoFoldLast() { 
     100                if (autoFolds == null) return; 
     101                if (annotations == null || annotations.isEmpty()) return; 
     102                Annotation anno = annotations.get(annotations.size() - 1); 
     103                autoFolds.add(anno); 
     104        } 
     105 
     106        public void makeAnnotation(ASTNode node) { 
     107                ISourcePositionLocator locator = parseController.getNodeLocator(); 
     108                int start = locator.getStartOffset(node); 
     109                int end = locator.getEndOffset(node); 
     110                IToken right = node.getRightIToken(); 
     111                IPrsStream stream = right.getPrsStream(); 
     112                int index = right.getTokenIndex(); 
     113                IToken token = stream.getIToken(index + 1); 
     114                int line = token.getLine(); 
     115                if (line > right.getEndLine()) { 
     116                        int tmpend = stream.getLexStream().getLineOffset(right.getEndLine() + 1); 
     117                        if (tmpend > 0) end = --tmpend; 
     118                } 
     119                Position pos = new Position(start, end - start + 1); 
     120                ProjectionAnnotation annotation = new ProjectionAnnotation(); 
     121                newAnnotations.put(annotation, pos); 
     122                annotations.add(annotation); 
     123        } 
     124 
     125        /** 
    73126         * A visitor for ASTs. Its purpose is to create ProjectionAnnotations for 
    74127         * regions of text corresponding to various types of AST node or to text 
     
    77130         */ 
    78131        private class FoldingVisitor extends AbstractVisitor { 
     132 
    79133                public void unimplementedVisitor(String s) { 
    80134                } 
    81135 
    82                 // START_HERE 
    83                 // 
    84                 // Include visit(..) functions for various types of AST nodes that are 
    85                 // associated with folding. These functions should call one of the two 
    86                 // versions of makeAnnotation(..) that are defined in FolderBase. The 
    87                 // usual case is to call the version of makeAnnotation that creates a 
    88                 // folding annotation corresponding to the extent of a particular AST 
    89                 // node. 
    90                 // The other possibility is to create an annotation with an extent that 
    91                 // is explicitly provided. An example is shown below ... 
     136                public boolean visit(PackageDeclaration n) { 
     137                        packageStart = n.getLeftIToken().getStartOffset(); 
     138                        return true; 
     139                } 
    92140 
    93                 // Create annotations for the folding of blocks (for example) 
     141                public boolean visit(ImportDeclarationList n) { 
     142                        makeAnnotation(n); 
     143                        if (foldImports) autoFoldLast(); 
     144                        return true; 
     145                } 
     146 
    94147                public boolean visit(MethodBody n) { 
    95148                        makeAnnotation(n); 
     149                        if (foldMembers) autoFoldLast(); 
    96150                        return true; 
    97151                } 
     
    111165                        return true; 
    112166                } 
    113         }; 
    114  
    115         // When instantiated will provide a concrete implementation of an abstract 
    116         // method 
    117         // defined in FolderBase 
    118         public void sendVisitorToAST(HashMap newAnnotations, List annotations, Object ast) { 
    119                 ASTNode theAST = (ASTNode) ast; 
    120                 prsStream = theAST.getLeftIToken().getPrsStream(); 
    121                 AbstractVisitor abstractVisitor = new FoldingVisitor(); 
    122                 theAST.accept(abstractVisitor); 
    123                 makeAdjunctAnnotations(theAST); 
    124167        } 
    125168}