Search in sources :

Example 41 with ReplaceRegion

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

the class ExtractMethodRefactoring method getMethodBodyWithRenamedParameters.

protected String getMethodBodyWithRenamedParameters(ITextRegion expressionsRegion) throws BadLocationException {
    String expressionsAsString = document.get(expressionsRegion.getOffset(), expressionsRegion.getLength());
    List<ReplaceRegion> parameterRenames = newArrayList();
    for (final String parameterName : externalFeatureCalls.keySet()) {
        ParameterInfo parameter = find(parameterInfos, new Predicate<ParameterInfo>() {

            @Override
            public boolean apply(ParameterInfo info) {
                return equal(info.getOldName(), parameterName);
            }
        });
        if (parameter.isRenamed()) {
            for (XFeatureCall featureCall : externalFeatureCalls.get(parameterName)) {
                ITextRegion textRegion = locationInFileProvider.getSignificantTextRegion(featureCall, XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE, -1);
                parameterRenames.add(new ReplaceRegion(textRegion, parameter.getNewName()));
            }
        }
    }
    sort(parameterRenames, new Comparator<ReplaceRegion>() {

        @Override
        public int compare(ReplaceRegion o1, ReplaceRegion o2) {
            return o2.getOffset() - o1.getOffset();
        }
    });
    StringBuffer buffer = new StringBuffer(expressionsAsString);
    for (ReplaceRegion parameterRename : parameterRenames) {
        buffer.replace(parameterRename.getOffset() - expressionsRegion.getOffset(), parameterRename.getEndOffset() - expressionsRegion.getOffset(), parameterRename.getText());
    }
    expressionsAsString = buffer.toString();
    return expressionsAsString;
}
Also used : ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) XFeatureCall(org.eclipse.xtext.xbase.XFeatureCall) ITextRegion(org.eclipse.xtext.util.ITextRegion) RichString(org.eclipse.xtend.core.xtend.RichString) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo)

Example 42 with ReplaceRegion

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

the class XtextResource method update.

public void update(int offset, int replacedTextLength, String newText) {
    if (!isLoaded()) {
        throw new IllegalStateException("You can't update an unloaded resource.");
    }
    try {
        isUpdating = true;
        IParseResult oldParseResult = parseResult;
        ReplaceRegion replaceRegion = new ReplaceRegion(new TextRegion(offset, replacedTextLength), newText);
        IParseResult newParseResult;
        ParserRule oldEntryPoint = NodeModelUtils.getEntryParserRule(oldParseResult.getRootNode());
        if (entryPoint == null || entryPoint == oldEntryPoint) {
            newParseResult = getParser().reparse(oldParseResult, replaceRegion);
        } else {
            StringBuilder builder = new StringBuilder(oldParseResult.getRootNode().getText());
            replaceRegion.applyTo(builder);
            newParseResult = getParser().parse(entryPoint, new StringReader(builder.toString()));
        }
        updateInternalState(oldParseResult, newParseResult);
    } finally {
        isUpdating = false;
    }
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) TextRegion(org.eclipse.xtext.util.TextRegion) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) StringReader(java.io.StringReader) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 43 with ReplaceRegion

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

the class PartialParsingHelper method reparse.

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public IParseResult reparse(IParser parser, IParseResult previousParseResult, ReplaceRegion changedRegion) {
    if (parser == null)
        throw new NullPointerException("parser may not be null");
    if (previousParseResult == null) {
        throw new NullPointerException("previousParseResult and previousParseResult.rootNode may not be null");
    }
    ICompositeNode oldRootNode = previousParseResult.getRootNode();
    if (changedRegion.getEndOffset() > oldRootNode.getTotalLength()) {
        log.error("Invalid " + changedRegion + " originalLength=" + oldRootNode.getTotalLength());
        return fullyReparse(parser, previousParseResult, changedRegion);
    }
    if (changedRegion.getOffset() >= oldRootNode.getTotalLength() && changedRegion.getText().trim().length() == 0) {
        return fullyReparse(parser, previousParseResult, changedRegion);
    }
    ReplaceRegion replaceRegion;
    if (tokenRegionProvider != null) {
        replaceRegion = tokenRegionProvider.getTokenReplaceRegion(insertChangeIntoReplaceRegion(oldRootNode, changedRegion), changedRegion);
    } else {
        replaceRegion = changedRegion;
    }
    if (isNullEdit(oldRootNode, replaceRegion)) {
        return previousParseResult;
    }
    PartialParsingPointers parsingPointers = calculatePartialParsingPointers(previousParseResult, replaceRegion.getOffset(), replaceRegion.getLength());
    List<ICompositeNode> validReplaceRootNodes = parsingPointers.getValidReplaceRootNodes();
    ICompositeNode oldCompositeNode = null;
    String reparseRegion = "";
    for (int i = validReplaceRootNodes.size() - 1; i >= 0; --i) {
        oldCompositeNode = validReplaceRootNodes.get(i);
        if (!(oldCompositeNode instanceof SyntheticCompositeNode) && !isRangePartOfExceedingLookAhead((CompositeNode) oldCompositeNode, replaceRegion)) {
            boolean replaceAtEnd = oldCompositeNode.getTotalEndOffset() == replaceRegion.getEndOffset();
            reparseRegion = insertChangeIntoReplaceRegion(oldCompositeNode, replaceRegion);
            if (!"".equals(reparseRegion)) {
                if (!replaceAtEnd || !Character.isWhitespace(reparseRegion.charAt(reparseRegion.length() - 1))) {
                    if (log.isDebugEnabled()) {
                        log.debug("replace region: [" + oldCompositeNode.getTotalOffset() + " / length: " + oldCompositeNode.getTotalLength() + " of [" + oldRootNode.getTotalOffset() + " / lenght: " + oldRootNode.getTotalLength() + "]");
                    }
                    break;
                }
            }
        }
    }
    if (oldCompositeNode == null || reparseRegion.equals("") || oldCompositeNode == oldRootNode) {
        return fullyReparse(parser, previousParseResult, replaceRegion);
    }
    EObject entryRuleOrRuleCall = parsingPointers.findEntryRuleOrRuleCall(oldCompositeNode);
    IParseResult newParseResult = null;
    try {
        if (entryRuleOrRuleCall instanceof RuleCall)
            newParseResult = parser.parse((RuleCall) entryRuleOrRuleCall, new StringReader(reparseRegion), oldCompositeNode.getLookAhead());
        else
            newParseResult = parser.parse((ParserRule) entryRuleOrRuleCall, new StringReader(reparseRegion));
    } catch (ParseException exc) {
    }
    if (newParseResult == null || newParseResult.hasSyntaxErrors()) {
        // on error fully reparse
        return fullyReparse(parser, previousParseResult, replaceRegion);
    }
    if (oldRootNode.equals(oldCompositeNode)) {
        unloadSemanticObject(previousParseResult.getRootASTElement());
        return newParseResult;
    }
    EObject oldSemanticParentElement = oldCompositeNode.getParent().getSemanticElement();
    EObject oldSemanticElement = null;
    if (oldCompositeNode.hasDirectSemanticElement()) {
        oldSemanticElement = oldCompositeNode.getSemanticElement();
    } else {
        List<ICompositeNode> nodesEnclosingRegion = parsingPointers.getNodesEnclosingRegion();
        for (int i = nodesEnclosingRegion.size() - 1; i >= 0; --i) {
            ICompositeNode enclosingNode = nodesEnclosingRegion.get(i);
            if (enclosingNode == oldCompositeNode) {
                break;
            }
            if (enclosingNode.hasDirectSemanticElement())
                oldSemanticElement = enclosingNode.getSemanticElement();
        }
        if (oldSemanticElement == null)
            return fullyReparse(parser, previousParseResult, replaceRegion);
    }
    if (oldSemanticElement == oldSemanticParentElement) {
        throw new IllegalStateException("oldParent == oldElement");
    }
    if (oldSemanticParentElement != null) {
        EStructuralFeature feature = oldSemanticElement.eContainingFeature();
        if (feature == null)
            return fullyReparse(parser, previousParseResult, replaceRegion);
        oldSemanticParentElement = oldSemanticElement.eContainer();
        if (feature.isMany()) {
            List featureValueList = (List) oldSemanticParentElement.eGet(feature);
            int index = featureValueList.indexOf(oldSemanticElement);
            unloadSemanticObject(oldSemanticElement);
            EObject newSemanticObject = newParseResult.getRootASTElement();
            if (newSemanticObject != null) {
                featureValueList.set(index, newParseResult.getRootASTElement());
            } else {
                featureValueList.remove(index);
            }
        } else {
            unloadSemanticObject(oldSemanticElement);
            oldSemanticParentElement.eSet(feature, newParseResult.getRootASTElement());
        }
        ((ParseResult) newParseResult).setRootASTElement(previousParseResult.getRootASTElement());
    } else {
        unloadSemanticObject(oldSemanticElement);
    }
    if (oldCompositeNode != oldRootNode) {
        nodeModelBuilder.replaceAndTransferLookAhead(oldCompositeNode, newParseResult.getRootNode());
        ((ParseResult) newParseResult).setRootNode(oldRootNode);
        StringBuilder builder = new StringBuilder(oldRootNode.getText());
        replaceRegion.applyTo(builder);
        nodeModelBuilder.setCompleteContent(oldRootNode, builder.toString());
    }
    return newParseResult;
}
Also used : SyntheticCompositeNode(org.eclipse.xtext.nodemodel.impl.SyntheticCompositeNode) ParseResult(org.eclipse.xtext.parser.ParseResult) IParseResult(org.eclipse.xtext.parser.IParseResult) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) RuleCall(org.eclipse.xtext.RuleCall) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) EObject(org.eclipse.emf.ecore.EObject) StringReader(java.io.StringReader) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) ArrayList(java.util.ArrayList) List(java.util.List) IParseResult(org.eclipse.xtext.parser.IParseResult) ParseException(org.eclipse.xtext.parser.ParseException)

Example 44 with ReplaceRegion

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

the class PartialParserTest method testInsertSlashInFirstNode.

@Test
public void testInsertSlashInFirstNode() throws Exception {
    IParseResult parseResult = resource.getParser().reparse(this.parseResult, new ReplaceRegion(model.indexOf('3'), 0, "/"));
    assertFalse(parseResult.hasSyntaxErrors());
    assertEquals(0, parseResult.getRootNode().getTotalOffset());
    assertSame(resource.getParseResult().getRootNode(), parseResult.getRootNode());
    String newModel = "a.b.c.d: 12/3;\n" + "e.f.g.h: 456;";
    EmfAssert.assertEObjectsEqual(getModel(newModel), resource.getParseResult().getRootASTElement());
}
Also used : ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) IParseResult(org.eclipse.xtext.parser.IParseResult) Test(org.junit.Test)

Example 45 with ReplaceRegion

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

the class PartialParserTest method testInsertSlashInSecondNode.

@Test
public void testInsertSlashInSecondNode() throws Exception {
    IParseResult parseResult = resource.getParser().reparse(this.parseResult, new ReplaceRegion(model.indexOf('6'), 0, "/"));
    assertFalse(parseResult.hasSyntaxErrors());
    assertEquals(0, parseResult.getRootNode().getTotalOffset());
    assertSame(resource.getParseResult().getRootNode(), parseResult.getRootNode());
    String newModel = "a.b.c.d: 123;\n" + "e.f.g.h: 45/6;";
    EmfAssert.assertEObjectsEqual(getModel(newModel), resource.getParseResult().getRootASTElement());
}
Also used : ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) IParseResult(org.eclipse.xtext.parser.IParseResult) Test(org.junit.Test)

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