use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.
the class FormattableDocument method renderToTextReplacements.
@Override
public List<ITextReplacement> renderToTextReplacements() {
ITextReplacerContext first = getFormatter().createTextReplacerContext(this);
ITextReplacerContext last = createReplacements(first);
List<ITextReplacement> replacements = last.getReplacementsUntil(first);
return replacements;
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.
the class MaxLineWidthDocument method createReplacements.
@Override
public ITextReplacerContext createReplacements(ITextReplacerContext context) {
ITextReplacerContext last = super.createReplacements(context);
List<ITextReplacement> replacements = last.getReplacementsUntil(context);
String string = applyTextReplacements(replacements);
if (string.contains("\n"))
throw new FormattingNotApplicableException();
int leadingCharCount = context.getLeadingCharsInLineCount();
int formattedLength = string.length();
int lineLength = leadingCharCount + formattedLength;
if (lineLength > maxLineWidth)
throw new FormattingNotApplicableException();
return last;
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.
the class TextReplacerContext method getReplacementsUntil.
@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;
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.
the class TextReplacerContext method isInUndefinedRegion.
protected boolean isInUndefinedRegion(ITextReplacement repl) {
ITextSegment replacerRegion = replacer.getRegion();
IHiddenRegion hidden = null;
if (replacerRegion instanceof IHiddenRegionPart) {
hidden = ((IHiddenRegionPart) replacerRegion).getHiddenRegion();
} else if (replacerRegion instanceof IHiddenRegion) {
hidden = (IHiddenRegion) replacerRegion;
}
return hidden == null || hidden.isUndefined();
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.
the class TextReplacerContext method isWrapInRegion.
@Override
public boolean isWrapInRegion() {
ITextRegionAccess access = getDocument().getRequest().getTextRegionAccess();
ITextSegment region = getReplacer().getRegion();
int lastOffset = region.getOffset();
for (ITextReplacement rep : this.getLocalReplacements()) {
int endOffset = rep.getOffset();
String between = access.textForOffset(lastOffset, endOffset - lastOffset);
if (between.contains("\n") || rep.getReplacementText().contains("\n")) {
return true;
}
lastOffset = rep.getEndOffset();
}
String rest = access.textForOffset(lastOffset, region.getEndOffset() - lastOffset);
if (rest.contains("\n")) {
return true;
}
return false;
}
Aggregations