Search in sources :

Example 1 with IScope

use of org.eclipse.xtext.scoping.IScope in project xtext-eclipse by eclipse.

the class ArithmeticsScopeProvider method getScope.

@Override
public IScope getScope(final EObject context, final EReference reference) {
    if ((reference == ArithmeticsPackage.Literals.IMPORT__MODULE)) {
        return super.getGlobalScope(context.eResource(), reference);
    }
    final org.eclipse.xtext.example.arithmetics.arithmetics.Module module = EcoreUtil2.<org.eclipse.xtext.example.arithmetics.arithmetics.Module>getContainerOfType(context, org.eclipse.xtext.example.arithmetics.arithmetics.Module.class);
    IScope result = IScope.NULLSCOPE;
    EList<Import> _imports = module.getImports();
    for (final Import import_ : _imports) {
        boolean _eIsProxy = import_.getModule().eIsProxy();
        boolean _not = (!_eIsProxy);
        if (_not) {
            result = this.getModuleScope(context, reference, import_.getModule(), result);
        }
    }
    result = this.getModuleScope(context, reference, module, result);
    return this.getDefinitionScope(context, reference, result);
}
Also used : Import(org.eclipse.xtext.example.arithmetics.arithmetics.Import) IScope(org.eclipse.xtext.scoping.IScope)

Example 2 with IScope

use of org.eclipse.xtext.scoping.IScope in project xtext-eclipse by eclipse.

the class XbaseProposalProvider method createLocalVariableAndImplicitProposals.

protected void createLocalVariableAndImplicitProposals(EObject context, IExpressionScope.Anchor anchor, ContentAssistContext contentAssistContext, ICompletionProposalAcceptor acceptor) {
    String prefix = contentAssistContext.getPrefix();
    if (prefix.length() > 0) {
        if (!Character.isJavaIdentifierStart(prefix.charAt(0))) {
            if (prefix.length() > 1) {
                if (prefix.charAt(0) == '^' && !Character.isJavaIdentifierStart(prefix.charAt(1))) {
                    return;
                }
            }
        }
    }
    // long time = System.currentTimeMillis();
    Function<IEObjectDescription, ICompletionProposal> proposalFactory = getProposalFactory(getFeatureCallRuleName(), contentAssistContext);
    IResolvedTypes resolvedTypes = context != null ? typeResolver.resolveTypes(context) : typeResolver.resolveTypes(contentAssistContext.getResource());
    IExpressionScope expressionScope = resolvedTypes.getExpressionScope(context, anchor);
    // TODO use the type name information
    IScope scope = expressionScope.getFeatureScope();
    getCrossReferenceProposalCreator().lookupCrossReference(scope, context, XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE, acceptor, getFeatureDescriptionPredicate(contentAssistContext), proposalFactory);
    // System.out.printf("XbaseProposalProvider.createLocalVariableAndImplicitProposals = %d\n", System.currentTimeMillis() - time);
    // time = System.currentTimeMillis();
    // TODO use the type name information
    proposeDeclaringTypeForStaticInvocation(context, null, /* ignore */
    contentAssistContext, acceptor);
// System.out.printf("XbaseProposalProvider.proposeDeclaringTypeForStaticInvocation = %d\n", System.currentTimeMillis() - time);
}
Also used : IResolvedTypes(org.eclipse.xtext.xbase.typesystem.IResolvedTypes) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) IExpressionScope(org.eclipse.xtext.xbase.typesystem.IExpressionScope) IScope(org.eclipse.xtext.scoping.IScope) StyledString(org.eclipse.jface.viewers.StyledString) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 3 with IScope

use of org.eclipse.xtext.scoping.IScope in project xtext-eclipse by eclipse.

the class CrossReferenceTemplateVariableResolver method resolveValues.

@Override
public List<String> resolveValues(TemplateVariable variable, XtextTemplateContext castedContext) {
    String abbreviatedCrossReference = (String) variable.getVariableType().getParams().iterator().next();
    int dotIndex = abbreviatedCrossReference.lastIndexOf('.');
    if (dotIndex <= 0) {
        // $NON-NLS-1$ //$NON-NLS-2$
        log.error("CrossReference '" + abbreviatedCrossReference + "' could not be resolved.");
        return Collections.emptyList();
    }
    String[] classReferencePair = new String[] { abbreviatedCrossReference.substring(0, dotIndex), abbreviatedCrossReference.substring(dotIndex + 1) };
    Grammar grammar = getGrammar(castedContext);
    if (grammar == null) {
        return Collections.emptyList();
    }
    EReference reference = getReference(classReferencePair[0], classReferencePair[1], grammar);
    if (reference == null) {
        log.debug(// $NON-NLS-1$ //$NON-NLS-2$
        "CrossReference to class '" + classReferencePair[0] + "' and reference '" + classReferencePair[1] + // $NON-NLS-1$
        "' could not be resolved.");
        return Collections.emptyList();
    }
    IScope scope = null;
    EObject currentModel = castedContext.getContentAssistContext().getCurrentModel();
    if (currentModel == null) {
        scope = globalScopeProvider.getScope(castedContext.getContentAssistContext().getResource(), reference, null);
    } else {
        scope = castedContext.getScopeProvider().getScope(currentModel, reference);
    }
    Iterable<IEObjectDescription> linkingCandidates = queryScope(scope);
    List<String> names = new ArrayList<String>();
    for (IEObjectDescription eObjectDescription : linkingCandidates) {
        names.add(qualifiedNameConverter.toString(eObjectDescription.getName()));
    }
    return names;
}
Also used : EObject(org.eclipse.emf.ecore.EObject) ArrayList(java.util.ArrayList) IScope(org.eclipse.xtext.scoping.IScope) Grammar(org.eclipse.xtext.Grammar) EReference(org.eclipse.emf.ecore.EReference) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 4 with IScope

use of org.eclipse.xtext.scoping.IScope in project xtext-eclipse by eclipse.

the class DefaultQuickfixProvider method createLinkingIssueResolutions.

public void createLinkingIssueResolutions(final Issue issue, final IssueResolutionAcceptor issueResolutionAcceptor) {
    final IModificationContext modificationContext = modificationContextFactory.createModificationContext(issue);
    final IXtextDocument xtextDocument = modificationContext.getXtextDocument();
    if (xtextDocument == null)
        return;
    xtextDocument.readOnly(new CancelableUnitOfWork<Void, XtextResource>() {

        IssueResolutionAcceptor myAcceptor = null;

        @Override
        public java.lang.Void exec(XtextResource state, CancelIndicator cancelIndicator) throws Exception {
            myAcceptor = getCancelableAcceptor(issueResolutionAcceptor, cancelIndicator);
            EObject target = state.getEObject(issue.getUriToProblem().fragment());
            EReference reference = getUnresolvedEReference(issue, target);
            if (reference == null)
                return null;
            fixUnresolvedReference(issue, xtextDocument, target, reference);
            return null;
        }

        protected void fixUnresolvedReference(final Issue issue, final IXtextDocument xtextDocument, EObject target, EReference reference) throws BadLocationException {
            boolean caseInsensitive = caseInsensitivityHelper.isIgnoreCase(reference);
            EObject crossReferenceTerminal = getCrossReference(issue, target);
            String ruleName = null;
            Keyword keyword = null;
            if (crossReferenceTerminal instanceof RuleCall) {
                RuleCall ruleCall = (RuleCall) crossReferenceTerminal;
                ruleName = ruleCall.getRule().getName();
            } else if (crossReferenceTerminal instanceof Keyword) {
                keyword = (Keyword) crossReferenceTerminal;
            }
            String issueString = xtextDocument.get(issue.getOffset(), issue.getLength());
            IScope scope = scopeProvider.getScope(target, reference);
            List<IEObjectDescription> discardedDescriptions = Lists.newArrayList();
            Set<String> qualifiedNames = Sets.newHashSet();
            int addedDescriptions = 0;
            int checkedDescriptions = 0;
            for (IEObjectDescription referableElement : queryScope(scope)) {
                String referableElementQualifiedName = qualifiedNameConverter.toString(referableElement.getQualifiedName());
                if (similarityMatcher.isSimilar(issueString, qualifiedNameConverter.toString(referableElement.getName()))) {
                    addedDescriptions++;
                    createResolution(issueString, referableElement, ruleName, keyword, caseInsensitive);
                    qualifiedNames.add(referableElementQualifiedName);
                } else {
                    if (qualifiedNames.add(referableElementQualifiedName))
                        discardedDescriptions.add(referableElement);
                }
                checkedDescriptions++;
                if (checkedDescriptions > 100)
                    break;
            }
            if (discardedDescriptions.size() + addedDescriptions <= 5) {
                for (IEObjectDescription referableElement : discardedDescriptions) {
                    createResolution(issueString, referableElement, ruleName, keyword, caseInsensitive);
                }
            }
        }

        protected AbstractElement getCrossReference(final Issue issue, EObject target) {
            final ICompositeNode node = NodeModelUtils.getNode(target);
            if (node == null)
                throw new IllegalStateException("Cannot happen since we found a reference");
            ICompositeNode rootNode = node.getRootNode();
            ILeafNode leaf = NodeModelUtils.findLeafNodeAtOffset(rootNode, issue.getOffset());
            CrossReference crossReference = findCrossReference(target, leaf);
            return crossReference.getTerminal();
        }

        public void createResolution(String issueString, IEObjectDescription solution, String ruleName, Keyword keyword, boolean caseInsensitive) {
            String replacement = qualifiedNameConverter.toString(solution.getName());
            String replaceLabel = fixCrossReferenceLabel(issueString, replacement);
            if (keyword != null) {
                if (caseInsensitive && !replacement.equalsIgnoreCase(keyword.getValue()))
                    return;
                if (!caseInsensitive && !replacement.equals(keyword.getValue()))
                    return;
            } else if (ruleName != null) {
                replacement = converter.convertToString(replacement, ruleName);
                if (replacement == null) {
                    return;
                }
            } else {
                logger.error("either keyword or ruleName have to present", new IllegalStateException());
            }
            myAcceptor.accept(issue, replaceLabel, replaceLabel, fixCrossReferenceImage(issueString, replacement), new ReplaceModification(issue, replacement));
        }
    });
}
Also used : Issue(org.eclipse.xtext.validation.Issue) Set(java.util.Set) XtextResource(org.eclipse.xtext.resource.XtextResource) RuleCall(org.eclipse.xtext.RuleCall) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) EObject(org.eclipse.emf.ecore.EObject) IScope(org.eclipse.xtext.scoping.IScope) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) ArrayList(java.util.ArrayList) List(java.util.List) EReference(org.eclipse.emf.ecore.EReference) Keyword(org.eclipse.xtext.Keyword) AbstractElement(org.eclipse.xtext.AbstractElement) BadLocationException(org.eclipse.jface.text.BadLocationException) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) CrossReference(org.eclipse.xtext.CrossReference) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) BadLocationException(org.eclipse.jface.text.BadLocationException) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 5 with IScope

use of org.eclipse.xtext.scoping.IScope in project xtext-eclipse by eclipse.

the class XbaseHoverDocumentationProvider method getResolvedDeclarator.

protected JvmDeclaredType getResolvedDeclarator(String name) {
    JvmDeclaredType jvmDeclaredType = null;
    if (Strings.isEmpty(name) || name.trim().length() == 0) {
        jvmDeclaredType = getDeclaringType(context);
    } else {
        HoverReference reference = new HoverReference(TypesPackage.Literals.JVM_TYPE);
        IScope scope = scopeProvider.getScope(context, reference);
        IEObjectDescription declarator = scope.getSingleElement(qualifiedNameConverter.toQualifiedName(name));
        if (declarator != null && EcoreUtil2.isAssignableFrom(TypesPackage.eINSTANCE.getJvmGenericType(), declarator.getEClass())) {
            jvmDeclaredType = (JvmDeclaredType) context.eResource().getResourceSet().getEObject(declarator.getEObjectURI(), true);
        }
    }
    return jvmDeclaredType;
}
Also used : JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) IScope(org.eclipse.xtext.scoping.IScope) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Aggregations

IScope (org.eclipse.xtext.scoping.IScope)144 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)53 Test (org.junit.Test)53 EObject (org.eclipse.emf.ecore.EObject)41 QualifiedName (org.eclipse.xtext.naming.QualifiedName)21 XtextResource (org.eclipse.xtext.resource.XtextResource)18 EReference (org.eclipse.emf.ecore.EReference)15 StringInputStream (org.eclipse.xtext.util.StringInputStream)15 EClass (org.eclipse.emf.ecore.EClass)14 URI (org.eclipse.emf.common.util.URI)11 Entity (org.eclipse.xtext.index.indexTestLanguage.Entity)10 SimpleScope (org.eclipse.xtext.scoping.impl.SimpleScope)9 AbstractScopingTest (com.avaloq.tools.ddk.xtext.test.scoping.AbstractScopingTest)8 ArrayList (java.util.ArrayList)8 Resource (org.eclipse.emf.ecore.resource.Resource)7 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)7 FilteringScope (org.eclipse.xtext.scoping.impl.FilteringScope)7 EClassifier (org.eclipse.emf.ecore.EClassifier)6 ExportModel (com.avaloq.tools.ddk.xtext.export.export.ExportModel)5 Predicate (com.google.common.base.Predicate)5