In progress: [ Issue 40 ] Add support for import resolution across multiple folders
https://code.google.com/p/protobuf-dt/issues/detail?id=40
Looks like it is working. Needs clean up and testing.
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/scoping/FileUriResolver_resolveUri_Test.java b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/scoping/FileUriResolver_resolveUri_Test.java
index bd05dac..7cba64e 100644
--- a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/scoping/FileUriResolver_resolveUri_Test.java
+++ b/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/scoping/FileUriResolver_resolveUri_Test.java
@@ -85,7 +85,7 @@
}
private void callStubs(FileResolutionType type, boolean resolvedUriExists) {
- when(resources.project(resource)).thenReturn(project);
+ when(resources.project(resource.getURI())).thenReturn(project);
when(preferenceReader.readFromPrefereceStore(project)).thenReturn(preferences);
when(preferences.fileResolutionType()).thenReturn(type);
when(resources.fileExists(any(URI.class))).thenReturn(resolvedUriExists);
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/FileUriResolver.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/FileUriResolver.java
index e5e6491..d3659fa 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/FileUriResolver.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/FileUriResolver.java
@@ -15,6 +15,8 @@
import java.util.ArrayList;
import java.util.List;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.util.Pair;
@@ -29,6 +31,7 @@
*/
public class FileUriResolver implements IFileUriResolver {
+ private static final String PREFIX = "platform:/resource";
private static final String SEPARATOR = "/";
@Inject private PreferenceReader preferenceReader;
@@ -52,23 +55,22 @@
public String resolveUri(String importUri, Resource declaringResource) {
if (importUri.startsWith(PREFIX)) return importUri;
Pair<String, List<String>> importUriPair = pair(importUri, createURI(importUri).segmentsList());
- Preferences preferences = preferenceReader.readFromPrefereceStore(resources.project(declaringResource));
- URI resourceUri = declaringResource.getURI();
- String fixed = resolveUri(importUriPair, resourceUri, preferences);
- System.out.println(resourceUri + " : " + importUri + " : " + fixed);
- if (fixed == null) return importUri;
- return fixed;
+ String resolved = resolveUri(importUriPair, declaringResource.getURI());
+// System.out.println(declaringResource.getURI() + " : " + importUri + " : " + resolved);
+ if (resolved == null) return importUri;
+ return resolved;
}
- private String resolveUri(Pair<String, List<String>> importUri, URI resourceUri, Preferences preferences) {
+ private String resolveUri(Pair<String, List<String>> importUri, URI resourceUri) {
+ IProject project = resources.project(resourceUri);
+ Preferences preferences = preferenceReader.readFromPrefereceStore(project);
List<String> segments = removeFirstAndLast(resourceUri.segmentsList());
if (preferences.fileResolutionType().equals(SINGLE_FOLDER)) {
return resolveUri(importUri, segments);
}
for (String folderName : preferences.folderNames()) {
- segments.set(1, folderName);
- String fixed = resolveUri(importUri, segments);
- if (fixed != null) return fixed;
+ String resolved = resolveUri(importUri, folderName, project);
+ if (resolved != null) return resolved;
}
return null;
}
@@ -82,16 +84,20 @@
}
private String resolveUri(Pair<String, List<String>> importUri, List<String> resourceUri) {
- StringBuilder prefix = new StringBuilder();
- // prefix.append(PREFIX);
+ StringBuilder pathBuilder = new StringBuilder();
String firstSegment = importUri.getSecond().get(0);
for (String segment : resourceUri) {
if (segment.equals(firstSegment)) break;
- prefix.append(SEPARATOR).append(segment);
+ pathBuilder.append(segment).append(SEPARATOR);
}
- prefix.append(SEPARATOR);
- String fixed = PREFIX + prefix.toString() + importUri.getFirst();
- if (resources.fileExists(createURI(fixed))) return fixed;
- return null;
+ String resolved = PREFIX + SEPARATOR + pathBuilder.toString() + importUri.getFirst();
+ return (resources.fileExists(createURI(resolved))) ? resolved : null;
+ }
+
+ private String resolveUri(Pair<String, List<String>> importUri, String folderName, IProject project) {
+ String path = folderName + SEPARATOR + importUri.getFirst();
+ IResource findMember = project.findMember(path);
+ boolean exists = (findMember != null) ? findMember.exists() : false;
+ return (exists) ? PREFIX + project.getFullPath() + SEPARATOR + path : null;
}
}
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/Resources.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/Resources.java
index 876f0d4..2839af5 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/Resources.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/scoping/Resources.java
@@ -12,7 +12,6 @@
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
-import org.eclipse.emf.ecore.resource.Resource;
/**
* Utility methods related to resources (e.g. files, directories.)
@@ -22,12 +21,12 @@
public class Resources {
/**
- * Returns the project that contains the given resource.
- * @param resource the given resource.
- * @return the project that contains the given resource.
+ * Returns the project that contains the resource at the given URI.
+ * @param resourceUri the given URI.
+ * @return the project that contains the resource at the given URI.
*/
- public IProject project(Resource resource) {
- return file(resource.getURI()).getProject();
+ public IProject project(URI resourceUri) {
+ return file(resourceUri).getProject();
}
/**
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/IFileUriResolver.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/IFileUriResolver.java
index 1c0a857..58257de 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/IFileUriResolver.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/IFileUriResolver.java
@@ -21,9 +21,6 @@
@ImplementedBy(NullFileUriResolver.class)
public interface IFileUriResolver {
- /** Prefix present in resolved URIs. */
- String PREFIX = "platform:/resource";
-
/**
* Resolves the given partial URI.
* @param importUri the partial URI (comes from a {@code Import}.)
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtobufImportUriResolver.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtobufImportUriResolver.java
index d0e2a67..4fdd459 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtobufImportUriResolver.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtobufImportUriResolver.java
@@ -8,8 +8,6 @@
*/
package com.google.eclipse.protobuf.scoping;
-import static com.google.eclipse.protobuf.scoping.IFileUriResolver.PREFIX;
-
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.scoping.impl.ImportUriResolver;
@@ -32,11 +30,6 @@
@Inject private IFileUriResolver delegate;
/**
- * Prefix used by EMF for resource URIs: "platform:/resource/".
- */
- public static final String URI_PREFIX = PREFIX + "/";
-
- /**
* If the given {@code EObject} is a <code>{@link Import}</code>, this method will add "platform:/resource" to the
* URI of such import if not specified already.
* @param from the given element to resolve.
@@ -45,10 +38,10 @@
@Override public String apply(EObject from) {
if (from instanceof Import) {
Import anImport = (Import) from;
- String originalUri = anImport.getImportURI();
+ // String originalUri = anImport.getImportURI();
anImport.setImportURI(resolveImportUri(anImport));
String applied = super.apply(from);
- anImport.setImportURI(originalUri);
+ // anImport.setImportURI(originalUri);
return applied;
}
if (from instanceof Import) resolveImportUri((Import) from);