Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2000, 2005 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; |
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; |
32 |
} |
33 |
|
34 |
/* |
35 |
* Do not parse comment if selection is not included. |
36 |
*/ |
37 |
public boolean checkDeprecation(int javadocStart, int javadocEnd) { |
38 |
this.selectionStart = ((SelectionParser)sourceParser).selectionStart; |
39 |
this.selectionEnd = ((SelectionParser)sourceParser).selectionEnd; |
40 |
if (javadocStart <= this.selectionStart && this.selectionEnd <= javadocEnd) { |
41 |
if (SelectionEngine.DEBUG) { |
42 |
System.out.println("SELECTION in Javadoc:"); //$NON-NLS-1$ |
43 |
} |
44 |
super.checkDeprecation(javadocStart, javadocEnd); |
45 |
} else { |
46 |
this.docComment = null; |
47 |
} |
48 |
return false; |
49 |
} |
50 |
|
51 |
/* |
52 |
* Replace stored Javadoc node with specific selection one. |
53 |
*/ |
54 |
protected boolean commentParse(int javadocStart, int javadocEnd) { |
55 |
this.docComment = new SelectionJavadoc(javadocStart, javadocEnd); |
56 |
return super.commentParse(javadocStart, javadocEnd); |
57 |
} |
58 |
|
59 |
/* |
60 |
* Create argument expression and store it if it includes selection. |
61 |
*/ |
62 |
protected Object createArgumentReference(char[] name, int dim, boolean isVarargs, Object typeRef, long[] dimPositions, long argNamePos) throws InvalidInputException { |
63 |
// Create argument as we may need it after |
64 |
Expression expression = (Expression) super.createArgumentReference(name, dim, isVarargs, typeRef, dimPositions, argNamePos); |
65 |
// See if selection is in argument |
66 |
int start = ((TypeReference)typeRef).sourceStart; |
67 |
int end = ((TypeReference)typeRef).sourceEnd; |
68 |
if (start <= this.selectionStart && this.selectionEnd <= end) { |
69 |
selectedNode = expression; |
70 |
this.abort = true; |
71 |
if (SelectionEngine.DEBUG) { |
72 |
System.out.println(" selected argument="+selectedNode); //$NON-NLS-1$ |
73 |
} |
74 |
} |
75 |
return expression; |
76 |
} |
77 |
|
78 |
/* |
79 |
* Verify if field identifier positions include selection. |
80 |
* If so, create field reference, store it and abort comment parse. |
81 |
* Otherwise return null as we do not need this reference. |
82 |
*/ |
83 |
protected Object createFieldReference(Object receiver) throws InvalidInputException { |
84 |
int start = (int) (this.identifierPositionStack[0] >>> 32); |
85 |
int end = (int) this.identifierPositionStack[0]; |
86 |
if (start <= this.selectionStart && this.selectionEnd <= end) { |
87 |
selectedNode = (ASTNode) super.createFieldReference(receiver); |
88 |
this.abort = true; |
89 |
if (SelectionEngine.DEBUG) { |
90 |
System.out.println(" selected field="+selectedNode); //$NON-NLS-1$ |
91 |
} |
92 |
} |
93 |
return null; |
94 |
} |
95 |
|
96 |
/* |
97 |
* Verify if method identifier positions include selection. |
98 |
* If so, create field reference, store it and abort comment parse. |
99 |
* Otherwise return null as we do not need this reference. |
100 |
*/ |
101 |
protected Object createMethodReference(Object receiver, List arguments) throws InvalidInputException { |
102 |
int start = (int) (this.identifierPositionStack[0] >>> 32); |
103 |
int end = (int) this.identifierPositionStack[0]; |
104 |
if (start <= this.selectionStart && this.selectionEnd <= end) { |
105 |
selectedNode = (ASTNode) super.createMethodReference(receiver, arguments); |
106 |
this.abort = true; |
107 |
if (SelectionEngine.DEBUG) { |
108 |
System.out.println(" selected method="+selectedNode); //$NON-NLS-1$ |
109 |
} |
110 |
} |
111 |
return null; |
112 |
} |
113 |
|
114 |
/* |
115 |
* Create type reference and verify if it includes selection. |
116 |
* If so, store it and abort comment parse. |
117 |
* Otherwise return null as we do not need this reference. |
118 |
*/ |
119 |
protected Object createTypeReference(int primitiveToken) { |
120 |
// Need to create type ref in case it was needed by members |
121 |
TypeReference typeRef = (TypeReference) super.createTypeReference(primitiveToken); |
122 |
|
123 |
// See if node is concerned by selection |
124 |
if (typeRef.sourceStart <= this.selectionStart && this.selectionEnd <= typeRef.sourceEnd) { |
125 |
// See if selection is in one of tokens of qualification |
126 |
if (typeRef instanceof JavadocQualifiedTypeReference) { |
127 |
JavadocQualifiedTypeReference qualifiedTypeRef = (JavadocQualifiedTypeReference) typeRef; |
128 |
int size = qualifiedTypeRef.tokens.length - 1; |
129 |
for (int i=0; i<size; i++) { |
130 |
int start = (int) (qualifiedTypeRef.sourcePositions[i] >>> 32); |
131 |
int end = (int) qualifiedTypeRef.sourcePositions[i]; |
132 |
if (start <= this.selectionStart && this.selectionEnd <= end) { |
133 |
int pos = i + 1; |
134 |
char[][] tokens = new char[pos][]; |
135 |
System.arraycopy(this.identifierStack, this.identifierPtr+1, tokens, 0, pos); |
136 |
long[] positions = new long[pos]; |
137 |
System.arraycopy(this.identifierPositionStack, this.identifierPtr + 1, positions, 0, pos); |
138 |
selectedNode = new JavadocQualifiedTypeReference(tokens, positions, this.tagSourceStart, this.tagSourceEnd); |
139 |
this.abort = true; // we got selected node => cancel parse |
140 |
if (SelectionEngine.DEBUG) { |
141 |
System.out.println(" selected partial qualified type="+selectedNode); //$NON-NLS-1$ |
142 |
} |
143 |
return typeRef; |
144 |
} |
145 |
} |
146 |
// Selection is in last token => we'll store type ref as this |
147 |
} |
148 |
// Store type ref as selected node |
149 |
selectedNode = typeRef; |
150 |
this.abort = true; // we got selected node => cancel parse |
151 |
if (SelectionEngine.DEBUG) { |
152 |
System.out.println(" selected type="+selectedNode); //$NON-NLS-1$ |
153 |
} |
154 |
} |
155 |
return typeRef; |
156 |
} |
157 |
|
158 |
/* |
159 |
* Push param reference and verify if it includes selection. |
160 |
* If so, store it and abort comment parse. |
161 |
*/ |
162 |
protected boolean pushParamName(boolean isTypeParam) { |
163 |
if (super.pushParamName(isTypeParam)) { |
164 |
Expression expression = (Expression) astStack[astPtr--]; |
165 |
// See if expression is concerned by selection |
166 |
if (expression.sourceStart <= this.selectionStart && this.selectionEnd <= expression.sourceEnd) { |
167 |
selectedNode = expression; |
168 |
this.abort = true; // we got selected node => cancel parse |
169 |
if (SelectionEngine.DEBUG) { |
170 |
System.out.println(" selected param="+selectedNode); //$NON-NLS-1$ |
171 |
} |
172 |
} |
173 |
} |
174 |
return false; |
175 |
} |
176 |
|
177 |
/* |
178 |
* Store selected node into doc comment. |
179 |
*/ |
180 |
protected void updateDocComment() { |
181 |
if (selectedNode instanceof Expression) { |
182 |
((SelectionJavadoc) this.docComment).selectedNode = (Expression) selectedNode; |
183 |
} |
184 |
} |
185 |
} |