Fixed: [ Issue 46 ] Use the location of workspace folders for import resolution https://code.google.com/p/protobuf-dt/issues/detail?id=46
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/FileResolverStrategies.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/FileResolverStrategies.java index 7a84aef..8b9c2b6 100644 --- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/FileResolverStrategies.java +++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/FileResolverStrategies.java
@@ -9,8 +9,8 @@ package com.google.eclipse.protobuf.ui.scoping; import static com.google.eclipse.protobuf.ui.preferences.paths.PathResolutionType.*; -import static com.google.inject.internal.Maps.newHashMap; +import java.util.HashMap; import java.util.Map; import com.google.eclipse.protobuf.ui.preferences.paths.PathResolutionType; @@ -24,11 +24,12 @@ @Singleton class FileResolverStrategies { - private final Map<PathResolutionType, FileResolverStrategy> strategies = newHashMap(); + private final Map<PathResolutionType, FileResolverStrategy> strategies = + new HashMap<PathResolutionType, FileResolverStrategy>(); - @Inject FileResolverStrategies(Resources resources) { + @Inject FileResolverStrategies(PathMapping mapping, Resources resources) { strategies.put(SINGLE_DIRECTORY, new SingleDirectoryFileResolver(resources)); - strategies.put(MULTIPLE_DIRECTORIES, new MultipleDirectoriesFileResolver(resources)); + strategies.put(MULTIPLE_DIRECTORIES, new MultipleDirectoriesFileResolver(mapping, resources)); } FileResolverStrategy strategyFor(PathResolutionType type) {
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/MultipleDirectoriesFileResolver.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/MultipleDirectoriesFileResolver.java index f07108f..0e5c309 100644 --- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/MultipleDirectoriesFileResolver.java +++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/MultipleDirectoriesFileResolver.java
@@ -8,6 +8,8 @@ */ package com.google.eclipse.protobuf.ui.scoping; +import java.util.List; + import org.eclipse.core.filesystem.*; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; @@ -22,35 +24,44 @@ */ class MultipleDirectoriesFileResolver implements FileResolverStrategy { + private final PathMapping mapping; private final Resources resources; - MultipleDirectoriesFileResolver(Resources resources) { + MultipleDirectoriesFileResolver(PathMapping mapping, Resources resources) { + this.mapping = mapping; this.resources = resources; } /** {@inheritDoc} */ public String resolveUri(String importUri, URI declaringResourceUri, PathsPreferences preferences) { - for (DirectoryPath directoryPath : preferences.directoryPaths()) { + List<DirectoryPath> directoryPaths = preferences.directoryPaths(); + for (DirectoryPath directoryPath : directoryPaths) { String resolved = resolveUri(importUri, directoryPath); if (resolved != null) return resolved; } + for (DirectoryPath directoryPath : directoryPaths) { + if (!directoryPath.isWorkspacePath()) continue; + String resolved = resolveUriFileSystem(importUri, mapping.folderLocation(directoryPath.value())); + if (resolved != null) return resolved; + } return null; } - private String resolveUri(String importUri, DirectoryPath directoryPath) { - if (directoryPath.isWorkspacePath()) return resolveUriInWorkspace(importUri, directoryPath.value()); - return resolveUriFileSystem(importUri, directoryPath.value()); + private String resolveUri(String importUri, DirectoryPath importRoot) { + String root = importRoot.value(); + if (importRoot.isWorkspacePath()) return resolveUriInWorkspace(importUri, root); + return resolveUriFileSystem(importUri, root); } - private String resolveUriInWorkspace(String importUri, String directoryPath) { - String path = PREFIX + directoryPath + SEPARATOR + importUri; + private String resolveUriInWorkspace(String importUri, String importRoot) { + String path = PREFIX + importRoot + SEPARATOR + importUri; boolean exists = resources.fileExists(URI.createURI(path)); return (exists) ? path : null; } - private String resolveUriFileSystem(String importUri, String directoryPath) { + private String resolveUriFileSystem(String importUri, String importRoot) { IFileSystem fileSystem = EFS.getLocalFileSystem(); - IPath path = new Path(directoryPath + SEPARATOR + importUri); + IPath path = new Path(importRoot + SEPARATOR + importUri); IFileInfo fileInfo = fileSystem.getStore(path).fetchInfo(); if (!fileInfo.exists()) return null; return URIUtil.toURI(path).toString();
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/PathMapping.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/PathMapping.java new file mode 100644 index 0000000..4bcab10 --- /dev/null +++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/PathMapping.java
@@ -0,0 +1,38 @@ +/* + * Copyright (c) 2011 Google Inc. + * + * 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 + * + * http://www.eclipse.org/legal/epl-v10.html + */ +package com.google.eclipse.protobuf.ui.scoping; + +import org.eclipse.core.resources.*; +import org.eclipse.core.runtime.Path; +import org.eclipse.xtext.util.SimpleCache; + +import com.google.common.base.Function; + +/** + * @author alruiz@google.com (Alex Ruiz) + */ +class PathMapping { + + private final SimpleCache<String, String> folderPathMapping = new SimpleCache<String, String>(new FolderPathMapper()); + + String folderLocation(String workspacePath) { + return folderPathMapping.get(workspacePath); + } + + private static class FolderPathMapper implements Function<String, String> { + public String apply(String workspacePath) { + return folder(workspacePath).getLocation().toOSString(); + } + + private static IFolder folder(String workspacePath) { + IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); + return root.getFolder(new Path(workspacePath)); + } + } +}