| 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>(); |
|---|
| 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 | } |
|---|
| 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(); |
|---|
| 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); |
|---|
| 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 | /** |
|---|
| 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 | } |
|---|