Search in sources :

Example 16 with ICompositeNode

use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-eclipse by eclipse.

the class AstSelectionProvider method selectEnclosing.

public ITextRegion selectEnclosing(XtextResource resource, ITextRegion currentEditorSelection) {
    Pair<EObject, EObject> currentlySelected = getSelectedAstElements(resource, currentEditorSelection);
    if (currentlySelected == null) {
        IParseResult parseResult = resource.getParseResult();
        if (parseResult != null) {
            ICompositeNode rootNode = parseResult.getRootNode();
            int offset = getSelectionOffset(rootNode, currentEditorSelection);
            INode node = findLeafNodeAtOffset(rootNode, offset);
            if (node != null) {
                ITextRegion fineGrainedRegion = computeInitialFineGrainedSelection(node, currentEditorSelection);
                if (fineGrainedRegion != null) {
                    selectionHistory.clear();
                    register(currentEditorSelection);
                    return register(fineGrainedRegion);
                }
                EObject eObject = findSemanticObjectFor(node);
                return register(getTextRegion(eObject));
            }
        }
    } else {
        EObject first = currentlySelected.getFirst();
        if (first.eContainer() != null) {
            return register(getTextRegion(first.eContainer()));
        }
    }
    return ITextRegion.EMPTY_REGION;
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 17 with ICompositeNode

use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-eclipse by eclipse.

the class AstSelectionProvider method getEObjectAtOffset.

protected EObject getEObjectAtOffset(XtextResource resource, ITextRegion currentEditorSelection) {
    IParseResult parseResult = resource.getParseResult();
    if (parseResult != null) {
        ICompositeNode rootNode = parseResult.getRootNode();
        INode nodeAtOffset = findLeafNodeAtOffset(rootNode, currentEditorSelection.getOffset());
        return findSemanticObjectFor(nodeAtOffset);
    }
    return null;
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 18 with ICompositeNode

use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-eclipse by eclipse.

the class XbaseQuickfixProvider method fixUnreachableCatchBlock.

@Fix(IssueCodes.UNREACHABLE_CATCH_BLOCK)
public void fixUnreachableCatchBlock(final Issue issue, IssueResolutionAcceptor acceptor) {
    acceptor.accept(issue, "Remove catch block", "Remove catch block", null, new ISemanticModification() {

        @Override
        public void apply(EObject element, IModificationContext context) throws Exception {
            remove(element, XCatchClause.class, context);
        }
    });
    acceptor.accept(issue, "Move catch block up", "Move catch block up", null, new ISemanticModification() {

        @Override
        public void apply(EObject element, IModificationContext context) throws Exception {
            XCatchClause catchClause = EcoreUtil2.getContainerOfType(element, XCatchClause.class);
            if (catchClause == null) {
                return;
            }
            ICompositeNode node = NodeModelUtils.findActualNodeFor(catchClause);
            if (node == null) {
                return;
            }
            XTryCatchFinallyExpression tryCatchFinallyExpression = EcoreUtil2.getContainerOfType(catchClause, XTryCatchFinallyExpression.class);
            if (tryCatchFinallyExpression == null) {
                return;
            }
            ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, tryCatchFinallyExpression);
            LightweightTypeReference actualTypeReference = owner.toLightweightTypeReference(catchClause.getDeclaredParam().getParameterType());
            for (XCatchClause previousCatchClause : tryCatchFinallyExpression.getCatchClauses()) {
                if (previousCatchClause == catchClause) {
                    return;
                }
                LightweightTypeReference previousTypeReference = owner.toLightweightTypeReference(previousCatchClause.getDeclaredParam().getParameterType());
                if (typesOrderUtil.isHandled(actualTypeReference, previousTypeReference)) {
                    ICompositeNode previousNode = NodeModelUtils.findActualNodeFor(previousCatchClause);
                    if (previousNode == null) {
                        return;
                    }
                    moveUp(node, previousNode, context);
                    return;
                }
            }
        }
    });
}
Also used : XCatchClause(org.eclipse.xtext.xbase.XCatchClause) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) EObject(org.eclipse.emf.ecore.EObject) ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) XTryCatchFinallyExpression(org.eclipse.xtext.xbase.XTryCatchFinallyExpression) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner) WrappedException(org.eclipse.emf.common.util.WrappedException) BadLocationException(org.eclipse.jface.text.BadLocationException) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 19 with ICompositeNode

use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-eclipse by eclipse.

the class XbaseQuickfixProvider method fixUnreachableIfBlock.

@Fix(IssueCodes.UNREACHABLE_IF_BLOCK)
public void fixUnreachableIfBlock(final Issue issue, IssueResolutionAcceptor acceptor) {
    acceptor.accept(issue, "Remove if block", "Remove if block", null, new ISemanticModification() {

        @Override
        public void apply(EObject element, IModificationContext context) throws Exception {
            XIfExpression ifExpression = EcoreUtil2.getContainerOfType(element, XIfExpression.class);
            if (ifExpression == null) {
                return;
            }
            ICompositeNode node = NodeModelUtils.findActualNodeFor(ifExpression);
            if (node == null) {
                return;
            }
            int[] offsetAndLength = getOffsetAndLength(ifExpression, node);
            context.getXtextDocument().replace(offsetAndLength[0], offsetAndLength[1], "");
        }
    });
    acceptor.accept(issue, "Move if block up", "Move if block up", null, new ISemanticModification() {

        @Override
        public void apply(EObject element, IModificationContext context) throws Exception {
            XIfExpression ifExpression = EcoreUtil2.getContainerOfType(element, XIfExpression.class);
            if (ifExpression == null) {
                return;
            }
            ICompositeNode node = NodeModelUtils.findActualNodeFor(ifExpression);
            if (node == null) {
                return;
            }
            XIfExpression firstIfExpression = getFirstIfExpression(ifExpression);
            if (firstIfExpression == null) {
                return;
            }
            XInstanceOfExpression actualIfPart = (XInstanceOfExpression) ifExpression.getIf();
            XAbstractFeatureCall actualFeatureCall = (XAbstractFeatureCall) actualIfPart.getExpression();
            JvmIdentifiableElement actualFeature = actualFeatureCall.getFeature();
            ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, firstIfExpression);
            LightweightTypeReference actualTypeReference = owner.toLightweightTypeReference(actualIfPart.getType());
            List<XExpression> ifParts = collectIfParts(firstIfExpression, new ArrayList<XExpression>());
            for (XExpression previousIfPart : ifParts) {
                if (actualIfPart == previousIfPart) {
                    return;
                }
                if (!(previousIfPart instanceof XInstanceOfExpression)) {
                    continue;
                }
                XInstanceOfExpression instanceOfExpression = (XInstanceOfExpression) previousIfPart;
                if (!(instanceOfExpression.getExpression() instanceof XAbstractFeatureCall)) {
                    continue;
                }
                XAbstractFeatureCall previousFeatureCall = (XAbstractFeatureCall) instanceOfExpression.getExpression();
                if (previousFeatureCall.getFeature() != actualFeature) {
                    continue;
                }
                LightweightTypeReference previousTypeReference = owner.toLightweightTypeReference(instanceOfExpression.getType());
                if (typesOrderUtil.isHandled(actualTypeReference, previousTypeReference)) {
                    ICompositeNode previousNode = NodeModelUtils.findActualNodeFor(instanceOfExpression.eContainer());
                    if (previousNode == null) {
                        return;
                    }
                    int[] offsetAndLength = getOffsetAndLength(ifExpression, node);
                    int offset = offsetAndLength[0];
                    int length = offsetAndLength[1];
                    int endOffset = offset + length;
                    String text = node.getRootNode().getText().substring(offset, endOffset).trim();
                    if (text.startsWith("else")) {
                        text = text.substring("else".length()).trim();
                    }
                    if (!text.endsWith("else")) {
                        text = text + " else ";
                    } else {
                        text = text + " ";
                    }
                    IXtextDocument document = context.getXtextDocument();
                    document.replace(offset, length, "");
                    document.replace(previousNode.getOffset(), 0, text);
                    return;
                }
            }
        }

        private XIfExpression getFirstIfExpression(XIfExpression ifExpression) {
            EObject container = ifExpression.eContainer();
            if (container instanceof XIfExpression) {
                XIfExpression parentIfExpression = (XIfExpression) container;
                if (parentIfExpression.getElse() == ifExpression) {
                    return getFirstIfExpression(parentIfExpression);
                }
            }
            return ifExpression;
        }

        private List<XExpression> collectIfParts(XIfExpression expression, List<XExpression> result) {
            result.add(expression.getIf());
            if (expression.getElse() instanceof XIfExpression) {
                collectIfParts((XIfExpression) expression.getElse(), result);
            }
            return result;
        }
    });
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) ArrayList(java.util.ArrayList) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) XAbstractFeatureCall(org.eclipse.xtext.xbase.XAbstractFeatureCall) WrappedException(org.eclipse.emf.common.util.WrappedException) BadLocationException(org.eclipse.jface.text.BadLocationException) XInstanceOfExpression(org.eclipse.xtext.xbase.XInstanceOfExpression) XIfExpression(org.eclipse.xtext.xbase.XIfExpression) EObject(org.eclipse.emf.ecore.EObject) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) List(java.util.List) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) XExpression(org.eclipse.xtext.xbase.XExpression) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 20 with ICompositeNode

use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-xtend by eclipse.

the class AbstractSmokeTest method testSkipNodesInBetweenWithoutResourceSet.

@Test
public void testSkipNodesInBetweenWithoutResourceSet() throws Exception {
    for (String string : smokeTestModels) {
        LazyLinkingResource resource = createResource(string);
        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);
                doParseAndCheckForSmokeWithoutResourceSet(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)

Aggregations

ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)263 Test (org.junit.Test)87 INode (org.eclipse.xtext.nodemodel.INode)79 EObject (org.eclipse.emf.ecore.EObject)70 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)57 XtextResource (org.eclipse.xtext.resource.XtextResource)41 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)24 ReplaceRegion (org.eclipse.xtext.util.ReplaceRegion)20 Resource (org.eclipse.emf.ecore.resource.Resource)19 Model (org.eclipse.xtext.valueconverter.bug250313.Model)15 IParseResult (org.eclipse.xtext.parser.IParseResult)14 ParserRule (org.eclipse.xtext.ParserRule)12 ITextRegion (org.eclipse.xtext.util.ITextRegion)12 RuleCall (org.eclipse.xtext.RuleCall)11 CrossReference (org.eclipse.xtext.CrossReference)10 ArrayList (java.util.ArrayList)8 List (java.util.List)8 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)8 AbstractRule (org.eclipse.xtext.AbstractRule)8 Keyword (org.eclipse.xtext.Keyword)8