Search in sources :

Example 56 with JvmIdentifiableElement

use of org.eclipse.xtext.common.types.JvmIdentifiableElement in project xtext-xtend by eclipse.

the class XtendQuickfixProvider method specifyTypeExplicitly.

@Fix(IssueCodes.API_TYPE_INFERENCE)
public void specifyTypeExplicitly(Issue issue, IssueResolutionAcceptor acceptor) {
    acceptor.accept(issue, "Infer type", "Infer type", null, new ISemanticModification() {

        @Override
        public void apply(EObject element, IModificationContext context) throws Exception {
            EStructuralFeature featureAfterType = null;
            JvmIdentifiableElement jvmElement = null;
            if (element instanceof XtendFunction) {
                XtendFunction function = (XtendFunction) element;
                if (function.getCreateExtensionInfo() == null) {
                    featureAfterType = XtendPackage.Literals.XTEND_FUNCTION__NAME;
                } else {
                    featureAfterType = XtendPackage.Literals.XTEND_FUNCTION__CREATE_EXTENSION_INFO;
                }
                jvmElement = associations.getDirectlyInferredOperation((XtendFunction) element);
            } else if (element instanceof XtendField) {
                featureAfterType = XtendPackage.Literals.XTEND_FIELD__NAME;
                jvmElement = associations.getJvmField((XtendField) element);
            }
            if (jvmElement != null) {
                LightweightTypeReference type = batchTypeResolver.resolveTypes(element).getActualType(jvmElement);
                INode node = Iterables.getFirst(NodeModelUtils.findNodesForFeature(element, featureAfterType), null);
                if (node == null) {
                    throw new IllegalStateException("Could not determine node for " + element);
                }
                if (type == null) {
                    throw new IllegalStateException("Could not determine type for " + element);
                }
                ReplacingAppendable appendable = appendableFactory.create(context.getXtextDocument(), (XtextResource) element.eResource(), node.getOffset(), 0);
                appendable.append(type);
                appendable.append(" ");
                appendable.commitChanges();
            }
        }
    });
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) INode(org.eclipse.xtext.nodemodel.INode) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) XtextResource(org.eclipse.xtext.resource.XtextResource) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) XtendField(org.eclipse.xtend.core.xtend.XtendField) EObject(org.eclipse.emf.ecore.EObject) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 57 with JvmIdentifiableElement

use of org.eclipse.xtext.common.types.JvmIdentifiableElement in project xtext-xtend by eclipse.

the class XtendReferenceFinder method addReferenceToTypeFromStaticImport.

protected void addReferenceToTypeFromStaticImport(final XAbstractFeatureCall sourceCandidate, final Predicate<URI> targetURISet, final IReferenceFinder.Acceptor acceptor) {
    final JvmIdentifiableElement feature = sourceCandidate.getFeature();
    if ((feature instanceof JvmMember)) {
        final JvmDeclaredType type = ((JvmMember) feature).getDeclaringType();
        this.addReferenceIfTarget(type, targetURISet, sourceCandidate, XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE, acceptor);
    }
}
Also used : JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) JvmMember(org.eclipse.xtext.common.types.JvmMember)

Example 58 with JvmIdentifiableElement

use of org.eclipse.xtext.common.types.JvmIdentifiableElement in project xtext-xtend by eclipse.

the class SyntheticNameClashResolver method resolveNameClashes.

public void resolveNameClashes(JvmGenericType type) {
    Multimap<String, JvmIdentifiableElement> classScope = HashMultimap.create();
    List<JvmMember> renameableMembers = newArrayList();
    int i = 1;
    for (JvmMember member : type.getMembers()) {
        String simpleName = member.getSimpleName();
        if (simpleName != null) {
            if (!isRenameable(member))
                classScope.put(simpleName, member);
            else
                renameableMembers.add(member);
        }
        if (member instanceof JvmFeature) {
            for (JvmGenericType localType : ((JvmFeature) member).getLocalClasses()) {
                localType.setSimpleName(localType.getSimpleName() + '_' + (i++));
            }
        }
    }
    for (JvmMember renameable : renameableMembers) {
        String simpleName = renameable.getSimpleName();
        if (collides(renameable, simpleName, classScope)) {
            int count = 0;
            String currentName;
            do {
                if (count == Integer.MAX_VALUE)
                    throw new IllegalStateException("Cannot find a collision-free name for " + renameable.getIdentifier());
                currentName = simpleName + "_" + ++count;
            } while (collides(renameable, currentName, classScope));
            renameable.setSimpleName(currentName);
            classScope.put(currentName, renameable);
        } else {
            classScope.put(simpleName, renameable);
        }
    }
}
Also used : JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) JvmFeature(org.eclipse.xtext.common.types.JvmFeature) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) JvmMember(org.eclipse.xtext.common.types.JvmMember)

Example 59 with JvmIdentifiableElement

use of org.eclipse.xtext.common.types.JvmIdentifiableElement in project xtext-xtend by eclipse.

the class XtendHoverSerializer method computeUnsugaredExpression.

public String computeUnsugaredExpression(EObject object) {
    if (object instanceof XAbstractFeatureCall) {
        StringBuilder stringBuilder = new StringBuilder();
        XAbstractFeatureCall featureCall = (XAbstractFeatureCall) object;
        JvmIdentifiableElement feature = featureCall.getFeature();
        if (feature != null && !feature.eIsProxy()) {
            // Static extensions which are no expliciteOperationCalls
            if (featureCall instanceof XMemberFeatureCall && feature instanceof JvmOperation && !((XMemberFeatureCall) featureCall).isExplicitOperationCall()) {
                JvmOperation jvmOperation = (JvmOperation) feature;
                if (jvmOperation.isStatic()) {
                    return stringBuilder.append(getStaticCallDesugaredVersion(featureCall, jvmOperation)).toString();
                }
            }
            // Static calls with implicit receiver or implicit first argument
            if (featureCall.getImplicitReceiver() != null || featureCall.getImplicitFirstArgument() != null) {
                if (featureCall.isStatic()) {
                    return stringBuilder.append(getStaticCallDesugaredVersion(featureCall, (JvmMember) feature)).toString();
                }
                XExpression receiver = featureCall.getActualReceiver();
                if (receiver instanceof XMemberFeatureCall) {
                    stringBuilder.append(THIS).append(DELIMITER);
                    stringBuilder.append(((XMemberFeatureCall) receiver).getFeature().getSimpleName()).append(DELIMITER);
                } else if (receiver instanceof XAbstractFeatureCall) {
                    JvmIdentifiableElement receiverFeature = ((XAbstractFeatureCall) receiver).getFeature();
                    if (receiverFeature == feature.eContainer()) {
                        stringBuilder.append(THIS).append(DELIMITER);
                    } else {
                        stringBuilder.append(receiverFeature.getSimpleName()).append(DELIMITER);
                    }
                }
                stringBuilder.append(feature.getSimpleName());
                if (feature instanceof JvmExecutable)
                    stringBuilder.append(computeArguments(featureCall));
                return stringBuilder.toString();
            }
        }
    }
    return "";
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmExecutable(org.eclipse.xtext.common.types.JvmExecutable) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) XMemberFeatureCall(org.eclipse.xtext.xbase.XMemberFeatureCall) XExpression(org.eclipse.xtext.xbase.XExpression) XAbstractFeatureCall(org.eclipse.xtext.xbase.XAbstractFeatureCall)

Example 60 with JvmIdentifiableElement

use of org.eclipse.xtext.common.types.JvmIdentifiableElement 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

JvmIdentifiableElement (org.eclipse.xtext.common.types.JvmIdentifiableElement)112 Test (org.junit.Test)65 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)63 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)62 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)60 XBlockExpression (org.eclipse.xtext.xbase.XBlockExpression)58 XMemberFeatureCall (org.eclipse.xtext.xbase.XMemberFeatureCall)39 XFeatureCall (org.eclipse.xtext.xbase.XFeatureCall)27 EObject (org.eclipse.emf.ecore.EObject)26 XAbstractFeatureCall (org.eclipse.xtext.xbase.XAbstractFeatureCall)13 XExpression (org.eclipse.xtext.xbase.XExpression)12 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)11 IJavaElement (org.eclipse.jdt.core.IJavaElement)10 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)8 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)6 JvmType (org.eclipse.xtext.common.types.JvmType)6 XtextResource (org.eclipse.xtext.resource.XtextResource)6 XtendField (org.eclipse.xtend.core.xtend.XtendField)5 JvmExecutable (org.eclipse.xtext.common.types.JvmExecutable)5 JvmMember (org.eclipse.xtext.common.types.JvmMember)5