Java Code Examples for org.eclipse.xtext.scoping.IScope#getAllElements()

The following examples show how to use org.eclipse.xtext.scoping.IScope#getAllElements() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScopeXpectMethod.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Compares scope including resource name and line number.
 */
@Xpect
@ParameterParser(syntax = "('at' arg1=OFFSET)?")
public void scopeWithPosition( //
		@N4JSCommaSeparatedValuesExpectation IN4JSCommaSeparatedValuesExpectation expectation, //
		ICrossEReferenceAndEObject arg1 //
) {
	EObject eobj = arg1.getEObject();
	IScope scope = scopeProvider.getScope(eobj, arg1.getCrossEReference());
	for (IEObjectDescription eo : scope.getAllElements()) {
		eo.getEObjectURI();
	}
	URI uri = eobj == null ? null : eobj.eResource() == null ? null : eobj.eResource().getURI();
	expectation.assertEquals(new ScopeAwareIterable(uri, true, scope),
			new IsInScopeWithOptionalPositionPredicate(converter,
					uri, true, scope));
}
 
Example 2
Source File: ScopeXpectMethod.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Compares scope including resource name but not line number.
 */
@Xpect
@ParameterParser(syntax = "('at' arg1=OFFSET)?")
public void scopeWithResource( //
		@N4JSCommaSeparatedValuesExpectation IN4JSCommaSeparatedValuesExpectation expectation, //
		ICrossEReferenceAndEObject arg1 //
) {
	EObject eobj = arg1.getEObject();
	IScope scope = scopeProvider.getScope(eobj, arg1.getCrossEReference());
	for (IEObjectDescription eo : scope.getAllElements()) {
		eo.getEObjectURI();
	}
	URI uri = eobj == null ? null : eobj.eResource() == null ? null : eobj.eResource().getURI();
	expectation.assertEquals(new ScopeAwareIterable(uri, false, scope),
			new IsInScopeWithOptionalPositionPredicate(converter,
					uri, false, scope));
}
 
Example 3
Source File: ExpressionScopeTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void assertContains(IScope scope, QualifiedName name) {
	Iterable<IEObjectDescription> elements = scope.getAllElements();
	String toString = elements.toString();
	assertNotNull(toString, scope.getSingleElement(name));
	assertFalse(toString, Iterables.isEmpty(scope.getElements(name)));
	assertTrue(toString, IterableExtensions.exists(elements, it -> Objects.equal(it.getName(), name)));
}
 
Example 4
Source File: ExpressionScopeTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void assertContainsNot(IScope scope, QualifiedName name) {
	Iterable<IEObjectDescription> elements = scope.getAllElements();
	String toString = elements.toString();
	assertNull(toString, scope.getSingleElement(name));
	assertTrue(toString, Iterables.isEmpty(scope.getElements(name)));
	assertFalse(toString, IterableExtensions.exists(elements, it -> Objects.equal(it.getName(), name)));
}
 
Example 5
Source File: AbstractScopingTest.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Check if scope expected is found in context provided.
 *
 * @param context
 *          element from which an element shall be referenced
 * @param reference
 *          to be used to filter the elements
 * @param expectedUriSet
 *          of source referenced
 */
protected void assertScope(final EObject context, final EReference reference, final Set<URI> expectedUriSet) {
  IScope scope = getScopeProvider().getScope(context, reference);
  for (IEObjectDescription description : scope.getAllElements()) {
    expectedUriSet.remove(description.getEObjectURI());
    if (expectedUriSet.isEmpty()) {
      return;
    }
  }
  assertTrue("Expected URIs not found in scope: " + expectedUriSet, expectedUriSet.isEmpty());
}
 
Example 6
Source File: AbstractScopingTest.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Checks if the scope of the given reference for the given context contains only the expected objects.
 * In addition, checks that the reference of the given context references at least one of the expected
 * objects. If the reference has multiplicity > 1, then every reference must reference at least
 * one of the expected objects.
 *
 * @param context
 *          {@link EObject} from which the given objects shall be referenced, must not be {@code null}
 * @param reference
 *          the structural feature of {@code context} for which the scope should be asserted, must not be {@code null} and part of the context element
 * @param expectedObjects
 *          the objects expected in the scope, must not be {@code null}
 */
protected void assertScopedObjects(final EObject context, final EReference reference, final Collection<? extends EObject> expectedObjects) {
  Assert.isNotNull(context, PARAMETER_CONTEXT);
  Assert.isNotNull(reference, PARAMETER_REFERENCE);
  Assert.isNotNull(expectedObjects, PARAMETER_EXPECTED_OBJECTS);
  Assert.isTrue(context.eClass().getEAllReferences().contains(reference), String.format("Contract for argument '%s' failed: Parameter is not within specified range (Expected: %s, Actual: %s).", PARAMETER_CONTEXT, "The context object must contain the given reference.", "Reference not contained by the context object!"));
  Set<URI> expectedUriSet = Sets.newHashSet();
  for (EObject object : expectedObjects) {
    expectedUriSet.add(EcoreUtil.getURI(object));
  }
  IScope scope = getScopeProvider().getScope(context, reference);
  Iterable<IEObjectDescription> allScopedElements = scope.getAllElements();
  Set<URI> scopedUriSet = Sets.newHashSet();
  for (IEObjectDescription description : allScopedElements) {
    URI uri = description.getEObjectURI();
    scopedUriSet.add(uri);
  }
  if (!expectedUriSet.equals(scopedUriSet)) {
    fail("The scope must exactly consist of the expected URIs. Missing " + Sets.difference(expectedUriSet, scopedUriSet) + " extra "
        + Sets.difference(scopedUriSet, expectedUriSet));
  }
  // test that link resolving worked
  boolean elementResolved;
  if (reference.isMany()) {
    @SuppressWarnings("unchecked")
    EList<EObject> objects = (EList<EObject>) context.eGet(reference, true);
    elementResolved = !objects.isEmpty(); // NOPMD
    for (Iterator<EObject> objectIter = objects.iterator(); objectIter.hasNext() && elementResolved;) {
      EObject eObject = EcoreUtil.resolve(objectIter.next(), context);
      elementResolved = expectedUriSet.contains(EcoreUtil.getURI(eObject));
    }
  } else {
    EObject resolvedObject = (EObject) context.eGet(reference, true);
    elementResolved = expectedUriSet.contains(EcoreUtil.getURI(resolvedObject));
  }
  assertTrue("Linking must have resolved one of the expected objects.", elementResolved);
}
 
Example 7
Source File: ImportsAwareReferenceProposalCreator.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
private Iterable<IEObjectDescription> getAllElements(final IScope scope) {
	try (Measurement m = contentAssistDataCollectors.dcGetAllElements().getMeasurement()) {
		return scope.getAllElements();
	}
}
 
Example 8
Source File: N4JSRenameElementProcessor.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
private RefactoringStatus checkDuplicateName(EObject context, String newName) {
	RefactoringStatus status = new RefactoringStatus();
	// Check name conflicts of TMembers
	if (context instanceof TMember) {
		TMember member = (TMember) context;
		status.merge(checkDuplicateMember(member, newName));
	}

	if (status.hasError()) {
		return status;
	}

	if (context instanceof TEnumLiteral) {
		TEnumLiteral enumLit = (TEnumLiteral) context;
		status.merge(checkDuplicateEnum((TEnum) enumLit.eContainer(), newName));
	}

	if (status.hasError()) {
		return status;
	}

	if (context instanceof FormalParameter) {
		FormalParameter fpar = (FormalParameter) context;
		FunctionDefinition method = (FunctionDefinition) fpar.eContainer();
		status.merge(checkDuplicateFormalParam(fpar, method.getFpars(), newName));
	}

	if (status.hasError()) {
		return status;
	}

	// Check name conflicts in variable environment scope using Scope for ContentAssist
	EObject astContext = null;
	if (context instanceof SyntaxRelatedTElement) {
		astContext = ((SyntaxRelatedTElement) context).getAstElement();
	} else {
		astContext = context;
	}

	IScope scope = scopeProvider.getScopeForContentAssist(astContext,
			N4JSPackage.Literals.IDENTIFIER_REF__ID);
	for (IEObjectDescription desc : scope.getAllElements()) {
		if (desc.getName().toString().equals(newName)) {
			status.merge(RefactoringStatus.createFatalErrorStatus(
					"Problem in " + trimPlatformPart(desc.getEObjectURI().trimFragment().toString())
							+ ": Another element in the same scope with name '"
							+ newName + "' already exists"));
			if (status.hasError()) {
				return status;
			}
		}
	}

	return status;
}
 
Example 9
Source File: ReferenceResolutionFinder.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
private Iterable<IEObjectDescription> getAllElements(IScope scope) {
	try (Measurement m = contentAssistDataCollectors.dcGetAllElements().getMeasurement()) {
		return scope.getAllElements();
	}
}
 
Example 10
Source File: AbstractJavaBasedContentProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public Iterable<IEObjectDescription> queryScope(IScope scope, EObject model, EReference reference, Predicate<IEObjectDescription> filter) {
	return scope.getAllElements();
}
 
Example 11
Source File: CrossReferenceTemplateVariableResolver.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected Iterable<IEObjectDescription> queryScope(IScope scope) {
	return scope.getAllElements();
}
 
Example 12
Source File: DefaultQuickfixProvider.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected Iterable<IEObjectDescription> queryScope(IScope scope) {
	return scope.getAllElements();
}
 
Example 13
Source File: IdeCrossrefProposalProvider.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected Iterable<IEObjectDescription> queryScope(IScope scope, CrossReference crossReference,
		ContentAssistContext context) {
	return scope.getAllElements();
}
 
Example 14
Source File: CastedExpressionTypeComputationState.java    From sarl with Apache License 2.0 4 votes vote down vote up
/** Compute the best candidates for the feature behind the cast operator.
 *
 * @param cast the cast operator.
 * @return the candidates.
 */
public List<? extends ILinkingCandidate> getLinkingCandidates(SarlCastedExpression cast) {
	// Prepare the type resolver.
	final StackedResolvedTypes demandComputedTypes = pushTypes();
	final AbstractTypeComputationState forked = withNonVoidExpectation(demandComputedTypes);
	final ForwardingResolvedTypes demandResolvedTypes = new ForwardingResolvedTypes() {
		@Override
		protected IResolvedTypes delegate() {
			return forked.getResolvedTypes();
		}

		@Override
		public LightweightTypeReference getActualType(XExpression expression) {
			final LightweightTypeReference type = super.getActualType(expression);
			if (type == null) {
				final ITypeComputationResult result = forked.computeTypes(expression);
				return result.getActualExpressionType();
			}
			return type;
		}
	};

	// Create the scope
	final IScope scope = getCastScopeSession().getScope(cast,
			// Must be the feature of the AbstractFeatureCall in order to enable the scoping for a function call.
			//XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE,
			SarlPackage.Literals.SARL_CASTED_EXPRESSION__FEATURE,
			demandResolvedTypes);

	// Search for the features into the scope
	final LightweightTypeReference targetType = getReferenceOwner().toLightweightTypeReference(cast.getType());
	final List<ILinkingCandidate> resultList = Lists.newArrayList();
	final LightweightTypeReference expressionType = getStackedResolvedTypes().getActualType(cast.getTarget());
	final ISelector validator = this.candidateValidator.prepare(
			getParent(), targetType, expressionType);
	// FIXME: The call to getAllElements() is not efficient; find another way in order to be faster.
	for (final IEObjectDescription description : scope.getAllElements()) {
		final IIdentifiableElementDescription idesc = toIdentifiableDescription(description);
		if (validator.isCastOperatorCandidate(idesc)) {
			final ExpressionAwareStackedResolvedTypes descriptionResolvedTypes = pushTypes(cast);
			final ExpressionTypeComputationState descriptionState = createExpressionComputationState(cast, descriptionResolvedTypes);
			final ILinkingCandidate candidate = createCandidate(cast, descriptionState, idesc);
			if (candidate != null) {
				resultList.add(candidate);
			}
		}
	}

	return resultList;
}