Search in sources :

Example 36 with IEObjectDescription

use of org.eclipse.xtext.resource.IEObjectDescription in project xtext-eclipse by eclipse.

the class AbstractTypeScopeTest method testGetElementByInstance_01.

@Test
public void testGetElementByInstance_01() {
    JvmVoid voidType = TypesFactory.eINSTANCE.createJvmVoid();
    IEObjectDescription element = getTypeScope().getSingleElement(voidType);
    assertNotNull(element);
    assertEquals(voidType.getIdentifier(), element.getName().toString());
}
Also used : JvmVoid(org.eclipse.xtext.common.types.JvmVoid) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) Test(org.junit.Test)

Example 37 with IEObjectDescription

use of org.eclipse.xtext.resource.IEObjectDescription in project xtext-eclipse by eclipse.

the class EObjectDescriptionBasedStubGeneratorTest method testNested.

@Test
public void testNested() {
    final EObjectDescription _top = new EObjectDescription(QualifiedName.create("foo", "Bar"), TypesFactory.eINSTANCE.createJvmGenericType(), Collections.<String, String>emptyMap());
    final EObjectDescription _nested0 = new EObjectDescription(QualifiedName.create("foo", "Bar$Baz0"), TypesFactory.eINSTANCE.createJvmGenericType(), Collections.singletonMap(JvmTypesResourceDescriptionStrategy.IS_NESTED_TYPE, Boolean.TRUE.toString()));
    final EObjectDescription _nested1 = new EObjectDescription(QualifiedName.create("foo", "Bar$Baz1"), TypesFactory.eINSTANCE.createJvmGenericType(), Collections.singletonMap(JvmTypesResourceDescriptionStrategy.IS_NESTED_TYPE, Boolean.TRUE.toString()));
    final EObjectDescription _nested10 = new EObjectDescription(QualifiedName.create("foo", "Bar$Baz1$FooBar0"), TypesFactory.eINSTANCE.createJvmGenericType(), Collections.singletonMap(JvmTypesResourceDescriptionStrategy.IS_NESTED_TYPE, Boolean.TRUE.toString()));
    final EObjectDescription _nested11 = new EObjectDescription(QualifiedName.create("foo", "Bar$Baz1$FooBar0"), TypesFactory.eINSTANCE.createJvmGenericType(), Collections.singletonMap(JvmTypesResourceDescriptionStrategy.IS_NESTED_TYPE, Boolean.TRUE.toString()));
    IResourceDescription resourceDescription = new AbstractResourceDescription() {

        @Override
        public URI getURI() {
            return null;
        }

        @Override
        public Iterable<IReferenceDescription> getReferenceDescriptions() {
            return Collections.emptyList();
        }

        @Override
        public Iterable<QualifiedName> getImportedNames() {
            return Collections.emptyList();
        }

        @Override
        protected List<IEObjectDescription> computeExportedObjects() {
            return Arrays.asList(new IEObjectDescription[] { _top, _nested0, _nested1, _nested10, _nested11 });
        }
    };
    assertEquals("package foo;\n" + "public class Bar{\n" + "public static class Baz0{\n" + "}\n" + "public static class Baz1{\n" + "public static class FooBar0{\n" + "}\n" + "public static class FooBar0{\n" + "}\n" + "}\n" + "}", gen.getJavaStubSource(_top, resourceDescription));
}
Also used : EObjectDescription(org.eclipse.xtext.resource.EObjectDescription) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) AbstractResourceDescription(org.eclipse.xtext.resource.impl.AbstractResourceDescription) QualifiedName(org.eclipse.xtext.naming.QualifiedName) IReferenceDescription(org.eclipse.xtext.resource.IReferenceDescription) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) Test(org.junit.Test)

Example 38 with IEObjectDescription

use of org.eclipse.xtext.resource.IEObjectDescription 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 39 with IEObjectDescription

use of org.eclipse.xtext.resource.IEObjectDescription in project xtext-eclipse by eclipse.

the class DefaultReferenceFinder method createExportedElementsMap.

@Deprecated
protected Map<EObject, URI> createExportedElementsMap(final Resource resource) {
    return new ForwardingMap<EObject, URI>() {

        private Map<EObject, URI> delegate;

        @Override
        protected Map<EObject, URI> delegate() {
            if (delegate != null) {
                return delegate;
            }
            URI uri = EcoreUtil2.getPlatformResourceOrNormalizedURI(resource);
            IResourceServiceProvider resourceServiceProvider = getServiceProviderRegistry().getResourceServiceProvider(uri);
            if (resourceServiceProvider == null) {
                return delegate = Collections.emptyMap();
            }
            IResourceDescription.Manager resourceDescriptionManager = resourceServiceProvider.getResourceDescriptionManager();
            if (resourceDescriptionManager == null) {
                return delegate = Collections.emptyMap();
            }
            IResourceDescription resourceDescription = resourceDescriptionManager.getResourceDescription(resource);
            Map<EObject, URI> exportedElementMap = newIdentityHashMap();
            if (resourceDescription != null) {
                for (IEObjectDescription exportedEObjectDescription : resourceDescription.getExportedObjects()) {
                    EObject eObject = resource.getEObject(exportedEObjectDescription.getEObjectURI().fragment());
                    if (eObject != null)
                        exportedElementMap.put(eObject, exportedEObjectDescription.getEObjectURI());
                }
            }
            return delegate = exportedElementMap;
        }
    };
}
Also used : IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) EObject(org.eclipse.emf.ecore.EObject) ForwardingMap(com.google.common.collect.ForwardingMap) Map(java.util.Map) ForwardingMap(com.google.common.collect.ForwardingMap) URI(org.eclipse.emf.common.util.URI) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 40 with IEObjectDescription

use of org.eclipse.xtext.resource.IEObjectDescription 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)

Aggregations

IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)324 Test (org.junit.Test)95 EObject (org.eclipse.emf.ecore.EObject)82 QualifiedName (org.eclipse.xtext.naming.QualifiedName)79 IScope (org.eclipse.xtext.scoping.IScope)56 URI (org.eclipse.emf.common.util.URI)41 IResourceDescription (org.eclipse.xtext.resource.IResourceDescription)33 EClass (org.eclipse.emf.ecore.EClass)30 Resource (org.eclipse.emf.ecore.resource.Resource)28 EObjectDescription (org.eclipse.xtext.resource.EObjectDescription)22 AliasedEObjectDescription (org.eclipse.xtext.resource.impl.AliasedEObjectDescription)22 ArrayList (java.util.ArrayList)19 IResourceDescriptions (org.eclipse.xtext.resource.IResourceDescriptions)14 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)12 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)12 StyledString (org.eclipse.jface.viewers.StyledString)12 XtextResource (org.eclipse.xtext.resource.XtextResource)11 StringInputStream (org.eclipse.xtext.util.StringInputStream)11 EReference (org.eclipse.emf.ecore.EReference)10 List (java.util.List)9