Search in sources :

Example 46 with XtendField

use of org.eclipse.xtend.core.xtend.XtendField in project xtext-xtend by eclipse.

the class XtendUIValidationTests method testForbiddenConstructorCallInnerClass.

@Test
public void testForbiddenConstructorCallInnerClass() {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("import org.eclipse.xtend.core.tests.restricted.RestrictedClass$InnerRestrictedClass");
        _builder.newLine();
        _builder.append("class Foo {");
        _builder.newLine();
        _builder.append("\t");
        _builder.append("InnerRestrictedClass x = new InnerRestrictedClass");
        _builder.newLine();
        _builder.append("}");
        _builder.newLine();
        final XtendFile xtendFile = this.testHelper.xtendFile("Clazz.xtend", _builder.toString());
        this.helper.assertError(xtendFile.getImportSection().getImportDeclarations().get(0), XtypePackage.Literals.XIMPORT_DECLARATION, org.eclipse.xtext.xbase.validation.IssueCodes.FORBIDDEN_REFERENCE);
        XtendMember _head = IterableExtensions.<XtendMember>head(IterableExtensions.<XtendClass>head(Iterables.<XtendClass>filter(xtendFile.getXtendTypes(), XtendClass.class)).getMembers());
        final XtendField field = ((XtendField) _head);
        this.helper.assertError(field.getType(), TypesPackage.Literals.JVM_TYPE_REFERENCE, org.eclipse.xtext.xbase.validation.IssueCodes.FORBIDDEN_REFERENCE);
        this.helper.assertError(field.getInitialValue(), XbasePackage.Literals.XCONSTRUCTOR_CALL, org.eclipse.xtext.xbase.validation.IssueCodes.FORBIDDEN_REFERENCE);
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtendField(org.eclipse.xtend.core.xtend.XtendField) Test(org.junit.Test)

Example 47 with XtendField

use of org.eclipse.xtend.core.xtend.XtendField in project xtext-xtend by eclipse.

the class XtendProposalProvider method completeMember_Name.

@Override
public void completeMember_Name(final EObject model, Assignment assignment, final ContentAssistContext context, final ICompletionProposalAcceptor acceptor) {
    if (announceProcessing(Lists.newArrayList("completeMember_Name", model, assignment.getFeature()))) {
        EObject previousModel = context.getPreviousModel();
        if (previousModel instanceof XExpression) {
            if (!(previousModel instanceof XBlockExpression)) {
                return;
            }
        }
        if (model instanceof XtendField) {
            // TODO go up type hierarchy and collect all local fields
            final List<XtendField> siblings = EcoreUtil2.getSiblingsOfType(model, XtendField.class);
            Set<String> alreadyTaken = Sets.newHashSet();
            for (XtendField sibling : siblings) {
                alreadyTaken.add(sibling.getName());
            }
            alreadyTaken.addAll(getAllKeywords());
            completions.getVariableProposals(model, XtendPackage.Literals.XTEND_FIELD__TYPE, VariableType.INSTANCE_FIELD, alreadyTaken, new JdtVariableCompletions.CompletionDataAcceptor() {

                @Override
                public void accept(String replaceText, StyledString label, Image img) {
                    acceptor.accept(createCompletionProposal(replaceText, label, img, context));
                }
            });
        } else if (model instanceof XtendFunction) {
            for (QualifiedName operator : operatorMapping.getOperators()) {
                StyledString displayString = new StyledString(operator.getFirstSegment());
                displayString.append(" " + operatorMapping.getMethodName(operator), StyledString.DECORATIONS_STYLER);
                acceptor.accept(createCompletionProposal(operator.getFirstSegment(), displayString, getImage(model), context));
            }
            super.completeMember_Name(model, assignment, context, acceptor);
        } else {
            super.completeMember_Name(model, assignment, context, acceptor);
        }
    }
}
Also used : XBlockExpression(org.eclipse.xtext.xbase.XBlockExpression) XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) QualifiedName(org.eclipse.xtext.naming.QualifiedName) JdtVariableCompletions(org.eclipse.xtext.common.types.xtext.ui.JdtVariableCompletions) StyledString(org.eclipse.jface.viewers.StyledString) StyledString(org.eclipse.jface.viewers.StyledString) Image(org.eclipse.swt.graphics.Image) XtendField(org.eclipse.xtend.core.xtend.XtendField) EObject(org.eclipse.emf.ecore.EObject) XExpression(org.eclipse.xtext.xbase.XExpression)

Example 48 with XtendField

use of org.eclipse.xtend.core.xtend.XtendField in project xtext-xtend by eclipse.

the class XtendProposalProvider method completeMember_Members.

@Override
public void completeMember_Members(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
    if (isValidTypeForOverriding(model)) {
        INode node = context.getCurrentNode();
        EObject eObject = NodeModelUtils.findActualSemanticObjectFor(node);
        if (!(eObject instanceof AnonymousClass)) {
            overrideAssist.createOverrideProposals((XtendTypeDeclaration) model, context, acceptor, getConflictHelper());
            return;
        }
    } else if (model instanceof XtendField) {
        /*
			 * class C {
			 *   static class C2 {
			 *     toS<|>
			 *   }
			 * }
			 *
			 * At this cursor position, we get a field without a name and the type 'toS' as the context.
			 * If there's a field decl preceding the cursor position, the field will have a name.
			 */
        XtendField field = (XtendField) model;
        if (isValidTypeForOverriding(field.eContainer())) {
            overrideAssist.createOverrideProposals((XtendTypeDeclaration) field.eContainer(), context, acceptor, getConflictHelper());
            return;
        }
    } else if (model instanceof XtendExecutable && context.getPrefix().length() == 0 && isValidTypeForOverriding(model.eContainer())) {
        overrideAssist.createOverrideProposals((XtendTypeDeclaration) model.eContainer(), context, acceptor, getConflictHelper());
        return;
    } else if (model instanceof XExpression) {
        XtendMember member = EcoreUtil2.getContainerOfType(model, XtendMember.class);
        INode memberNode = NodeModelUtils.findActualNodeFor(member);
        if (memberNode.getTotalEndOffset() <= context.getOffset()) {
            if (isValidTypeForOverriding(member.eContainer())) {
                overrideAssist.createOverrideProposals((XtendTypeDeclaration) member.eContainer(), context, acceptor, getConflictHelper());
                return;
            }
        }
    }
    INode node = context.getCurrentNode();
    EObject eObject = NodeModelUtils.findActualSemanticObjectFor(node);
    if (isValidTypeForOverriding(eObject))
        overrideAssist.createOverrideProposals((XtendTypeDeclaration) eObject, context, acceptor, getConflictHelper());
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) AnonymousClass(org.eclipse.xtend.core.xtend.AnonymousClass) EObject(org.eclipse.emf.ecore.EObject) XtendExecutable(org.eclipse.xtend.core.xtend.XtendExecutable) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) XExpression(org.eclipse.xtext.xbase.XExpression) XtendField(org.eclipse.xtend.core.xtend.XtendField)

Example 49 with XtendField

use of org.eclipse.xtend.core.xtend.XtendField in project xtext-xtend by eclipse.

the class XtendProposalProvider method completeXConstructorCall_Members.

@Override
public void completeXConstructorCall_Members(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
    if (model instanceof AnonymousClass) {
        overrideAssist.createOverrideProposals((AnonymousClass) model, context, acceptor, getConflictHelper());
        return;
    } else if (model instanceof XtendField) {
        /*
			 * class C {
			 *   val x = new Object() {
			 *     toS<|>
			 *   }
			 * }
			 *
			 * At this cursor position, we get a field without a name and the type 'toS' as the context.
			 * If there's a field decl preceding the cursor position, the field will have a name.
			 */
        XtendField field = (XtendField) model;
        if (field.eContainer() instanceof AnonymousClass) {
            overrideAssist.createOverrideProposals((AnonymousClass) field.eContainer(), context, acceptor, getConflictHelper());
            return;
        }
    } else if (model instanceof XtendExecutable && context.getPrefix().length() == 0 && model.eContainer() instanceof AnonymousClass) {
        overrideAssist.createOverrideProposals((AnonymousClass) model.eContainer(), context, acceptor, getConflictHelper());
        return;
    } else if (model instanceof XExpression) {
        XtendMember member = EcoreUtil2.getContainerOfType(model, XtendMember.class);
        INode memberNode = NodeModelUtils.findActualNodeFor(member);
        if (memberNode.getTotalEndOffset() <= context.getOffset()) {
            if (member.eContainer() instanceof AnonymousClass) {
                overrideAssist.createOverrideProposals((AnonymousClass) member.eContainer(), context, acceptor, getConflictHelper());
                return;
            }
        }
    }
    INode node = context.getCurrentNode();
    EObject eObject = NodeModelUtils.findActualSemanticObjectFor(node);
    if (eObject instanceof AnonymousClass)
        overrideAssist.createOverrideProposals((XtendTypeDeclaration) eObject, context, acceptor, getConflictHelper());
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) AnonymousClass(org.eclipse.xtend.core.xtend.AnonymousClass) XtendExecutable(org.eclipse.xtend.core.xtend.XtendExecutable) EObject(org.eclipse.emf.ecore.EObject) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) XExpression(org.eclipse.xtext.xbase.XExpression) XtendField(org.eclipse.xtend.core.xtend.XtendField)

Example 50 with XtendField

use of org.eclipse.xtend.core.xtend.XtendField in project xtext-xtend by eclipse.

the class XtendHyperlinkHelper method createHyperlinksByOffset.

@Override
public void createHyperlinksByOffset(XtextResource resource, int offset, IHyperlinkAcceptor acceptor) {
    super.createHyperlinksByOffset(resource, offset, acceptor);
    if (canShowMany(acceptor)) {
        final EObject element = getEObjectAtOffsetHelper().resolveElementAt(resource, offset);
        if (element instanceof XtendField) {
            XtendField member = (XtendField) element;
            ILeafNode node = NodeModelUtils.findLeafNodeAtOffset(resource.getParseResult().getRootNode(), offset);
            if (isNameNode(member, XtendPackage.Literals.XTEND_FIELD__NAME, node) && member.getType() == null) {
                EObject jvmElement = associations.getPrimaryJvmElement(member);
                if (jvmElement instanceof JvmIdentifiableElement) {
                    addOpenInferredTypeHyperlink(resource, (JvmIdentifiableElement) jvmElement, node, acceptor);
                }
            }
        }
        if (element instanceof XtendFunction) {
            XtendFunction member = (XtendFunction) element;
            ILeafNode node = NodeModelUtils.findLeafNodeAtOffset(resource.getParseResult().getRootNode(), offset);
            if (isNameNode(member, XtendPackage.Literals.XTEND_FUNCTION__NAME, node)) {
                EObject jvmElement = associations.getPrimaryJvmElement(member);
                if (jvmElement instanceof JvmIdentifiableElement) {
                    JvmIdentifiableElement identifiableElement = (JvmIdentifiableElement) jvmElement;
                    if (member.getReturnType() == null) {
                        addOpenInferredTypeHyperlink(resource, identifiableElement, node, acceptor);
                    }
                    IJavaElement javaElement = javaElementFinder.findExactElementFor(identifiableElement);
                    if (sourceViewer != null && javaElement != null && (javaElement.getElementType() == IJavaElement.METHOD && canBeOverridden((IMethod) javaElement))) {
                        Region region = new Region(node.getOffset(), node.getLength());
                        acceptor.accept(new XbaseImplementatorsHyperlink(javaElement, region, sourceViewer, implOpener));
                    }
                }
            }
        }
    }
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) IJavaElement(org.eclipse.jdt.core.IJavaElement) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) EObject(org.eclipse.emf.ecore.EObject) XbaseImplementatorsHyperlink(org.eclipse.xtext.xbase.ui.navigation.XbaseImplementatorsHyperlink) Region(org.eclipse.jface.text.Region) IMethod(org.eclipse.jdt.core.IMethod) XtendField(org.eclipse.xtend.core.xtend.XtendField)

Aggregations

XtendField (org.eclipse.xtend.core.xtend.XtendField)71 Test (org.junit.Test)50 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)38 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)33 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)29 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)23 XExpression (org.eclipse.xtext.xbase.XExpression)23 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)14 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)12 EObject (org.eclipse.emf.ecore.EObject)8 RichString (org.eclipse.xtend.core.xtend.RichString)7 XtendConstructor (org.eclipse.xtend.core.xtend.XtendConstructor)6 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)6 XClosure (org.eclipse.xtext.xbase.XClosure)6 JvmIdentifiableElement (org.eclipse.xtext.common.types.JvmIdentifiableElement)5 XBlockExpression (org.eclipse.xtext.xbase.XBlockExpression)5 XtendAnnotationType (org.eclipse.xtend.core.xtend.XtendAnnotationType)4 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)4 IResolvedTypes (org.eclipse.xtext.xbase.typesystem.IResolvedTypes)4 AnonymousClass (org.eclipse.xtend.core.xtend.AnonymousClass)3