Java Code Examples for org.eclipse.xtext.util.Tuples#create()

The following examples show how to use org.eclipse.xtext.util.Tuples#create() . 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: AbstractTypeProvider.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Gets the containing info for an expression.
 * <p>
 * The containing info for an expression consists of
 * <ul>
 * <li>the EObject that contains the expression,
 * <li>the containment EReference of that containing EObject
 * <li>if the multiplicity is > 1, the index of the expression in the containing EReference; else -1
 * </ul>
 *
 * @param expression
 *          the expression to get the containing info for
 * @return the containing info for {@code expression}
 */
protected Triple<EObject, EReference, Integer> getContainingInfo(final IExpression expression) {
  if (expression == null) {
    return null;
  }
  if (expression.eIsProxy()) {
    return null;
  }
  EReference containmentReference = expression.eContainmentFeature();
  if (containmentReference == null) {
    return null;
  }
  EObject container = expression.eContainer();
  int index = (containmentReference.isMany()) ? ((List<?>) container.eGet(containmentReference)).indexOf(expression) : -1;
  return Tuples.create(container, containmentReference, index);
}
 
Example 2
Source File: NfaToProduction.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected <T> Pair<Integer, StateAlias<T>> findSplitState(StateAlias<T> state, Integer depth,
		Set<StateAlias<T>> visited) {
	if (!visited.add(state))
		return null;
	Pair<Integer, StateAlias<T>> result;
	if (state.getOutgoing().size() > 0 && state.getIncoming().size() > 0
			&& state.getOutgoing().size() + state.getIncoming().size() > 2)
		result = Tuples.create(depth, state);
	else
		result = null;
	for (StateAlias<T> out : state.getOutgoing()) {
		Pair<Integer, StateAlias<T>> cand = findSplitState(out, depth + 1, visited);
		if (cand != null && (result == null || isPreferredSplitState(cand, result)))
			result = cand;
	}
	return result;
}
 
Example 3
Source File: DotEObjectHover.java    From gef with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Out of the box, Xtext supports hovers only for identifying features of
 * model artifacts, i.e. the name of an object or crosslinks to other
 * objects (see locationInFileProvider.getSignificantTextRegion). That's why
 * this customization is needed to be able to also hover on the current dot
 * attribute values.
 */
@Override
protected Pair<EObject, IRegion> getXtextElementAt(XtextResource resource,
		int offset) {

	Pair<EObject, IRegion> result = super.getXtextElementAt(resource,
			offset);

	if (result == null) {
		EObject o = eObjectAtOffsetHelper.resolveElementAt(resource,
				offset);
		if (o != null) {
			// use fullTextRegion instead of the significantTtextRegion
			ITextRegion region = locationInFileProvider
					.getFullTextRegion(o);
			final IRegion region2 = new Region(region.getOffset(),
					region.getLength());
			if (TextUtilities.overlaps(region2, new Region(offset, 0)))
				return Tuples.create(o, region2);
		}
	}
	return result;
}
 
Example 4
Source File: FormattableDocument.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void addReplacer(ITextReplacer replacer) {
	if (!this.getRegion().contains(replacer.getRegion())) {
		String frameTitle = getClass().getSimpleName();
		ITextSegment frameRegion = getRegion();
		String replacerTitle = replacer.getClass().getSimpleName();
		ITextSegment replacerRegion = replacer.getRegion();
		RegionsOutsideFrameException exception = new RegionsOutsideFrameException(frameTitle, frameRegion,
				Tuples.create(replacerTitle, replacerRegion));
		getRequest().getExceptionHandler().accept(exception);
		return;
	}
	try {
		getReplacers().add(replacer, getFormatter().createTextReplacerMerger());
	} catch (ConflictingRegionsException e) {
		getRequest().getExceptionHandler().accept(e);
	}
}
 
Example 5
Source File: ImportsUtil.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected Pair<String, String> splitInTypeAndMember(String featuresQName) {
	int memberIdx = featuresQName.lastIndexOf('.');
	String type = featuresQName.substring(0, memberIdx);
	String member = featuresQName.substring(memberIdx + 1);
	int indexOf = member.indexOf("(");
	if (indexOf != -1) {
		member = member.substring(0, indexOf);
	}
	Pair<String, String> pair = Tuples.create(type, member);
	return pair;
}
 
Example 6
Source File: ConcreteSyntaxConstraintProvider.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected Pair<Set<EClass>, Set<EClass>> getAllSemanticTypesPairs(Set<ISyntaxConstraint> exclude) {
	Set<EClass> mandatory = Sets.newHashSet();
	Set<EClass> optional = Sets.newHashSet();
	boolean allChildrenContributeMandatoryType = !getContents().isEmpty();
	for (ISyntaxConstraint sc : getContents())
		if (exclude == null || !exclude.contains(sc)) {
			Pair<Set<EClass>, Set<EClass>> t = ((SyntaxConstraintNode) sc).getAllSemanticTypesPairs(exclude);
			if (sc.isOptional()) {
				optional.addAll(t.getFirst());
				optional.addAll(t.getSecond());
				allChildrenContributeMandatoryType = false;
			} else {
				mandatory.addAll(t.getFirst());
				optional.addAll(t.getSecond());
				if (t.getFirst().isEmpty())
					allChildrenContributeMandatoryType = false;
			}
		}
	if ((isRoot() && isOptional())
			|| (type == ConstraintType.ALTERNATIVE && !allChildrenContributeMandatoryType)) {
		optional.addAll(mandatory);
		mandatory.clear();
	}
	if (semanticType != null) {
		if (mandatory.isEmpty()
				&& (optional.isEmpty() || (optional.size() == 1 && optional.contains(semanticType))))
			mandatory.add(semanticType);
		else
			optional.add(semanticType);
	}
	if (exclude == null && !isRoot() && mandatory.isEmpty() && optional.isEmpty())
		optional.addAll(((SyntaxConstraintNode) getContainer()).getSemanticTypeByParent(Sets
				.<ISyntaxConstraint> newHashSet(this)));
	return Tuples.create(mandatory, optional);
}
 
Example 7
Source File: AbstractEObjectHover.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Call this method only from within an IUnitOfWork
 */
protected Pair<EObject, IRegion> getXtextElementAt(XtextResource resource, final int offset) {
	// check for cross reference
	EObject crossLinkedEObject = eObjectAtOffsetHelper.resolveCrossReferencedElementAt(resource, offset);
	if (crossLinkedEObject != null) {
		if (!crossLinkedEObject.eIsProxy()) {
			IParseResult parseResult = resource.getParseResult();
			if (parseResult != null) {
				ILeafNode leafNode = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), offset);
				if(leafNode != null && leafNode.isHidden() && leafNode.getOffset() == offset) {
					leafNode = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), offset - 1);
				}
				if (leafNode != null) {
					ITextRegion leafRegion = leafNode.getTextRegion();
					return Tuples.create(crossLinkedEObject, (IRegion) new Region(leafRegion.getOffset(), leafRegion.getLength()));
				}
			}
		}
	} else {
		EObject o = eObjectAtOffsetHelper.resolveElementAt(resource, offset);
		if (o != null) {
			ITextRegion region = locationInFileProvider.getSignificantTextRegion(o);
			final IRegion region2 = new Region(region.getOffset(), region.getLength());
			if (TextUtilities.overlaps(region2, new Region(offset, 0)))
				return Tuples.create(o, region2);
		}
	}
	return null;
}
 
Example 8
Source File: AbstractTypeProvider.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public <E> E get(final Object key, final Resource resource, final Provider<E> provider) {
  if (resource == null) {
    return provider.get();
  }
  CacheAdapter adapter = getOrCreate(resource);
  Pair<AbstractCyclicHandlingSupport<T>, Object> composedKey = Tuples.create(AbstractCyclicHandlingSupport.this, key);
  E element = adapter.<E> get(composedKey);
  // we could have unloaded cached values that were contained in another resource
  // thus #resource was never notified about the change
  boolean validEntry = element != null;
  if (validEntry && element instanceof EObject) {
    validEntry = !((EObject) element).eIsProxy(); // NOPMD
  }
  if (!validEntry) {
    cacheMiss(adapter);
    element = provider.get();
    @SuppressWarnings("unchecked")
    ImmutableLinkedItem<E> linkedItem = (ImmutableLinkedItem<E>) key;
    if (!(linkedItem.object instanceof EObject)) {
      if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("cache skip: " + element); //$NON-NLS-1$
      }
      return element;
    }
    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("cache: " + element); //$NON-NLS-1$
    }
    adapter.set(composedKey, element);
  } else {
    cacheHit(adapter);
  }
  return element;
}
 
Example 9
Source File: AbstractXtextInspectorTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void acceptError(String message, EObject object, EStructuralFeature feature, int index, String code, String... issueData) {
	if (!isExpectingErrors())
		fail("unexpected call to acceptError");
	Triple<String,EObject,EStructuralFeature> error = Tuples.create(message, object, feature);
	errors.add(error);
}
 
Example 10
Source File: MonitoredClusteringBuilderState.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Iterable<IResourceDescription> findExactReferencingResources(final Set<IEObjectDescription> targetObjects, final ReferenceMatchPolicy matchPolicy) {
  Pair<Set<IEObjectDescription>, ReferenceMatchPolicy> key = Tuples.create(targetObjects, matchPolicy);
  Iterable<IResourceDescription> result = findExactReferencingResourcesCache.get(key);
  if (result == null) {
    result = Lists.newArrayList(delegate().findExactReferencingResources(targetObjects, matchPolicy));
    findExactReferencingResourcesCache.put(key, result);
  }
  return result;
}
 
Example 11
Source File: JvmTypesBuilder.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected Pair<String, String> splitQualifiedName(String name) {
	String simpleName = name;
	String packageName = null;
	final int dotIdx = name.lastIndexOf('.');
	if (dotIdx != -1) {
		simpleName = name.substring(dotIdx + 1);
		packageName = name.substring(0, dotIdx);
	}
	Pair<String,String> fullName = Tuples.create(packageName, simpleName);
	return fullName;
}
 
Example 12
Source File: XbaseSeverityConverter.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns decoded delegation key or <code>null</code> if encodedValue can not be parsed.
 * @return {@link Pair} where getFirst() is delegationKey and getSecond() is the defaultSeverity.
 * @see XbaseSeverityConverter#encodeDefaultSeverity(String, String)
 */
public static Pair<String, String> decodeDelegationKey(String encodedValue) {
	List<String> split = Strings.split(encodedValue, DEFAULT_SEVERITY_SEPARATOR);
	if (split.size() == 2) {
		return Tuples.create(split.get(0), split.get(1));
	} else if (split.size() == 1) {
		return Tuples.create(split.get(0), SeverityConverter.SEVERITY_WARNING);
	} else {
		return null;
	}
}
 
Example 13
Source File: BuiltinSchemeUriMapperContribution.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Iterable<Pair<IStorage, IProject>> getStorages(URI uri) {
	if (N4Scheme.isN4Scheme(uri)) {
		Pair<IStorage, IProject> result = Tuples.create(new N4SchemeURIBasedStorage(uri.trimFragment(), registrar),
				null);
		return Collections.singletonList(result);
	}
	return Collections.emptyList();
}
 
Example 14
Source File: MarkOccurrencesTest.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected Triple<String, Integer, Integer> tuple(Annotation a, Position position) {
	return Tuples.create(a.getType(), position.getOffset(), position.getLength());
}
 
Example 15
Source File: ValidEntryRuleInspector.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Pair<Boolean, Boolean> caseAbstractElement(AbstractElement object) {
	return Tuples.create(false, false);
}
 
Example 16
Source File: AbstractFormattingConfig.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Pair<AbstractElement, AbstractElement> matchBetween() {
	return Tuples.create(after, before);
}
 
Example 17
Source File: EClassifierInfos.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
private Triple<String, String, String> getKey(AbstractMetamodelDeclaration metamodelDecl, String name) {
	if (metamodelDecl == null || name == null)
		throw new NullPointerException("metamodelDecl: " + metamodelDecl + " / name: " + name);
	return Tuples.create(metamodelDecl.getEPackage().getNsURI(), Strings.emptyIfNull(metamodelDecl.getAlias()), name);
}
 
Example 18
Source File: ContextPredicateProvider.java    From statecharts with Eclipse Public License 1.0 4 votes vote down vote up
protected Pair<EClass, EReference> key(EClass eClass, EReference ref) {
	return Tuples.create(eClass, ref);
}
 
Example 19
Source File: XbaseImportedNamespaceScopeProvider.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected Object getKey(Notifier context, EReference reference) {
	return Tuples.create(XImportSectionNamespaceScopeProvider.class, context, reference);
}
 
Example 20
Source File: ImportsUtil.java    From xtext-eclipse with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Collects import declarations in XtextResource for the given range (offset -> endOffset)
 * 
 * @param xtextResource
 *            {@link XtextResource} to work with
 * @param textRegion
 *            start and end of the range to inspect
 * @return {@link Triple} where the first element holds the Type imports, the second staticImport and the third
 *         static extension Imports
 */
public Triple<Set<String>, Set<String>, Set<String>> collectImports(XtextResource xtextResource,
		final ITextRegion textRegion) {
	DefaultImportsAcceptor acceptor = new DefaultImportsAcceptor();
	importsCollector.collectImports(xtextResource, textRegion, acceptor);
	return Tuples.create(acceptor.getTypes(), acceptor.getStaticImport(), acceptor.getExtensions());
}