use of org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart in project xtext-core by eclipse.
the class HiddenRegionPartAssociator method associate.
@Override
public void associate(IHiddenRegion region, BiConsumer<IHiddenRegionPart, HiddenRegionPartAssociation> handler) {
List<IHiddenRegionPart> parts = region.getParts();
if (!region.isMultiline()) {
// rather than one of the surrounding semantic tokens.
for (IHiddenRegionPart p : parts) {
handler.accept(p, HiddenRegionPartAssociation.CONTAINER);
}
} else {
int i = 0;
if (region.getPreviousSemanticRegion() != null) {
// collect whitespace and comments from the same line as belonging to the PREVIOUS semantic token
while (i < parts.size()) {
IHiddenRegionPart part = parts.get(i);
if (part.isMultiline()) {
if (part.getText().endsWith("\n")) {
handler.accept(part, HiddenRegionPartAssociation.PREVIOUS);
i++;
}
break;
} else {
handler.accept(part, HiddenRegionPartAssociation.PREVIOUS);
i++;
}
}
}
int j = parts.size() - 1;
if (region.getNextSemanticRegion() != null) {
// associated with the NEXT semantic token.
while (j >= i) {
IHiddenRegionPart part = parts.get(j);
if (part instanceof IWhitespace && part.getLineCount() > 2) {
break;
} else {
handler.accept(part, HiddenRegionPartAssociation.NEXT);
j--;
}
}
}
// Associate the remaining tokens with the CONTAINER
for (int k = i; k <= j; k++) {
IHiddenRegionPart part = parts.get(k);
handler.accept(part, HiddenRegionPartAssociation.CONTAINER);
}
}
}
use of org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart 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;
}
Aggregations