Search in sources :

Example 1 with ISourceAppender

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

the class ConstructorBuilderTest method testJavaCustomBody.

@Test
public void testJavaCustomBody() {
    AbstractConstructorBuilder _createConstructorBuilder = this._codeBuilderFactory.createConstructorBuilder(this.getJavaClass());
    final Procedure1<AbstractConstructorBuilder> _function = (AbstractConstructorBuilder it) -> {
        it.setContext(this.getJavaClass());
        final Procedure1<ISourceAppender> _function_1 = (ISourceAppender it_1) -> {
            it_1.append("return");
        };
        it.setBodyGenerator(_function_1);
    };
    AbstractConstructorBuilder _doubleArrow = ObjectExtensions.<AbstractConstructorBuilder>operator_doubleArrow(_createConstructorBuilder, _function);
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("Bar() {");
    _builder.newLine();
    _builder.append("  ");
    _builder.append("return;");
    _builder.newLine();
    _builder.append("}");
    this.assertBuilds(_doubleArrow, _builder.toString());
}
Also used : AbstractConstructorBuilder(org.eclipse.xtend.ide.codebuilder.AbstractConstructorBuilder) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ISourceAppender(org.eclipse.xtext.xbase.compiler.ISourceAppender) Test(org.junit.Test) AbstractBuilderTest(org.eclipse.xtend.ide.tests.codebuilder.AbstractBuilderTest)

Example 2 with ISourceAppender

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

the class MethodBuilderTest method testXtendCustomBody.

@Test
public void testXtendCustomBody() {
    AbstractMethodBuilder _createMethodBuilder = this._codeBuilderFactory.createMethodBuilder(this.getXtendClass());
    final Procedure1<AbstractMethodBuilder> _function = (AbstractMethodBuilder it) -> {
        it.setContext(this.getXtendClass());
        it.setMethodName("foo");
        final Procedure1<ISourceAppender> _function_1 = (ISourceAppender it_1) -> {
            it_1.append("return");
        };
        it.setBodyGenerator(_function_1);
    };
    AbstractMethodBuilder _doubleArrow = ObjectExtensions.<AbstractMethodBuilder>operator_doubleArrow(_createMethodBuilder, _function);
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("def foo() {");
    _builder.newLine();
    _builder.append("  ");
    _builder.append("return");
    _builder.newLine();
    _builder.append("}");
    this.assertBuilds(_doubleArrow, _builder.toString());
}
Also used : Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ISourceAppender(org.eclipse.xtext.xbase.compiler.ISourceAppender) AbstractMethodBuilder(org.eclipse.xtend.ide.codebuilder.AbstractMethodBuilder) Test(org.junit.Test) AbstractBuilderTest(org.eclipse.xtend.ide.tests.codebuilder.AbstractBuilderTest)

Example 3 with ISourceAppender

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

the class MethodBuilderTest method testJavaCustomBody.

@Test
public void testJavaCustomBody() {
    AbstractMethodBuilder _createMethodBuilder = this._codeBuilderFactory.createMethodBuilder(this.getJavaClass());
    final Procedure1<AbstractMethodBuilder> _function = (AbstractMethodBuilder it) -> {
        it.setContext(this.getJavaClass());
        it.setMethodName("foo");
        final Procedure1<ISourceAppender> _function_1 = (ISourceAppender it_1) -> {
            it_1.append("return");
        };
        it.setBodyGenerator(_function_1);
    };
    AbstractMethodBuilder _doubleArrow = ObjectExtensions.<AbstractMethodBuilder>operator_doubleArrow(_createMethodBuilder, _function);
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("void foo() {");
    _builder.newLine();
    _builder.append("  ");
    _builder.append("return;");
    _builder.newLine();
    _builder.append("}");
    this.assertBuilds(_doubleArrow, _builder.toString());
}
Also used : Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) ISourceAppender(org.eclipse.xtext.xbase.compiler.ISourceAppender) AbstractMethodBuilder(org.eclipse.xtend.ide.codebuilder.AbstractMethodBuilder) Test(org.junit.Test) AbstractBuilderTest(org.eclipse.xtend.ide.tests.codebuilder.AbstractBuilderTest)

Example 4 with ISourceAppender

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

the class MemberFromSuperImplementor method appendOverrideFunction.

public void appendOverrideFunction(final XtendTypeDeclaration overrider, final IResolvedOperation overriddenOperation, final ISourceAppender appendable) {
    final JvmDeclaredType inferredType = this.associations.getInferredType(overrider);
    final AbstractMethodBuilder methodBuilder = this.codeBuilderFactory.createMethodBuilder(inferredType);
    this.initializeExecutableBuilder(methodBuilder, inferredType, overriddenOperation);
    methodBuilder.setOverrideFlag(true);
    methodBuilder.setMethodName(overriddenOperation.getDeclaration().getSimpleName());
    methodBuilder.setReturnType(overriddenOperation.getResolvedReturnType());
    boolean _isSynchronized = overriddenOperation.getDeclaration().isSynchronized();
    if (_isSynchronized) {
        methodBuilder.setSynchronizedFlag(true);
    }
    boolean _isEmpty = overriddenOperation.getResolvedTypeParameters().isEmpty();
    boolean _not = (!_isEmpty);
    if (_not) {
        final ArrayList<JvmTypeParameter> typeParameters = CollectionLiterals.<JvmTypeParameter>newArrayList();
        final Procedure2<JvmTypeParameter, Integer> _function = (JvmTypeParameter typeParam, Integer idx) -> {
            final JvmTypeParameter newTypeParam = this.typesFactory.createJvmTypeParameter();
            newTypeParam.setName(typeParam.getName());
            final Consumer<LightweightTypeReference> _function_1 = (LightweightTypeReference it) -> {
                final JvmUpperBound upperBound = this.typesFactory.createJvmUpperBound();
                upperBound.setTypeReference(it.toJavaCompliantTypeReference());
                EList<JvmTypeConstraint> _constraints = newTypeParam.getConstraints();
                _constraints.add(upperBound);
            };
            overriddenOperation.getResolvedTypeParameterConstraints((idx).intValue()).forEach(_function_1);
            typeParameters.add(newTypeParam);
        };
        IterableExtensions.<JvmTypeParameter>forEach(overriddenOperation.getResolvedTypeParameters(), _function);
        methodBuilder.setTypeParameters(typeParameters);
    }
    boolean _isAbstract = overriddenOperation.getDeclaration().isAbstract();
    boolean _not_1 = (!_isAbstract);
    if (_not_1) {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("super.");
        {
            List<JvmTypeParameter> _resolvedTypeParameters = overriddenOperation.getResolvedTypeParameters();
            boolean _hasElements = false;
            for (final JvmTypeParameter typeParam : _resolvedTypeParameters) {
                if (!_hasElements) {
                    _hasElements = true;
                    _builder.append("<");
                } else {
                    _builder.appendImmediate(", ", "");
                }
                String _simpleName = typeParam.getSimpleName();
                _builder.append(_simpleName);
            }
            if (_hasElements) {
                _builder.append(">");
            }
        }
        String _simpleName_1 = overriddenOperation.getDeclaration().getSimpleName();
        _builder.append(_simpleName_1);
        _builder.append("(");
        final Function1<JvmFormalParameter, String> _function_1 = (JvmFormalParameter it) -> {
            return it.getSimpleName();
        };
        String _join = IterableExtensions.join(ListExtensions.<JvmFormalParameter, String>map(overriddenOperation.getDeclaration().getParameters(), _function_1), ", ");
        _builder.append(_join);
        _builder.append(")");
        final String body = _builder.toString();
        final Procedure1<? super ISourceAppender> superTypeRef = this.getImplementedInterface(inferredType, overriddenOperation.getDeclaration().getDeclaringType());
        final Procedure1<ISourceAppender> _function_2 = (ISourceAppender it) -> {
            if (superTypeRef != null) {
                superTypeRef.apply(it);
            }
            it.append(body);
        };
        methodBuilder.setBodyGenerator(_function_2);
    }
    boolean _isValid = methodBuilder.isValid();
    if (_isValid) {
        methodBuilder.build(appendable);
    }
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) ISourceAppender(org.eclipse.xtext.xbase.compiler.ISourceAppender) JvmUpperBound(org.eclipse.xtext.common.types.JvmUpperBound) Consumer(java.util.function.Consumer) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) JvmTypeConstraint(org.eclipse.xtext.common.types.JvmTypeConstraint) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) List(java.util.List) AbstractMethodBuilder(org.eclipse.xtend.ide.codebuilder.AbstractMethodBuilder)

Example 5 with ISourceAppender

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

the class MemberFromSuperImplementor method appendConstructorFromSuper.

public void appendConstructorFromSuper(final XtendClass overrider, final IResolvedConstructor superConstructor, final ISourceAppender appendable) {
    final JvmGenericType inferredType = this.associations.getInferredType(overrider);
    final AbstractConstructorBuilder constructorBuilder = this.codeBuilderFactory.createConstructorBuilder(inferredType);
    this.initializeExecutableBuilder(constructorBuilder, inferredType, superConstructor);
    final Procedure1<ISourceAppender> _function = (ISourceAppender it) -> {
        boolean _isEmpty = superConstructor.getResolvedParameterTypes().isEmpty();
        boolean _not = (!_isEmpty);
        if (_not) {
            StringConcatenation _builder = new StringConcatenation();
            _builder.append("super(");
            final Function1<JvmFormalParameter, String> _function_1 = (JvmFormalParameter it_1) -> {
                return it_1.getSimpleName();
            };
            String _join = IterableExtensions.join(ListExtensions.<JvmFormalParameter, String>map(superConstructor.getDeclaration().getParameters(), _function_1), ", ");
            _builder.append(_join);
            _builder.append(")");
            it.append(_builder);
        }
    };
    constructorBuilder.setBodyGenerator(_function);
    boolean _isValid = constructorBuilder.isValid();
    if (_isValid) {
        constructorBuilder.build(appendable);
    }
}
Also used : AbstractConstructorBuilder(org.eclipse.xtend.ide.codebuilder.AbstractConstructorBuilder) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) ISourceAppender(org.eclipse.xtext.xbase.compiler.ISourceAppender)

Aggregations

ISourceAppender (org.eclipse.xtext.xbase.compiler.ISourceAppender)10 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)6 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)5 AbstractBuilderTest (org.eclipse.xtend.ide.tests.codebuilder.AbstractBuilderTest)4 Test (org.junit.Test)4 AbstractConstructorBuilder (org.eclipse.xtend.ide.codebuilder.AbstractConstructorBuilder)3 AbstractMethodBuilder (org.eclipse.xtend.ide.codebuilder.AbstractMethodBuilder)3 Iterator (java.util.Iterator)2 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)2 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)2 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)2 JvmTypeParameter (org.eclipse.xtext.common.types.JvmTypeParameter)2 JvmUpperBound (org.eclipse.xtext.common.types.JvmUpperBound)2 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Consumer (java.util.function.Consumer)1 EList (org.eclipse.emf.common.util.EList)1 AbstractParameterBuilder (org.eclipse.xtend.ide.codebuilder.AbstractParameterBuilder)1