Added check for null roots in SmartSemicolonHandler. Code cleanup.
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/commands/semicolon/SmartSemicolonHandler.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/commands/semicolon/SmartSemicolonHandler.java
index 8bf92ce..1478c89 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/commands/semicolon/SmartSemicolonHandler.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/commands/semicolon/SmartSemicolonHandler.java
@@ -54,10 +54,12 @@
private static Logger logger = Logger.getLogger(SmartSemicolonHandler.class);
@Inject private CommentNodesFinder commentNodesFinder;
+ @Inject private ParserBasedContentAssistContextFactory contextFactory;
@Inject private IndexedElements indexedElements;
@Inject private Literals literals;
@Inject private INodes nodes;
- @Inject private ParserBasedContentAssistContextFactory contextFactory;
+ @Inject private Protobufs protobufs;
+ @Inject private Resources resources;
@Inject private IPreferenceStoreAccess storeAccess;
private static final String SEMICOLON = CommonKeyword.SEMICOLON.toString();
@@ -80,6 +82,10 @@
try {
document.modify(new IUnitOfWork.Void<XtextResource>() {
@Override public void process(XtextResource resource) {
+ Protobuf root = resources.rootOf(resource);
+ if (!protobufs.isProto2(root)) {
+ return;
+ }
int offset = styledTextAccess.caretOffset();
ContentAssistContext[] context = contextFactory.create(editor.getInternalSourceViewer(), offset, resource);
for (ContentAssistContext c : context) {
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/Protobufs.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/Protobufs.java
index a6c4a44..66b4458 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/Protobufs.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/Protobufs.java
@@ -11,12 +11,12 @@
import static com.google.common.collect.Lists.newArrayList;
import static java.util.Collections.unmodifiableList;
+import java.util.List;
+
import com.google.eclipse.protobuf.parser.NonProto2Protobuf;
import com.google.eclipse.protobuf.protobuf.*;
import com.google.inject.Singleton;
-import java.util.List;
-
/**
* Utility methods related to <code>{@link Protobuf}</code>s.
*
@@ -24,7 +24,8 @@
*/
@Singleton public class Protobufs {
/**
- * Indicates whether the given <code>{@link Protobuf}</code> is not {@code null} and has "proto2" syntax.
+ * Indicates whether the given <code>{@link Protobuf}</code> is not {@code null} and has "proto2" syntax (not
+ * necessarily in a explicit way.)
* @param protobuf the {@code Protobuf} to check.
* @return {@code true} if the given <code>{@link Protobuf}</code> is not {@code null} and has "proto2" syntax,
* {@code false} otherwise.
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/CustomOptionFieldFinder.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/CustomOptionFieldFinder.java
index 4e5f5b7..d76411f 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/CustomOptionFieldFinder.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/CustomOptionFieldFinder.java
@@ -25,11 +25,11 @@
@Inject private OptionFields optionFields;
@Inject private Options options;
- Collection<IEObjectDescription> findOptionFields(AbstractCustomOption customOption, FinderStrategy finderStrategy) {
- return findOptionFields(customOption, finderStrategy, null);
+ Collection<IEObjectDescription> findOptionFields(AbstractCustomOption customOption, FinderStrategy strategy) {
+ return findOptionFields(customOption, strategy, null);
}
- Collection<IEObjectDescription> findOptionFields(AbstractCustomOption customOption, FinderStrategy finderStrategy,
+ Collection<IEObjectDescription> findOptionFields(AbstractCustomOption customOption, FinderStrategy strategy,
OptionField field) {
// TODO(alruiz): remove Provider of IndexedElement.
final AbstractOption option = (AbstractOption) customOption;
@@ -39,7 +39,7 @@
}
});
if (e != null) {
- return finderStrategy.findOptionFields(e);
+ return strategy.findOptionFields(e);
}
return emptySet();
}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/CustomOptionFieldNameFinder.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/CustomOptionFieldNameFinder.java
index 986699b..4410159 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/CustomOptionFieldNameFinder.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/CustomOptionFieldNameFinder.java
@@ -25,12 +25,12 @@
class CustomOptionFieldNameFinder {
@Inject private Options options;
- Collection<IEObjectDescription> findFieldNamesSources(ComplexValue value, FinderStrategy finderStrategy) {
+ Collection<IEObjectDescription> findFieldNamesSources(ComplexValue value, FinderStrategy strategy) {
MessageField source = sourceOf(value);
if (source == null) {
return emptySet();
}
- return finderStrategy.findMessageFields(source);
+ return strategy.findMessageFields(source);
}
private MessageField sourceOf(ComplexValue value) {
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ModelElementFinder.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ModelElementFinder.java
index 53fa8bc..6428fbb 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ModelElementFinder.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ModelElementFinder.java
@@ -36,57 +36,57 @@
@Inject private Resources resources;
@Inject private ResourceSets resourceSets;
- Collection<IEObjectDescription> find(EObject start, FinderStrategy finderStrategy, Object criteria) {
+ Collection<IEObjectDescription> find(EObject start, FinderStrategy strategy, Object criteria) {
Set<IEObjectDescription> descriptions = newHashSet();
- descriptions.addAll(local(start, finderStrategy, criteria));
+ descriptions.addAll(local(start, strategy, criteria));
Protobuf root = modelObjects.rootOf(start);
- descriptions.addAll(imported(root, finderStrategy, criteria));
+ descriptions.addAll(imported(root, strategy, criteria));
return unmodifiableSet(descriptions);
}
- private Collection<IEObjectDescription> local(EObject start, FinderStrategy finderStrategy, Object criteria) {
+ private Collection<IEObjectDescription> local(EObject start, FinderStrategy strategy, Object criteria) {
UniqueDescriptions descriptions = new UniqueDescriptions();
EObject current = start.eContainer();
while (current != null) {
- descriptions.addAll(local(current, finderStrategy, criteria, 0));
+ descriptions.addAll(local(current, strategy, criteria, 0));
current = current.eContainer();
}
return descriptions.values();
}
- Collection<IEObjectDescription> find(Protobuf start, FinderStrategy finderStrategy, Object criteria) {
+ Collection<IEObjectDescription> find(Protobuf start, FinderStrategy strategy, Object criteria) {
Set<IEObjectDescription> descriptions = newHashSet();
- descriptions.addAll(local(start, finderStrategy, criteria, 0));
- descriptions.addAll(imported(start, finderStrategy, criteria));
+ descriptions.addAll(local(start, strategy, criteria, 0));
+ descriptions.addAll(imported(start, strategy, criteria));
return unmodifiableSet(descriptions);
}
- private Collection<IEObjectDescription> local(EObject start, FinderStrategy finder, Object criteria, int level) {
+ private Collection<IEObjectDescription> local(EObject start, FinderStrategy strategy, Object criteria, int level) {
UniqueDescriptions descriptions = new UniqueDescriptions();
for (EObject element : start.eContents()) {
- descriptions.addAll(finder.local(element, criteria, level));
+ descriptions.addAll(strategy.local(element, criteria, level));
if (element instanceof Message || element instanceof Group) {
- descriptions.addAll(local(element, finder, criteria, level + 1));
+ descriptions.addAll(local(element, strategy, criteria, level + 1));
}
}
return descriptions.values();
}
- private Collection<IEObjectDescription> imported(Protobuf start, FinderStrategy finderStrategy, Object criteria) {
+ private Collection<IEObjectDescription> imported(Protobuf start, FinderStrategy strategy, Object criteria) {
List<Import> allImports = protobufs.importsIn(start);
if (allImports.isEmpty()) {
return emptyList();
}
ResourceSet resourceSet = start.eResource().getResourceSet();
- return imported(allImports, modelObjects.packageOf(start), resourceSet, finderStrategy, criteria);
+ return imported(allImports, modelObjects.packageOf(start), resourceSet, strategy, criteria);
}
private Collection<IEObjectDescription> imported(List<Import> allImports, Package fromImporter,
- ResourceSet resourceSet, FinderStrategy finderStrategy, Object criteria) {
+ ResourceSet resourceSet, FinderStrategy strategy, Object criteria) {
Set<IEObjectDescription> descriptions = newHashSet();
for (Import anImport : allImports) {
if (imports.isImportingDescriptor(anImport)) {
- descriptions.addAll(finderStrategy.inDescriptor(anImport, criteria));
+ descriptions.addAll(strategy.inDescriptor(anImport, criteria));
continue;
}
URI resolvedUri = imports.resolvedUriOf(anImport);
@@ -102,20 +102,19 @@
continue;
}
if (rootOfImported != null) {
- descriptions.addAll(publicImported(rootOfImported, finderStrategy, criteria));
+ descriptions.addAll(publicImported(rootOfImported, strategy, criteria));
if (arePackagesRelated(fromImporter, rootOfImported)) {
- descriptions.addAll(local(rootOfImported, finderStrategy, criteria, 0));
+ descriptions.addAll(local(rootOfImported, strategy, criteria, 0));
continue;
}
Package packageOfImported = modelObjects.packageOf(rootOfImported);
- descriptions.addAll(imported(fromImporter, packageOfImported, imported, finderStrategy, criteria));
+ descriptions.addAll(imported(fromImporter, packageOfImported, imported, strategy, criteria));
}
}
return descriptions;
}
- private Collection<IEObjectDescription> publicImported(Protobuf start, FinderStrategy finderStrategy,
- Object criteria) {
+ private Collection<IEObjectDescription> publicImported(Protobuf start, FinderStrategy strategy, Object criteria) {
if (!protobufs.isProto2(start)) {
return emptySet();
}
@@ -124,7 +123,7 @@
return emptyList();
}
ResourceSet resourceSet = start.eResource().getResourceSet();
- return imported(allImports, modelObjects.packageOf(start), resourceSet, finderStrategy, criteria);
+ return imported(allImports, modelObjects.packageOf(start), resourceSet, strategy, criteria);
}
private boolean arePackagesRelated(Package aPackage, EObject root) {
@@ -133,12 +132,12 @@
}
private Collection<IEObjectDescription> imported(Package fromImporter, Package fromImported, Resource resource,
- FinderStrategy finderStrategy, Object criteria) {
+ FinderStrategy strategy, Object criteria) {
Set<IEObjectDescription> descriptions = newHashSet();
TreeIterator<Object> contents = getAllContents(resource, true);
while (contents.hasNext()) {
Object next = contents.next();
- descriptions.addAll(finderStrategy.imported(fromImporter, fromImported, next, criteria));
+ descriptions.addAll(strategy.imported(fromImporter, fromImported, next, criteria));
}
return descriptions;
}