1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-09 10:46:02 +02:00

Bug 167162 - Change Open Declarations to pick definitions first.

This commit is contained in:
Doug Schaefer 2006-12-07 21:21:50 +00:00
parent b11cd377d4
commit 3dfef1cc05

View file

@ -13,19 +13,26 @@
package org.eclipse.cdt.internal.ui.search.actions; package org.eclipse.cdt.internal.ui.search.actions;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job; import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.text.ITextSelection; import org.eclipse.jface.text.ITextSelection;
import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Display;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.IName; import org.eclipse.cdt.core.dom.IName;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IProblemBinding; import org.eclipse.cdt.core.dom.ast.IProblemBinding;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexManager;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.model.IWorkingCopy; import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.CUIPlugin;
@ -60,56 +67,64 @@ public class OpenDeclarationsAction extends SelectionParseAction {
if (workingCopy == null) if (workingCopy == null)
return Status.CANCEL_STATUS; return Status.CANCEL_STATUS;
int style = 0; IIndex index = CCorePlugin.getIndexManager().getIndex(workingCopy.getCProject(),
// IPDOM pdom = CCorePlugin.getPDOMManager().getPDOM(workingCopy.getCProject()); IIndexManager.ADD_DEPENDENCIES | IIndexManager.ADD_DEPENDENT);
// if (!pdom.isEmpty())
// style |= ITranslationUnit.AST_SKIP_ALL_HEADERS; try {
IASTTranslationUnit ast = workingCopy.getAST(null, style); index.acquireReadLock();
IASTName[] selectedNames = workingCopy.getLanguage().getSelectedNames(ast, selectionStart, selectionLength); } catch (InterruptedException e) {
return Status.CANCEL_STATUS;
}
try {
IASTTranslationUnit ast = workingCopy.getAST(null, ITranslationUnit.AST_SKIP_ALL_HEADERS);
IASTName[] selectedNames = workingCopy.getLanguage().getSelectedNames(ast, selectionStart, selectionLength);
if (selectedNames.length > 0 && selectedNames[0] != null) { // just right, only one name selected if (selectedNames.length > 0 && selectedNames[0] != null) { // just right, only one name selected
IASTName searchName = selectedNames[0]; IASTName searchName = selectedNames[0];
IBinding binding = searchName.resolveBinding(); IBinding binding = searchName.resolveBinding();
if (binding != null && !(binding instanceof IProblemBinding)) { if (binding != null && !(binding instanceof IProblemBinding)) {
final IName[] declNames = ast.getDeclarations(binding); // 1. Try definition
if (declNames.length > 0) { IName[] declNames = ast.getDefinitions(binding);
runInUIThread(new Runnable() {
public void run() { if (declNames.length == 0) {
try { // 2. Try definition
open(declNames[0]); declNames = index.findDefinitions(binding);
} catch (CoreException e) {
CUIPlugin.getDefault().log(e); if (declNames.length == 0) {
// 3. Try declaration in TU
declNames = ast.getDeclarations(binding);
if (declNames.length == 0) {
// 4. Try declaration in Index
declNames = index.findDeclarations(binding);
} }
} }
}); }
}
// mstodo revisit if (declNames.length > 0) {
// else if (binding instanceof IIndexBinding) { IASTFileLocation fileloc = declNames[0].getFileLocation();
// IIndexBinding pdomBinding = (IIndexBinding)binding; if (fileloc != null) {
// IName name = pdomBinding.getFirstDefinition(); final IPath path = new Path(fileloc.getFileName());
// if (name == null) final int offset = fileloc.getNodeOffset();
// name = pdomBinding.getFirstDeclaration(); final int length = fileloc.getNodeLength();
// // no source location - TODO spit out an error in the status bar
// if (name != null) { runInUIThread(new Runnable() {
// IASTFileLocation fileloc = name.getFileLocation(); public void run() {
// if (fileloc != null) { try {
// final IPath path = new Path(fileloc.getFileName()); open(path, offset, length);
// final int offset = fileloc.getNodeOffset(); } catch (CoreException e) {
// final int length = fileloc.getNodeLength(); CUIPlugin.getDefault().log(e);
// Display.getDefault().asyncExec(new Runnable() { }
// public void run() { }
// try { });
// open(path, offset, length); }
// } catch (CoreException e) { }
// CUIPlugin.getDefault().log(e); }
// }
// }
// });
// }
// }
// }
} }
} catch (CoreException e) {
CUIPlugin.getDefault().log(e);
} }
return Status.OK_STATUS; return Status.OK_STATUS;