diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/CoreModelUtil.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/CoreModelUtil.java index 2f0801a2f38..90fed85f2e8 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/CoreModelUtil.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/CoreModelUtil.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2002, 2009 QNX Software Systems and others. + * Copyright (c) 2002, 2010 QNX Software Systems and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -550,6 +550,7 @@ public class CoreModelUtil { */ public static ITranslationUnit findTranslationUnitForLocation(IPath location, ICProject preferredProject) throws CModelException { IFile[] files= ResourceLookup.findFilesForLocation(location); + ResourceLookup.sortFilesByRelevance(files, preferredProject != null ? preferredProject.getProject() : null); boolean oneExisting= false; if (files.length > 0) { for (IFile file : files) { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/includebrowser/IBViewPart.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/includebrowser/IBViewPart.java index 777811ca5d3..2ae58cf8759 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/includebrowser/IBViewPart.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/includebrowser/IBViewPart.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2008 Wind River Systems, Inc. and others. + * Copyright (c) 2006, 2010 Wind River Systems, Inc. and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -84,6 +84,7 @@ import org.eclipse.cdt.core.index.IIndexFileLocation; import org.eclipse.cdt.core.index.IndexLocationFactory; import org.eclipse.cdt.core.model.CModelException; import org.eclipse.cdt.core.model.CoreModel; +import org.eclipse.cdt.core.model.CoreModelUtil; import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ICProject; import org.eclipse.cdt.core.model.ITranslationUnit; @@ -221,15 +222,27 @@ public class IBViewPart extends ViewPart index.acquireReadLock(); try { if (!IndexUI.isIndexed(index, input)) { - final String msg = IndexUI.getFileNotIndexedMessage(input); - display.asyncExec(new Runnable() { - public void run() { - if (fTreeViewer.getInput() == input) { - setMessage(msg); - fTreeViewer.setInput(null); + // Bug 306879: Try to find an alternative translation unit for the file by the location. + final ITranslationUnit alt= CoreModelUtil.findTranslationUnitForLocation(input.getLocation(), input.getCProject()); + if (alt != null && IndexUI.isIndexed(index, alt)) { + display.asyncExec(new Runnable() { + public void run() { + if (fTreeViewer.getInput() == input) { + setInput(alt); + } } - } - }); + }); + } else { + final String msg = IndexUI.getFileNotIndexedMessage(input); + display.asyncExec(new Runnable() { + public void run() { + if (fTreeViewer.getInput() == input) { + setMessage(msg); + fTreeViewer.setInput(null); + } + } + }); + } } return Status.OK_STATUS; } finally { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/IndexUI.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/IndexUI.java index d64e606dc72..12e793bfda3 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/IndexUI.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/IndexUI.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2009 Wind River Systems, Inc. and others. + * Copyright (c) 2007, 2010 Wind River Systems, Inc. and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -33,7 +33,6 @@ import org.eclipse.ui.IEditorInput; import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.IPositionConverter; -import org.eclipse.cdt.core.dom.IName; import org.eclipse.cdt.core.dom.ast.DOMException; import org.eclipse.cdt.core.dom.ast.IASTDeclarator; import org.eclipse.cdt.core.dom.ast.IASTExpression; @@ -329,10 +328,19 @@ public class IndexUI { return null; } - public static ITranslationUnit getTranslationUnit(ICProject cproject, IName name) { + public static ITranslationUnit getTranslationUnit(ICProject cproject, IASTName name) { return getTranslationUnit(cproject, name.getFileLocation()); } + public static ITranslationUnit getTranslationUnit(ICProject cproject, IIndexName name) { + try { + return CoreModelUtil.findTranslationUnitForLocation(name.getFile().getLocation(), cproject); + } catch (CoreException e) { + CUIPlugin.log(e); + } + return null; + } + private static ITranslationUnit getTranslationUnit(ICProject cproject, final IASTFileLocation fileLocation) { if (fileLocation != null) { IPath path= Path.fromOSString(fileLocation.getFileName());