use of org.eclipse.xtext.formatting2.ITextReplacer in project xtext-core by eclipse.
the class FormattableDocument method addReplacer.
@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);
}
}
use of org.eclipse.xtext.formatting2.ITextReplacer in project xtext-xtend by eclipse.
the class RichStringFormatter method setSpace.
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);
}
use of org.eclipse.xtext.formatting2.ITextReplacer in project xtext-core by eclipse.
the class ConditionalReplacer method createReplacements.
@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();
}
use of org.eclipse.xtext.formatting2.ITextReplacer in project xtext-core by eclipse.
the class FormattableDocument method createReplacements.
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)) {
// 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());
}
use of org.eclipse.xtext.formatting2.ITextReplacer in project xtext-core by eclipse.
the class FormattableDocument method toString.
@Override
public String toString() {
TextRegionsToString toString = new TextRegionsToString();
toString.setFrame(this.getRegion());
toString.setTitle(getClass().getSimpleName() + " with ITextReplacers");
for (ITextReplacer repl : getReplacers()) toString.add(repl.getRegion(), repl.getClass().getSimpleName() + ": " + repl.toString());
return toString.toString();
}
Aggregations