Search in sources :

Example 1 with Entity

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

the class DomainmodelParsingTest method testParsing.

@Test
public void testParsing() throws Exception {
    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 _head = IterableExtensions.<AbstractElement>head(model.getElements());
    final PackageDeclaration pack = ((PackageDeclaration) _head);
    Assert.assertEquals("example", pack.getName());
    AbstractElement _head_1 = IterableExtensions.<AbstractElement>head(pack.getElements());
    final Entity entity = ((Entity) _head_1);
    Assert.assertEquals("MyEntity", entity.getName());
    Feature _head_2 = IterableExtensions.<Feature>head(entity.getFeatures());
    final Property property = ((Property) _head_2);
    Assert.assertEquals("property", property.getName());
    Assert.assertEquals("java.lang.String", property.getType().getIdentifier());
}
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 2 with Entity

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

the class DomainmodelParsingTest method testReturnTypeInference.

@Test
public void testReturnTypeInference() throws Exception {
    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());
}
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 3 with Entity

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

the class DomainmodelCodeMiningProvider method createCodeMinings.

@Override
protected void createCodeMinings(IDocument document, XtextResource resource, CancelIndicator indicator, IAcceptor<? super ICodeMining> acceptor) throws BadLocationException {
    if (resource.getContents().isEmpty()) {
        return;
    }
    // get all entities in the open document
    List<Entity> allEntities = EcoreUtil2.eAllOfType(resource.getContents().get(0), Entity.class);
    for (Entity e : allEntities) {
        int propertiesCount = (int) e.getFeatures().stream().filter(f -> (f instanceof Property)).count();
        String propertiesHeaderText = propertiesCount + " " + (propertiesCount == 1 ? "property" : "properties");
        int operationsCount = (int) e.getFeatures().stream().filter(f -> (f instanceof Operation)).count();
        String operationsHeaderText = operationsCount + " operation" + (operationsCount == 1 ? "" : "s");
        ICompositeNode node = NodeModelUtils.getNode(e);
        int beforeLineNumber = document.getLineOfOffset(node.getOffset());
        // create two line header code minings before the entity: one for the properties, one for the operations
        acceptor.accept(createNewLineHeaderCodeMining(beforeLineNumber, document, propertiesHeaderText));
        acceptor.accept(createNewLineHeaderCodeMining(beforeLineNumber, document, operationsHeaderText));
    }
    // get all operations in the open document
    List<Operation> allOperations = EcoreUtil2.eAllOfType(resource.getContents().get(0), Operation.class);
    // get keyword for ')'
    Keyword rightParenthesisKeyword_4 = grammar.getOperationAccess().getRightParenthesisKeyword_4();
    for (Operation o : allOperations) {
        // inline annotations only for methods with no return type
        if (o.getType() != null) {
            continue;
        }
        // get return type name from operation
        JvmOperation inferredOp = (JvmOperation) jvmModelAssociations.getPrimaryJvmElement(o);
        if (inferredOp == null || inferredOp.getReturnType() == null) {
            // broken model
            continue;
        }
        String returnTypeName = inferredOp.getReturnType().getSimpleName();
        // find document offset for inline annotation
        ICompositeNode node = NodeModelUtils.findActualNodeFor(o);
        for (Iterator<INode> it = node.getAsTreeIterable().iterator(); it.hasNext(); ) {
            INode child = it.next();
            if (rightParenthesisKeyword_4.equals(child.getGrammarElement())) {
                // create line content code mining for inline annotation after grammarElement ')'
                String annotationText = " : " + returnTypeName;
                acceptor.accept(createNewLineContentCodeMining(child.getTotalOffset() + 1, annotationText));
            }
        }
    }
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) Entity(org.eclipse.xtext.example.domainmodel.domainmodel.Entity) INode(org.eclipse.xtext.nodemodel.INode) Keyword(org.eclipse.xtext.Keyword) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) Operation(org.eclipse.xtext.example.domainmodel.domainmodel.Operation) Property(org.eclipse.xtext.example.domainmodel.domainmodel.Property)

Example 4 with Entity

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

the class DomainmodelLinkingDiagnosticMessageProvider method getUnresolvedProxyMessage.

@Override
public DiagnosticMessage getUnresolvedProxyMessage(final ILinkingDiagnosticContext context) {
    EObject element = context.getContext();
    if (element instanceof JvmTypeReference) {
        JvmTypeReference jvmTypeReference = (JvmTypeReference) element;
        DiagnosticMessage diagnosticMessage = new DomainmodelSwitch<DiagnosticMessage>() {

            @Override
            public DiagnosticMessage caseEntity(Entity entity) {
                return new DiagnosticMessage("Missing supertype " + context.getLinkText(), Severity.ERROR, IssueCodes.MISSING_TYPE, context.getLinkText());
            }

            @Override
            public DiagnosticMessage caseProperty(Property property) {
                return new DiagnosticMessage("Missing property type " + context.getLinkText(), Severity.ERROR, IssueCodes.MISSING_TYPE, context.getLinkText());
            }

            @Override
            public DiagnosticMessage caseOperation(Operation operation) {
                return new DiagnosticMessage("Missing return type " + context.getLinkText(), Severity.ERROR, IssueCodes.MISSING_TYPE, context.getLinkText());
            }
        }.doSwitch(jvmTypeReference.eContainer());
        if (diagnosticMessage != null)
            return diagnosticMessage;
    }
    return super.getUnresolvedProxyMessage(context);
}
Also used : Entity(org.eclipse.xtext.example.domainmodel.domainmodel.Entity) DiagnosticMessage(org.eclipse.xtext.diagnostics.DiagnosticMessage) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) EObject(org.eclipse.emf.ecore.EObject) Operation(org.eclipse.xtext.example.domainmodel.domainmodel.Operation) Property(org.eclipse.xtext.example.domainmodel.domainmodel.Property)

Example 5 with Entity

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

the class AssociationHierarchyBuilder method findDeclaration.

@Override
protected IEObjectDescription findDeclaration(final URI objectURI) {
    final IEObjectDescription description = this.getDescription(objectURI);
    EClass _eClass = null;
    if (description != null) {
        _eClass = description.getEClass();
    }
    boolean _isJvmType = this.isJvmType(_eClass);
    if (_isJvmType) {
        final IUnitOfWork<IEObjectDescription, EObject> _function = (EObject targetElement) -> {
            final EObject sourceElement = this._iJvmModelAssociations.getPrimarySourceElement(targetElement);
            EClass _eClass_1 = null;
            if (sourceElement != null) {
                _eClass_1 = sourceElement.eClass();
            }
            boolean _isEntity = this.isEntity(_eClass_1);
            if (_isEntity) {
                return this.getDescription(sourceElement);
            }
            return null;
        };
        return this.<IEObjectDescription>readOnly(description.getEObjectURI(), _function);
    }
    EClass _eClass_1 = null;
    if (description != null) {
        _eClass_1 = description.getEClass();
    }
    boolean _isEntity = this.isEntity(_eClass_1);
    if (_isEntity) {
        return description;
    }
    final IUnitOfWork<IEObjectDescription, EObject> _function_1 = (EObject object) -> {
        return this.getDescription(EcoreUtil2.<Entity>getContainerOfType(object, Entity.class));
    };
    return this.<IEObjectDescription>readOnly(objectURI, _function_1);
}
Also used : Entity(org.eclipse.xtext.example.domainmodel.domainmodel.Entity) EClass(org.eclipse.emf.ecore.EClass) EObject(org.eclipse.emf.ecore.EObject) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Aggregations

Entity (org.eclipse.xtext.example.domainmodel.domainmodel.Entity)8 Operation (org.eclipse.xtext.example.domainmodel.domainmodel.Operation)5 Property (org.eclipse.xtext.example.domainmodel.domainmodel.Property)5 EObject (org.eclipse.emf.ecore.EObject)4 PackageDeclaration (org.eclipse.xtext.example.domainmodel.domainmodel.PackageDeclaration)4 DomainModel (org.eclipse.xtext.example.domainmodel.domainmodel.DomainModel)3 Feature (org.eclipse.xtext.example.domainmodel.domainmodel.Feature)3 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)2 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)2 AbstractElement (org.eclipse.xtext.example.domainmodel.domainmodel.AbstractElement)2 INode (org.eclipse.xtext.nodemodel.INode)2 Test (org.junit.Test)2 Iterables (com.google.common.collect.Iterables)1 Inject (com.google.inject.Inject)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 EClass (org.eclipse.emf.ecore.EClass)1 EPackage (org.eclipse.emf.ecore.EPackage)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1