<commit_before>/*******************************************************************************
 * Copyright (c) 2008 itemis AG (http://www.itemis.eu) and others.
 * 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 org.eclipse.xtext.crossref.indexImpl;

import java.util.Iterator;

import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.index.EObjectDescriptor;
import org.eclipse.emf.index.IIndexStore;
import org.eclipse.emf.index.ResourceDescriptor;
import org.eclipse.emf.index.EObjectDescriptor.Query;
import org.eclipse.emf.index.ecore.EClassDescriptor;
import org.eclipse.xtext.crossref.IScope;
import org.eclipse.xtext.crossref.IScopedElement;
import org.eclipse.xtext.crossref.impl.AbstractScopeProvider;
import org.eclipse.xtext.crossref.impl.SimpleAttributeResolver;
import org.eclipse.xtext.crossref.impl.SimpleNestedScope;
import org.eclipse.xtext.util.AdaptingIterable;
import org.eclipse.xtext.util.Function;

import com.google.inject.Inject;

/**
 * @author <NAME> - Initial contribution and API
 */
public class IndexBasedScopeProvider extends AbstractScopeProvider {

	private final IIndexStore index;

	@Inject
	public IndexBasedScopeProvider(IIndexStore index) {
		this.index = index;
	}

	public IScope getScope(EObject context, EClass type) {
		Resource eResource = context.eResource();
		Iterable<IScopedElement> iterable = getElements(eResource.getURI().toPlatformString(true), type);
		TreeIterator<EObject> eAllContents = EcoreUtil.getRootContainer(context).eAllContents();

		return new SimpleNestedScope(createOuter(eAllContents, type), iterable);
	}

	private Iterable<IScopedElement> getElements(String uri, EClass eClazz) {
		ResourceDescriptor descriptor = index.resourceDAO().createQuery().uri(uri).executeSingleResult();
		EClassDescriptor clazzDesc = index.eClassDAO().createQueryEClass(eClazz).executeSingleResult();
		Query eClassQuery = index.eObjectDAO().createQuery().resource(descriptor).eClass(clazzDesc);
		final Iterable<EObjectDescriptor> iter = eClassQuery.executeListResult();
		return AdaptingIterable.create(iter, new Function<EObjectDescriptor, IScopedElement>(){

			public IScopedElement exec(EObjectDescriptor param) {
				return new IndexBasedScopedElement(param);
			}});
	}

	private final SimpleAttributeResolver<String> importResolver = SimpleAttributeResolver.newResolver(String.class, "importURI");

	/**
	 * @param context
	 * @param reference
	 * @return
	 */
	private IScope createOuter(Iterator<EObject> iter, EClass type) {
		while (iter.hasNext()) {
			EObject object = iter.next();
			String uri = importResolver.getValue(object);
			if (uri != null) {
				Iterable<IScopedElement> elements = getElements(uri, type);
				return new SimpleNestedScope(createOuter(iter, type), elements);
			}
		}
		return IScope.NULLSCOPE;
	}

}
<commit_msg>Simplified 3 queries to one<commit_after>/*******************************************************************************
 * Copyright (c) 2008 itemis AG (http://www.itemis.eu) and others.
 * 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 org.eclipse.xtext.crossref.indexImpl;

import java.util.Iterator;

import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.index.EObjectDescriptor;
import org.eclipse.emf.index.IIndexStore;
import org.eclipse.emf.index.EObjectDescriptor.Query;
import org.eclipse.xtext.crossref.IScope;
import org.eclipse.xtext.crossref.IScopedElement;
import org.eclipse.xtext.crossref.impl.AbstractScopeProvider;
import org.eclipse.xtext.crossref.impl.SimpleAttributeResolver;
import org.eclipse.xtext.crossref.impl.SimpleNestedScope;
import org.eclipse.xtext.util.AdaptingIterable;
import org.eclipse.xtext.util.Function;

import com.google.inject.Inject;

/**
 * @author <NAME> - Initial contribution and API
 */
public class IndexBasedScopeProvider extends AbstractScopeProvider {
	
	private final IIndexStore index;
	
	@Inject
	public IndexBasedScopeProvider(IIndexStore index) {
		this.index = index;
	}

	public IScope getScope(EObject context, EClass type) {
		Resource eResource = context.eResource();
		Iterable<IScopedElement> iterable = getElements(eResource.getURI().toPlatformString(true), type);
		TreeIterator<EObject> eAllContents = EcoreUtil.getRootContainer(context).eAllContents();
			
		return new SimpleNestedScope(createOuter(eAllContents, type), iterable);
	}

	private Iterable<IScopedElement> getElements(String uri, EClass eClazz) {
		Query eClassQuery = index.eObjectDAO().createQuery();
		eClassQuery.resource().uri(uri);
		eClassQuery.eClass().name(eClazz.getName()).ePackage().nsURI(eClazz.getEPackage().getNsURI());
		final Iterable<EObjectDescriptor> iter = eClassQuery.executeListResult();
		return AdaptingIterable.create(iter, new Function<EObjectDescriptor, IScopedElement>(){

			public IScopedElement exec(EObjectDescriptor param) {
				return new IndexBasedScopedElement(param);
			}});
	}

	private final SimpleAttributeResolver<String> importResolver = SimpleAttributeResolver.newResolver(String.class, "importURI");
	/**
	 * @param context
	 * @param reference
	 * @return
	 */
	private IScope createOuter(Iterator<EObject> iter, EClass type) {
		while (iter.hasNext()) {
			EObject object = iter.next();
			String uri = importResolver.getValue(object);
			if (uri != null) {
				Iterable<IScopedElement> elements = getElements(uri, type);
				return new SimpleNestedScope(createOuter(iter, type), elements);
			}
		}
		return IScope.NULLSCOPE;
	}

}

{'fraction_non_alphanumeric': 0.15402731983042864, 'fraction_numerical': 0.0026691788349819437, 'mean_word_length': 5.908893709327549, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '835627', 'n_tokens_mistral': 1968, 'n_tokens_neox': 1851, 'n_words': 378}