use of org.eclipse.xtext.formatting2.regionaccess.ITextSegment in project xtext-core by eclipse.
the class FormatterTestHelper method assertAllWhitespaceIsFormatted.
protected void assertAllWhitespaceIsFormatted(ITextRegionAccess access, List<ITextReplacement> replacements) {
List<ITextSegment> expected = Lists.newArrayList();
IHiddenRegion current = access.regionForRootEObject().getPreviousHiddenRegion();
while (current != null) {
expected.addAll(current.getMergedSpaces());
current = current.getNextHiddenRegion();
}
List<ITextSegment> missing = TextRegions.difference(expected, replacements);
if (!missing.isEmpty()) {
TextRegionsToString toString = new TextRegionsToString().setTextRegionAccess(access);
for (ITextSegment region : missing) toString.add(region, region.getClass().getSimpleName());
String msg = "The following regions are not formatted:\n" + toString;
System.err.println(msg);
Assert.fail(msg);
}
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextSegment in project xtext-core by eclipse.
the class RegionDiffFormatter method mergeReplacements.
protected List<ITextReplacement> mergeReplacements(ITextRegionAccessDiff regions, List<ITextReplacement> rep) {
ITextRegionRewriter rewriter = regions.getOriginalTextRegionAccess().getRewriter();
List<ITextReplacement> result = Lists.newArrayList();
for (ITextSegmentDiff r : regions.getRegionDifferences()) {
ITextSegment original = r.getOriginalRegion();
ITextSegment modified = r.getModifiedRegion();
List<ITextReplacement> local = Lists.newArrayList();
for (ITextReplacement re : rep) {
if (modified.contains(re)) {
local.add(re);
}
}
String text;
if (local.isEmpty()) {
text = modified.getText();
} else {
text = regions.getRewriter().renderToString(modified, local);
}
ITextReplacement replacement = rewriter.createReplacement(original.getOffset(), original.getLength(), text);
result.add(replacement);
}
return result;
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextSegment in project xtext-core by eclipse.
the class TextRegionRewriter method renderToAppendable.
@Override
public void renderToAppendable(ITextSegment input, Iterable<? extends ITextReplacement> rep, Appendable result) throws IOException {
int offset = input.getOffset();
String text = input.getText();
List<ITextReplacement> list = new TextReplacementList<ITextReplacement>(rep);
Collections.sort(list);
int lastOffset = 0;
for (ITextReplacement r : list) {
result.append(text.subSequence(lastOffset, r.getOffset() - offset));
result.append(r.getReplacementText());
lastOffset = (r.getOffset() - offset) + r.getLength();
}
if (lastOffset <= text.length())
result.append(text.subSequence(lastOffset, text.length()));
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextSegment in project xtext-core by eclipse.
the class RegionsOutsideFrameException method getMessage.
@Override
public String getMessage() {
String regionsStr;
if (regions.length == 1) {
regionsStr = "region " + regions[0].getFirst();
} else {
List<String> titles = Lists.newArrayList();
for (Pair<String, ITextSegment> p : regions) titles.add(p.getFirst());
regionsStr = "regions " + Joiner.on(", ").join(titles);
}
StringBuilder builder = new StringBuilder();
builder.append("The " + regionsStr + " must not be outside " + frameTitle + ".\n");
TextRegionsWithTitleToString toStr = new TextRegionsWithTitleToString();
toStr.add(frameTitle, frameRegion);
for (Pair<String, ITextSegment> p : regions) toStr.add(p.getFirst(), p.getSecond());
builder.append(toStr);
return builder.toString();
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextSegment 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();
}
Aggregations