KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > codeassist > select > SelectionJavadocParser


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.codeassist.select;
12
13 import java.util.List JavaDoc;
14
15 import org.eclipse.jdt.core.compiler.InvalidInputException;
16 import org.eclipse.jdt.internal.codeassist.SelectionEngine;
17 import org.eclipse.jdt.internal.compiler.ast.*;
18 import org.eclipse.jdt.internal.compiler.parser.JavadocParser;
19
20 /**
21  * Parser specialized for decoding javadoc comments which includes code selection.
22  */

23 public class SelectionJavadocParser extends JavadocParser {
24     
25     int selectionStart;
26     int selectionEnd;
27     ASTNode selectedNode;
28
29     public SelectionJavadocParser(SelectionParser sourceParser) {
30         super(sourceParser);
31         this.kind = SELECTION_PARSER | TEXT_PARSE;
32     }
33
34     /*
35      * Do not parse comment if selection is not included.
36      */

37     public boolean checkDeprecation(int commentPtr) {
38         this.selectionStart = ((SelectionParser)sourceParser).selectionStart;
39         this.selectionEnd = ((SelectionParser)sourceParser).selectionEnd;
40         this.javadocStart = this.sourceParser.scanner.commentStarts[commentPtr];
41         this.javadocEnd = this.sourceParser.scanner.commentStops[commentPtr];
42         if (this.javadocStart <= this.selectionStart && this.selectionEnd <= this.javadocEnd) {
43             if (SelectionEngine.DEBUG) {
44                 System.out.println("SELECTION in Javadoc:"); //$NON-NLS-1$
45
}
46             super.checkDeprecation(commentPtr);
47         } else {
48             this.docComment = null;
49         }
50         return false;
51     }
52
53     /*
54      * Replace stored Javadoc node with specific selection one.
55      */

56     protected boolean commentParse() {
57         this.docComment = new SelectionJavadoc(this.javadocStart, this.javadocEnd);
58         return super.commentParse();
59     }
60
61     /*
62      * Create argument expression and store it if it includes selection.
63      */

64     protected Object JavaDoc createArgumentReference(char[] name, int dim, boolean isVarargs, Object JavaDoc typeRef, long[] dimPositions, long argNamePos) throws InvalidInputException {
65         // Create argument as we may need it after
66
Expression expression = (Expression) super.createArgumentReference(name, dim, isVarargs, typeRef, dimPositions, argNamePos);
67         // See if selection is in argument
68
int start = ((TypeReference)typeRef).sourceStart;
69         int end = ((TypeReference)typeRef).sourceEnd;
70         if (start <= this.selectionStart && this.selectionEnd <= end) {
71             selectedNode = expression;
72             this.abort = true;
73             if (SelectionEngine.DEBUG) {
74                 System.out.println(" selected argument="+selectedNode); //$NON-NLS-1$
75
}
76         }
77         return expression;
78     }
79
80     /*
81      * Verify if field identifier positions include selection.
82      * If so, create field reference, store it and abort comment parse.
83      * Otherwise return null as we do not need this reference.
84      */

85     protected Object JavaDoc createFieldReference(Object JavaDoc receiver) throws InvalidInputException {
86         int start = (int) (this.identifierPositionStack[0] >>> 32);
87         int end = (int) this.identifierPositionStack[0];
88         if (start <= this.selectionStart && this.selectionEnd <= end) {
89             selectedNode = (ASTNode) super.createFieldReference(receiver);
90             this.abort = true;
91             if (SelectionEngine.DEBUG) {
92                 System.out.println(" selected field="+selectedNode); //$NON-NLS-1$
93
}
94         }
95         return null;
96     }
97
98     /*
99      * Verify if method identifier positions include selection.
100      * If so, create field reference, store it and abort comment parse.
101      * Otherwise return null as we do not need this reference.
102      */

103     protected Object JavaDoc createMethodReference(Object JavaDoc receiver, List JavaDoc arguments) throws InvalidInputException {
104         int memberPtr = this.identifierLengthStack[0] - 1; // may be > 0 for inner class constructor reference
105
int start = (int) (this.identifierPositionStack[memberPtr] >>> 32);
106         int end = (int) this.identifierPositionStack[memberPtr];
107         if (start <= this.selectionStart && this.selectionEnd <= end) {
108             selectedNode = (ASTNode) super.createMethodReference(receiver, arguments);
109             this.abort = true;
110             if (SelectionEngine.DEBUG) {
111                 System.out.println(" selected method="+selectedNode); //$NON-NLS-1$
112
}
113         }
114         return null;
115     }
116
117     /*
118      * Create type reference and verify if it includes selection.
119      * If so, store it and abort comment parse.
120      * Otherwise return null as we do not need this reference.
121      */

122     protected Object JavaDoc createTypeReference(int primitiveToken) {
123         // Need to create type ref in case it was needed by members
124
TypeReference typeRef = (TypeReference) super.createTypeReference(primitiveToken);
125     
126         // See if node is concerned by selection
127
if (typeRef.sourceStart <= this.selectionStart && this.selectionEnd <= typeRef.sourceEnd) {
128             // See if selection is in one of tokens of qualification
129
if (typeRef instanceof JavadocQualifiedTypeReference) {
130                 JavadocQualifiedTypeReference qualifiedTypeRef = (JavadocQualifiedTypeReference) typeRef;
131                 int size = qualifiedTypeRef.tokens.length - 1;
132                 for (int i=0; i<size; i++) {
133                     int start = (int) (qualifiedTypeRef.sourcePositions[i] >>> 32);
134                     int end = (int) qualifiedTypeRef.sourcePositions[i];
135                     if (start <= this.selectionStart && this.selectionEnd <= end) {
136                         int pos = i + 1;
137                         char[][] tokens = new char[pos][];
138                         int ptr = this.identifierPtr - size;
139                         System.arraycopy(this.identifierStack, ptr, tokens, 0, pos);
140                         long[] positions = new long[pos];
141                         System.arraycopy(this.identifierPositionStack, ptr, positions, 0, pos);
142                         selectedNode = new JavadocQualifiedTypeReference(tokens, positions, this.tagSourceStart, this.tagSourceEnd);
143                         this.abort = true; // we got selected node => cancel parse
144
if (SelectionEngine.DEBUG) {
145                             System.out.println(" selected partial qualified type="+selectedNode); //$NON-NLS-1$
146
}
147                         return typeRef;
148                     }
149                 }
150                 // Selection is in last token => we'll store type ref as this
151
}
152             // Store type ref as selected node
153
selectedNode = typeRef;
154             this.abort = true; // we got selected node => cancel parse
155
if (SelectionEngine.DEBUG) {
156                 System.out.println(" selected type="+selectedNode); //$NON-NLS-1$
157
}
158         }
159         return typeRef;
160     }
161
162     /*
163      * Push param reference and verify if it includes selection.
164      * If so, store it and abort comment parse.
165      */

166     protected boolean pushParamName(boolean isTypeParam) {
167         if (super.pushParamName(isTypeParam)) {
168             Expression expression = (Expression) astStack[astPtr--];
169             // See if expression is concerned by selection
170
if (expression.sourceStart <= this.selectionStart && this.selectionEnd <= expression.sourceEnd) {
171                 selectedNode = expression;
172                 this.abort = true; // we got selected node => cancel parse
173
if (SelectionEngine.DEBUG) {
174                     System.out.println(" selected param="+selectedNode); //$NON-NLS-1$
175
}
176             }
177         }
178         return false;
179     }
180
181     /*
182      * Store selected node into doc comment.
183      */

184     protected void updateDocComment() {
185         if (selectedNode instanceof Expression) {
186             ((SelectionJavadoc) this.docComment).selectedNode = (Expression) selectedNode;
187         }
188     }
189 }
190
Popular Tags