37
|
1 |
/*******************************************************************************
|
|
2 |
* Copyright (c) 2009 Wind River Systems, Inc. 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 |
* Markus Schorn - initial API and implementation
|
|
10 |
*******************************************************************************/
|
|
11 |
package org.eclipse.cdt.internal.ui.search.actions;
|
|
12 |
|
|
13 |
import java.util.ArrayList;
|
|
14 |
import java.util.Arrays;
|
|
15 |
import java.util.Collection;
|
|
16 |
import java.util.HashSet;
|
|
17 |
import java.util.Iterator;
|
|
18 |
import java.util.List;
|
|
19 |
import java.util.Set;
|
|
20 |
|
|
21 |
import org.eclipse.core.runtime.CoreException;
|
|
22 |
import org.eclipse.core.runtime.IPath;
|
|
23 |
import org.eclipse.core.runtime.IProgressMonitor;
|
|
24 |
import org.eclipse.core.runtime.IStatus;
|
|
25 |
import org.eclipse.core.runtime.Path;
|
|
26 |
import org.eclipse.core.runtime.Status;
|
|
27 |
import org.eclipse.core.runtime.jobs.Job;
|
|
28 |
import org.eclipse.jface.text.IRegion;
|
|
29 |
import org.eclipse.jface.text.ITextSelection;
|
|
30 |
import org.eclipse.jface.text.Region;
|
|
31 |
import org.eclipse.swt.widgets.Display;
|
|
32 |
|
|
33 |
import org.eclipse.cdt.core.CCorePlugin;
|
|
34 |
import org.eclipse.cdt.core.dom.IName;
|
|
35 |
import org.eclipse.cdt.core.dom.ast.ASTNameCollector;
|
|
36 |
import org.eclipse.cdt.core.dom.ast.DOMException;
|
|
37 |
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
|
|
38 |
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
|
|
39 |
import org.eclipse.cdt.core.dom.ast.IASTFunctionStyleMacroParameter;
|
|
40 |
import org.eclipse.cdt.core.dom.ast.IASTImplicitName;
|
|
41 |
import org.eclipse.cdt.core.dom.ast.IASTImplicitNameOwner;
|
|
42 |
import org.eclipse.cdt.core.dom.ast.IASTName;
|
|
43 |
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
|
44 |
import org.eclipse.cdt.core.dom.ast.IASTNodeSelector;
|
|
45 |
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorFunctionStyleMacroDefinition;
|
|
46 |
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement;
|
|
47 |
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
|
48 |
import org.eclipse.cdt.core.dom.ast.IBinding;
|
|
49 |
import org.eclipse.cdt.core.dom.ast.IParameter;
|
|
50 |
import org.eclipse.cdt.core.dom.ast.IProblemBinding;
|
|
51 |
import org.eclipse.cdt.core.dom.ast.IType;
|
|
52 |
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration;
|
|
53 |
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
|
|
54 |
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
|
|
55 |
import org.eclipse.cdt.core.dom.ast.cpp.ICPPParameter;
|
|
56 |
import org.eclipse.cdt.core.dom.ast.cpp.ICPPSpecialization;
|
|
57 |
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameter;
|
|
58 |
import org.eclipse.cdt.core.dom.ast.cpp.ICPPUsingDeclaration;
|
|
59 |
import org.eclipse.cdt.core.index.IIndex;
|
|
60 |
import org.eclipse.cdt.core.index.IIndexBinding;
|
|
61 |
import org.eclipse.cdt.core.index.IIndexMacro;
|
|
62 |
import org.eclipse.cdt.core.index.IIndexManager;
|
|
63 |
import org.eclipse.cdt.core.index.IIndexName;
|
|
64 |
import org.eclipse.cdt.core.index.IndexFilter;
|
|
65 |
import org.eclipse.cdt.core.model.ICElement;
|
|
66 |
import org.eclipse.cdt.core.model.ICProject;
|
|
67 |
import org.eclipse.cdt.core.model.ILanguage;
|
|
68 |
import org.eclipse.cdt.core.model.ISourceRange;
|
|
69 |
import org.eclipse.cdt.core.model.ISourceReference;
|
|
70 |
import org.eclipse.cdt.core.model.ITranslationUnit;
|
|
71 |
import org.eclipse.cdt.core.model.util.CElementBaseLabels;
|
|
72 |
import org.eclipse.cdt.core.parser.util.ArrayUtil;
|
|
73 |
import org.eclipse.cdt.ui.CUIPlugin;
|
|
74 |
|
|
75 |
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
|
|
76 |
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPUnknownBinding;
|
|
77 |
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPSemantics;
|
|
78 |
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
|
|
79 |
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.LookupData;
|
|
80 |
import org.eclipse.cdt.internal.core.index.IIndexFragmentName;
|
|
81 |
import org.eclipse.cdt.internal.core.model.ASTCache.ASTRunnable;
|
|
82 |
import org.eclipse.cdt.internal.core.model.ext.CElementHandleFactory;
|
|
83 |
import org.eclipse.cdt.internal.core.model.ext.ICElementHandle;
|
|
84 |
|
|
85 |
import org.eclipse.cdt.internal.ui.actions.OpenActionUtil;
|
|
86 |
import org.eclipse.cdt.internal.ui.editor.ASTProvider;
|
|
87 |
import org.eclipse.cdt.internal.ui.editor.CEditorMessages;
|
|
88 |
import org.eclipse.cdt.internal.ui.viewsupport.IndexUI;
|
|
89 |
|
|
90 |
class OpenDeclarationsJob extends Job implements ASTRunnable {
|
|
91 |
|
|
92 |
private enum NameKind { REFERENCE, DECLARATION, USING_DECL, DEFINITION }
|
|
93 |
|
|
94 |
private final SelectionParseAction fAction;
|
|
95 |
private IProgressMonitor fMonitor;
|
|
96 |
private final ITranslationUnit fTranslationUnit;
|
|
97 |
private IIndex fIndex;
|
|
98 |
private final ITextSelection fTextSelection;
|
|
99 |
private final String fSelectedText;
|
|
100 |
|
|
101 |
OpenDeclarationsJob(SelectionParseAction action, ITranslationUnit editorInput, ITextSelection textSelection, String text) {
|
|
102 |
super(CEditorMessages.OpenDeclarations_dialog_title);
|
|
103 |
fAction= action;
|
|
104 |
fTranslationUnit= editorInput;
|
|
105 |
fTextSelection= textSelection;
|
|
106 |
fSelectedText= text;
|
|
107 |
}
|
|
108 |
|
|
109 |
@Override
|
|
110 |
protected IStatus run(IProgressMonitor monitor) {
|
|
111 |
try {
|
|
112 |
return performNavigation(monitor);
|
|
113 |
} catch (CoreException e) {
|
|
114 |
return e.getStatus();
|
|
115 |
}
|
|
116 |
}
|
|
117 |
|
|
118 |
IStatus performNavigation(IProgressMonitor monitor) throws CoreException {
|
|
119 |
fAction.clearStatusLine();
|
|
120 |
|
|
121 |
assert fIndex == null;
|
|
122 |
if (fIndex != null)
|
|
123 |
return Status.CANCEL_STATUS;
|
|
124 |
|
|
125 |
fMonitor= monitor;
|
|
126 |
fIndex= CCorePlugin.getIndexManager().getIndex(fTranslationUnit.getCProject(),
|
|
127 |
IIndexManager.ADD_DEPENDENCIES | IIndexManager.ADD_DEPENDENT);
|
|
128 |
|
|
129 |
try {
|
|
130 |
fIndex.acquireReadLock();
|
|
131 |
} catch (InterruptedException e) {
|
|
132 |
return Status.CANCEL_STATUS;
|
|
133 |
}
|
|
134 |
|
|
135 |
try {
|
|
136 |
return ASTProvider.getASTProvider().runOnAST(fTranslationUnit, ASTProvider.WAIT_ACTIVE_ONLY, monitor, this);
|
|
137 |
} finally {
|
|
138 |
fIndex.releaseReadLock();
|
|
139 |
}
|
|
140 |
}
|
|
141 |
|
|
142 |
public IStatus runOnAST(ILanguage lang, IASTTranslationUnit ast) throws CoreException {
|
|
143 |
if (ast == null) {
|
|
144 |
return Status.OK_STATUS;
|
|
145 |
}
|
|
146 |
int selectionStart = fTextSelection.getOffset();
|
|
147 |
int selectionLength = fTextSelection.getLength();
|
|
148 |
|
|
149 |
final IASTNodeSelector nodeSelector = ast.getNodeSelector(null);
|
|
150 |
|
|
151 |
IASTName sourceName= nodeSelector.findEnclosingName(selectionStart, selectionLength);
|
|
152 |
if (sourceName == null) {
|
|
153 |
IASTName implicit = nodeSelector.findEnclosingImplicitName(selectionStart, selectionLength);
|
|
154 |
if (implicit != null) {
|
|
155 |
IASTImplicitNameOwner owner = (IASTImplicitNameOwner) implicit.getParent();
|
|
156 |
IASTImplicitName[] implicits = owner.getImplicitNames();
|
|
157 |
// There may be more than one name in the same spot
|
|
158 |
if (implicits.length > 0) {
|
|
159 |
List<IName> allNames = new ArrayList<IName>();
|
|
160 |
for (IASTImplicitName name : implicits) {
|
|
161 |
if (((ASTNode) name).getOffset() == ((ASTNode) implicit).getOffset()) {
|
|
162 |
IBinding binding = name.resolveBinding(); // guaranteed to resolve
|
|
163 |
IName[] declNames = findDeclNames(ast, NameKind.REFERENCE, binding);
|
|
164 |
allNames.addAll(Arrays.asList(declNames));
|
|
165 |
}
|
|
166 |
}
|
|
167 |
if (navigateViaCElements(fTranslationUnit.getCProject(), fIndex, allNames.toArray(new IName[0])))
|
|
168 |
return Status.OK_STATUS;
|
|
169 |
}
|
|
170 |
}
|
|
171 |
} else {
|
|
172 |
boolean found= false;
|
|
173 |
final IASTNode parent = sourceName.getParent();
|
|
174 |
if (parent instanceof IASTPreprocessorIncludeStatement) {
|
|
175 |
openInclude(((IASTPreprocessorIncludeStatement) parent));
|
|
176 |
return Status.OK_STATUS;
|
|
177 |
}
|
|
178 |
NameKind kind = getNameKind(sourceName);
|
|
179 |
IBinding b = sourceName.resolveBinding();
|
|
180 |
IBinding[] bindings = new IBinding[] { b };
|
|
181 |
if (b instanceof IProblemBinding) {
|
|
182 |
IBinding[] candidateBindings = ((IProblemBinding) b).getCandidateBindings();
|
|
183 |
if (candidateBindings.length != 0) {
|
|
184 |
bindings = candidateBindings;
|
|
185 |
}
|
|
186 |
} else if (kind == NameKind.DEFINITION && b instanceof IType) {
|
|
187 |
// Don't navigate away from a type definition.
|
|
188 |
// Select the name at the current location instead.
|
|
189 |
navigateToName(sourceName);
|
|
190 |
return Status.OK_STATUS;
|
|
191 |
}
|
|
192 |
IName[] declNames = null;
|
|
193 |
String filename = ast.getFilePath();
|
|
194 |
for (IBinding binding : bindings) {
|
|
195 |
if (binding != null && !(binding instanceof IProblemBinding)) {
|
|
196 |
IName[] names = findDeclNames(ast, kind, binding);
|
|
197 |
for (int i = 0; i < names.length; i++) {
|
|
198 |
if (names[i] instanceof IIndexName &&
|
|
199 |
filename.equals(((IIndexName) names[i]).getFileLocation().getFileName())) {
|
|
200 |
// Exclude index names from the current file.
|
|
201 |
names[i] = null;
|
|
202 |
} else if (isSameName(names[i], sourceName)) {
|
|
203 |
// Exclude the current location.
|
|
204 |
names[i] = null;
|
|
205 |
} else if (binding instanceof IParameter) {
|
|
206 |
if (!isInSameFunction(sourceName, names[i])) {
|
|
207 |
names[i] = null;
|
|
208 |
}
|
|
209 |
} else if (binding instanceof ICPPTemplateParameter) {
|
|
210 |
if (!isInSameTemplate(sourceName, names[i])) {
|
|
211 |
names[i] = null;
|
|
212 |
}
|
|
213 |
}
|
|
214 |
}
|
|
215 |
compact(names);
|
|
216 |
if (declNames == null) {
|
|
217 |
declNames = names;
|
|
218 |
} else {
|
|
219 |
declNames = (IName[]) ArrayUtil.addAll(IName.class, declNames, names);
|
|
220 |
}
|
|
221 |
}
|
|
222 |
}
|
|
223 |
declNames = (IName[]) ArrayUtil.removeNulls(IName.class, declNames);
|
|
224 |
|
|
225 |
if (navigateViaCElements(fTranslationUnit.getCProject(), fIndex, declNames)) {
|
|
226 |
found= true;
|
|
227 |
} else {
|
|
228 |
// Leave old method as fallback for local variables, parameters and
|
|
229 |
// everything else not covered by ICElementHandle.
|
|
230 |
found = navigateOneLocation(declNames);
|
|
231 |
}
|
|
232 |
if (!found && !navigationFallBack(ast, sourceName, kind)) {
|
|
233 |
fAction.reportSymbolLookupFailure(new String(sourceName.toCharArray()));
|
|
234 |
}
|
|
235 |
return Status.OK_STATUS;
|
|
236 |
}
|
|
237 |
|
|
238 |
// No enclosing name, check if we're in an include statement
|
|
239 |
IASTNode node= nodeSelector.findEnclosingNode(selectionStart, selectionLength);
|
|
240 |
if (node instanceof IASTPreprocessorIncludeStatement) {
|
|
241 |
openInclude((IASTPreprocessorIncludeStatement) node);
|
|
242 |
return Status.OK_STATUS;
|
|
243 |
} else if (node instanceof IASTPreprocessorFunctionStyleMacroDefinition) {
|
|
244 |
IASTPreprocessorFunctionStyleMacroDefinition mdef= (IASTPreprocessorFunctionStyleMacroDefinition) node;
|
|
245 |
for (IASTFunctionStyleMacroParameter par: mdef.getParameters()) {
|
|
246 |
String parName= par.getParameter();
|
|
247 |
if (parName.equals(fSelectedText)) {
|
|
248 |
if (navigateToLocation(par.getFileLocation())) {
|
|
249 |
return Status.OK_STATUS;
|
|
250 |
}
|
|
251 |
}
|
|
252 |
}
|
|
253 |
}
|
|
254 |
if (!navigationFallBack(ast, null, NameKind.REFERENCE)) {
|
|
255 |
fAction.reportSelectionMatchFailure();
|
|
256 |
}
|
|
257 |
return Status.OK_STATUS;
|
|
258 |
}
|
|
259 |
|
|
260 |
private IName[] findDeclNames(IASTTranslationUnit ast, NameKind kind, IBinding binding) throws CoreException {
|
|
261 |
IName[] declNames = findNames(fIndex, ast, kind, binding);
|
|
262 |
if (declNames.length == 0) {
|
|
263 |
if (binding instanceof ICPPSpecialization) {
|
|
264 |
// bug 207320, handle template instances
|
|
265 |
IBinding specialized= ((ICPPSpecialization) binding).getSpecializedBinding();
|
|
266 |
if (specialized != null && !(specialized instanceof IProblemBinding)) {
|
|
267 |
declNames = findNames(fIndex, ast, NameKind.DEFINITION, specialized);
|
|
268 |
}
|
|
269 |
} else if (binding instanceof ICPPMethod) {
|
|
270 |
// bug 86829, handle implicit methods.
|
|
271 |
ICPPMethod method= (ICPPMethod) binding;
|
|
272 |
if (method.isImplicit()) {
|
|
273 |
try {
|
|
274 |
IBinding clsBinding= method.getClassOwner();
|
|
275 |
if (clsBinding != null && !(clsBinding instanceof IProblemBinding)) {
|
|
276 |
declNames= findNames(fIndex, ast, NameKind.REFERENCE, clsBinding);
|
|
277 |
}
|
|
278 |
} catch (DOMException e) {
|
|
279 |
// don't log problem bindings.
|
|
280 |
}
|
|
281 |
}
|
|
282 |
}
|
|
283 |
}
|
|
284 |
return declNames;
|
|
285 |
}
|
|
286 |
|
|
287 |
private IName[] findNames(IIndex index, IASTTranslationUnit ast, NameKind kind, IBinding binding) throws CoreException {
|
|
288 |
IName[] declNames;
|
|
289 |
if (kind == NameKind.DEFINITION) {
|
|
290 |
declNames= findDeclarations(index, ast, binding);
|
|
291 |
} else {
|
|
292 |
declNames= findDefinitions(index, ast, kind, binding);
|
|
293 |
}
|
|
294 |
|
|
295 |
if (declNames.length == 0) {
|
|
296 |
if (kind == NameKind.DEFINITION) {
|
|
297 |
declNames= findDefinitions(index, ast, kind, binding);
|
|
298 |
} else {
|
|
299 |
declNames= findDeclarations(index, ast, binding);
|
|
300 |
}
|
|
301 |
}
|
|
302 |
return declNames;
|
|
303 |
}
|
|
304 |
|
|
305 |
private IName[] findDefinitions(IIndex index, IASTTranslationUnit ast, NameKind kind, IBinding binding) throws CoreException {
|
|
306 |
List<IASTName> declNames= new ArrayList<IASTName>();
|
|
307 |
declNames.addAll(Arrays.asList(ast.getDefinitionsInAST(binding)));
|
|
308 |
for (Iterator<IASTName> i = declNames.iterator(); i.hasNext();) {
|
|
309 |
IASTName name= i.next();
|
|
310 |
if (name.resolveBinding() instanceof ICPPUsingDeclaration) {
|
|
311 |
i.remove();
|
|
312 |
}
|
|
313 |
}
|
|
314 |
if (!declNames.isEmpty()) {
|
|
315 |
return declNames.toArray(new IASTName[declNames.size()]);
|
|
316 |
}
|
|
317 |
|
|
318 |
// 2. Try definition in index
|
|
319 |
return index.findNames(binding, IIndex.FIND_DEFINITIONS | IIndex.SEARCH_ACROSS_LANGUAGE_BOUNDARIES);
|
|
320 |
}
|
|
321 |
|
|
322 |
private IName[] findDeclarations(IIndex index, IASTTranslationUnit ast,
|
|
323 |
IBinding binding) throws CoreException {
|
|
324 |
IName[] declNames= ast.getDeclarationsInAST(binding);
|
|
325 |
for (int i = 0; i < declNames.length; i++) {
|
|
326 |
IName name = declNames[i];
|
|
327 |
if (name.isDefinition())
|
|
328 |
declNames[i]= null;
|
|
329 |
}
|
|
330 |
declNames= (IName[]) ArrayUtil.removeNulls(IName.class, declNames);
|
|
331 |
if (declNames.length == 0) {
|
|
332 |
declNames= index.findNames(binding, IIndex.FIND_DECLARATIONS | IIndex.SEARCH_ACROSS_LANGUAGE_BOUNDARIES);
|
|
333 |
}
|
|
334 |
return declNames;
|
|
335 |
}
|
|
336 |
|
|
337 |
private static NameKind getNameKind(IName name) {
|
|
338 |
if (name.isDefinition()) {
|
|
339 |
if (getBinding(name) instanceof ICPPUsingDeclaration) {
|
|
340 |
return NameKind.USING_DECL;
|
|
341 |
} else {
|
|
342 |
return NameKind.DEFINITION;
|
|
343 |
}
|
|
344 |
} else if (name.isDeclaration()) {
|
|
345 |
return NameKind.DECLARATION;
|
|
346 |
}
|
|
347 |
return NameKind.REFERENCE;
|
|
348 |
}
|
|
349 |
|
|
350 |
private static IBinding getBinding(IName name) {
|
|
351 |
if (name instanceof IASTName) {
|
|
352 |
return ((IASTName) name).resolveBinding();
|
|
353 |
} else if (name instanceof IIndexFragmentName) {
|
|
354 |
try {
|
|
355 |
return ((IIndexFragmentName) name).getBinding();
|
|
356 |
} catch (CoreException e) {
|
|
357 |
// Fall through to return null.
|
|
358 |
}
|
|
359 |
}
|
|
360 |
return null;
|
|
361 |
}
|
|
362 |
|
|
363 |
/**
|
|
364 |
* Compacts an array by moving all <code>null</code> elements to the end.
|
|
365 |
* @param array
|
|
366 |
*/
|
|
367 |
private void compact(Object[] array) {
|
|
368 |
for (int i = 0, j = 0; i < array.length; i++) {
|
|
369 |
if (array[i] != null) {
|
|
370 |
if (i != j) {
|
|
371 |
array[j] = array[i];
|
|
372 |
array[i] = null;
|
|
373 |
}
|
|
374 |
j++;
|
|
375 |
}
|
|
376 |
}
|
|
377 |
}
|
|
378 |
|
|
379 |
private boolean isSameName(IName n1, IName n2) {
|
128
|
380 |
if (n1 == n2)
|
|
381 |
return true;
|
|
382 |
|
37
|
383 |
IASTFileLocation loc1 = n1.getFileLocation();
|
|
384 |
IASTFileLocation loc2 = n2.getFileLocation();
|
128
|
385 |
if (loc1 == null || loc2 == null) {
|
|
386 |
return false;
|
|
387 |
}
|
|
388 |
return loc1.getFileName().equals(loc2.getFileName())
|
|
389 |
&& loc1.getNodeOffset() == loc2.getNodeOffset()
|
|
390 |
&& loc1.getNodeLength() == loc2.getNodeLength();}
|
37
|
391 |
|
|
392 |
private static boolean isInSameFunction(IASTName name1, IName name2) {
|
|
393 |
IASTDeclaration decl1 = getEnclosingDeclaration(name1);
|
|
394 |
IASTDeclaration decl2 = name2 instanceof IASTName ? getEnclosingDeclaration((IASTName) name2) : null;
|
|
395 |
return decl1 != null && decl1.equals(decl2) || decl1 == null && decl2 == null;
|
|
396 |
}
|
|
397 |
|
|
398 |
private static IASTDeclaration getEnclosingDeclaration(IASTNode node) {
|
|
399 |
while (node != null && !(node instanceof IASTDeclaration)) {
|
|
400 |
node= node.getParent();
|
|
401 |
}
|
|
402 |
return (IASTDeclaration) node;
|
|
403 |
}
|
|
404 |
|
|
405 |
private static boolean isInSameTemplate(IASTName name1, IName name2) {
|
|
406 |
IASTDeclaration decl1 = getEnclosingTemplateDeclaration(name1);
|
|
407 |
IASTDeclaration decl2 = name2 instanceof IASTName ?
|
|
408 |
getEnclosingTemplateDeclaration((IASTName) name2) : null;
|
|
409 |
return decl1 != null && decl1.equals(decl2) || decl1 == null && decl2 == null;
|
|
410 |
}
|
|
411 |
|
|
412 |
private static IASTDeclaration getEnclosingTemplateDeclaration(IASTNode node) {
|
|
413 |
while (node != null && !(node instanceof ICPPASTTemplateDeclaration)) {
|
|
414 |
node= node.getParent();
|
|
415 |
}
|
|
416 |
return (IASTDeclaration) node;
|
|
417 |
}
|
|
418 |
|
|
419 |
private void convertToCElements(ICProject project, IIndex index, IName[] declNames, List<ICElement> elements) {
|
|
420 |
for (IName declName : declNames) {
|
|
421 |
try {
|
|
422 |
ICElement elem = getCElementForName(project, index, declName);
|
|
423 |
if (elem instanceof ISourceReference) {
|
|
424 |
elements.add(elem);
|
|
425 |
}
|
|
426 |
} catch (CoreException e) {
|
|
427 |
CUIPlugin.log(e);
|
|
428 |
}
|
|
429 |
}
|
|
430 |
}
|
|
431 |
|
|
432 |
private ICElementHandle getCElementForName(ICProject project, IIndex index, IName declName) throws CoreException {
|
|
433 |
if (declName instanceof IIndexName) {
|
|
434 |
return IndexUI.getCElementForName(project, index, (IIndexName) declName);
|
|
435 |
}
|
|
436 |
if (declName instanceof IASTName) {
|
|
437 |
IASTName astName = (IASTName) declName;
|
|
438 |
IBinding binding= astName.resolveBinding();
|
|
439 |
if (binding != null) {
|
|
440 |
ITranslationUnit tu= IndexUI.getTranslationUnit(project, astName);
|
|
441 |
if (tu != null) {
|
|
442 |
IASTFileLocation loc= astName.getFileLocation();
|
|
443 |
IRegion region= new Region(loc.getNodeOffset(), loc.getNodeLength());
|
|
444 |
return CElementHandleFactory.create(tu, binding, astName.isDefinition(), region, 0);
|
|
445 |
}
|
|
446 |
}
|
|
447 |
return null;
|
|
448 |
}
|
|
449 |
return null;
|
|
450 |
}
|
|
451 |
|
|
452 |
private boolean navigateViaCElements(ICProject project, IIndex index, IName[] declNames) {
|
|
453 |
final ArrayList<ICElement> elements= new ArrayList<ICElement>();
|
|
454 |
convertToCElements(project, index, declNames, elements);
|
|
455 |
return navigateCElements(elements);
|
|
456 |
}
|
|
457 |
|
|
458 |
private boolean navigateCElements(final List<ICElement> elements) {
|
|
459 |
if (elements.isEmpty()) {
|
|
460 |
return false;
|
|
461 |
}
|
|
462 |
|
|
463 |
runInUIThread(new Runnable() {
|
|
464 |
public void run() {
|
|
465 |
ISourceReference target= null;
|
|
466 |
if (elements.size() == 1) {
|
|
467 |
target= (ISourceReference) elements.get(0);
|
|
468 |
} else {
|
|
469 |
if (OpenDeclarationsAction.sIsJUnitTest) {
|
|
470 |
throw new RuntimeException("ambiguous input: " + elements.size()); //$NON-NLS-1$
|
|
471 |
}
|
|
472 |
ICElement[] elemArray= elements.toArray(new ICElement[elements.size()]);
|
|
473 |
target = (ISourceReference) OpenActionUtil.selectCElement(elemArray, fAction.getSite().getShell(),
|
|
474 |
CEditorMessages.OpenDeclarationsAction_dialog_title, CEditorMessages.OpenDeclarationsAction_selectMessage,
|
|
475 |
CElementBaseLabels.ALL_DEFAULT | CElementBaseLabels.ALL_FULLY_QUALIFIED | CElementBaseLabels.MF_POST_FILE_QUALIFIED, 0);
|
|
476 |
}
|
|
477 |
if (target != null) {
|
|
478 |
ITranslationUnit tu= target.getTranslationUnit();
|
|
479 |
ISourceRange sourceRange;
|
|
480 |
try {
|
|
481 |
sourceRange = target.getSourceRange();
|
|
482 |
if (tu != null && sourceRange != null) {
|
|
483 |
fAction.open(tu.getLocation(), sourceRange.getIdStartPos(), sourceRange.getIdLength());
|
|
484 |
}
|
|
485 |
} catch (CoreException e) {
|
|
486 |
CUIPlugin.log(e);
|
|
487 |
}
|
|
488 |
}
|
|
489 |
}
|
|
490 |
});
|
|
491 |
return true;
|
|
492 |
}
|
|
493 |
|
|
494 |
private boolean navigateOneLocation(IName[] names) {
|
|
495 |
for (IName name : names) {
|
|
496 |
if (navigateToName(name)) {
|
|
497 |
return true;
|
|
498 |
}
|
|
499 |
}
|
|
500 |
return false;
|
|
501 |
}
|
|
502 |
|
|
503 |
private boolean navigateToName(IName name) {
|
|
504 |
return navigateToLocation(name.getFileLocation());
|
|
505 |
}
|
|
506 |
|
|
507 |
private boolean navigateToLocation(IASTFileLocation fileloc) {
|
|
508 |
if (fileloc == null) {
|
|
509 |
return false;
|
|
510 |
}
|
|
511 |
final IPath path = new Path(fileloc.getFileName());
|
|
512 |
final int offset = fileloc.getNodeOffset();
|
|
513 |
final int length = fileloc.getNodeLength();
|
|
514 |
|
|
515 |
runInUIThread(new Runnable() {
|
|
516 |
public void run() {
|
|
517 |
try {
|
|
518 |
fAction.open(path, offset, length);
|
|
519 |
} catch (CoreException e) {
|
|
520 |
CUIPlugin.log(e);
|
|
521 |
}
|
|
522 |
}
|
|
523 |
});
|
|
524 |
return true;
|
|
525 |
}
|
|
526 |
|
|
527 |
private void runInUIThread(Runnable runnable) {
|
|
528 |
if (Display.getCurrent() != null) {
|
|
529 |
runnable.run();
|
|
530 |
} else {
|
|
531 |
Display.getDefault().asyncExec(runnable);
|
|
532 |
}
|
|
533 |
}
|
|
534 |
|
|
535 |
private void openInclude(IASTPreprocessorIncludeStatement incStmt) {
|
|
536 |
String name = null;
|
|
537 |
if (incStmt.isResolved())
|
|
538 |
name = incStmt.getPath();
|
|
539 |
|
|
540 |
if (name != null) {
|
|
541 |
final IPath path = new Path(name);
|
|
542 |
runInUIThread(new Runnable() {
|
|
543 |
public void run() {
|
|
544 |
try {
|
|
545 |
fAction.open(path, 0, 0);
|
|
546 |
} catch (CoreException e) {
|
|
547 |
CUIPlugin.log(e);
|
|
548 |
}
|
|
549 |
}
|
|
550 |
});
|
|
551 |
} else {
|
|
552 |
fAction.reportIncludeLookupFailure(new String(incStmt.getName().toCharArray()));
|
|
553 |
}
|
|
554 |
}
|
|
555 |
|
|
556 |
private boolean navigationFallBack(IASTTranslationUnit ast, IASTName sourceName, NameKind kind) {
|
|
557 |
// bug 102643, as a fall-back we look up the selected word in the index
|
|
558 |
if (fSelectedText != null && fSelectedText.length() > 0) {
|
|
559 |
try {
|
|
560 |
final ICProject project = fTranslationUnit.getCProject();
|
|
561 |
final char[] name = fSelectedText.toCharArray();
|
|
562 |
List<ICElement> elems= new ArrayList<ICElement>();
|
|
563 |
|
|
564 |
// bug 252549, search for names in the AST first
|
|
565 |
Set<IBinding> primaryBindings= new HashSet<IBinding>();
|
|
566 |
Set<IBinding> ignoreIndexBindings= new HashSet<IBinding>();
|
|
567 |
ASTNameCollector nc= new ASTNameCollector(fSelectedText);
|
|
568 |
ast.accept(nc);
|
|
569 |
IASTName[] candidates= nc.getNames();
|
|
570 |
for (IASTName astName : candidates) {
|
|
571 |
try {
|
|
572 |
IBinding b= astName.resolveBinding();
|
|
573 |
if (b != null && !(b instanceof IProblemBinding)) {
|
|
574 |
if (primaryBindings.add(b)) {
|
|
575 |
ignoreIndexBindings.add(fIndex.adaptBinding(b));
|
|
576 |
}
|
|
577 |
}
|
|
578 |
} catch (RuntimeException e) {
|
|
579 |
CUIPlugin.log(e);
|
|
580 |
}
|
|
581 |
}
|
|
582 |
|
|
583 |
// Search the index, also
|
|
584 |
final IndexFilter filter = IndexFilter.getDeclaredBindingFilter(ast.getLinkage().getLinkageID(), false);
|
|
585 |
final IIndexBinding[] idxBindings = fIndex.findBindings(name, false, filter, fMonitor);
|
|
586 |
for (IIndexBinding idxBinding : idxBindings) {
|
|
587 |
if (!ignoreIndexBindings.contains(idxBinding)) {
|
|
588 |
primaryBindings.add(idxBinding);
|
|
589 |
}
|
|
590 |
}
|
|
591 |
|
|
592 |
// Search for a macro in the index
|
|
593 |
IIndexMacro[] macros= fIndex.findMacros(name, filter, fMonitor);
|
|
594 |
for (IIndexMacro macro : macros) {
|
|
595 |
ICElement elem= IndexUI.getCElementForMacro(project, fIndex, macro);
|
|
596 |
if (elem != null) {
|
|
597 |
elems.add(elem);
|
|
598 |
}
|
|
599 |
}
|
|
600 |
|
|
601 |
Collection<IBinding> secondaryBindings= removeSecondaryBindings(primaryBindings, sourceName);
|
|
602 |
// Convert bindings to CElements
|
|
603 |
Collection<IBinding> bs= primaryBindings;
|
|
604 |
for (int k=0; k<2; k++) {
|
|
605 |
for (IBinding binding : bs) {
|
|
606 |
IName[] names = findNames(fIndex, ast, kind, binding);
|
|
607 |
// Exclude names of the same kind.
|
|
608 |
for (int i = 0; i < names.length; i++) {
|
|
609 |
if (getNameKind(names[i]) == kind) {
|
|
610 |
names[i] = null;
|
|
611 |
}
|
|
612 |
}
|
|
613 |
names = (IName[]) ArrayUtil.removeNulls(IName.class, names);
|
|
614 |
convertToCElements(project, fIndex, names, elems);
|
|
615 |
}
|
|
616 |
// in case we did not find anything, consider the secondary bindings
|
|
617 |
if (!elems.isEmpty())
|
|
618 |
break;
|
|
619 |
bs= secondaryBindings;
|
|
620 |
}
|
|
621 |
if (navigateCElements(elems)) {
|
|
622 |
return true;
|
|
623 |
}
|
|
624 |
if (sourceName != null && sourceName.isDeclaration()) {
|
|
625 |
// Select the name at the current location as the last resort.
|
|
626 |
return navigateToName(sourceName);
|
|
627 |
}
|
|
628 |
} catch (CoreException e) {
|
|
629 |
CUIPlugin.log(e);
|
|
630 |
}
|
|
631 |
}
|
|
632 |
return false;
|
|
633 |
}
|
|
634 |
|
|
635 |
private Collection<IBinding> removeSecondaryBindings(Set<IBinding> primaryBindings, IASTName sourceName) {
|
|
636 |
List<IBinding> result= new ArrayList<IBinding>();
|
|
637 |
String[] sourceQualifiedName= null;
|
|
638 |
int funcArgCount= -1;
|
|
639 |
if (sourceName != null) {
|
|
640 |
sourceQualifiedName= CPPVisitor.getQualifiedName(sourceName.resolveBinding());
|
|
641 |
if (sourceName.resolveBinding() instanceof ICPPUnknownBinding) {
|
|
642 |
LookupData data= CPPSemantics.createLookupData(sourceName, false);
|
|
643 |
if (data.functionCall()) {
|
|
644 |
funcArgCount= data.getFunctionArgumentCount();
|
|
645 |
}
|
|
646 |
}
|
|
647 |
}
|
|
648 |
|
|
649 |
for (Iterator<IBinding> iterator = primaryBindings.iterator(); iterator.hasNext();) {
|
|
650 |
IBinding binding = iterator.next();
|
|
651 |
if (sourceQualifiedName != null) {
|
|
652 |
String[] qualifiedName = CPPVisitor.getQualifiedName(binding);
|
|
653 |
if (!Arrays.equals(qualifiedName, sourceQualifiedName)) {
|
|
654 |
iterator.remove();
|
|
655 |
continue;
|
|
656 |
}
|
|
657 |
}
|
|
658 |
if (funcArgCount != -1) {
|
|
659 |
// for c++ we can check the number of parameters
|
|
660 |
if (binding instanceof ICPPFunction) {
|
|
661 |
ICPPFunction f= (ICPPFunction) binding;
|
|
662 |
try {
|
|
663 |
IParameter[] pars= f.getParameters();
|
|
664 |
if (pars.length < funcArgCount) {
|
|
665 |
if (!f.takesVarArgs()) {
|
|
666 |
iterator.remove();
|
|
667 |
result.add(binding);
|
|
668 |
continue;
|
|
669 |
}
|
|
670 |
} else if (pars.length > funcArgCount) {
|
|
671 |
IParameter p= pars[funcArgCount];
|
|
672 |
if (!(p instanceof ICPPParameter) || !((ICPPParameter) p).hasDefaultValue()) {
|
|
673 |
iterator.remove();
|
|
674 |
result.add(binding);
|
|
675 |
continue;
|
|
676 |
}
|
|
677 |
}
|
|
678 |
} catch (DOMException e) {
|
|
679 |
// ignore problem bindings
|
|
680 |
continue;
|
|
681 |
}
|
|
682 |
}
|
|
683 |
}
|
|
684 |
}
|
|
685 |
|
|
686 |
return result;
|
|
687 |
}
|
|
688 |
} |