Search in sources :

Example 46 with ReplaceRegion

use of org.eclipse.xtext.util.ReplaceRegion in project xtext-eclipse by eclipse.

the class PartialParsingProcessor method processFile.

@Override
public String processFile(String completeData, String data, int offset, int len, String change) throws Exception {
    IParseResult initialParseResult = parser.parse(new StringReader(data));
    String newData = applyDelta(data, offset, len, change);
    ReplaceRegion replaceRegion = new ReplaceRegion(offset, len, change);
    try {
        IParseResult reparsed = parser.reparse(initialParseResult, replaceRegion);
        IParseResult parsedFromScratch = parser.parse(new StringReader(newData));
        assertEqual(data, newData, parsedFromScratch, reparsed);
        return newData;
    } catch (Throwable e) {
        ComparisonFailure throwMe = new ComparisonFailure(e.getMessage(), newData, replaceRegion + DELIM + data);
        throwMe.initCause(e);
        throw throwMe;
    }
}
Also used : ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) ComparisonFailure(org.junit.ComparisonFailure) StringReader(java.io.StringReader) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 47 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 48 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) throws Exception {
    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());
}
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 49 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 50 with ReplaceRegion

use of org.eclipse.xtext.util.ReplaceRegion in project xtext-eclipse by eclipse.

the class XbaseReferenceUpdater method processReferringResource.

@Override
protected void processReferringResource(Resource referringResource, Iterable<IReferenceDescription> referenceDescriptions, ElementRenameArguments elementRenameArguments, IRefactoringUpdateAcceptor updateAcceptor, IProgressMonitor monitor) {
    ImportAwareUpdateAcceptor importAwareUpdateAcceptor = createUpdateAcceptor(referringResource, updateAcceptor);
    Iterable<IReferenceDescription> importTypeReferences = getImportTypeReferences(referenceDescriptions);
    super.processReferringResource(referringResource, importTypeReferences, elementRenameArguments, importAwareUpdateAcceptor, monitor);
    importAwareUpdateAcceptor.getImportSection().update();
    Iterable<IReferenceDescription> notImportTypeReferences = getNotImportTypeReferences(referenceDescriptions);
    super.processReferringResource(referringResource, notImportTypeReferences, elementRenameArguments, importAwareUpdateAcceptor, monitor);
    List<ReplaceRegion> importChanges = importAwareUpdateAcceptor.getImportSection().rewrite();
    TextEdit importChangeEdit = replaceConverter.convertToTextEdit(importChanges);
    if (importChangeEdit != null)
        updateAcceptor.accept(referringResource.getURI(), importChangeEdit);
}
Also used : ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) TextEdit(org.eclipse.text.edits.TextEdit) IReferenceDescription(org.eclipse.xtext.resource.IReferenceDescription)

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