use of org.eclipse.xtext.util.ReplaceRegion in project xtext-eclipse by eclipse.
the class AbstractSmokeTest method testSkipNodesInBetweenWithoutResourceSet.
@Test
public void testSkipNodesInBetweenWithoutResourceSet() throws Exception {
for (String string : getSmokeTestModels()) {
LazyLinkingResource resource = createResource(string);
if (resource != null) {
ICompositeNode rootNode = resource.getParseResult().getRootNode();
ReplaceRegion replaceRegion = null;
for (INode node : rootNode.getAsTreeIterable()) {
int offset = node.getTotalOffset();
int length = node.getTotalLength();
if (replaceRegion == null || replaceRegion.getOffset() != offset || replaceRegion.getLength() != length) {
replaceRegion = new ReplaceRegion(offset, length, "");
StringBuilder builder = new StringBuilder(string);
replaceRegion.applyTo(builder);
processModelWithoutResourceSet(builder.toString());
}
}
}
}
}
use of org.eclipse.xtext.util.ReplaceRegion in project xtext-eclipse by eclipse.
the class DeltaScenarioProcessor method applyDelta.
protected String applyDelta(String data, int offset, int len, String change) {
StringBuilder builder = new StringBuilder(data);
new ReplaceRegion(offset, len, change).applyTo(builder);
String result = builder.toString();
return result;
}
use of org.eclipse.xtext.util.ReplaceRegion in project xtext-eclipse by eclipse.
the class DefaultTextEditComposer method getObjectEdits.
protected List<TextEdit> getObjectEdits() {
final Collection<EObject> modifiedObjects = getModifiedObjects();
Collection<EObject> topLevelObjects = EcoreUtil.filterDescendants(modifiedObjects);
Iterable<EObject> containedModifiedObjects = Collections.emptyList();
if (!resource.getContents().isEmpty()) {
final EObject root = resource.getContents().get(0);
containedModifiedObjects = Iterables.filter(topLevelObjects, new Predicate<EObject>() {
@Override
public boolean apply(EObject input) {
return EcoreUtil.isAncestor(root, input);
}
});
}
List<TextEdit> edits = Lists.newArrayListWithExpectedSize(Iterables.size(containedModifiedObjects));
for (EObject modifiedObject : containedModifiedObjects) {
ReplaceRegion replaceRegion = serializer.serializeReplacement(modifiedObject, getSaveOptions());
TextEdit edit = new ReplaceEdit(replaceRegion.getOffset(), replaceRegion.getLength(), replaceRegion.getText());
edits.add(edit);
}
return edits;
}
use of org.eclipse.xtext.util.ReplaceRegion in project xtext-eclipse by eclipse.
the class OrganizeImportsTest method assertIsOrganizedTo.
protected void assertIsOrganizedTo(final CharSequence model, final CharSequence expected) {
try {
final DomainModel domainModel = this._parseHelper.parse(model.toString());
Resource _eResource = domainModel.eResource();
final List<ReplaceRegion> changes = this.importOrganizer.getOrganizedImportChanges(((XtextResource) _eResource));
final StringBuilder builder = new StringBuilder(model);
final Function1<ReplaceRegion, Integer> _function = (ReplaceRegion it) -> {
return Integer.valueOf(it.getOffset());
};
final List<ReplaceRegion> sortedChanges = IterableExtensions.<ReplaceRegion, Integer>sortBy(changes, _function);
ReplaceRegion lastChange = null;
for (final ReplaceRegion it : sortedChanges) {
{
if (((lastChange != null) && (lastChange.getEndOffset() > it.getOffset()))) {
Assert.fail(((("Overlapping text edits: " + lastChange) + " and ") + it));
}
lastChange = it;
}
}
List<ReplaceRegion> _reverse = ListExtensions.<ReplaceRegion>reverse(sortedChanges);
for (final ReplaceRegion it_1 : _reverse) {
int _offset = it_1.getOffset();
int _offset_1 = it_1.getOffset();
int _length = it_1.getLength();
int _plus = (_offset_1 + _length);
builder.replace(_offset, _plus, it_1.getText());
}
Assert.assertEquals(expected.toString(), builder.toString());
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
use of org.eclipse.xtext.util.ReplaceRegion in project xtext-eclipse by eclipse.
the class XbaseQuickfixProvider method fixObsoletCast.
@Fix(IssueCodes.OBSOLETE_CAST)
public void fixObsoletCast(final Issue issue, IssueResolutionAcceptor acceptor) {
String fixup = "Remove unnecessary cast";
acceptor.accept(issue, fixup, fixup, null, new IModification() {
@Override
public void apply(IModificationContext context) throws Exception {
final IXtextDocument document = context.getXtextDocument();
ReplaceRegion replacement = document.readOnly(new IUnitOfWork<ReplaceRegion, XtextResource>() {
@Override
public ReplaceRegion exec(XtextResource state) throws Exception {
EObject type = state.getEObject(issue.getUriToProblem().fragment());
XCastedExpression cast = EcoreUtil2.getContainerOfType(type, XCastedExpression.class);
INode castNode = NodeModelUtils.findActualNodeFor(cast);
INode targetNode = NodeModelUtils.findActualNodeFor(cast.getTarget());
return new ReplaceRegion(castNode.getTotalTextRegion(), targetNode.getText());
}
});
document.replace(replacement.getOffset(), replacement.getLength(), replacement.getText());
}
});
}
Aggregations