Search in sources :

Example 1 with XFeatureCall

use of org.eclipse.xtext.xbase.XFeatureCall 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 2 with XFeatureCall

use of org.eclipse.xtext.xbase.XFeatureCall in project xtext-eclipse by eclipse.

the class XbaseReferenceUpdater method createReferenceUpdate.

@Override
protected void createReferenceUpdate(EObject referringElement, URI referringResourceURI, EReference reference, int indexInList, EObject newTargetElement, IRefactoringUpdateAcceptor updateAcceptor) {
    if (referringElement instanceof XImportDeclaration) {
        XImportDeclaration importDeclaration = (XImportDeclaration) referringElement;
        JvmDeclaredType importedType = importDeclaration.getImportedType();
        boolean isStatic = importDeclaration.isStatic();
        boolean isExtension = importDeclaration.isExtension();
        String memberName = importDeclaration.getMemberName();
        if (newTargetElement instanceof JvmDeclaredType) {
            JvmDeclaredType type = (JvmDeclaredType) newTargetElement;
            ImportAwareUpdateAcceptor importAwareUpdateAcceptor = (ImportAwareUpdateAcceptor) updateAcceptor;
            importAwareUpdateAcceptor.removeImport(type, isStatic, isExtension, memberName);
            importAwareUpdateAcceptor.acceptImport(type, isStatic, isExtension, memberName);
            return;
        }
        if (newTargetElement instanceof JvmFeature) {
            JvmFeature feature = (JvmFeature) newTargetElement;
            String featureName = feature.getSimpleName();
            if (featureName.equals(memberName)) {
                // type rename is handled separately
                return;
            }
            ImportAwareUpdateAcceptor importAwareUpdateAcceptor = (ImportAwareUpdateAcceptor) updateAcceptor;
            if (!importAwareUpdateAcceptor.isUsed(importedType, isStatic, isExtension, memberName)) {
                importAwareUpdateAcceptor.removeImport(importedType, isStatic, isExtension, memberName);
            }
            if (importAwareUpdateAcceptor.isConflicted(importedType, isStatic, isExtension, featureName)) {
                JvmDeclaredType parentType = importedType;
                while (parentType != null && !importAwareUpdateAcceptor.getImportSection().hasImportedType(parentType) && !importAwareUpdateAcceptor.acceptImport(parentType, false, false, null)) {
                    parentType = parentType.getDeclaringType();
                }
            } else {
                importAwareUpdateAcceptor.acceptImport(importedType, isStatic, isExtension, featureName);
            }
            return;
        }
    }
    if (referringElement instanceof XAbstractFeatureCall) {
        if (newTargetElement instanceof JvmDeclaredType) {
            XAbstractFeatureCall featureCall = (XAbstractFeatureCall) referringElement;
            JvmIdentifiableElement feature = featureCall.getFeature();
            if (isStaticExtensionFeatureCall(referringElement, reference, feature)) {
                return;
            }
            if (isStaticFeatureCall(referringElement, reference, feature)) {
                return;
            }
        }
    }
    if (!(referringElement instanceof XFeatureCall && newTargetElement instanceof JvmConstructor)) {
        // skip constructor calls like this() or super()
        super.createReferenceUpdate(referringElement, referringResourceURI, reference, indexInList, newTargetElement, updateAcceptor);
    }
}
Also used : JvmFeature(org.eclipse.xtext.common.types.JvmFeature) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) XFeatureCall(org.eclipse.xtext.xbase.XFeatureCall) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) XAbstractFeatureCall(org.eclipse.xtext.xbase.XAbstractFeatureCall) XImportDeclaration(org.eclipse.xtext.xtype.XImportDeclaration)

Example 3 with XFeatureCall

use of org.eclipse.xtext.xbase.XFeatureCall in project xtext-xtend by eclipse.

the class ParserTest method testFunction_1.

@Test
public void testFunction_1() throws Exception {
    XtendFunction func = function("def String foo() {foo}");
    assertEquals("foo", func.getName());
    assertTrue(((XBlockExpression) func.getExpression()).getExpressions().get(0) instanceof XFeatureCall);
    assertEquals(0, func.getParameters().size());
    assertNotNull(func.getReturnType());
    assertEquals(0, func.getTypeParameters().size());
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) XFeatureCall(org.eclipse.xtext.xbase.XFeatureCall) Test(org.junit.Test)

Example 4 with XFeatureCall

use of org.eclipse.xtext.xbase.XFeatureCall in project xtext-xtend by eclipse.

the class ParserTest method testFunction_0.

@Test
public void testFunction_0() throws Exception {
    XtendFunction func = function("def foo() {foo}");
    assertEquals("foo", func.getName());
    assertTrue(((XBlockExpression) func.getExpression()).getExpressions().get(0) instanceof XFeatureCall);
    assertEquals(0, func.getParameters().size());
    assertNull(func.getReturnType());
    assertEquals(0, func.getTypeParameters().size());
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) XFeatureCall(org.eclipse.xtext.xbase.XFeatureCall) Test(org.junit.Test)

Example 5 with XFeatureCall

use of org.eclipse.xtext.xbase.XFeatureCall in project xtext-xtend by eclipse.

the class ParserTest method testFunction_6.

@Test
public void testFunction_6() throws Exception {
    XtendFunction func = function("override dispatch foo(String s) { foo('x')}");
    assertEquals("foo", func.getName());
    assertTrue(((XBlockExpression) func.getExpression()).getExpressions().get(0) instanceof XFeatureCall);
    assertEquals(1, func.getParameters().size());
    assertNull(func.getReturnType());
    assertTrue(func.isOverride());
    assertTrue(func.isDispatch());
    assertEquals(0, func.getTypeParameters().size());
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) XFeatureCall(org.eclipse.xtext.xbase.XFeatureCall) Test(org.junit.Test)

Aggregations

XFeatureCall (org.eclipse.xtext.xbase.XFeatureCall)101 Test (org.junit.Test)84 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)75 XBlockExpression (org.eclipse.xtext.xbase.XBlockExpression)73 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)62 JvmIdentifiableElement (org.eclipse.xtext.common.types.JvmIdentifiableElement)30 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)29 XMemberFeatureCall (org.eclipse.xtext.xbase.XMemberFeatureCall)22 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)20 XExpression (org.eclipse.xtext.xbase.XExpression)15 XAbstractFeatureCall (org.eclipse.xtext.xbase.XAbstractFeatureCall)8 RichString (org.eclipse.xtend.core.xtend.RichString)7 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)7 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)6 XAssignment (org.eclipse.xtext.xbase.XAssignment)6 XTryCatchFinallyExpression (org.eclipse.xtext.xbase.XTryCatchFinallyExpression)6 EObject (org.eclipse.emf.ecore.EObject)5 XtendConstructor (org.eclipse.xtend.core.xtend.XtendConstructor)5 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)5 XClosure (org.eclipse.xtext.xbase.XClosure)5