Search in sources :

Example 91 with ITextRegion

use of org.eclipse.xtext.util.ITextRegion 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 92 with ITextRegion

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

the class XtendExpressionUtil method getRichStringPartTextRegion.

protected ITextRegion getRichStringPartTextRegion(EObject element) {
    if (element instanceof RichStringLiteral) {
        return locationInFileProvider.getSignificantTextRegion(element);
    }
    ICompositeNode elementNode = NodeModelUtils.getNode(element);
    ITextRegion totalTextRegion = elementNode.getTotalTextRegion();
    int offset = totalTextRegion.getOffset() - 1;
    int length = totalTextRegion.getLength() + 2;
    ILeafNode nextNode = NodeModelUtils.findLeafNodeAtOffset(elementNode.getRootNode(), elementNode.getEndOffset());
    while (nextNode != null && nextNode.isHidden()) {
        length += nextNode.getLength();
        INode nextSibling = nextNode.getNextSibling();
        if (nextSibling instanceof ILeafNode) {
            nextNode = (ILeafNode) nextSibling;
        } else {
            nextNode = null;
        }
    }
    return new TextRegion(offset, length);
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) RichStringLiteral(org.eclipse.xtend.core.xtend.RichStringLiteral) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode)

Example 93 with ITextRegion

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

the class FormatterTester method assertReplacementsAreInRegion.

protected void assertReplacementsAreInRegion(List<ITextReplacement> rep, Collection<ITextRegion> regions, String doc) {
    Set<ITextReplacement> invalid = Sets.newHashSet();
    ALLOWED: for (ITextRegion allowed : regions) for (ITextReplacement r : rep) {
        if (allowed.contains(r))
            continue ALLOWED;
        invalid.add(r);
    }
    if (!invalid.isEmpty()) {
        String visualized = new TextRegionsToString().addAllReplacements(invalid).toString();
        fail("One or more TextReplacements are outside of the allowed region. Region: " + regions, visualized);
    }
}
Also used : ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) ITextRegion(org.eclipse.xtext.util.ITextRegion) TextRegionAccessToString(org.eclipse.xtext.formatting2.debug.TextRegionAccessToString) TextRegionsToString(org.eclipse.xtext.formatting2.debug.TextRegionsToString) TextRegionsToString(org.eclipse.xtext.formatting2.debug.TextRegionsToString)

Example 94 with ITextRegion

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

the class SerializerTester method assertSerializeWithoutNodeModel.

public void assertSerializeWithoutNodeModel(EObject semanticObject) {
    try {
        EObject parsed;
        if (semanticObject.eResource().getContents().contains(semanticObject)) {
            List<Pair<EObject, ICompositeNode>> nodes = detachNodeModel(semanticObject);
            String serialized = serializeWithoutNodeModel(semanticObject);
            parsed = parseHelper.parse(serialized, semanticObject.eResource().getResourceSet());
            reattachNodes(nodes);
        } else {
            INode oldNode = NodeModelUtils.getNode(semanticObject);
            String oldtext = oldNode.getRootNode().getText();
            String oldURI = semanticObject.eResource().getURIFragment(semanticObject);
            List<Pair<EObject, ICompositeNode>> nodes = detachNodeModel(semanticObject);
            String serialized = serializeWithoutNodeModel(semanticObject);
            ITextRegion oldRegion = oldNode.getTextRegion();
            String newtext = oldtext.substring(0, oldRegion.getOffset()) + serialized + oldtext.substring(oldRegion.getOffset() + oldRegion.getLength());
            EObject newmodel = parseHelper.parse(newtext, semanticObject.eResource().getResourceSet());
            parsed = newmodel.eResource().getEObject(oldURI);
            reattachNodes(nodes);
        }
        EcoreUtil.resolveAll(parsed);
        Assert.assertTrue(parsed.eResource().getErrors().toString(), parsed.eResource().getErrors().isEmpty());
        parsed.eResource().getResourceSet().getResources().remove(parsed.eResource());
        assertEqualWithEmfFormatter(semanticObject, parsed);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) Pair(org.eclipse.xtext.util.Pair)

Example 95 with ITextRegion

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

the class DefaultFoldingRegionAcceptor method accept.

/**
 * @since 2.8
 */
@Override
public void accept(int offset, int length, boolean initiallyFolded, ITextRegion significantRegion) {
    IRegion position = getLineRegion(offset, length);
    try {
        if (xtextDocument != null && significantRegion != null) {
            int firstLine = xtextDocument.getLineOfOffset(significantRegion.getOffset());
            int lastLine = xtextDocument.getLineOfOffset(significantRegion.getOffset() + significantRegion.getLength());
            if (firstLine != lastLine) {
                int endOffset = xtextDocument.getLineOffset(firstLine) + xtextDocument.getLineLength(firstLine);
                significantRegion = new TextRegion(significantRegion.getOffset(), endOffset - significantRegion.getOffset());
            }
        }
    } catch (BadLocationException e) {
    }
    FoldedPosition foldingRegion = newFoldedPosition(position, significantRegion);
    if (foldingRegion != null) {
        if (foldingRegion instanceof DefaultFoldedPosition) {
            ((DefaultFoldedPosition) foldingRegion).setInitiallyFolded(initiallyFolded);
        }
        result.add(foldingRegion);
    }
}
Also used : ITextRegion(org.eclipse.xtext.util.ITextRegion) TextRegion(org.eclipse.xtext.util.TextRegion) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

ITextRegion (org.eclipse.xtext.util.ITextRegion)122 TextRegion (org.eclipse.xtext.util.TextRegion)44 EObject (org.eclipse.emf.ecore.EObject)33 Test (org.junit.Test)20 INode (org.eclipse.xtext.nodemodel.INode)19 XtextResource (org.eclipse.xtext.resource.XtextResource)17 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)15 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)15 BadLocationException (org.eclipse.jface.text.BadLocationException)11 Region (org.eclipse.jface.text.Region)9 XExpression (org.eclipse.xtext.xbase.XExpression)9 IOException (java.io.IOException)8 CoreException (org.eclipse.core.runtime.CoreException)8 IParseResult (org.eclipse.xtext.parser.IParseResult)7 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)7 IRegion (org.eclipse.jface.text.IRegion)6 ITextSelection (org.eclipse.jface.text.ITextSelection)6 Collection (java.util.Collection)5 ArrayList (java.util.ArrayList)4 TextSelection (org.eclipse.jface.text.TextSelection)4