Fixed: [ Issue 66 ] Add support for partial package names in qualified names https://code.google.com/p/protobuf-dt/issues/detail?id=66
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ImportedNamesProvider.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ImportedNamesProvider.java new file mode 100644 index 0000000..a45374c --- /dev/null +++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ImportedNamesProvider.java
@@ -0,0 +1,56 @@ +/* + * 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.scoping; + +import static com.google.eclipse.protobuf.scoping.QualifiedNames.addPackageNameSegments; +import static java.util.Collections.*; +import static org.eclipse.xtext.util.SimpleAttributeResolver.newResolver; +import static org.eclipse.xtext.util.Strings.isEmpty; +import static org.eclipse.xtext.util.Tuples.pair; + +import com.google.common.base.Function; +import com.google.eclipse.protobuf.util.ProtobufElementFinder; +import com.google.inject.*; + +import org.eclipse.emf.ecore.EObject; +import org.eclipse.xtext.naming.*; +import org.eclipse.xtext.util.*; + +import java.util.*; + +/** + * Provides alternative qualified names for imported protobuf elements. + * + * @author alruiz@google.com (Alex Ruiz) + */ +class ImportedNamesProvider { + + @Inject private final IResourceScopeCache cache = IResourceScopeCache.NullImpl.INSTANCE; + @Inject private final IQualifiedNameConverter converter = new IQualifiedNameConverter.DefaultImpl(); + + @Inject private ProtobufElementFinder finder; + + private final Function<EObject, String> resolver = newResolver(String.class, "name"); + + List<QualifiedName> namesOf(final EObject obj) { + Pair<EObject, String> key = pair(obj, "importedFqns"); + return cache.get(key, obj.eResource(), new Provider<List<QualifiedName>>() { + public List<QualifiedName> get() { + List<QualifiedName> allNames = new ArrayList<QualifiedName>(); + EObject current = obj; + String name = resolver.apply(current); + if (isEmpty(name)) return emptyList(); + QualifiedName qualifiedName = converter.toQualifiedName(name); + allNames.add(qualifiedName); + allNames.addAll(addPackageNameSegments(qualifiedName, finder.packageOf(obj), converter)); + return unmodifiableList(allNames); + } + }); + } +}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/LocalNamesProvider.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/LocalNamesProvider.java index 98ccb0b..e24a2e8 100644 --- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/LocalNamesProvider.java +++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/LocalNamesProvider.java
@@ -8,21 +8,21 @@ */ package com.google.eclipse.protobuf.scoping; +import static com.google.eclipse.protobuf.scoping.QualifiedNames.addPackageNameSegments; import static java.util.Collections.*; import static org.eclipse.xtext.util.SimpleAttributeResolver.newResolver; import static org.eclipse.xtext.util.Strings.isEmpty; import static org.eclipse.xtext.util.Tuples.pair; -import java.util.*; +import com.google.common.base.Function; +import com.google.eclipse.protobuf.util.ProtobufElementFinder; +import com.google.inject.*; import org.eclipse.emf.ecore.EObject; import org.eclipse.xtext.naming.*; import org.eclipse.xtext.util.*; -import com.google.common.base.Function; -import com.google.eclipse.protobuf.protobuf.Package; -import com.google.eclipse.protobuf.util.ProtobufElementFinder; -import com.google.inject.*; +import java.util.*; /** * Provides alternative qualified names for protobuf elements. @@ -65,7 +65,7 @@ private final Function<EObject, String> resolver = newResolver(String.class, "name"); List<QualifiedName> namesOf(final EObject obj) { - Pair<EObject, String> key = pair(obj, "fqns"); + Pair<EObject, String> key = pair(obj, "localFqns"); return cache.get(key, obj.eResource(), new Provider<List<QualifiedName>>() { public List<QualifiedName> get() { List<QualifiedName> allNames = new ArrayList<QualifiedName>(); @@ -81,29 +81,9 @@ qualifiedName = converter.toQualifiedName(containerName).append(qualifiedName); allNames.add(qualifiedName); } - allNames.addAll(addPackageNameSegments(qualifiedName, finder.packageOf(obj))); + allNames.addAll(addPackageNameSegments(qualifiedName, finder.packageOf(obj), converter)); return unmodifiableList(allNames); } }); } - - private List<QualifiedName> addPackageNameSegments(QualifiedName qualifiedName, Package p) { - QualifiedName name = qualifiedName; - List<String> segments = fqnSegments(p); - int segmentCount = segments.size(); - if (segmentCount <= 1) return emptyList(); - List<QualifiedName> allNames = new ArrayList<QualifiedName>(); - for (int i = segmentCount - 1; i > 0; i--) { - name = QualifiedName.create(segments.get(i)).append(name); - allNames.add(name); - } - return unmodifiableList(allNames); - } - - private List<String> fqnSegments(Package p) { - if (p == null) return emptyList(); - String packageName = p.getName(); - if (isEmpty(packageName)) return emptyList(); - return converter.toQualifiedName(packageName).getSegments(); - } }
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 90d9e62..461b2a5 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
@@ -45,6 +45,7 @@ @Inject private IQualifiedNameProvider nameProvider; @Inject private ImportUriResolver uriResolver; @Inject private LocalNamesProvider localNamesProvider; + @Inject private ImportedNamesProvider importedNamesProvider; @Inject private PackageResolver packageResolver; @SuppressWarnings("unused") @@ -149,6 +150,9 @@ if (!targetType.isInstance(next)) continue; T type = targetType.cast(next); descriptions.add(create(nameProvider.getFullyQualifiedName(type), type)); + for (QualifiedName name : importedNamesProvider.namesOf(type)) { + descriptions.add(create(name, type)); + } } return descriptions; }
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/QualifiedNames.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/QualifiedNames.java new file mode 100644 index 0000000..047a80a --- /dev/null +++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/QualifiedNames.java
@@ -0,0 +1,47 @@ +/* + * 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.scoping; + +import static java.util.Collections.*; +import static org.eclipse.xtext.util.Strings.isEmpty; + +import com.google.eclipse.protobuf.protobuf.Package; + +import org.eclipse.xtext.naming.*; + +import java.util.*; + +/** + * @author alruiz@google.com (Alex Ruiz) + */ +final class QualifiedNames { + + static List<QualifiedName> addPackageNameSegments(QualifiedName qualifiedName, Package p, + IQualifiedNameConverter converter) { + QualifiedName name = qualifiedName; + List<String> segments = fqnSegments(p, converter); + int segmentCount = segments.size(); + if (segmentCount <= 1) return emptyList(); + List<QualifiedName> allNames = new ArrayList<QualifiedName>(); + for (int i = segmentCount - 1; i > 0; i--) { + name = QualifiedName.create(segments.get(i)).append(name); + allNames.add(name); + } + return unmodifiableList(allNames); + } + + static private List<String> fqnSegments(Package p, IQualifiedNameConverter converter) { + if (p == null) return emptyList(); + String packageName = p.getName(); + if (isEmpty(packageName)) return emptyList(); + return converter.toQualifiedName(packageName).getSegments(); + } + + private QualifiedNames() {} +}