| /** |
| * |
| * Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Florian Pirchner - Initial implementation |
| * |
| * This copyright notice shows up in the generated Java code |
| * |
| */ |
| package org.eclipse.osbp.xtext.oxtype.scoping.jvmtype |
| |
| import com.google.common.collect.Lists |
| import java.util.Iterator |
| import java.util.List |
| import org.eclipse.emf.ecore.EObject |
| import org.eclipse.xtext.common.types.JvmType |
| import org.eclipse.xtext.naming.QualifiedName |
| import org.eclipse.xtext.resource.IEObjectDescription |
| import org.eclipse.xtext.scoping.IScope |
| |
| abstract class AbstractOXTypeTypeScope implements IScope { |
| |
| override IEObjectDescription getSingleElement(QualifiedName name) { |
| throw new UnsupportedOperationException(); |
| } |
| |
| override Iterable<IEObjectDescription> getElements(QualifiedName name) { |
| throw new UnsupportedOperationException(); |
| } |
| |
| override IEObjectDescription getSingleElement(EObject object) { |
| val Iterator<IEObjectDescription> elements = getElements(object).iterator(); |
| if (elements.hasNext()) { |
| var IEObjectDescription result = elements.next(); |
| var String resultName = result.getName().toString(); |
| while(elements.hasNext()) { |
| val IEObjectDescription candidate = elements.next(); |
| val String candidateName = candidate.getName().toString(); |
| if (candidateName.length() < resultName.length()) { |
| result = candidate; |
| resultName = candidateName; |
| } else if (candidateName.length() == resultName.length()) { |
| // prefer '.' over '$' |
| if (candidate.getQualifiedName().getSegmentCount() > result.getQualifiedName().getSegmentCount()) { |
| result = candidate; |
| resultName = candidateName; |
| } |
| } |
| } |
| return result; |
| } |
| return null; |
| } |
| |
| override final Iterable<IEObjectDescription> getElements(EObject object) { |
| if (!(object instanceof JvmType) || object.eIsProxy()) { |
| throw new IllegalArgumentException(String.valueOf(object)); |
| } |
| val List<IEObjectDescription> result = Lists.newLinkedList(); |
| doGetElements( object as JvmType, result); |
| val Iterator<IEObjectDescription> iterator = result.iterator(); |
| while(iterator.hasNext()) { |
| val IEObjectDescription description = iterator.next(); |
| val IEObjectDescription lookUp = getSingleElement(description.getName()); |
| if (lookUp == null || lookUp.getEObjectOrProxy() != object) { |
| iterator.remove(); |
| } |
| } |
| return result; |
| } |
| |
| def protected void doGetElements(JvmType type, List<IEObjectDescription> result); |
| |
| override Iterable<IEObjectDescription> getAllElements() { |
| throw new UnsupportedOperationException(); |
| } |
| |
| } |