use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.
the class TextReplacerContext method getLeadingCharsInLineCount.
@Override
public int getLeadingCharsInLineCount() {
ITextRegionAccess access = getDocument().getRequest().getTextRegionAccess();
int lastOffset = replacer.getRegion().getOffset();
ITextReplacerContext current = this;
int count = 0;
while (current != null) {
Iterator<ITextReplacement> localReplacements = current.getLocalReplacementsReverse().iterator();
while (localReplacements.hasNext()) {
ITextReplacement rep = localReplacements.next();
int endOffset = rep.getEndOffset();
if (endOffset > lastOffset) {
// System.out.println("error");
continue;
}
String between = access.textForOffset(endOffset, lastOffset - endOffset);
int idx = between.lastIndexOf('\n');
if (idx >= 0)
return count + logicalLength(between.substring(idx + 1));
count += logicalLength(between);
String text = rep.getReplacementText();
int idx2 = text.lastIndexOf('\n');
if (idx2 >= 0)
return count + logicalLength(text.substring(idx2 + 1));
count += logicalLength(text);
lastOffset = rep.getOffset();
}
current = current.getPreviousContext();
}
String rest = access.textForOffset(0, lastOffset);
int idx = rest.lastIndexOf('\n');
if (idx >= 0)
return count + logicalLength(rest.substring(idx + 1));
count += lastOffset;
return count;
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.
the class TextReplacerContext method toStringLocal.
protected String toStringLocal() {
List<String> items = Lists.newArrayList();
if (autowrap)
items.add("autowrap");
if (canAutowrap != null && canAutowrap >= 0)
items.add("canAutowrap");
if (replacer != null) {
ITextSegment region = replacer.getRegion();
items.add(format("replacer=[%d-%d-%s|%s]", region.getOffset(), region.getLength(), replacer.getClass().getSimpleName(), replacer.toString()));
}
if (replacements != null)
for (ITextReplacement r : replacements) {
String fmt = "replacement=[%d-%d|%s]";
items.add(format(fmt, r.getOffset(), r.getLength(), r.getReplacementText()));
}
return Joiner.on("; ").join(items);
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.
the class AbstractFormatter2 method postProcess.
protected List<ITextReplacement> postProcess(IFormattableDocument document, List<ITextReplacement> replacements) {
List<ITextSegment> expected = Lists.newArrayList();
IHiddenRegion current = getTextRegionAccess().regionForRootEObject().getPreviousHiddenRegion();
while (current != null) {
if (current.isUndefined() && isInRequestedRange(current.getOffset(), current.getEndOffset()))
expected.addAll(current.getMergedSpaces());
current = current.getNextHiddenRegion();
}
if (expected.isEmpty())
return replacements;
List<ITextSegment> missing = TextRegions.difference(expected, replacements);
if (missing.isEmpty())
return replacements;
List<ITextReplacement> result = Lists.newArrayList(replacements);
for (ITextSegment seg : missing) {
IHiddenRegion h = null;
if (seg instanceof IHiddenRegion)
h = (IHiddenRegion) seg;
if (seg instanceof IHiddenRegionPart)
h = ((IHiddenRegionPart) seg).getHiddenRegion();
if (h != null && (h.getNextSemanticRegion() == null || h.getPreviousSemanticRegion() == null))
result.add(seg.replaceWith(""));
else
result.add(seg.replaceWith(" "));
}
return result;
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-core by eclipse.
the class AbstractFormatter2 method format.
@Override
public final List<ITextReplacement> format(FormatterRequest request) {
try {
initialize(request);
XtextResource xtextResource = request.getTextRegionAccess().getResource();
IFormattableDocument document = createFormattableRootDocument();
try {
format(xtextResource, document);
} catch (RegionTraceMissingException e) {
document = handleTraceMissing(document, e);
}
List<ITextReplacement> rendered = document.renderToTextReplacements();
List<ITextReplacement> postprocessed = postProcess(document, rendered);
return postprocessed;
} finally {
reset();
}
}
use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement in project xtext-eclipse by eclipse.
the class FormatterTester method assertReplacementsAreInRegion.
protected void assertReplacementsAreInRegion(List<ITextReplacement> rep, Collection<ITextRegion> regions, String doc) {
Set<ITextReplacement> invalid = Sets.newHashSet();
ALLOWED: for (ITextRegion allowed : regions) for (ITextReplacement r : rep) {
if (allowed.contains(r))
continue ALLOWED;
invalid.add(r);
}
if (!invalid.isEmpty()) {
String visualized = new TextRegionsToString().addAllReplacements(invalid).toString();
fail("One or more TextReplacements are outside of the allowed region. Region: " + regions, visualized);
}
}
Aggregations