Fixed: [ Issue 24 ] Fix qualified names
https://code.google.com/p/protobuf-dt/issues/detail?id=24
Qualified names for imported types fixed.
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/scoping/ImportUriFixerAndResolver_apply_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/scoping/ImportUriFixerAndResolver_apply_Test.java
index d333050..f5e9a1a 100644
--- a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/scoping/ImportUriFixerAndResolver_apply_Test.java
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/scoping/ImportUriFixerAndResolver_apply_Test.java
@@ -16,8 +16,7 @@
import org.junit.*;
import com.google.eclipse.protobuf.junit.core.XtextRule;
-import com.google.eclipse.protobuf.protobuf.Import;
-import com.google.eclipse.protobuf.protobuf.Protobuf;
+import com.google.eclipse.protobuf.protobuf.*;
/**
* Tests for <code>{@link ImportUriFixerAndResolver#apply(EObject)}</code>.
@@ -27,26 +26,26 @@
public class ImportUriFixerAndResolver_apply_Test {
@Rule public XtextRule xtext = new XtextRule();
-
+
private ImportUriFixerAndResolver resolver;
-
+
@Before public void setUp() {
resolver = (ImportUriFixerAndResolver) xtext.getInstanceOf(ImportUriResolver.class);
}
-
+
@Test public void should_fix_import_URI_if_missing_scheme() {
StringBuilder proto = new StringBuilder();
- proto.append("import \"folder1/test.proto\";");
+ proto.append("import \"folder1/test.proto\";");
Protobuf root = xtext.parse(proto);
Import anImport = root.getImports().get(0);
String resolved = resolver.apply(anImport);
assertThat(resolved, equalTo("platform:/resource/folder1/test.proto"));
}
-
+
@Test public void should_not_fix_import_URI_if_not_missing_scheme() {
StringBuilder proto = new StringBuilder();
- proto.append("import \"platform:/resource/folder1/test.proto\";");
+ proto.append("import \"platform:/resource/folder1/test.proto\";");
Protobuf root = xtext.parse(proto);
Import anImport = root.getImports().get(0);
String resolved = resolver.apply(anImport);
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ImportUriFixerAndResolver.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ImportUriFixerAndResolver.java
index 2de2ca2..2faf1c5 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ImportUriFixerAndResolver.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ImportUriFixerAndResolver.java
@@ -8,6 +8,8 @@
*/
package com.google.eclipse.protobuf.scoping;
+import java.util.List;
+
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.scoping.impl.ImportUriResolver;
@@ -15,14 +17,14 @@
import com.google.eclipse.protobuf.protobuf.Import;
/**
- * Resolves URIs. This implementation mimics how protoc understands imported file URIs. For example, the URI
- * "platform:/resource/proto1.proto" is understood by EMF but not by protoc. The URI in the proto file needs to be
- * simply "proto1.proto" for protoc to understand it.
+ * Resolves URIs. This implementation mimics how protoc understands imported file URIs. For example, the URI
+ * "platform:/resource/proto1.proto" is understood by EMF but not by protoc. The URI in the proto file needs to be
+ * simply "proto1.proto" for protoc to understand it.
* <p>
* This {@link ImportUriResolver} adds "platform:/resource" to any URI if is not specified, so EMF can find the
* imported resource.
* </p>
- *
+ *
* @author alruiz@google.com (Alex Ruiz)
*/
public class ImportUriFixerAndResolver extends ImportUriResolver {
@@ -34,7 +36,7 @@
*/
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.
@@ -61,9 +63,22 @@
* We need to have the import URI as "platform:/resource/protobuf-test/folder/proto2.proto" for the editor to see it.
*/
private void fixUri(Import i) {
- URI importUri = URI.createURI(i.getImportURI());
- if (importUri.scheme() != null) return;
- URI fixedUri = URI.createURI(PREFIX).appendSegments(importUri.segments());
- i.setImportURI(fixedUri.toString());
+ String prefix = uriPrefix(i.eResource().getURI());
+ String uri = i.getImportURI();
+ if (!uri.startsWith(prefix)) {
+ if (!uri.startsWith(prefix)) prefix += "/";
+ i.setImportURI(prefix + uri);
+ }
+ }
+
+ private String uriPrefix(URI containerUri) {
+ StringBuilder prefix = new StringBuilder();
+ prefix.append(PREFIX);
+ int start = (containerUri.scheme() == null) ? 0 : 1; // ignore the scheme if present (e.g. "platform")
+ List<String> segments = containerUri.segmentsList();
+ int end = segments.size() - 1; // ignore file name (at the end of the URI)
+ for (int j = start; j < end; j++)
+ prefix.append("/").append(segments.get(j));
+ return prefix.toString();
}
}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtobufScopeProvider.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtobufScopeProvider.java
index 843b5e0..8a0cc00 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtobufScopeProvider.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtobufScopeProvider.java
@@ -53,9 +53,7 @@
descriptions.addAll(innerTypes(message.eContainer()));
descriptions.addAll(innerTypes(root));
descriptions.addAll(importedTypes(root));
- IScope scope = createScope(descriptions);
- System.out.println("scope for property: " + ((Property) typeRef.eContainer()).getName() + ": " + scope);
- return scope;
+ return createScope(descriptions);
}
private Collection<IEObjectDescription> innerTypes(EObject root) {