use of org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion in project xtext-core by eclipse.
the class FormattableDocument method surround.
@Override
public <T extends EObject> T surround(T owner, Procedure1<? super IHiddenRegionFormatter> beforeAndAfter) {
if (owner != null && !owner.eIsProxy()) {
IEObjectRegion region = getTextRegionAccess().regionForEObject(owner);
if (region == null)
return owner;
IHiddenRegion previous = region.getPreviousHiddenRegion();
IHiddenRegion next = region.getNextHiddenRegion();
set(previous, next, beforeAndAfter);
}
return owner;
}
use of org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion in project xtext-core by eclipse.
the class PartialSerializer method trySerializeMultiValue.
protected List<SerializationStrategy> trySerializeMultiValue(EObject owner, FeatureChange change, IEObjectRegion ownerRegion, IConstraint constraint) {
EStructuralFeature feature = change.getFeature();
List<SerializationStrategy> result = Lists.newArrayList();
IFeatureInfo featureInfo = constraint.getFeatures()[owner.eClass().getFeatureID(feature)];
List<IConstraintElement> assignments = featureInfo.getAssignments();
if (assignments.size() != 1) {
return null;
}
IConstraintElement assignment = assignments.get(0);
if (!assignment.isMany()) {
return null;
}
List<IAstRegion> originals = findRegions(ownerRegion, change);
EList<ListChange> listChanges = change.getListChanges();
if (listChanges.isEmpty() && originals.isEmpty()) {
ISerializationContext ctx = getSerializationContext(owner);
AbstractElement ins = assignment.getGrammarElement();
IHiddenRegion insertAt = insertionPointFinder.findInsertionPoint(ctx, ownerRegion, ins);
if (insertAt == null) {
return null;
}
for (Object value : (List<?>) owner.eGet(feature)) {
EObject obj = (EObject) value;
ISerializationContext context = getSerializationContext(obj);
result.add(new SerializeRecursiveStrategy(insertAt, obj, context));
}
return result;
}
// ListTransient listTransient = transientValues.isListTransient(owner, feature);
List<Object> modifying = Lists.newArrayList(((List<?>) owner.eGet(feature)));
for (ListChange lc : listChanges) {
ChangeKind kind = lc.getKind();
if (kind == ADD_LITERAL) {
IAstRegion region = originals.get(lc.getIndex());
result.add(new DeleteRegionStrategy(region));
} else if (kind == MOVE_LITERAL || kind == REMOVE_LITERAL) {
if (originals.isEmpty()) {
return null;
}
int index = lc.getIndex();
IHiddenRegion insertAt;
if (index >= originals.size()) {
insertAt = ((ISequentialRegion) originals.get(originals.size() - 1)).getNextHiddenRegion();
} else {
insertAt = ((ISequentialRegion) originals.get(index)).getPreviousHiddenRegion();
}
EObject value = (EObject) modifying.get(index);
modifying.remove(index);
if (kind == REMOVE_LITERAL) {
ISerializationContext context = getSerializationContext(value);
result.add(new SerializeRecursiveStrategy(insertAt, value, context));
} else if (kind == ChangeKind.MOVE_LITERAL) {
int moveToIndex = lc.getMoveToIndex();
IAstRegion source = originals.get(moveToIndex);
result.add(new DeleteRegionStrategy(source));
result.add(new InsertRegionStrategy(insertAt, source));
}
}
}
return result;
}
use of org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion in project xtext-core by eclipse.
the class StringBasedTextRegionAccessDiffBuilder method createList.
protected List<Rewrite> createList() {
List<Rewrite> sorted = Lists.newArrayList(rewrites);
Collections.sort(sorted);
List<Rewrite> result = Lists.newArrayListWithExpectedSize(sorted.size() * 2);
IHiddenRegion last = original.regionForRootEObject().getPreviousHiddenRegion();
for (Rewrite rw : sorted) {
int lastOffset = last.getOffset();
int rwOffset = rw.originalFirst.getOffset();
if (rwOffset == lastOffset) {
result.add(rw);
last = rw.originalLast;
} else if (rwOffset > lastOffset) {
result.add(new Preserve(last, rw.originalFirst));
result.add(rw);
last = rw.originalLast;
} else {
LOG.error("Error, conflicting document modifications.");
}
}
IHiddenRegion end = original.regionForRootEObject().getNextHiddenRegion();
if (last.getOffset() < end.getOffset()) {
result.add(new Preserve(last, end));
}
return result;
}
use of org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion in project xtext-core by eclipse.
the class StringBasedTextRegionAccessDiffBuilder method replace.
@Override
public void replace(IHiddenRegion originalFirst, IHiddenRegion originalLast, ITextRegionAccess acc) {
checkOriginal(originalFirst);
checkOriginal(originalLast);
IEObjectRegion substituteRoot = acc.regionForRootEObject();
IHiddenRegion substituteFirst = substituteRoot.getPreviousHiddenRegion();
IHiddenRegion substituteLast = substituteRoot.getNextHiddenRegion();
replace(originalFirst, originalLast, substituteFirst, substituteLast);
}
use of org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion in project xtext-core by eclipse.
the class StringBasedTextRegionAccessDiffBuilder method create.
@Override
public StringBasedTextRegionAccessDiff create() {
StringBasedTextRegionAccessDiffAppender appender = createAppender();
IEObjectRegion root = original.regionForRootEObject();
appender.copyAndAppend(root.getPreviousHiddenRegion(), PREVIOUS);
appender.copyAndAppend(root.getPreviousHiddenRegion(), CONTAINER);
List<Rewrite> rws = createList();
IHiddenRegion last = null;
for (Rewrite rw : rws) {
boolean diff = rw.isDiff();
if (diff) {
appender.beginDiff();
}
if (rw instanceof Insert) {
Insert ins = (Insert) rw;
IHiddenRegion f = ins.getInsertFirst();
IHiddenRegion l = ins.getInsertLast();
appender.copyAndAppend(f, NEXT);
if (f != l) {
appender.copyAndAppend(f.getNextSemanticRegion(), l.getPreviousSemanticRegion());
}
appender.copyAndAppend(l, PREVIOUS);
}
if (diff) {
appender.endDiff();
}
if (rw.originalLast != last) {
appender.copyAndAppend(rw.originalLast, CONTAINER);
}
last = rw.originalLast;
}
appender.copyAndAppend(root.getNextHiddenRegion(), NEXT);
StringBasedTextRegionAccessDiff result = appender.finish();
AbstractEObjectRegion newRoot = result.regionForEObject(root.getSemanticElement());
result.setRootEObject(newRoot);
return result;
}
Aggregations