From 1d25bf5abce25919c43b8a916b6841e8c4b6f24b Mon Sep 17 00:00:00 2001 From: Anton Leherbauer Date: Thu, 24 Jan 2008 09:18:08 +0000 Subject: Fix for 216186: [patch] EditorUtility.getWorkspaceFileAtLocation gets wrong IFile in some cases --- .../cdt/internal/ui/util/EditorUtility.java | 62 ++++++++++++---------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/EditorUtility.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/EditorUtility.java index 134badda1e6..247fbc1e371 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/EditorUtility.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/EditorUtility.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2007 QNX Software Systems and others. + * Copyright (c) 2000, 2008 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 @@ -19,7 +19,6 @@ import java.io.File; import java.io.IOException; import java.text.MessageFormat; -import org.eclipse.core.filebuffers.FileBuffers; import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; @@ -343,37 +342,46 @@ public class EditorUtility { * @return an IFile or null */ private static IFile getWorkspaceFileAtLocation(IPath location, ICElement context) { - IFile file= FileBuffers.getWorkspaceFileAtLocation(location); - if (file == null) { - // try to find a linked resource - IProject project= null; - if (context != null) { - ICProject cProject= context.getCProject(); - if (cProject != null) { - project= cProject.getProject(); - } + IProject project= null; + if (context != null) { + ICProject cProject= context.getCProject(); + if (cProject != null) { + project= cProject.getProject(); } - IFile bestMatch= null; - IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot(); - IFile[] files= root.findFilesForLocation(location); - for (int i= 0; i < files.length; i++) { - file= files[i]; - if (file.isAccessible()) { - if (project != null && file.getProject() == project) { - bestMatch= file; + } + IFile bestMatch= null; + IFile secondBestMatch= null; + IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot(); + IFile[] files= root.findFilesForLocation(location); + for (int i= 0; i < files.length; i++) { + IFile file= files[i]; + if (file.isAccessible()) { + if (project != null && file.getProject().equals(project)) { + bestMatch= file; + break; + } else if (CoreModel.hasCNature(file.getProject())) { + bestMatch= file; + if (project == null) { break; } - if (bestMatch == null) { - bestMatch= file; - if (project == null) { - break; - } - } + } else { + // match in non-CDT project + secondBestMatch= file; + } + } + } + bestMatch= bestMatch != null ? bestMatch : secondBestMatch; + if (bestMatch == null) { + // try workspace relative path + if (location.segmentCount() >= 2) { + // @see IContainer#getFile for the required number of segments + IFile file= root.getFile(location); + if (file != null && file.isAccessible()) { + bestMatch= file; } } - return bestMatch; } - return file; + return bestMatch; } /** -- cgit v1.2.3