org.eclipse.xtext.formatting2.ITextReplacer Java Examples

The following examples show how to use org.eclipse.xtext.formatting2.ITextReplacer. 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: RichStringFormatter.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected void setNewLines(final IFormattableDocument doc, final int offset, final int length, final int indentationIncrease, final int indentationDecrease, final int newLines) {
  IHiddenRegionFormatting _createHiddenRegionFormatting = doc.getFormatter().createHiddenRegionFormatting();
  final Procedure1<IHiddenRegionFormatting> _function = (IHiddenRegionFormatting it) -> {
    it.setIndentationIncrease(Integer.valueOf(indentationIncrease));
    it.setIndentationDecrease(Integer.valueOf(indentationDecrease));
    it.setNewLinesMin(Integer.valueOf(newLines));
    it.setNewLinesDefault(Integer.valueOf(newLines));
    it.setNewLinesMax(Integer.valueOf(newLines));
  };
  final IHiddenRegionFormatting fmt = ObjectExtensions.<IHiddenRegionFormatting>operator_doubleArrow(_createHiddenRegionFormatting, _function);
  AbstractFormatter2 _formatter = doc.getFormatter();
  ITextRegionAccess _textRegionAccess = this._iTextRegionExtensions.getTextRegionAccess();
  TextSegment _textSegment = new TextSegment(_textRegionAccess, offset, length);
  final ITextReplacer replacer = _formatter.createWhitespaceReplacer(_textSegment, fmt);
  doc.addReplacer(replacer);
}
 
Example #2
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 #3
Source File: SARLFormatter.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Override
public ITextReplacer createCommentReplacer(IComment comment) {
	final EObject grammarElement = comment.getGrammarElement();
	if (grammarElement instanceof AbstractRule) {
		final String ruleName = ((AbstractRule) grammarElement).getName();
		CommentReplacer replacer = null;
		if (ruleName.startsWith("ML")) { //$NON-NLS-1$
			replacer = new SARLMultilineCommentReplacer(comment);
		} else if (ruleName.startsWith("SL")) { //$NON-NLS-1$
			replacer = new SARLSinglelineCommentReplacer(comment);
		}
		if (replacer != null) {
			this.injector.injectMembers(replacer);
			return replacer;
		}
	}
	final String elementName = new GrammarElementTitleSwitch().showQualified().showRule().doSwitch(grammarElement);
	throw new IllegalStateException(
			MessageFormat.format(Messages.SARLFormatter_0,
					ITextReplacer.class.getSimpleName(), elementName));
}
 
Example #4
Source File: HiddenRegionReplacer.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public ITextReplacerContext createReplacements(ITextReplacerContext context) {
	AbstractFormatter2 formatter = context.getFormatter();
	List<IHiddenRegionPart> hiddens = region.getParts();
	if (hiddens.isEmpty()) {
		return formatter.createWhitespaceReplacer(region, formatting).createReplacements(context);
	} else if ((hiddens.size() == 1 && hiddens.get(0) instanceof IWhitespace)) {
		return formatter.createWhitespaceReplacer(hiddens.get(0), formatting).createReplacements(context);
	} else {
		List<ITextReplacer> replacers = createReplacers(formatter);
		applyHiddenRegionFormatting(replacers);
		ITextReplacerContext current = context;
		current.setNextReplacerIsChild();
		for (ITextReplacer replacer : replacers)
			current = replacer.createReplacements(current.withReplacer(replacer));
		return current;
	}
}
 
Example #5
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 #6
Source File: TextReplacerMerger.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected ITextReplacer mergeCompositeReplacers(List<? extends ITextReplacer> conflicting) {
	ICompositeTextReplacer composite = null;
	for (ITextReplacer replacer : conflicting)
		if (replacer instanceof ICompositeTextReplacer) {
			if (composite == null)
				composite = ((ICompositeTextReplacer) replacer);
			else
				return null;
		}
	if (composite == null)
		return null;
	for (ITextReplacer r : conflicting)
		if (r != composite)
			composite.addReplacer(r);
	return composite;
}
 
Example #7
Source File: TextReplacerMerger.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected ITextReplacer mergeHiddenRegionReplacers(List<? extends ITextReplacer> conflicting) {
	List<IHiddenRegionFormatting> formattings = Lists.newArrayList();
	IHiddenRegion region = null;
	for (ITextReplacer replacer : conflicting) {
		if (replacer instanceof HiddenRegionReplacer) {
			HiddenRegionReplacer hiddenRegionReplacer = (HiddenRegionReplacer) replacer;
			formattings.add(hiddenRegionReplacer.getFormatting());
			if (region == null)
				region = hiddenRegionReplacer.getRegion();
			else if (region != hiddenRegionReplacer.getRegion())
				return null;
		} else
			return null;
	}
	IHiddenRegionFormatting mergedFormatting = merger.merge(formattings);
	if (mergedFormatting != null)
		return formatter.createHiddenRegionReplacer(region, mergedFormatting);
	return null;
}
 
Example #8
Source File: ConditionalReplacer.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public ITextReplacerContext createReplacements(ITextReplacerContext context) {
	context.setNextReplacerIsChild();
	for (ISubFormatter formatter : subFormatters) {
		try {
			ITextSegment region = getRegion();
			SubDocument subDocument = new SubDocument(region, getDocument());
			for (ITextReplacer replacer : replacers)
				subDocument.addReplacer(replacer);
			formatter.format(subDocument);
			ITextReplacerContext first = context.withReplacer(subDocument);
			ITextReplacerContext last = subDocument.createReplacements(first);
			return last;
		} catch (FormattingNotApplicableException e) {
			// no need to do anything.
			// Try the next SubFormatter until one doens't throw a FormattingNotApplicableException 
		}
	}
	throw new FormattingNotApplicableException();
}
 
Example #9
Source File: FormattableDocument.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IHiddenRegion set(IHiddenRegion hiddenRegion, Procedure1<? super IHiddenRegionFormatter> init) {
	if (hiddenRegion != null) {
		AbstractFormatter2 formatter = getFormatter();
		IHiddenRegionFormatting formatting = formatter.createHiddenRegionFormatting();
		init.apply(formatter.createHiddenRegionFormatter(formatting));
		ITextReplacer replacer = formatter.createHiddenRegionReplacer(hiddenRegion, formatting);
		addReplacer(replacer);
	}
	return hiddenRegion;
}
 
Example #10
Source File: FormattableDocument.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Pair<IHiddenRegion, IHiddenRegion> set(IHiddenRegion first, IHiddenRegion second,
		Procedure1<? super IHiddenRegionFormatter> init) {
	if (first != null && second != null) {
		AbstractFormatter2 formatter = getFormatter();
		IHiddenRegionFormatting f1 = formatter.createHiddenRegionFormatting();
		IHiddenRegionFormatting f2 = formatter.createHiddenRegionFormatting();
		init.apply(formatter.createHiddenRegionFormatter(f1, f2));
		ITextReplacer replacer1 = formatter.createHiddenRegionReplacer(first, f1);
		ITextReplacer replacer2 = formatter.createHiddenRegionReplacer(second, f2);
		addReplacer(replacer1);
		addReplacer(replacer2);
	}
	return Pair.of(first, second);
}
 
Example #11
Source File: FormattableDocument.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected TextSegmentSet<ITextReplacer> createTextReplacerSet() {
	return new ArrayListTextSegmentSet<ITextReplacer>(ITextReplacer.GET_REGION,
			new Function<ITextReplacer, String>() {
				@Override
				public String apply(ITextReplacer input) {
					if (input instanceof HiddenRegionReplacer)
						return new HiddenRegionFormattingToString()
								.apply(((HiddenRegionReplacer) input).getFormatting());
					return input.getClass().getSimpleName();
				}
			}, getRequest().isEnableDebugTracing());
}
 
Example #12
Source File: TextReplacerContext.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected TextReplacerContext(IFormattableDocument document, ITextReplacerContext previous, int indentation,
		ITextReplacer replacer) {
	super();
	this.document = document;
	this.indentation = indentation;
	this.previous = previous;
	this.replacer = replacer;
	this.replacements = createTextReplacementsSet();
}
 
Example #13
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 #14
Source File: TextReplacerMerger.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public ITextReplacer merge(List<? extends ITextReplacer> conflicting) {
	ITextReplacer result = mergeHiddenRegionReplacers(conflicting);
	if (result == null)
		result = mergeCompositeReplacers(conflicting);
	return result;
}
 
Example #15
Source File: HiddenRegionReplacer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected WhitespaceReplacer findWhitespaceThatSeparatesSemanticRegions(List<ITextReplacer> replacers) {
	boolean hasSeenWrap = false;
	for (ITextReplacer replacer : replacers) {
		if (replacer instanceof WhitespaceReplacer) {
			WhitespaceReplacer whitespaceReplacer = (WhitespaceReplacer) replacer;
			hasSeenWrap |= whitespaceReplacer.getRegion().isMultiline();
			if (hasSeenWrap)
				return whitespaceReplacer;
		}
	}
	return (WhitespaceReplacer) replacers.get(replacers.size() - 1);
}
 
Example #16
Source File: RichStringFormatter.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected void suppressLineWraps(final Object it) {
  if (it instanceof HiddenRegionReplacer) {
    _suppressLineWraps((HiddenRegionReplacer)it);
    return;
  } else if (it instanceof IHiddenRegionFormatting) {
    _suppressLineWraps((IHiddenRegionFormatting)it);
    return;
  } else if (it instanceof ITextReplacer) {
    _suppressLineWraps((ITextReplacer)it);
    return;
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(it).toString());
  }
}
 
Example #17
Source File: RichStringFormatter.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected void formatIntoSingleLine(final IFormattableDocument doc, final EObject obj) {
  final Predicate<ITextReplacer> _function = (ITextReplacer it) -> {
    boolean _xblockexpression = false;
    {
      this.suppressLineWraps(it);
      _xblockexpression = true;
    }
    return _xblockexpression;
  };
  doc.getFormatter().format(obj, doc.withReplacerFilter(_function));
}
 
Example #18
Source File: RichStringFormatter.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected void setSpace(final IFormattableDocument doc, final int offset, final int length, final String space) {
  IHiddenRegionFormatting _createHiddenRegionFormatting = doc.getFormatter().createHiddenRegionFormatting();
  final Procedure1<IHiddenRegionFormatting> _function = (IHiddenRegionFormatting it) -> {
    it.setSpace(space);
  };
  final IHiddenRegionFormatting fmt = ObjectExtensions.<IHiddenRegionFormatting>operator_doubleArrow(_createHiddenRegionFormatting, _function);
  AbstractFormatter2 _formatter = doc.getFormatter();
  ITextRegionAccess _textRegionAccess = this._iTextRegionExtensions.getTextRegionAccess();
  TextSegment _textSegment = new TextSegment(_textRegionAccess, offset, length);
  final ITextReplacer replacer = _formatter.createWhitespaceReplacer(_textSegment, fmt);
  doc.addReplacer(replacer);
}
 
Example #19
Source File: TextReplacerContext.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public ITextReplacer getReplacer() {
	return replacer;
}
 
Example #20
Source File: BugMultilineCommentIndentation.java    From sarl with Apache License 2.0 4 votes vote down vote up
@Override
public ITextReplacerContext withReplacer(ITextReplacer replacer) {
	return this.context.withReplacer(replacer);
}
 
Example #21
Source File: FormattableDocument.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public IFormattableSubDocument withReplacerFilter(Predicate<? super ITextReplacer> filter) {
	return new FilteredSubDocument(getRegion(), this, filter);
}
 
Example #22
Source File: BugMultilineCommentIndentation.java    From sarl with Apache License 2.0 4 votes vote down vote up
@Override
public ITextReplacer getReplacer() {
	return this.context.getReplacer();
}
 
Example #23
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());
}
 
Example #24
Source File: FormattableDocument.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected TextSegmentSet<ITextReplacer> getReplacers() {
	if (replacers == null)
		replacers = createTextReplacerSet();
	return replacers;
}
 
Example #25
Source File: MaxLineWidthDocument.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected void validate(ITextReplacer replacer) throws FormattingNotApplicableException {
	if (replacer instanceof HiddenRegionReplacer)
		validate((HiddenRegionReplacer) replacer);
}
 
Example #26
Source File: MaxLineWidthDocument.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void addReplacer(ITextReplacer replacer) {
	validate(replacer);
	super.addReplacer(replacer);
}
 
Example #27
Source File: ConditionalReplacer.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void addReplacer(ITextReplacer replacer) {
	replacers.add(replacer);
}
 
Example #28
Source File: FilteredSubDocument.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void addReplacer(ITextReplacer replacer) {
	if (filter.apply(replacer))
		super.addReplacer(replacer);
}
 
Example #29
Source File: FilteredSubDocument.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public FilteredSubDocument(ITextSegment region, IFormattableDocument parent, Predicate<? super ITextReplacer> filter) {
	super(region, parent);
	this.filter = filter;
}
 
Example #30
Source File: RichStringFormatter.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
protected void _suppressLineWraps(final ITextReplacer it) {
}