Search in sources :

Example 21 with ReplaceRegion

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());
                }
            }
        }
    }
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) LazyLinkingResource(org.eclipse.xtext.linking.lazy.LazyLinkingResource) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) Test(org.junit.Test)

Example 22 with ReplaceRegion

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;
}
Also used : ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion)

Example 23 with ReplaceRegion

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;
}
Also used : ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) EObject(org.eclipse.emf.ecore.EObject) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) Predicate(com.google.common.base.Predicate)

Example 24 with ReplaceRegion

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);
    }
}
Also used : DomainModel(org.eclipse.xtext.example.domainmodel.domainmodel.DomainModel) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource)

Example 25 with ReplaceRegion

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());
        }
    });
}
Also used : XCastedExpression(org.eclipse.xtext.xbase.XCastedExpression) IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) INode(org.eclipse.xtext.nodemodel.INode) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) EObject(org.eclipse.emf.ecore.EObject) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) XtextResource(org.eclipse.xtext.resource.XtextResource) WrappedException(org.eclipse.emf.common.util.WrappedException) BadLocationException(org.eclipse.jface.text.BadLocationException) IModification(org.eclipse.xtext.ui.editor.model.edit.IModification) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Aggregations

ReplaceRegion (org.eclipse.xtext.util.ReplaceRegion)52 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)20 Test (org.junit.Test)19 XtextResource (org.eclipse.xtext.resource.XtextResource)18 Resource (org.eclipse.emf.ecore.resource.Resource)13 INode (org.eclipse.xtext.nodemodel.INode)11 IParseResult (org.eclipse.xtext.parser.IParseResult)10 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)9 EObject (org.eclipse.emf.ecore.EObject)7 LazyLinkingResource (org.eclipse.xtext.linking.lazy.LazyLinkingResource)6 StringReader (java.io.StringReader)5 SyntheticCompositeNode (org.eclipse.xtext.nodemodel.impl.SyntheticCompositeNode)4 TextRegion (org.eclipse.xtext.util.TextRegion)4 List (java.util.List)3 BadLocationException (org.eclipse.jface.text.BadLocationException)3 TextEdit (org.eclipse.text.edits.TextEdit)3 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)3 PartialParsingPointers (org.eclipse.xtext.parser.impl.PartialParsingPointers)3 ITextRegion (org.eclipse.xtext.util.ITextRegion)3 ArrayList (java.util.ArrayList)2