Java Code Examples for org.eclipse.xtext.xbase.lib.Procedures.Procedure2#apply()

The following examples show how to use org.eclipse.xtext.xbase.lib.Procedures.Procedure2#apply() . 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: DocumentRewriterTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected String rewrite(final CharSequence model, final Procedure2<? super DocumentRewriter, ? super XtextResource> test) {
  try {
    String _xblockexpression = null;
    {
      final XtextDocument document = this.createDocument(model.toString());
      final IUnitOfWork<TextEdit, XtextResource> _function = (XtextResource it) -> {
        TextEdit _xblockexpression_1 = null;
        {
          final DocumentRewriter rewriter = this.factory.create(document, it);
          test.apply(rewriter, it);
          _xblockexpression_1 = this._replaceConverter.convertToTextEdit(rewriter.getChanges());
        }
        return _xblockexpression_1;
      };
      document.<TextEdit>readOnly(_function).apply(document);
      _xblockexpression = document.get();
    }
    return _xblockexpression;
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example 2
Source File: DefaultCallHierarchyBuilder.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected void findSourceDeclarations(URI targetDeclarationURI, IProgressMonitor monitor,
		Procedure2<? super IEObjectDescription, ? super IReferenceDescription> acceptor) {
	TargetURIs targetURIs = collectTargetURIs(targetDeclarationURI);
	ReferenceAcceptor referenceAcceptor = new ReferenceAcceptor(getResourceServiceProviderRegistry(),
			reference -> {
				if (filterReference(reference)) {
					IEObjectDescription sourceDeclaration = null;
					if (reference != null) {
						sourceDeclaration = findSourceDeclaration(reference);
					}
					acceptor.apply(sourceDeclaration, reference);
				}
			});
	getReferenceFinder().findAllReferences(targetURIs, getResourceAccess(), getIndexData(), referenceAcceptor,
			monitor);
}
 
Example 3
Source File: ExtraLanguagePreferenceAccess.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Parse the given input which is the preference string representation.
 *
 * @param input the string representation from the preferences.
 * @param output the function to call for saving the parsed element. The first parameter is the element to be
 *     converted. The second parameter is the target string.
 * @return {@code true} if a data was parsed. {@code false} if no value was parsed.
 */
public static boolean parseConverterPreferenceValue(String input, Procedure2<? super String, ? super String> output) {
	final StringTokenizer tokenizer = new StringTokenizer(input, PREFERENCE_SEPARATOR);
	String key = null;
	boolean foundValue = false;
	while (tokenizer.hasMoreTokens()) {
		final String token = tokenizer.nextToken();
		if (key != null) {
			output.apply(key,  token);
			foundValue = true;
			key = null;
		} else {
			key = token;
		}
	}
	return foundValue;
}
 
Example 4
Source File: ClassFileCache.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public boolean popCompileResult(char[] fileName,
		Procedure2<? super List<String>, ? super Map<String, byte[]>> consumer) {
	if (allTopLevelTypes.containsKey(fileName)) {
		consumer.apply(allTopLevelTypes.remove(fileName), allClassMaps.remove(fileName));
		return true;
	}
	return false;
}
 
Example 5
Source File: ProcedureExtensions.java    From xtext-lib with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Curries a procedure that takes two arguments.
 * 
 * @param procedure
 *            the original procedure. May not be <code>null</code>.
 * @param argument
 *            the fixed first argument of {@code procedure}.
 * @return a procedure that takes one argument. Never <code>null</code>.
 */
@Pure
public static <P1, P2> Procedure1<P2> curry(final Procedure2<? super P1, ? super P2> procedure, final P1 argument) {
	if (procedure == null)
		throw new NullPointerException("procedure");
	return new Procedure1<P2>() {
		@Override
		public void apply(P2 p) {
			procedure.apply(argument, p);
		}
	};
}
 
Example 6
Source File: IteratorExtensions.java    From xtext-lib with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Applies {@code procedure} for each element of the given iterator.
 * The procedure takes the element and a loop counter. If the counter would overflow, {@link Integer#MAX_VALUE}
 * is returned for all subsequent elements. The first element is at index zero.
 * 
 * @param iterator
 *            the iterator. May not be <code>null</code>.
 * @param procedure
 *            the procedure. May not be <code>null</code>.
 */
public static <T> void forEach(Iterator<T> iterator, Procedure2<? super T, ? super Integer> procedure) {
	if (procedure == null)
		throw new NullPointerException("procedure");
	int i = 0;
	while(iterator.hasNext()) {
		procedure.apply(iterator.next(), i);
		if (i != Integer.MAX_VALUE)
			i++;
	}
}
 
Example 7
Source File: ErrorSafeExtensions.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public <T extends EObject> void forEachSafely(ITreeAppendable appendable, Iterable<T> elements,
		Procedure1<? super LoopParams> loopInitializer, Procedure2<? super T, ? super ITreeAppendable> body) {
	if (Iterables.isEmpty(elements)) {
		return;
	}
	LoopParams loopParams = ObjectExtensions.operator_doubleArrow(new LoopParams(), loopInitializer);
	boolean allElementsBroken = Iterables
			.size(Iterables.<T>filter(elements, (T it) -> hasErrors(it))) == Iterables.size(elements);
	ITreeAppendable currentAppendable = null;
	if (allElementsBroken) {
		currentAppendable = openErrorAppendable(appendable, null);
	} else {
		currentAppendable = appendable;
	}
	loopParams.appendPrefix(currentAppendable);
	boolean isFirst = true;
	boolean isFirstBroken = true;
	for (T element : elements) {
		if (!hasErrors(element)) {
			currentAppendable = closeErrorAppendable(appendable, currentAppendable);
			if (!isFirst) {
				loopParams.appendSeparator(appendable);
			}
			isFirst = false;
			body.apply(element, appendable);
		} else {
			if (!allElementsBroken) {
				currentAppendable = openErrorAppendable(appendable, currentAppendable);
			}
			if (!isFirst || !isFirstBroken) {
				loopParams.appendSeparator(currentAppendable);
			}
			isFirstBroken = false;
			try {
				body.apply(element, currentAppendable);
			} catch (Exception ignoreMe) {
			}
		}
	}
	if (!allElementsBroken) {
		currentAppendable = closeErrorAppendable(appendable, currentAppendable);
	}
	loopParams.appendSuffix(currentAppendable);
	closeErrorAppendable(appendable, currentAppendable);
}
 
Example 8
Source File: PyGenerator.java    From sarl with Apache License 2.0 4 votes vote down vote up
/** Generate the given type.
 *
 * @param fullyQualifiedName the fully qualified name of the type.
 * @param name the name of the type.
 * @param isAbstract indicates if the type is abstract.
 * @param superTypes the super types.
 * @param comment the comment.
 * @param ignoreObjectType ignores the "Object" type if the super types.
 * @param members the members.
 * @param it the output.
 * @param context the context.
 * @param memberGenerator the generator of members.
 * @return {@code true} if the type declaration was generated.
 */
@SuppressWarnings({ "checkstyle:parameternumber" })
protected boolean generateTypeDeclaration(
		String fullyQualifiedName,
		String name, boolean isAbstract,
		List<? extends JvmTypeReference> superTypes,
		String comment,
		boolean ignoreObjectType, List<? extends XtendMember> members, PyAppendable it,
		IExtraLanguageGeneratorContext context, Procedure2<? super PyAppendable, ? super IExtraLanguageGeneratorContext> memberGenerator) {
	if (!Strings.isEmpty(name)) {
		if (!generatePythonClassDeclaration(name, isAbstract, superTypes, comment, ignoreObjectType, it, context)
			|| context.getCancelIndicator().isCanceled()) {
			return false;
		}
		//
		it.openScope();
		//
		if (!generateSarlMembers(members, it, context)
			|| context.getCancelIndicator().isCanceled()) {
			return false;
		}
		//
		if (memberGenerator != null) {
			memberGenerator.apply(it,  context);
		}
		//
		if (!generatePythonConstructors(fullyQualifiedName, members, it, context)
			|| context.getCancelIndicator().isCanceled()) {
			return false;
		}
		//
		it.decreaseIndentation().newLine().newLine();
		//
		it.closeScope();
		//
		if (context.getCancelIndicator().isCanceled()) {
			return false;
		}
		return true;
	}
	return false;
}
 
Example 9
Source File: MapExtensions.java    From xtext-lib with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Applies the given {@code procedure} for each {@link java.util.Map.Entry key value pair} of the given {@code map}. 
 * 
 * @param map
 *            the map. May not be <code>null</code>.
 * @param procedure
 *            the procedure. May not be <code>null</code>.
 */
public static <K, V> void forEach(Map<K, V> map, Procedure2<? super K, ? super V> procedure) {
	if (procedure == null)
		throw new NullPointerException("procedure");
	for (Map.Entry<K, V> entry : map.entrySet()) {
		procedure.apply(entry.getKey(), entry.getValue());
	}
}