Search in sources :

Example 1 with Fix

use of org.eclipse.xtext.ui.editor.quickfix.Fix in project xtext-eclipse by eclipse.

the class XtextGrammarQuickfixProvider method fixUnresolvedRule.

@Fix(XtextLinkingDiagnosticMessageProvider.UNRESOLVED_RULE)
public void fixUnresolvedRule(final Issue issue, IssueResolutionAcceptor acceptor) {
    final String ruleName = issue.getData()[0];
    acceptor.accept(issue, "Create rule '" + ruleName + "'", "Create rule '" + ruleName + "'", NULL_QUICKFIX_IMAGE, new ISemanticModification() {

        @Override
        public void apply(final EObject element, IModificationContext context) throws BadLocationException {
            AbstractRule abstractRule = EcoreUtil2.getContainerOfType(element, ParserRule.class);
            ICompositeNode node = NodeModelUtils.getNode(abstractRule);
            int offset = node.getEndOffset();
            StringBuilder builder = new StringBuilder("\n\n");
            if (abstractRule instanceof TerminalRule)
                builder.append("terminal ");
            String newRule = builder.append(ruleName).append(":\n\t\n;").toString();
            context.getXtextDocument().replace(offset, 0, newRule);
        }
    });
    createLinkingIssueResolutions(issue, acceptor);
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) 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) TerminalRule(org.eclipse.xtext.TerminalRule) AbstractRule(org.eclipse.xtext.AbstractRule) BadLocationException(org.eclipse.jface.text.BadLocationException) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 2 with Fix

use of org.eclipse.xtext.ui.editor.quickfix.Fix in project xtext-eclipse by eclipse.

the class XbaseQuickfixProvider method fixInvisibleFeature.

@Fix(IssueCodes.FEATURE_NOT_VISIBLE)
public void fixInvisibleFeature(final Issue issue, IssueResolutionAcceptor acceptor) {
    if (issue.getData() != null && issue.getData().length >= 1 && "subclass-context".equals(issue.getData()[0])) {
        String fixup;
        if (issue.getData().length == 1)
            fixup = "Add type cast.";
        else
            fixup = "Add cast to " + issue.getData()[1] + ".";
        acceptor.accept(issue, fixup, fixup, null, new ISemanticModification() {

            @Override
            public void apply(EObject element, IModificationContext context) throws Exception {
                if (!(element instanceof XAbstractFeatureCall))
                    return;
                XAbstractFeatureCall featureCall = (XAbstractFeatureCall) element;
                if (!(featureCall.getFeature() instanceof JvmMember))
                    return;
                JvmType declaringType = ((JvmMember) featureCall.getFeature()).getDeclaringType();
                if (featureCall instanceof XMemberFeatureCall) {
                    addTypeCastToExplicitReceiver(featureCall, context, declaringType, XbasePackage.Literals.XMEMBER_FEATURE_CALL__MEMBER_CALL_TARGET);
                } else if (featureCall instanceof XAssignment) {
                    addTypeCastToExplicitReceiver(featureCall, context, declaringType, XbasePackage.Literals.XASSIGNMENT__ASSIGNABLE);
                } else if (featureCall instanceof XFeatureCall) {
                    addTypeCastToImplicitReceiver((XFeatureCall) featureCall, context, declaringType);
                }
            }
        });
    }
}
Also used : XMemberFeatureCall(org.eclipse.xtext.xbase.XMemberFeatureCall) XFeatureCall(org.eclipse.xtext.xbase.XFeatureCall) EObject(org.eclipse.emf.ecore.EObject) ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) JvmMember(org.eclipse.xtext.common.types.JvmMember) JvmType(org.eclipse.xtext.common.types.JvmType) XAbstractFeatureCall(org.eclipse.xtext.xbase.XAbstractFeatureCall) WrappedException(org.eclipse.emf.common.util.WrappedException) BadLocationException(org.eclipse.jface.text.BadLocationException) XAssignment(org.eclipse.xtext.xbase.XAssignment) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 3 with Fix

use of org.eclipse.xtext.ui.editor.quickfix.Fix 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 4 with Fix

use of org.eclipse.xtext.ui.editor.quickfix.Fix in project xtext-eclipse by eclipse.

the class XbaseQuickfixProvider method fixAmbiguousMethodCall.

@Fix(IssueCodes.AMBIGUOUS_FEATURE_CALL)
public void fixAmbiguousMethodCall(final Issue issue, IssueResolutionAcceptor acceptor) {
    String[] data = issue.getData();
    if (data == null || data.length == 0) {
        return;
    }
    for (String replacement : data) {
        String replaceLabel = "Change to '" + replacement + "'";
        acceptor.accept(issue, replaceLabel, replaceLabel, null, new ReplaceModification(issue, replacement));
    }
}
Also used : ReplaceModification(org.eclipse.xtext.ui.editor.quickfix.ReplaceModification) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 5 with Fix

use of org.eclipse.xtext.ui.editor.quickfix.Fix 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)

Aggregations

Fix (org.eclipse.xtext.ui.editor.quickfix.Fix)43 IModificationContext (org.eclipse.xtext.ui.editor.model.edit.IModificationContext)35 BadLocationException (org.eclipse.jface.text.BadLocationException)29 EObject (org.eclipse.emf.ecore.EObject)25 ISemanticModification (org.eclipse.xtext.ui.editor.model.edit.ISemanticModification)18 IModification (org.eclipse.xtext.ui.editor.model.edit.IModification)17 CoreException (org.eclipse.core.runtime.CoreException)16 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)16 XtextResource (org.eclipse.xtext.resource.XtextResource)13 List (java.util.List)9 WrappedException (org.eclipse.emf.common.util.WrappedException)9 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)9 ReplacingAppendable (org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable)7 ArrayList (java.util.ArrayList)5 EList (org.eclipse.emf.common.util.EList)5 URI (org.eclipse.emf.common.util.URI)5 INode (org.eclipse.xtext.nodemodel.INode)5 IOException (java.io.IOException)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 ITextRegion (org.eclipse.xtext.util.ITextRegion)4