Search in sources :

Example 1 with IIdentifiableElementDescription

use of org.eclipse.xtext.xbase.scoping.batch.IIdentifiableElementDescription in project xtext-eclipse by eclipse.

the class JvmModelReferenceUpdater method getRefTextEvaluator.

/**
 * Preserves the syntax of method calls if the target is refactored.
 *
 * @since 2.4
 */
@Override
protected RefTextEvaluator getRefTextEvaluator(final EObject referringElement, URI referringResourceURI, final EReference reference, int indexInList, EObject newTargetElement) {
    final ReferenceSyntax oldReferenceSyntax = getReferenceSyntax(referringElement, reference, indexInList);
    if (oldReferenceSyntax == null)
        return super.getRefTextEvaluator(referringElement, referringResourceURI, reference, indexInList, newTargetElement);
    return new RefTextEvaluator() {

        @Override
        public boolean isValid(IEObjectDescription newTarget) {
            IScope scope = linkingScopeProvider.getScope(referringElement, reference);
            IEObjectDescription element = scope.getSingleElement(newTarget.getName());
            // TODO here we need to simulate linking with the new name instead of the old name
            if (element instanceof IIdentifiableElementDescription) {
                IIdentifiableElementDescription casted = (IIdentifiableElementDescription) element;
                if (!casted.isVisible() || !casted.isValidStaticState())
                    return false;
            }
            return element != null && element.getEObjectURI() != null && element.getEObjectURI().equals(newTarget.getEObjectURI());
        }

        @Override
        public boolean isBetterThan(String newText, String currentText) {
            ReferenceSyntax newSyntax = getReferenceSyntax(newText);
            ReferenceSyntax currentSyntax = getReferenceSyntax(currentText);
            // prefer the one with the same syntax as before
            if (newSyntax == oldReferenceSyntax && currentSyntax != oldReferenceSyntax)
                return true;
            else if (newSyntax != oldReferenceSyntax && currentSyntax == oldReferenceSyntax)
                return false;
            else
                // in doubt shorter is better
                return newText.length() < currentText.length();
        }
    };
}
Also used : RefTextEvaluator(org.eclipse.xtext.ui.refactoring.impl.RefactoringCrossReferenceSerializer.RefTextEvaluator) IIdentifiableElementDescription(org.eclipse.xtext.xbase.scoping.batch.IIdentifiableElementDescription) IScope(org.eclipse.xtext.scoping.IScope) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 2 with IIdentifiableElementDescription

use of org.eclipse.xtext.xbase.scoping.batch.IIdentifiableElementDescription in project xtext-eclipse by eclipse.

the class XbaseProposalProvider method getProposalBracketInfo.

protected ProposalBracketInfo getProposalBracketInfo(IEObjectDescription proposedDescription, ContentAssistContext contentAssistContext) {
    ProposalBracketInfo info = new ProposalBracketInfo();
    if (proposedDescription instanceof IIdentifiableElementDescription) {
        IIdentifiableElementDescription jvmFeatureDescription = (IIdentifiableElementDescription) proposedDescription;
        JvmIdentifiableElement jvmFeature = jvmFeatureDescription.getElementOrProxy();
        if (jvmFeature instanceof JvmExecutable) {
            List<JvmFormalParameter> parameters = ((JvmExecutable) jvmFeature).getParameters();
            if (jvmFeatureDescription.getNumberOfParameters() == 1) {
                if (jvmFeature.getSimpleName().startsWith("set") && !proposedDescription.getName().getFirstSegment().startsWith("set")) {
                    info.brackets = " = value";
                    info.selectionOffset = -"value".length();
                    info.selectionLength = "value".length();
                    return info;
                }
                JvmTypeReference parameterType = parameters.get(parameters.size() - 1).getParameterType();
                LightweightTypeReference light = getTypeConverter(contentAssistContext.getResource()).toLightweightReference(parameterType);
                if (light.isFunctionType()) {
                    int numParameters = light.getAsFunctionTypeReference().getParameterTypes().size();
                    if (numParameters == 1) {
                        info.brackets = "[]";
                        info.caretOffset = -1;
                        return info;
                    } else if (numParameters == 0) {
                        info.brackets = "[|]";
                        info.caretOffset = -1;
                        return info;
                    } else {
                        final StringBuilder b = new StringBuilder();
                        for (int i = 0; i < numParameters; ++i) {
                            if (i != 0) {
                                b.append(", ");
                            }
                            b.append("p" + (i + 1));
                        }
                        info.brackets = "[" + b.toString() + "|]";
                        info.caretOffset = -1;
                        info.selectionOffset = -b.length() - 2;
                        info.selectionLength = b.length();
                        return info;
                    }
                }
            }
        }
        if (isExplicitOperationCall(jvmFeatureDescription)) {
            info.brackets = "()";
            info.selectionOffset = -1;
        }
    }
    return info;
}
Also used : JvmExecutable(org.eclipse.xtext.common.types.JvmExecutable) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) IIdentifiableElementDescription(org.eclipse.xtext.xbase.scoping.batch.IIdentifiableElementDescription)

Example 3 with IIdentifiableElementDescription

use of org.eclipse.xtext.xbase.scoping.batch.IIdentifiableElementDescription in project xtext-eclipse by eclipse.

the class ParameterContextInformationProvider method getContextInformation.

@Override
public void getContextInformation(ContentAssistContext context, IContextInformationAcceptor acceptor) {
    XExpression containerCall = getContainerCall(eObjectAtOffsetHelper.resolveContainedElementAt(context.getResource(), context.getOffset()));
    LightweightTypeReferenceFactory factory = proposalProvider.getTypeConverter(context.getResource());
    if (containerCall != null) {
        ICompositeNode containerCallNode = NodeModelUtils.findActualNodeFor(containerCall);
        ITextRegion containerCallRegion = containerCallNode.getTextRegion();
        if (containerCallRegion.getOffset() > context.getOffset() || containerCallRegion.getOffset() + containerCallRegion.getLength() < context.getOffset())
            return;
        JvmIdentifiableElement calledFeature = getCalledFeature(containerCall);
        if (calledFeature instanceof JvmExecutable) {
            if (getParameterListOffset(containerCall) > context.getOffset())
                return;
            ParameterData parameterData = new ParameterData();
            IScope scope = getScope(containerCall);
            QualifiedName qualifiedName = QualifiedName.create(getCalledFeatureName(containerCall));
            boolean candidatesFound = false;
            for (IEObjectDescription element : scope.getElements(qualifiedName)) {
                if (element instanceof IIdentifiableElementDescription) {
                    IIdentifiableElementDescription featureDescription = (IIdentifiableElementDescription) element;
                    JvmIdentifiableElement featureCandidate = featureDescription.getElementOrProxy();
                    if (featureCandidate instanceof JvmExecutable) {
                        JvmExecutable executable = (JvmExecutable) featureCandidate;
                        if (!executable.getParameters().isEmpty()) {
                            StyledString styledString = new StyledString();
                            proposalProvider.appendParameters(styledString, executable, featureDescription.getNumberOfIrrelevantParameters(), factory);
                            parameterData.addOverloaded(styledString.toString(), executable.isVarArgs());
                            candidatesFound = true;
                        }
                    }
                }
            }
            if (candidatesFound) {
                StyledString displayString = proposalProvider.getStyledDisplayString((JvmExecutable) calledFeature, true, 0, qualifiedNameConverter.toString(qualifiedNameProvider.getFullyQualifiedName(calledFeature)), calledFeature.getSimpleName(), factory);
                ParameterContextInformation parameterContextInformation = new ParameterContextInformation(parameterData, displayString.toString(), getParameterListOffset(containerCall), context.getOffset());
                acceptor.accept(parameterContextInformation);
            }
        }
    }
}
Also used : JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) QualifiedName(org.eclipse.xtext.naming.QualifiedName) IIdentifiableElementDescription(org.eclipse.xtext.xbase.scoping.batch.IIdentifiableElementDescription) StyledString(org.eclipse.jface.viewers.StyledString) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) JvmExecutable(org.eclipse.xtext.common.types.JvmExecutable) LightweightTypeReferenceFactory(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReferenceFactory) ITextRegion(org.eclipse.xtext.util.ITextRegion) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) IScope(org.eclipse.xtext.scoping.IScope) XExpression(org.eclipse.xtext.xbase.XExpression)

Example 4 with IIdentifiableElementDescription

use of org.eclipse.xtext.xbase.scoping.batch.IIdentifiableElementDescription in project xtext-eclipse by eclipse.

the class XbaseContentProposalPriorities method adjustCrossReferencePriority.

@Override
public void adjustCrossReferencePriority(final ICompletionProposal proposal, final String prefix) {
    if ((proposal instanceof ConfigurableCompletionProposal)) {
        Object _additionalData = ((ConfigurableCompletionProposal) proposal).getAdditionalData(XbaseProposalProvider.DESCRIPTION_KEY);
        final Object desc = _additionalData;
        boolean _matched = false;
        if (desc instanceof SimpleIdentifiableElementDescription) {
            if (((!Objects.equal(((ConfigurableCompletionProposal) proposal).getReplacementString(), "this")) && (!Objects.equal(((ConfigurableCompletionProposal) proposal).getReplacementString(), "super")))) {
                _matched = true;
                this.adjustPriority(proposal, prefix, 570);
                return;
            }
        }
        if (!_matched) {
            if (desc instanceof StaticFeatureDescriptionWithTypeLiteralReceiver) {
                _matched = true;
                this.adjustPriority(proposal, prefix, 560);
            }
        }
        if (!_matched) {
            if (desc instanceof IIdentifiableElementDescription) {
                _matched = true;
                JvmIdentifiableElement _elementOrProxy = ((IIdentifiableElementDescription) desc).getElementOrProxy();
                boolean _matched_1 = false;
                if (_elementOrProxy instanceof JvmField) {
                    _matched_1 = true;
                    this.adjustPriority(proposal, prefix, 550);
                    return;
                }
                if (!_matched_1) {
                    if (_elementOrProxy instanceof JvmExecutable) {
                        _matched_1 = true;
                        this.adjustPriority(proposal, prefix, 520);
                        return;
                    }
                }
            }
        }
    }
    super.adjustCrossReferencePriority(proposal, prefix);
}
Also used : SimpleIdentifiableElementDescription(org.eclipse.xtext.xbase.scoping.batch.SimpleIdentifiableElementDescription) ConfigurableCompletionProposal(org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal) JvmExecutable(org.eclipse.xtext.common.types.JvmExecutable) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) IIdentifiableElementDescription(org.eclipse.xtext.xbase.scoping.batch.IIdentifiableElementDescription) JvmField(org.eclipse.xtext.common.types.JvmField) StaticFeatureDescriptionWithTypeLiteralReceiver(org.eclipse.xtext.xbase.scoping.batch.StaticFeatureDescriptionWithTypeLiteralReceiver)

Aggregations

IIdentifiableElementDescription (org.eclipse.xtext.xbase.scoping.batch.IIdentifiableElementDescription)4 JvmExecutable (org.eclipse.xtext.common.types.JvmExecutable)3 JvmIdentifiableElement (org.eclipse.xtext.common.types.JvmIdentifiableElement)3 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)2 IScope (org.eclipse.xtext.scoping.IScope)2 StyledString (org.eclipse.jface.viewers.StyledString)1 JvmField (org.eclipse.xtext.common.types.JvmField)1 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)1 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)1 QualifiedName (org.eclipse.xtext.naming.QualifiedName)1 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)1 ConfigurableCompletionProposal (org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal)1 RefTextEvaluator (org.eclipse.xtext.ui.refactoring.impl.RefactoringCrossReferenceSerializer.RefTextEvaluator)1 ITextRegion (org.eclipse.xtext.util.ITextRegion)1 XExpression (org.eclipse.xtext.xbase.XExpression)1 SimpleIdentifiableElementDescription (org.eclipse.xtext.xbase.scoping.batch.SimpleIdentifiableElementDescription)1 StaticFeatureDescriptionWithTypeLiteralReceiver (org.eclipse.xtext.xbase.scoping.batch.StaticFeatureDescriptionWithTypeLiteralReceiver)1 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)1 LightweightTypeReferenceFactory (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReferenceFactory)1