Merge branch 'master' of https://code.google.com/p/protobuf-dt/
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/commands/SmartInsertHandler.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/commands/SmartInsertHandler.java
index 4ad6640..4a8a2c3 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/commands/SmartInsertHandler.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/commands/SmartInsertHandler.java
@@ -32,10 +32,7 @@
 
   private StyledText styledTextFrom(XtextEditor editor) {
     Object adapter = editor.getAdapter(Control.class);
-    if (adapter instanceof StyledText) {
-      return (StyledText) adapter;
-    }
-    return null;
+    return (adapter instanceof StyledText) ? (StyledText) adapter : null;
   }
 
   protected abstract void insertContent(XtextEditor editor, StyledText styledText);
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 5b9790a..7e6dadb 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
@@ -73,13 +73,12 @@
 
   @Override public void completeComplexTypeLink_Target(EObject model, Assignment assignment,
       ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
-    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);
+    if (model instanceof MessageField) {
+      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);
+      }
     }
   }
 
@@ -233,15 +232,8 @@
     if (model instanceof MessageField) {
       return (MessageField) model;
     }
-    if (model instanceof Option) {
-      Option option = (Option) model;
-      IndexedElement source = options.rootSourceOf(option);
-      if (source instanceof MessageField) {
-        return (MessageField) source;
-      }
-    }
-    if (model instanceof FieldOption) {
-      FieldOption option = (FieldOption) model;
+    if (model instanceof AbstractOption) {
+      AbstractOption option = (AbstractOption) model;
       IndexedElement source = options.rootSourceOf(option);
       if (source instanceof MessageField) {
         return (MessageField) source;
@@ -300,10 +292,7 @@
     if (model instanceof DefaultValueFieldOption) {
       field = (MessageField) model.eContainer();
     }
-    if (field == null) {
-      return;
-    }
-    if (!messageFields.isOptional(field)) {
+    if (field == null || !messageFields.isOptional(field)) {
       return;
     }
     Enum enumType = finder.enumTypeOf(field);
@@ -390,28 +379,27 @@
 
   private void proposeDefaultKeyword(IndexedElement e, List<String> existingOptionNames, ContentAssistContext context,
       ICompletionProposalAcceptor acceptor) {
-    if (!(e instanceof MessageField)) {
-      return;
+    if (e instanceof MessageField) {
+      MessageField field = (MessageField) e;
+      if (!messageFields.isOptional(field) || existingOptionNames.contains(DEFAULT.toString())) {
+        return;
+      }
+      CompoundElement display = DEFAULT_EQUAL;
+      int cursorPosition = display.charCount();
+      if (messageFields.isString(field)) {
+        display = DEFAULT_EQUAL_STRING;
+        cursorPosition++;
+      }
+      createAndAccept(display, cursorPosition, context, acceptor);
     }
-    MessageField field = (MessageField) e;
-    if (!messageFields.isOptional(field) || existingOptionNames.contains(DEFAULT.toString())) {
-      return;
-    }
-    CompoundElement display = DEFAULT_EQUAL;
-    int cursorPosition = display.charCount();
-    if (messageFields.isString(field)) {
-      display = DEFAULT_EQUAL_STRING;
-      cursorPosition++;
-    }
-    createAndAccept(display, cursorPosition, context, acceptor);
   }
 
   private boolean canBePacked(IndexedElement e) {
-    if (!(e instanceof MessageField)) {
-      return false;
+    if (e instanceof MessageField) {
+      MessageField field = (MessageField) e;
+      return messageFields.isPrimitive(field) && REPEATED.equals(field.getModifier());
     }
-    MessageField field = (MessageField) e;
-    return messageFields.isPrimitive(field) && REPEATED.equals(field.getModifier());
+    return false;
   }
 
   private void proposeOption(MessageField optionSource, ContentAssistContext context,
@@ -450,10 +438,7 @@
     if (model instanceof MessageField) {
       field = (MessageField) model;
     }
-    if (field == null) {
-      return;
-    }
-    if (!messageFields.isOptional(field)) {
+    if (field == null || !messageFields.isOptional(field)) {
       return;
     }
     proposeFieldValue(field, context, acceptor);
@@ -461,18 +446,17 @@
 
   @Override public void completeNativeFieldOption_Value(EObject model, Assignment assignment,
       ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
-    if (!(model instanceof NativeFieldOption)) {
-      return;
+    if (model instanceof NativeFieldOption) {
+      NativeFieldOption option = (NativeFieldOption) model;
+      ProtoDescriptor descriptor = descriptorProvider.primaryDescriptor();
+      MessageField field = (MessageField) options.rootSourceOf(option);
+      Enum enumType = descriptor.enumTypeOf(field);
+      if (enumType != null) {
+        proposeAndAccept(enumType, context, acceptor);
+        return;
+      }
+      proposePrimitiveValues(field, context, acceptor);
     }
-    NativeFieldOption option = (NativeFieldOption) model;
-    ProtoDescriptor descriptor = descriptorProvider.primaryDescriptor();
-    MessageField field = (MessageField) options.rootSourceOf(option);
-    Enum enumType = descriptor.enumTypeOf(field);
-    if (enumType != null) {
-      proposeAndAccept(enumType, context, acceptor);
-      return;
-    }
-    proposePrimitiveValues(field, context, acceptor);
   }
 
   private boolean proposePrimitiveValues(MessageField field, ContentAssistContext context,
@@ -526,12 +510,11 @@
 
   private void completeAbstractCustomOptionSource(EObject model, ContentAssistContext context,
       ICompletionProposalAcceptor acceptor) {
-    if (!(model instanceof AbstractCustomOption)) {
-      return;
+    if (model instanceof AbstractCustomOption) {
+      AbstractCustomOption option = (AbstractCustomOption) model;
+      Collection<IEObjectDescription> scope = scoping().allPossibleSourcesOf(option);
+      proposeAndAcceptOptions(scope, context, acceptor);
     }
-    AbstractCustomOption option = (AbstractCustomOption) model;
-    Collection<IEObjectDescription> scope = scoping().allPossibleSourcesOf(option);
-    proposeAndAcceptOptions(scope, context, acceptor);
   }
 
   private void proposeAndAcceptOptions(Collection<IEObjectDescription> scope, ContentAssistContext context,
@@ -553,22 +536,20 @@
 
   @Override public void completeCustomOption_Fields(EObject model, Assignment assignment,
       ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
-    if (!(model instanceof CustomOption)) {
-      return;
+    if (model instanceof CustomOption) {
+      CustomOption option = (CustomOption) model;
+      proposeAndAccept(scoping().allPossibleNormalFieldsOf(option), context, acceptor);
+      proposeAndAccept(scoping().allPossibleExtensionFieldsOf(option), "(%s)", "(%s)", context, acceptor);
     }
-    CustomOption option = (CustomOption) model;
-    proposeAndAccept(scoping().allPossibleNormalFieldsOf(option), context, acceptor);
-    proposeAndAccept(scoping().allPossibleExtensionFieldsOf(option), "(%s)", "(%s)", context, acceptor);
   }
 
   @Override public void completeCustomFieldOption_Fields(EObject model, Assignment assignment,
       ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
-    if (!(model instanceof CustomFieldOption)) {
-      return;
+    if (model instanceof CustomFieldOption) {
+      CustomFieldOption option = (CustomFieldOption) model;
+      proposeAndAccept(scoping().allPossibleNormalFieldsOf(option), context, acceptor);
+      proposeExtensionFields(scoping().allPossibleExtensionFieldsOf(option), context, acceptor);
     }
-    CustomFieldOption option = (CustomFieldOption) model;
-    proposeAndAccept(scoping().allPossibleNormalFieldsOf(option), context, acceptor);
-    proposeExtensionFields(scoping().allPossibleExtensionFieldsOf(option), context, acceptor);
   }
 
   private void proposeExtensionFields(Collection<IEObjectDescription> scope, ContentAssistContext context,
@@ -602,42 +583,37 @@
 
   @Override public void completeCustomOption_Value(EObject model, Assignment assignment, ContentAssistContext context,
       ICompletionProposalAcceptor acceptor) {
-    if (!(model instanceof CustomOption)) {
-      return;
-    }
-    CustomOption option = (CustomOption) model;
-    IndexedElement e = options.sourceOfLastFieldIn(option);
-    if (e == null) {
-      e = options.rootSourceOf(option);
-    }
-    if (e instanceof MessageField) {
-      proposeFieldValue((MessageField) e, context, acceptor);
+    if (model instanceof CustomOption) {
+      CustomOption option = (CustomOption) model;
+      IndexedElement e = options.sourceOfLastFieldIn(option);
+      if (e == null) {
+        e = options.rootSourceOf(option);
+      }
+      if (e instanceof MessageField) {
+        proposeFieldValue((MessageField) e, context, acceptor);
+      }
     }
   }
 
   @Override public void completeCustomFieldOption_Value(EObject model, Assignment assignment,
       ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
     // TODO content assist returns "{"
-    if (!(model instanceof CustomFieldOption)) {
-      return;
-    }
-    // TODO check if this is the same as sourceOf
-    CustomFieldOption option = (CustomFieldOption) model;
-    IndexedElement e = options.sourceOfLastFieldIn(option);
-    if (e == null) {
-      e = options.rootSourceOf(option);
-    }
-    if (e instanceof MessageField) {
-      proposeFieldValue((MessageField) e, context, acceptor);
+    if (model instanceof CustomFieldOption) {
+      // TODO check if this is the same as sourceOf
+      CustomFieldOption option = (CustomFieldOption) model;
+      IndexedElement e = options.sourceOfLastFieldIn(option);
+      if (e == null) {
+        e = options.rootSourceOf(option);
+      }
+      if (e instanceof MessageField) {
+        proposeFieldValue((MessageField) e, context, acceptor);
+      }
     }
   }
 
   private void proposeFieldValue(MessageField field, ContentAssistContext context,
       ICompletionProposalAcceptor acceptor) {
-    if (field == null) {
-      return;
-    }
-    if (proposePrimitiveValues(field, context, acceptor)) {
+    if (field == null || proposePrimitiveValues(field, context, acceptor)) {
       return;
     }
     Enum enumType = finder.enumTypeOf(field);
@@ -663,24 +639,22 @@
 
   @Override public void completeSimpleValueField_Value(EObject model, Assignment assignment,
       ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
-    if (!(model instanceof SimpleValueField)) {
-      return;
-    }
-    SimpleValueField field = (SimpleValueField) model;
-    FieldName name = field.getName();
-    if (name != null) {
-      proposeFieldValue(name.getTarget(), context, acceptor);
+    if (model instanceof SimpleValueField) {
+      SimpleValueField field = (SimpleValueField) model;
+      FieldName name = field.getName();
+      if (name != null) {
+        proposeFieldValue(name.getTarget(), context, acceptor);
+      }
     }
   }
 
   @Override public void completeSimpleValueField_Name(EObject model, Assignment assignment,
       ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
-    if (!(model instanceof ComplexValue)) {
-      return;
+    if (model instanceof ComplexValue) {
+      ComplexValue value = (ComplexValue) model;
+      proposeAndAccept(scoping().allPossibleNamesOfNormalFieldsOf(value), "%s:", null, context, acceptor);
+      proposeAndAccept(scoping().allPossibleNamesOfExtensionFieldsOf(value), "[%s]:", "[%s]", context, acceptor);
     }
-    ComplexValue value = (ComplexValue) model;
-    proposeAndAccept(scoping().allPossibleNamesOfNormalFieldsOf(value), "%s:", null, context, acceptor);
-    proposeAndAccept(scoping().allPossibleNamesOfExtensionFieldsOf(value), "[%s]:", "[%s]", context, acceptor);
   }
 
   private void proposeAndAccept(Collection<IEObjectDescription> scope, String proposalFormat, String displayFormat,
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/documentation/SLCommentDocumentationProvider.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/documentation/SLCommentDocumentationProvider.java
index 4e51b2e..d136c8a 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/documentation/SLCommentDocumentationProvider.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/documentation/SLCommentDocumentationProvider.java
@@ -62,12 +62,8 @@
   }
 
   private EObject findRealTarget(EObject o) {
-    if (o instanceof Option) {
-      IndexedElement e = options.rootSourceOf((Option) o);
-      return e != null ? e : o;
-    }
-    if (o instanceof FieldOption) {
-      IndexedElement e = options.rootSourceOf((FieldOption) o);
+    if (o instanceof AbstractOption) {
+      IndexedElement e = options.rootSourceOf((AbstractOption) o);
       return e != null ? e : o;
     }
     return o;
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/model/DocumentContentsFactoryRegistry.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/model/DocumentContentsFactoryRegistry.java
index dac5e07..afb9873 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/model/DocumentContentsFactoryRegistry.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/model/DocumentContentsFactoryRegistry.java
@@ -27,13 +27,12 @@
   }
 
   DocumentContentsFactory findFactory(Object element) {
-    if (!(element instanceof IEditorInput)) {
-      return null;
-    }
-    IEditorInput input = (IEditorInput) element;
-    for (DocumentContentsFactory factory : factories) {
-      if (factory.supportsEditorInputType(input)) {
-        return factory;
+    if (element instanceof IEditorInput) {
+      IEditorInput input = (IEditorInput) element;
+      for (DocumentContentsFactory factory : factories) {
+        if (factory.supportsEditorInputType(input)) {
+          return factory;
+        }
       }
     }
     return null;
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/quickfix/ProtobufQuickfixProvider.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/quickfix/ProtobufQuickfixProvider.java
index bcf166c..60b4325 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/quickfix/ProtobufQuickfixProvider.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/quickfix/ProtobufQuickfixProvider.java
@@ -76,10 +76,9 @@
     }
     ISemanticModification modification = new ISemanticModification() {
       @Override public void apply(EObject element, IModificationContext context) throws Exception {
-        if (!(element instanceof Package)) {
-          return;
+        if (element == aPackage) {
+          remove(aPackage);
         }
-        remove(aPackage);
       }
     };
     INode node = findActualNodeFor(aPackage);
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Resources.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Resources.java
index feec96c..6ad1c8d 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Resources.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Resources.java
@@ -45,13 +45,12 @@
   public IProject activeProject() {
     for (IViewReference reference : viewReferencesInActivePage()) {
       IViewPart part = reference.getView(false);
-      if (!(part instanceof ResourceNavigator)) {
-        continue;
+      if (part instanceof ResourceNavigator) {
+        ResourceNavigator navigator = (ResourceNavigator) part;
+        StructuredSelection selection = (StructuredSelection) navigator.getTreeViewer().getSelection();
+        IResource resource = (IResource) selection.getFirstElement();
+        return resource.getProject();
       }
-      ResourceNavigator navigator = (ResourceNavigator) part;
-      StructuredSelection selection = (StructuredSelection) navigator.getTreeViewer().getSelection();
-      IResource resource = (IResource) selection.getFirstElement();
-      return resource.getProject();
     }
     return null;
   }
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/OptionFields.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/OptionFields.java
index 798d7a9..592bb0e 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/OptionFields.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/OptionFields.java
@@ -23,8 +23,6 @@
    * @return the field the given option field is referring to, or {@code null} if one cannot be found.
    */
   public IndexedElement sourceOf(OptionField field) {
-    if (field instanceof MessageOptionField) { return ((MessageOptionField) field).getTarget(); }
-    if (field instanceof ExtensionOptionField) { return ((ExtensionOptionField) field).getTarget(); }
-    return null;
+    return (field == null) ? null : field.getTarget();
   }
 }
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/FieldNotationScopeFinder.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/FieldNotationScopeFinder.java
index 18a735b..849c905 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/FieldNotationScopeFinder.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/FieldNotationScopeFinder.java
@@ -54,10 +54,7 @@
     if (container instanceof ComplexValueField) {
       source = sourceOf((ComplexValueField) container);
     }
-    if (source instanceof MessageField) {
-      return (MessageField) source;
-    }
-    return null;
+    return (source instanceof MessageField) ? (MessageField) source : null;
   }
 
   private MessageField sourceOf(ComplexValueField field) {
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/DataTypeValidator.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/DataTypeValidator.java
index 4f7bbfc..2fc96a9 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/DataTypeValidator.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/DataTypeValidator.java
@@ -83,6 +83,10 @@
     }
   }
 
+  private boolean isInteger(Value value) {
+    return value instanceof LongLink || value instanceof HexNumberLink;
+  }
+
   private void validateUnsignedInteger(DefaultValueFieldOption option) {
     Value value = option.getValue();
     long longValue = longValueIn(value);
@@ -91,10 +95,6 @@
     }
   }
 
-  private boolean isInteger(Value value) {
-    return value instanceof LongLink || value instanceof HexNumberLink;
-  }
-
   private long longValueIn(Value value) {
     if (value instanceof LongLink) {
       LongLink link = (LongLink) value;