/*
 * Copyright (c) 2012 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.common.collect.Sets.newHashSet;
import static org.eclipse.xtext.resource.EObjectDescription.create;

import com.google.common.base.Strings;
import com.google.eclipse.protobuf.model.util.MessageFields;
import com.google.eclipse.protobuf.protobuf.Group;
import com.google.eclipse.protobuf.protobuf.IndexedElement;
import com.google.eclipse.protobuf.protobuf.MessageField;
import com.google.inject.Inject;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.resource.IEObjectDescription;

import java.util.Collection;
import java.util.Set;

/**
 * @author alruiz@google.com (Alex Ruiz)
 */
class NormalFieldNameFinderStrategy implements CustomOptionFieldNameFinder.FinderStrategy {
  @Inject private MessageFields messageFields;

  @Override public Collection<IEObjectDescription> findMessageFields(IndexedElement reference) {
    Set<IEObjectDescription> descriptions = newHashSet();
    Iterable<? extends EObject> elements = reference instanceof Group 
        ? ((Group) reference).getElements()
        : reference instanceof MessageField 
            ? messageFields.messageTypeOf((MessageField) reference).getElements()
            : null;

    if (elements != null) {
      for (EObject element : elements) {
        if (element instanceof MessageField) {
          String name = ((MessageField) element).getName();
          descriptions.add(create(name, element));
        } else if (element instanceof Group) {
          String name = ((Group) element).getName();
          if (!Strings.isNullOrEmpty(name)) {
            descriptions.add(create(name, element));
          }
        }
      }
    }
    return descriptions;
  }
}

@@ -16,17 +16,17 @@
  (c) 201
-2
+4
  Google 
@@ -494,16 +494,63 @@
 trings;%0A
+import com.google.common.collect.ImmutableSet;%0A
 import c
@@ -771,16 +771,67 @@
 eField;%0A
+import com.google.eclipse.protobuf.protobuf.OneOf;%0A
 import c
@@ -1292,66 +1292,8 @@
 ) %7B%0A
-    Set%3CIEObjectDescription%3E descriptions = newHashSet();%0A
     
@@ -1357,17 +1357,16 @@
 of Group
- 
 %0A       
@@ -1445,17 +1445,16 @@
 ageField
- 
 %0A       
@@ -1548,16 +1548,149 @@
 : null;%0A
+    return getDescriptions(elements);%0A  %7D%0A%0A  private Set%3CIEObjectDescription%3E getDescriptions(Iterable%3C? extends EObject%3E elements) %7B
 %0A    if 
@@ -1699,17 +1699,17 @@
 lements 
-!
+=
 = null) 
@@ -1708,24 +1708,118 @@
 = null) %7B%0A  
+    return ImmutableSet.of();%0A    %7D%0A    Set%3CIEObjectDescription%3E descriptions = newHashSet();%0A
     for (EOb
@@ -1841,26 +1841,24 @@
 elements) %7B%0A
-  
       if (el
@@ -1890,34 +1890,32 @@
 ield) %7B%0A        
-  
 String name = ((
@@ -1948,34 +1948,32 @@
 Name();%0A        
- 
- 
 descriptions.add
@@ -1993,26 +1993,24 @@
  element));%0A
-  
       %7D else
@@ -2046,26 +2046,24 @@
 ) %7B%0A        
-  
 String name 
@@ -2097,26 +2097,24 @@
 ();%0A        
- 
- 
 if (!Strings
@@ -2133,26 +2133,24 @@
 ty(name)) %7B%0A
-  
           de
@@ -2200,21 +2200,133 @@
     
-  %7D%0A        %7D
+%7D%0A      %7D else if (element instanceof OneOf) %7B%0A        descriptions.addAll(getDescriptions(((OneOf) element).getElements()));
 %0A   

Resolve oneof field names when doing scoping for complex values