Code cleanup. Fixed content assist for message and type references.
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/contentassist/IEObjectDescriptionChooser.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/contentassist/IEObjectDescriptionChooser.java
index 297afb1..5345983 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/contentassist/IEObjectDescriptionChooser.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/contentassist/IEObjectDescriptionChooser.java
@@ -8,7 +8,7 @@
  */
 package com.google.eclipse.protobuf.ui.contentassist;
 
-import static java.util.Collections.unmodifiableCollection;
+import static java.util.Collections.*;
 
 import org.eclipse.emf.ecore.EObject;
 import org.eclipse.xtext.naming.QualifiedName;
@@ -22,6 +22,7 @@
 class IEObjectDescriptionChooser {
 
   Collection<IEObjectDescription> shortestQualifiedNamesIn(Collection<IEObjectDescription> descriptions) {
+    if (descriptions.isEmpty()) return emptySet();
     Map<EObject, IEObjectDescription> shortestOnes = new HashMap<EObject, IEObjectDescription>();
     for (IEObjectDescription d : descriptions) {
       EObject e = d.getEObjectOrProxy();
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/contentassist/ProtobufProposalProvider.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/contentassist/ProtobufProposalProvider.java
index 791577d..6894a39 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/contentassist/ProtobufProposalProvider.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/contentassist/ProtobufProposalProvider.java
@@ -76,7 +76,8 @@
 
   @Override public void completeComplexTypeLink_Target(EObject model, Assignment assignment,
       ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
-    Collection<IEObjectDescription> scope = scoping().findMessageScope(model);
+    if (!(model instanceof MessageField)) return;
+    Collection<IEObjectDescription> scope = scoping().allPossibleTypesFor((MessageField) model);
     for (IEObjectDescription d : descriptionChooser.shortestQualifiedNamesIn(scope)) {
       Image image = imageHelper.getImage(images.imageFor(d.getEObjectOrProxy()));
       proposeAndAccept(d, image, context, acceptor);
@@ -85,8 +86,15 @@
 
   @Override public void completeMessageLink_Target(EObject model, Assignment assignment, ContentAssistContext context, 
       ICompletionProposalAcceptor acceptor) {
-    if (!(model instanceof MessageField)) return;
-    Collection<IEObjectDescription> scope = scoping().findScope((MessageField) model);
+    Collection<IEObjectDescription> scope = emptySet();
+    if (model instanceof MessageExtension) {
+      MessageExtension extension = (MessageExtension) model;
+      scope = scoping().allPossibleMessagesFor(extension);
+    }
+    if (model instanceof Rpc) {
+      Rpc rpc = (Rpc) model;
+      scope = scoping().allPossibleMessagesFor(rpc);
+    }
     for (IEObjectDescription d : descriptionChooser.shortestQualifiedNamesIn(scope)) {
       Image image = imageHelper.getImage(images.imageFor(d.getEObjectOrProxy()));
       proposeAndAccept(d, image, context, acceptor);
@@ -246,6 +254,20 @@
     proposeIndex(index, context, acceptor);
   }
 
+  @Override public void completeLiteralLink_Target(EObject model, Assignment assignment, ContentAssistContext context,
+      ICompletionProposalAcceptor acceptor) {
+    MessageField field = null;
+    if (model instanceof DefaultValueFieldOption) {
+      field = (MessageField) model.eContainer();
+    }
+    if (field == null) return;
+    if (!properties.isOptional(field)) return;
+    Enum enumType = finder.enumTypeOf(field);
+    if (enumType != null) {
+      proposeAndAccept(enumType, context, acceptor);
+    }
+  }
+  
   @Override public void completeMessageField_Index(EObject model, Assignment assignment, ContentAssistContext context,
       ICompletionProposalAcceptor acceptor) {
     long index = indexedElements.calculateTagNumberOf((MessageField) model);
@@ -355,19 +377,17 @@
   
   @Override public void completeDefaultValueFieldOption_Value(EObject model, Assignment assignment, 
       ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
-    if (!(model instanceof MessageField)) return;
-    MessageField field = (MessageField) model;
+    MessageField field = null;
+    if (model instanceof DefaultValueFieldOption) {
+      field = (MessageField) model.eContainer();
+    }
+    if (model instanceof MessageField) {
+      field = (MessageField) model;
+    }
+    if (field == null) return;
     if (!properties.isOptional(field)) return;
     proposeDefaultValue(field, context, acceptor);
   }
-  
-  @Override public ICompletionProposal createCompletionProposal(String proposal, String displayString, Image image,
-      ContentAssistContext contentAssistContext) {
-    StyledString styled = null;
-    if (displayString != null) styled = new StyledString(displayString);
-    return createCompletionProposal(proposal, styled, image, getPriorityHelper().getDefaultPriority(), 
-        contentAssistContext.getPrefix(), contentAssistContext);
-  }
 
   @Override public void completeNativeFieldOption_Value(EObject model, Assignment assignment,
       ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
@@ -425,7 +445,7 @@
       ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
     if (!(model instanceof CustomOption)) return;
     CustomOption option = (CustomOption) model;
-    Collection<IEObjectDescription> scope = scoping().findScope(option);
+    Collection<IEObjectDescription> scope = scoping().allPossibleSourcesOf(option);
     proposeAndAcceptOptions(scope, context, acceptor);
   }
 
@@ -433,7 +453,7 @@
       ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
     if (!(model instanceof CustomFieldOption)) return;
     CustomFieldOption option = (CustomFieldOption) model;
-    Collection<IEObjectDescription> scope = scoping().findScope(option);
+    Collection<IEObjectDescription> scope = scoping().allPossibleSourcesOf(option);
     proposeAndAcceptOptions(scope, context, acceptor);
   }
 
@@ -470,15 +490,15 @@
     Collection<IEObjectDescription> scope = emptySet();
     if (e instanceof CustomOption) {
       CustomOption option = (CustomOption) e;
-      scope = scoping().findFieldScope(option);
+      scope = scoping().allPossibleSourcesOfFieldOf(option);
     }
     if (e instanceof CustomFieldOption) {
       CustomFieldOption option = (CustomFieldOption) e;
-      scope = scoping().findFieldScope(option);
+      scope = scoping().allPossibleSourcesOfFieldOf(option);
     }
     if (e instanceof MessageOptionField) {
       MessageOptionField field = (MessageOptionField) e;
-      scope = scoping().findScope(field);
+      scope = scoping().allPossibleSourcesOf(field);
     }
     for (IEObjectDescription d : descriptionChooser.shortestQualifiedNamesIn(scope)) {
       Image image = imageHelper.getImage(images.imageFor(d.getEObjectOrProxy()));
@@ -494,15 +514,15 @@
     Collection<IEObjectDescription> scope = emptySet();
     if (e instanceof CustomOption) {
       CustomOption option = (CustomOption) e;
-      scope = scoping().findFieldScope(option);
+      scope = scoping().allPossibleSourcesOfFieldOf(option);
     }
     if (e instanceof CustomFieldOption) {
       CustomFieldOption option = (CustomFieldOption) e;
-      scope = scoping().findFieldScope(option);
+      scope = scoping().allPossibleSourcesOfFieldOf(option);
     }
     if (e instanceof ExtensionOptionField) {
-      ExtensionOptionField source = (ExtensionOptionField) e;
-      scope = scoping().findScope(source);
+      ExtensionOptionField field = (ExtensionOptionField) e;
+      scope = scoping().allPossibleSourcesOf(field);
     }
     for (IEObjectDescription d : descriptionChooser.shortestQualifiedNamesIn(scope)) {
       Image image = imageHelper.getImage(images.imageFor(d.getEObjectOrProxy()));
@@ -566,4 +586,12 @@
   private Scoping scoping() {
     return (ProtobufScopeProvider) super.getScopeProvider();
   }
+  
+  @Override public ICompletionProposal createCompletionProposal(String proposal, String displayString, Image image,
+      ContentAssistContext contentAssistContext) {
+    StyledString styled = null;
+    if (displayString != null) styled = new StyledString(displayString);
+    return createCompletionProposal(proposal, styled, image, getPriorityHelper().getDefaultPriority(), 
+        contentAssistContext.getPrefix(), contentAssistContext);
+  }
 }
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 05da44b..60460a0 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
@@ -48,28 +48,32 @@
 
   @SuppressWarnings("unused")
   public IScope scope_ComplexTypeLink_target(ComplexTypeLink link, EReference r) {
-    try {
-      EObject c = link.eContainer();
-      if (c instanceof MessageField) {
-        return createScope(findScope((MessageField) c));
-      }
-    } catch (Throwable t) {
-      t.printStackTrace();
+    EObject c = link.eContainer();
+    if (c instanceof MessageField) {
+      return createScope(allPossibleTypesFor((MessageField) c));
     }
     Set<IEObjectDescription> descriptions = emptySet();
     return createScope(descriptions);
   }
   
-  @Override public Collection<IEObjectDescription> findScope(MessageField field) {
+  @Override public Collection<IEObjectDescription> allPossibleTypesFor(MessageField field) {
     return astWalker.traverseAst(field, typeScopeFinder, ComplexType.class);
   }
 
   @SuppressWarnings("unused")
   public IScope scope_MessageLink_target(MessageLink link, EReference r) {
-    return createScope(findMessageScope(link));
+    return createScope(messagesFor(link));
   }
 
-  @Override public Collection<IEObjectDescription> findMessageScope(EObject o) {
+  @Override public Collection<IEObjectDescription> allPossibleMessagesFor(MessageExtension extension) {
+    return messagesFor(extension);
+  }
+
+  @Override public Collection<IEObjectDescription> allPossibleMessagesFor(Rpc rpc) {
+    return messagesFor(rpc);
+  }
+
+  private Collection<IEObjectDescription> messagesFor(EObject o) {
     Protobuf root = modelFinder.rootOf(o);
     return astWalker.traverseAst(root, typeScopeFinder, Message.class);
   }
@@ -118,17 +122,17 @@
     }
     if (c instanceof CustomOption) {
       CustomOption option = (CustomOption) c;
-      return createScope(findScope(option));
+      return createScope(allPossibleSourcesOf(option));
     }
     if (c instanceof CustomFieldOption) {
       CustomFieldOption option = (CustomFieldOption) c;
-      return createScope(findScope(option));
+      return createScope(allPossibleSourcesOf(option));
     }
     Set<IEObjectDescription> descriptions = emptySet();
     return createScope(descriptions);
   }
   
-  @Override public Collection<IEObjectDescription> findScope(CustomOption option) {
+  @Override public Collection<IEObjectDescription> allPossibleSourcesOf(CustomOption option) {
     OptionType optionType = typeOf(option);
     Collection<IEObjectDescription> descriptions = emptySet();
     if (optionType != null) {
@@ -137,7 +141,7 @@
     return descriptions;
   }
 
-  @Override public Collection<IEObjectDescription> findScope(CustomFieldOption option) {
+  @Override public Collection<IEObjectDescription> allPossibleSourcesOf(CustomFieldOption option) {
     OptionType optionType = typeOf(option);
     Collection<IEObjectDescription> descriptions = emptySet();
     if (optionType != null) {
@@ -148,10 +152,10 @@
 
   @SuppressWarnings("unused") 
   public IScope scope_OptionField_target(OptionField field, EReference r) {
-    return createScope(findScope(field));
+    return createScope(allPossibleSourcesOf(field));
   }
   
-  @Override public Collection<IEObjectDescription> findScope(OptionField field) {
+  @Override public Collection<IEObjectDescription> allPossibleSourcesOf(OptionField field) {
     EObject container = field.eContainer();
     if (container instanceof CustomOption) {
       return findSources((CustomOption) container, field);
@@ -162,11 +166,11 @@
     return emptySet();
   }
   
-  @Override public Collection<IEObjectDescription> findFieldScope(CustomOption option) {
+  @Override public Collection<IEObjectDescription> allPossibleSourcesOfFieldOf(CustomOption option) {
     return findSources(option, (MessageOptionField) null);
   }
 
-  @Override public Collection<IEObjectDescription> findFieldScope(CustomFieldOption option) {
+  @Override public Collection<IEObjectDescription> allPossibleSourcesOfFieldOf(CustomFieldOption option) {
     return findSources(option, (MessageOptionField) null);
   }
 
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/Scoping.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/Scoping.java
index b74f062..5d5df0c 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/Scoping.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/Scoping.java
@@ -10,7 +10,6 @@
 
 import com.google.eclipse.protobuf.protobuf.*;
 
-import org.eclipse.emf.ecore.EObject;
 import org.eclipse.xtext.resource.IEObjectDescription;
 
 import java.util.Collection;
@@ -19,19 +18,20 @@
  * @author alruiz@google.com (Alex Ruiz)
  */
 public interface Scoping {
+  
+  Collection<IEObjectDescription> allPossibleMessagesFor(MessageExtension extension);
+  
+  Collection<IEObjectDescription> allPossibleMessagesFor(Rpc rpc);
+  
+  Collection<IEObjectDescription> allPossibleSourcesOf(CustomOption option);
 
-  // TODO redo this interface taking same parameters as ProtobufScopeProvider
-  Collection<IEObjectDescription> findFieldScope(CustomFieldOption option);
+  Collection<IEObjectDescription> allPossibleSourcesOf(CustomFieldOption option);
 
-  Collection<IEObjectDescription> findFieldScope(CustomOption option);
+  Collection<IEObjectDescription> allPossibleSourcesOf(OptionField field);
+  
+  Collection<IEObjectDescription> allPossibleSourcesOfFieldOf(CustomOption option);
+  
+  Collection<IEObjectDescription> allPossibleSourcesOfFieldOf(CustomFieldOption option);
 
-  Collection<IEObjectDescription> findMessageScope(EObject o);
-
-  Collection<IEObjectDescription> findScope(CustomFieldOption option);
-
-  Collection<IEObjectDescription> findScope(CustomOption option);
-
-  Collection<IEObjectDescription> findScope(MessageField o);
-
-  Collection<IEObjectDescription> findScope(OptionField field);
+  Collection<IEObjectDescription> allPossibleTypesFor(MessageField field);
 }