Java Code Examples for org.eclipse.xtext.formatting2.ITextReplacerContext#getPreviousContext()

The following examples show how to use org.eclipse.xtext.formatting2.ITextReplacerContext#getPreviousContext() . 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: TextReplacerContext.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public List<ITextReplacement> getReplacementsUntil(ITextReplacerContext first) {
	ITextReplacerContext current = this;
	List<Iterable<ITextReplacement>> reversedReplacements = Lists.newArrayList();
	while (current != null) {
		Iterable<ITextReplacement> localReplacements = current.getLocalReplacements();
		if (!Iterables.isEmpty(localReplacements))
			reversedReplacements.add(localReplacements);
		if (current == first)
			break;
		current = current.getPreviousContext();
	}
	Collections.reverse(reversedReplacements);
	List<ITextReplacement> flattenedReplacements = new TextReplacementList<ITextReplacement>();
	for (Iterable<ITextReplacement> chunk : reversedReplacements)
		Iterables.addAll(flattenedReplacements, chunk);
	return flattenedReplacements;
}
 
Example 2
Source File: TextReplacerContext.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public ITextReplacerContext withReplacer(ITextReplacer replacer) {
	ITextReplacerContext current = this;
	while (current != null) {
		ITextReplacer lastReplacer = current.getReplacer();
		if (lastReplacer != null) {
			if (nextReplacerIsChild) {
				Preconditions.checkArgument(lastReplacer.getRegion().contains(replacer.getRegion()));
			} else {
				Preconditions
						.checkArgument(lastReplacer.getRegion().getEndOffset() <= replacer.getRegion().getOffset());
			}
			break;
		}
		current = current.getPreviousContext();
	}
	return new TextReplacerContext(document, this, indentation, replacer);
}
 
Example 3
Source File: TextReplacerContext.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected ITextSegment getRegion(int index) {
	ITextReplacerContext current = this;
	while (current != null) {
		ITextReplacer replacer2 = current.getReplacer();
		if (replacer2 != null) {
			if (index == 0) {
				return replacer2.getRegion();
			} else
				index--;
		}
		current = current.getPreviousContext();
	}
	return null;
}
 
Example 4
Source File: FormattableDocument.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected ITextReplacerContext createReplacements(ITextReplacerContext previous) {
	Integer maxLineWidth = getRequest().getPreferences().getPreference(FormatterPreferenceKeys.maxLineWidth);
	ITextReplacerContext context = previous.withDocument(this);
	ITextReplacerContext wrappable = null;
	Set<ITextReplacer> wrapped = Sets.newHashSet();
	Iterator<ITextReplacer> replacers = getReplacers().iterator();
	while (replacers.hasNext()) {
		ITextReplacer replacer = replacers.next();
		context = context.withReplacer(replacer);
		if (wrappable != null && context.isWrapSincePrevious()) {
			wrappable = null;
		}
		if (wrappable != null && needsAutowrap(wrappable, context, maxLineWidth)) {
			// TODO: raise report if replacer claims it can do autowrap but
			// then doesn't
			while (context != wrappable) {
				context = context.getPreviousContext();
			}
			replacer = context.getReplacer();
			replacers = getReplacers().iteratorAfter(replacer);
			context.setAutowrap(true);
			wrappable = null;
		}
		ITextReplacerContext nextContext = replacer.createReplacements(context);
		if (wrappable != null && context.isWrapInRegion()) {
			wrappable = null;
		} else {
			Integer canAutowrap = context.canAutowrap();
			if (canAutowrap != null && canAutowrap >= 0 && !context.isAutowrap() && !wrapped.contains(replacer)) {
				boolean can = true;
				if (wrappable != null) {
					int lastEndOffset = wrappable.canAutowrap()
							+ wrappable.getReplacer().getRegion().getEndOffset();
					int thisEndOffset = canAutowrap + context.getReplacer().getRegion().getEndOffset();
					can = lastEndOffset < thisEndOffset;
				}
				if (can) {
					wrappable = context;
					wrapped.add(replacer);
				}
			}
		}
		context = nextContext;
	}
	return context.withDocument(previous.getDocument());
}