Search in sources :

Example 1 with Feature

use of org.eclipse.xtext.example.domainmodel.domainmodel.Feature in project xtext-eclipse by eclipse.

the class DomainmodelParsingTest method testReturnTypeInference.

@Test
public void testReturnTypeInference() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("package example {");
        _builder.newLine();
        _builder.append("  ");
        _builder.append("entity MyEntity {");
        _builder.newLine();
        _builder.append("    ");
        _builder.append("property : String");
        _builder.newLine();
        _builder.append("    ");
        _builder.append("op foo(String s) {");
        _builder.newLine();
        _builder.append("    \t");
        _builder.append("return property.toUpperCase + s");
        _builder.newLine();
        _builder.append("    ");
        _builder.append("}");
        _builder.newLine();
        _builder.append("  ");
        _builder.append("}");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        final DomainModel model = this._parseHelper.parse(_builder);
        AbstractElement _head = IterableExtensions.<AbstractElement>head(model.getElements());
        final PackageDeclaration pack = ((PackageDeclaration) _head);
        AbstractElement _head_1 = IterableExtensions.<AbstractElement>head(pack.getElements());
        final Entity entity = ((Entity) _head_1);
        Feature _last = IterableExtensions.<Feature>last(entity.getFeatures());
        final Operation op = ((Operation) _last);
        EObject _head_2 = IterableExtensions.<EObject>head(this._iJvmModelAssociations.getJvmElements(op));
        final JvmOperation method = ((JvmOperation) _head_2);
        Assert.assertEquals("String", method.getReturnType().getSimpleName());
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) Entity(org.eclipse.xtext.example.domainmodel.domainmodel.Entity) DomainModel(org.eclipse.xtext.example.domainmodel.domainmodel.DomainModel) AbstractElement(org.eclipse.xtext.example.domainmodel.domainmodel.AbstractElement) EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) Operation(org.eclipse.xtext.example.domainmodel.domainmodel.Operation) Feature(org.eclipse.xtext.example.domainmodel.domainmodel.Feature) PackageDeclaration(org.eclipse.xtext.example.domainmodel.domainmodel.PackageDeclaration) Test(org.junit.Test)

Example 2 with Feature

use of org.eclipse.xtext.example.domainmodel.domainmodel.Feature in project xtext-eclipse by eclipse.

the class DomainmodelParsingTest method testParsing.

@Test
public void testParsing() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("package example {");
        _builder.newLine();
        _builder.append("  ");
        _builder.append("entity MyEntity {");
        _builder.newLine();
        _builder.append("    ");
        _builder.append("property : String");
        _builder.newLine();
        _builder.append("  ");
        _builder.append("}");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        final DomainModel model = this._parseHelper.parse(_builder);
        AbstractElement _get = model.getElements().get(0);
        final PackageDeclaration pack = ((PackageDeclaration) _get);
        Assert.assertEquals("example", pack.getName());
        AbstractElement _get_1 = pack.getElements().get(0);
        final Entity entity = ((Entity) _get_1);
        Assert.assertEquals("MyEntity", entity.getName());
        Feature _get_2 = entity.getFeatures().get(0);
        final Property property = ((Property) _get_2);
        Assert.assertEquals("property", property.getName());
        Assert.assertEquals("java.lang.String", property.getType().getIdentifier());
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : Entity(org.eclipse.xtext.example.domainmodel.domainmodel.Entity) DomainModel(org.eclipse.xtext.example.domainmodel.domainmodel.DomainModel) AbstractElement(org.eclipse.xtext.example.domainmodel.domainmodel.AbstractElement) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Feature(org.eclipse.xtext.example.domainmodel.domainmodel.Feature) Property(org.eclipse.xtext.example.domainmodel.domainmodel.Property) PackageDeclaration(org.eclipse.xtext.example.domainmodel.domainmodel.PackageDeclaration) Test(org.junit.Test)

Example 3 with Feature

use of org.eclipse.xtext.example.domainmodel.domainmodel.Feature in project xtext-eclipse by eclipse.

the class DomainmodelJvmModelInferrer method _infer.

protected void _infer(final Entity entity, @Extension final IJvmDeclaredTypeAcceptor acceptor, final boolean prelinkingPhase) {
    final Procedure1<JvmGenericType> _function = (JvmGenericType it) -> {
        this._jvmTypesBuilder.setDocumentation(it, this._jvmTypesBuilder.getDocumentation(entity));
        JvmParameterizedTypeReference _superType = entity.getSuperType();
        boolean _tripleNotEquals = (_superType != null);
        if (_tripleNotEquals) {
            EList<JvmTypeReference> _superTypes = it.getSuperTypes();
            JvmTypeReference _cloneWithProxies = this._jvmTypesBuilder.cloneWithProxies(entity.getSuperType());
            this._jvmTypesBuilder.<JvmTypeReference>operator_add(_superTypes, _cloneWithProxies);
        }
        EList<JvmMember> _members = it.getMembers();
        final Procedure1<JvmConstructor> _function_1 = (JvmConstructor it_1) -> {
        };
        JvmConstructor _constructor = this._jvmTypesBuilder.toConstructor(entity, _function_1);
        this._jvmTypesBuilder.<JvmConstructor>operator_add(_members, _constructor);
        final JvmTypeReference procedureType = this._typeReferenceBuilder.typeRef(Procedure1.class, this._typeReferenceBuilder.typeRef(it));
        EList<JvmMember> _members_1 = it.getMembers();
        final Procedure1<JvmConstructor> _function_2 = (JvmConstructor it_1) -> {
            EList<JvmFormalParameter> _parameters = it_1.getParameters();
            JvmFormalParameter _parameter = this._jvmTypesBuilder.toParameter(entity, "initializer", procedureType);
            this._jvmTypesBuilder.<JvmFormalParameter>operator_add(_parameters, _parameter);
            StringConcatenationClient _client = new StringConcatenationClient() {

                @Override
                protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
                    _builder.append("initializer.apply(this);");
                    _builder.newLine();
                }
            };
            this._jvmTypesBuilder.setBody(it_1, _client);
        };
        JvmConstructor _constructor_1 = this._jvmTypesBuilder.toConstructor(entity, _function_2);
        this._jvmTypesBuilder.<JvmConstructor>operator_add(_members_1, _constructor_1);
        EList<Feature> _features = entity.getFeatures();
        for (final Feature f : _features) {
            boolean _matched = false;
            if (f instanceof Property) {
                _matched = true;
                final JvmField field = this._jvmTypesBuilder.toField(f, ((Property) f).getName(), ((Property) f).getType());
                EList<JvmMember> _members_2 = it.getMembers();
                this._jvmTypesBuilder.<JvmField>operator_add(_members_2, field);
                EList<JvmMember> _members_3 = it.getMembers();
                JvmOperation _getter = this._jvmTypesBuilder.toGetter(f, ((Property) f).getName(), ((Property) f).getType());
                this._jvmTypesBuilder.<JvmOperation>operator_add(_members_3, _getter);
                EList<JvmMember> _members_4 = it.getMembers();
                JvmOperation _setter = this._jvmTypesBuilder.toSetter(f, ((Property) f).getName(), ((Property) f).getType());
                this._jvmTypesBuilder.<JvmOperation>operator_add(_members_4, _setter);
            }
            if (!_matched) {
                if (f instanceof Operation) {
                    _matched = true;
                    EList<JvmMember> _members_2 = it.getMembers();
                    String _name = ((Operation) f).getName();
                    JvmTypeReference _elvis = null;
                    JvmTypeReference _type = ((Operation) f).getType();
                    if (_type != null) {
                        _elvis = _type;
                    } else {
                        JvmTypeReference _inferredType = this._jvmTypesBuilder.inferredType();
                        _elvis = _inferredType;
                    }
                    final Procedure1<JvmOperation> _function_3 = (JvmOperation it_1) -> {
                        this._jvmTypesBuilder.setDocumentation(it_1, this._jvmTypesBuilder.getDocumentation(f));
                        EList<JvmFormalParameter> _params = ((Operation) f).getParams();
                        for (final JvmFormalParameter p : _params) {
                            EList<JvmFormalParameter> _parameters = it_1.getParameters();
                            JvmFormalParameter _parameter = this._jvmTypesBuilder.toParameter(p, p.getName(), p.getParameterType());
                            this._jvmTypesBuilder.<JvmFormalParameter>operator_add(_parameters, _parameter);
                        }
                        this._jvmTypesBuilder.setBody(it_1, ((Operation) f).getBody());
                    };
                    JvmOperation _method = this._jvmTypesBuilder.toMethod(f, _name, _elvis, _function_3);
                    this._jvmTypesBuilder.<JvmOperation>operator_add(_members_2, _method);
                }
            }
        }
        EList<JvmMember> _members_2 = it.getMembers();
        JvmOperation _toStringMethod = this._jvmTypesBuilder.toToStringMethod(entity, it);
        this._jvmTypesBuilder.<JvmOperation>operator_add(_members_2, _toStringMethod);
    };
    acceptor.<JvmGenericType>accept(this._jvmTypesBuilder.toClass(entity, this._iQualifiedNameProvider.getFullyQualifiedName(entity)), _function);
}
Also used : StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) Operation(org.eclipse.xtext.example.domainmodel.domainmodel.Operation) Feature(org.eclipse.xtext.example.domainmodel.domainmodel.Feature) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) EList(org.eclipse.emf.common.util.EList) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) JvmField(org.eclipse.xtext.common.types.JvmField) JvmParameterizedTypeReference(org.eclipse.xtext.common.types.JvmParameterizedTypeReference) Property(org.eclipse.xtext.example.domainmodel.domainmodel.Property)

Example 4 with Feature

use of org.eclipse.xtext.example.domainmodel.domainmodel.Feature in project xtext-eclipse by eclipse.

the class DomainmodelFormatter method _format.

protected void _format(final Entity entity, @Extension final IFormattableDocument document) {
    final ISemanticRegion open = this.textRegionExtensions.regionFor(entity).keyword("{");
    final ISemanticRegion close = this.textRegionExtensions.regionFor(entity).keyword("}");
    final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it) -> {
        it.oneSpace();
    };
    document.surround(this.textRegionExtensions.regionFor(entity).feature(DomainmodelPackage.Literals.ABSTRACT_ELEMENT__NAME), _function);
    final Procedure1<IHiddenRegionFormatter> _function_1 = (IHiddenRegionFormatter it) -> {
        it.oneSpace();
    };
    document.<JvmParameterizedTypeReference>surround(entity.getSuperType(), _function_1);
    final Procedure1<IHiddenRegionFormatter> _function_2 = (IHiddenRegionFormatter it) -> {
        it.newLine();
    };
    document.append(open, _function_2);
    final Procedure1<IHiddenRegionFormatter> _function_3 = (IHiddenRegionFormatter it) -> {
        it.indent();
    };
    document.<ISemanticRegion, ISemanticRegion>interior(open, close, _function_3);
    this.format(entity.getSuperType(), document);
    EList<Feature> _features = entity.getFeatures();
    for (final Feature feature : _features) {
        {
            document.<Feature>format(feature);
            final Procedure1<IHiddenRegionFormatter> _function_4 = (IHiddenRegionFormatter it) -> {
                it.setNewLines(1, 1, 2);
            };
            document.<Feature>append(feature, _function_4);
        }
    }
}
Also used : IHiddenRegionFormatter(org.eclipse.xtext.formatting2.IHiddenRegionFormatter) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion) JvmParameterizedTypeReference(org.eclipse.xtext.common.types.JvmParameterizedTypeReference) Feature(org.eclipse.xtext.example.domainmodel.domainmodel.Feature)

Aggregations

Feature (org.eclipse.xtext.example.domainmodel.domainmodel.Feature)4 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)2 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)2 JvmParameterizedTypeReference (org.eclipse.xtext.common.types.JvmParameterizedTypeReference)2 AbstractElement (org.eclipse.xtext.example.domainmodel.domainmodel.AbstractElement)2 DomainModel (org.eclipse.xtext.example.domainmodel.domainmodel.DomainModel)2 Entity (org.eclipse.xtext.example.domainmodel.domainmodel.Entity)2 Operation (org.eclipse.xtext.example.domainmodel.domainmodel.Operation)2 PackageDeclaration (org.eclipse.xtext.example.domainmodel.domainmodel.PackageDeclaration)2 Property (org.eclipse.xtext.example.domainmodel.domainmodel.Property)2 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)2 Test (org.junit.Test)2 EList (org.eclipse.emf.common.util.EList)1 EObject (org.eclipse.emf.ecore.EObject)1 StringConcatenationClient (org.eclipse.xtend2.lib.StringConcatenationClient)1 JvmConstructor (org.eclipse.xtext.common.types.JvmConstructor)1 JvmField (org.eclipse.xtext.common.types.JvmField)1 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)1 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)1 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)1