Search in sources :

Example 46 with XFeatureCall

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

the class VarArgFeatureCallArgumentsTest method toArguments.

protected IFeatureCallArguments toArguments(final String signature, final String invocation, final boolean receiver) {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("def void m(");
        _builder.append(signature);
        _builder.append(") {");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append("m(");
        _builder.append(invocation, "\t");
        _builder.append(")");
        _builder.newLineIfNotEmpty();
        _builder.append("}");
        _builder.newLine();
        final String functionString = _builder.toString();
        final XtendFunction function = this.function(functionString);
        XExpression _expression = function.getExpression();
        final XBlockExpression body = ((XBlockExpression) _expression);
        XExpression _head = IterableExtensions.<XExpression>head(body.getExpressions());
        final XFeatureCall featureCall = ((XFeatureCall) _head);
        final EList<XExpression> arguments = featureCall.getFeatureCallArguments();
        final JvmOperation operation = this._iXtendJvmAssociations.getDirectlyInferredOperation(function);
        final IFeatureCallArguments result = this.factory.createVarArgArguments(arguments, operation.getParameters(), receiver, this.getOwner());
        Class<? extends IFeatureCallArguments> _class = result.getClass();
        boolean _equals = Objects.equal(_class, VarArgFeatureCallArguments.class);
        Assert.assertTrue(_equals);
        return result;
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) XBlockExpression(org.eclipse.xtext.xbase.XBlockExpression) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) XFeatureCall(org.eclipse.xtext.xbase.XFeatureCall) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XExpression(org.eclipse.xtext.xbase.XExpression) IFeatureCallArguments(org.eclipse.xtext.xbase.typesystem.arguments.IFeatureCallArguments)

Example 47 with XFeatureCall

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

the class CreateMemberQuickfixes method addQuickfixes.

@Override
public void addQuickfixes(Issue issue, IssueResolutionAcceptor issueResolutionAcceptor, IXtextDocument xtextDocument, XtextResource resource, EObject referenceOwner, EReference unresolvedReference) throws Exception {
    if (referenceOwner instanceof XAbstractFeatureCall) {
        XAbstractFeatureCall call = (XAbstractFeatureCall) referenceOwner;
        String newMemberName = (issue.getData() != null && issue.getData().length > 0) ? issue.getData()[0] : null;
        if (newMemberName != null) {
            if (call instanceof XMemberFeatureCall) {
                if (!call.isExplicitOperationCallOrBuilderSyntax()) {
                    newFieldQuickfix(newMemberName, call, issue, issueResolutionAcceptor);
                    newGetterQuickfixes(newMemberName, call, issue, issueResolutionAcceptor);
                }
                newMethodQuickfixes(newMemberName, call, issue, issueResolutionAcceptor);
            } else if (call instanceof XFeatureCall) {
                if (!call.isExplicitOperationCallOrBuilderSyntax()) {
                    if (logicalContainerProvider.getNearestLogicalContainer(call) instanceof JvmExecutable)
                        newLocalVariableQuickfix(newMemberName, call, issue, issueResolutionAcceptor);
                    newFieldQuickfix(newMemberName, call, issue, issueResolutionAcceptor);
                    newGetterQuickfixes(newMemberName, call, issue, issueResolutionAcceptor);
                }
                newMethodQuickfixes(newMemberName, call, issue, issueResolutionAcceptor);
            } else if (call instanceof XAssignment) {
                newSetterQuickfix(issue, issueResolutionAcceptor, newMemberName, call);
                XAssignment assigment = (XAssignment) call;
                if (assigment.getAssignable() == null) {
                    newLocalVariableQuickfix(newMemberName, call, issue, issueResolutionAcceptor);
                    newFieldQuickfix(newMemberName, call, issue, issueResolutionAcceptor);
                } else if (isThis(assigment)) {
                    newFieldQuickfix(newMemberName, call, issue, issueResolutionAcceptor);
                }
            }
        }
        if (call.isOperation()) {
            JvmIdentifiableElement feature = call.getFeature();
            if (feature.eIsProxy()) {
                String operatorMethodName = getOperatorMethodName(call);
                if (operatorMethodName != null)
                    newMethodQuickfixes(operatorMethodName, call, issue, issueResolutionAcceptor);
            }
        }
        if (call instanceof XFeatureCall && call.getFeature() instanceof JvmConstructor) {
            newConstructorQuickfix(issue, issueResolutionAcceptor, (XFeatureCall) call);
        }
    }
    if (referenceOwner instanceof XConstructorCall) {
        newConstructorQuickfix(issue, issueResolutionAcceptor, (XConstructorCall) referenceOwner);
    }
}
Also used : JvmExecutable(org.eclipse.xtext.common.types.JvmExecutable) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) XMemberFeatureCall(org.eclipse.xtext.xbase.XMemberFeatureCall) XFeatureCall(org.eclipse.xtext.xbase.XFeatureCall) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) XConstructorCall(org.eclipse.xtext.xbase.XConstructorCall) XAbstractFeatureCall(org.eclipse.xtext.xbase.XAbstractFeatureCall) XAssignment(org.eclipse.xtext.xbase.XAssignment)

Example 48 with XFeatureCall

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

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

the class ExtractMethodRefactoring method calculateExpressions.

protected List<XExpression> calculateExpressions(final List<XExpression> expressions) {
    final XExpression firstExpression = expressions.get(0);
    // instead of the complete XVariableDeclaration
    if (expressions.size() == 1 && firstExpression instanceof XVariableDeclaration) {
        final XtendFunction originalMethod = EcoreUtil2.getContainerOfType(firstExpression, XtendFunction.class);
        for (final EObject element : Iterables.filter(EcoreUtil2.eAllContents(originalMethod.getExpression()), new Predicate<EObject>() {

            @Override
            public boolean apply(EObject input) {
                return !EcoreUtil.isAncestor(expressions, input);
            }
        })) {
            if (element instanceof XFeatureCall) {
                XFeatureCall featureCall = (XFeatureCall) element;
                JvmIdentifiableElement feature = featureCall.getFeature();
                if (EcoreUtil.isAncestor(expressions, feature)) {
                    return singletonList(((XVariableDeclaration) firstExpression).getRight());
                }
            }
        }
    }
    return expressions;
}
Also used : XVariableDeclaration(org.eclipse.xtext.xbase.XVariableDeclaration) XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) XFeatureCall(org.eclipse.xtext.xbase.XFeatureCall) EObject(org.eclipse.emf.ecore.EObject) XExpression(org.eclipse.xtext.xbase.XExpression)

Example 50 with XFeatureCall

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

the class ExtractMethodRefactoring method createMethodCallEdit.

protected void createMethodCallEdit(DocumentRewriter.Section methodCallSection, ITextRegion expressionRegion) throws BadLocationException {
    if (firstExpression.eContainer() instanceof RichString) {
        methodCallSection.append("�");
    }
    if (isNeedsReturnExpression()) {
        JvmIdentifiableElement returnFeature = ((XFeatureCall) returnExpression).getFeature();
        if (isFinalFeature(returnFeature))
            methodCallSection.append("val ");
        else
            methodCallSection.append("var ");
        methodCallSection.append(returnFeature.getSimpleName()).append(" = ");
    }
    boolean needsSurroundingParentheses = false;
    if (firstExpression.eContainer() instanceof XMemberFeatureCall) {
        if (((XMemberFeatureCall) firstExpression.eContainer()).getMemberCallArguments().size() == 1) {
            String expressionExpanded = document.get(expressionRegion.getOffset() - 1, expressionRegion.getLength() + 2);
            if (!expressionExpanded.startsWith("(") || !expressionExpanded.endsWith(")")) {
                needsSurroundingParentheses = true;
                methodCallSection.append("(");
            }
        }
    }
    methodCallSection.append(methodName).append("(");
    boolean isFirst = true;
    for (ParameterInfo parameterInfo : getParameterInfos()) {
        if (!isFirst)
            methodCallSection.append(", ");
        isFirst = false;
        methodCallSection.append(parameterInfo.getOldName());
    }
    methodCallSection.append(")");
    if (needsSurroundingParentheses)
        methodCallSection.append(")");
    if (lastExpression.eContainer() instanceof RichString) {
        methodCallSection.append("�");
    }
}
Also used : RichString(org.eclipse.xtend.core.xtend.RichString) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) XFeatureCall(org.eclipse.xtext.xbase.XFeatureCall) XMemberFeatureCall(org.eclipse.xtext.xbase.XMemberFeatureCall) RichString(org.eclipse.xtend.core.xtend.RichString) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo)

Aggregations

XFeatureCall (org.eclipse.xtext.xbase.XFeatureCall)93 Test (org.junit.Test)81 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)72 XBlockExpression (org.eclipse.xtext.xbase.XBlockExpression)67 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)62 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)29 JvmIdentifiableElement (org.eclipse.xtext.common.types.JvmIdentifiableElement)26 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)20 XMemberFeatureCall (org.eclipse.xtext.xbase.XMemberFeatureCall)19 XExpression (org.eclipse.xtext.xbase.XExpression)14 RichString (org.eclipse.xtend.core.xtend.RichString)7 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)7 XAbstractFeatureCall (org.eclipse.xtext.xbase.XAbstractFeatureCall)6 XtendConstructor (org.eclipse.xtend.core.xtend.XtendConstructor)5 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)5 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)5 EObject (org.eclipse.emf.ecore.EObject)4 XtendField (org.eclipse.xtend.core.xtend.XtendField)4 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)4 ParameterInfo (org.eclipse.jdt.internal.corext.refactoring.ParameterInfo)3