Search in sources :

Example 21 with IResourceDescriptions

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

the class AbstractScopeResourceDescriptionsTest method assertLiveModelScopeLocal.

protected void assertLiveModelScopeLocal(boolean enabled) throws IOException {
    final URI resourceURI = URI.createPlatformResourceURI("test/assertLiveModelScopeLocal." + fileExt.getPrimaryFileExtension(), true);
    Resource resource = resourceSet.createResource(resourceURI);
    resource.load(new StringInputStream("stuff foo"), null);
    Stuff stuffFoo = ((File) resource.getContents().get(0)).getStuff().get(0);
    if (enabled) {
        assertExportedObject(resource, "foo");
        stuffFoo.setName("bar");
        assertExportedObject(resource, "bar");
    } else {
        IResourceDescriptions resourceDescriptions = resourceDescriptionsProvider.getResourceDescriptions(resource);
        IResourceDescription resourceDescription = resourceDescriptions.getResourceDescription(resource.getURI());
        assertNull(resourceDescription);
    }
}
Also used : StringInputStream(org.eclipse.xtext.util.StringInputStream) IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) IDirtyResource(org.eclipse.xtext.ui.editor.IDirtyResource) Stuff(org.eclipse.xtext.ui.tests.foo.Stuff) URI(org.eclipse.emf.common.util.URI)

Example 22 with IResourceDescriptions

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

the class OpenAssociationHierarchyHandler method createHierarchyBuilder.

@Override
protected IHierarchyBuilder createHierarchyBuilder(final EObject target) {
    final AssociationHierarchyBuilder xtextCallHierarchyBuilder = this._iGlobalServiceProvider.<AssociationHierarchyBuilder>findService(target, AssociationHierarchyBuilder.class);
    xtextCallHierarchyBuilder.setResourceAccess(this.resourceAccess);
    xtextCallHierarchyBuilder.setIndexData(this._iGlobalServiceProvider.<IResourceDescriptions>findService(target, IResourceDescriptions.class));
    final DeferredHierarchyBuilder deferredHierarchyBuilder = this._iGlobalServiceProvider.<DeferredHierarchyBuilder>findService(target, DeferredHierarchyBuilder.class);
    deferredHierarchyBuilder.setHierarchyBuilder(xtextCallHierarchyBuilder);
    return deferredHierarchyBuilder;
}
Also used : IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) AssociationHierarchyBuilder(org.eclipse.xtext.example.domainmodel.ui.editor.hierarchy.AssociationHierarchyBuilder) DeferredHierarchyBuilder(org.eclipse.xtext.ui.editor.hierarchy.DeferredHierarchyBuilder)

Example 23 with IResourceDescriptions

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

the class LiveShadowedAllContainerStateTest method formatContainers.

private String formatContainers(final ResourceSet rs) {
    String _xblockexpression = null;
    {
        final IResourceDescriptions resourceDescriptions = this.resourceDescriptionProvider.getResourceDescriptions(rs);
        final IAllContainersState containerState = this.containerStateProvider.get(resourceDescriptions);
        final Function1<Resource, URI> _function = (Resource it) -> {
            return it.getURI();
        };
        final Set<URI> allURIs = IterableExtensions.<URI>toSet(ListExtensions.<Resource, URI>map(rs.getResources(), _function));
        final Function1<URI, String> _function_1 = (URI it) -> {
            return containerState.getContainerHandle(it);
        };
        final Set<String> allContainers = IterableExtensions.<String>toSet(IterableExtensions.<String>filterNull(IterableExtensions.<URI, String>map(allURIs, _function_1)));
        final Function1<IProject, String> _function_2 = (IProject it) -> {
            return it.getName();
        };
        List<String> _map = ListExtensions.<IProject, String>map(((List<IProject>) Conversions.doWrapArray(ResourcesPlugin.getWorkspace().getRoot().getProjects())), _function_2);
        Iterables.<String>addAll(allContainers, _map);
        StringConcatenation _builder = new StringConcatenation();
        {
            for (final String container : allContainers) {
                _builder.append("container ");
                _builder.append(container);
                _builder.append(" isEmpty=");
                boolean _isEmpty = containerState.isEmpty(container);
                _builder.append(_isEmpty);
                _builder.append(" {");
                _builder.newLineIfNotEmpty();
                {
                    final Function1<URI, String> _function_3 = (URI it) -> {
                        return it.toString();
                    };
                    List<URI> _sortBy = IterableExtensions.<URI, String>sortBy(containerState.getContainedURIs(container), _function_3);
                    for (final URI uri : _sortBy) {
                        _builder.append("\t");
                        _builder.append("resourceURI=");
                        _builder.append(uri, "\t");
                        _builder.append(" exported=[");
                        final Function1<IEObjectDescription, QualifiedName> _function_4 = (IEObjectDescription it) -> {
                            return it.getName();
                        };
                        String _join = IterableExtensions.join(IterableExtensions.<IEObjectDescription, QualifiedName>map(resourceDescriptions.getResourceDescription(uri).getExportedObjects(), _function_4), ", ");
                        _builder.append(_join, "\t");
                        _builder.append("]");
                        _builder.newLineIfNotEmpty();
                    }
                }
                _builder.append("}");
                _builder.newLine();
            }
        }
        _xblockexpression = _builder.toString();
    }
    return _xblockexpression;
}
Also used : IAllContainersState(org.eclipse.xtext.resource.containers.IAllContainersState) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) Set(java.util.Set) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) QualifiedName(org.eclipse.xtext.naming.QualifiedName) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) Resource(org.eclipse.emf.ecore.resource.Resource) URI(org.eclipse.emf.common.util.URI) IProject(org.eclipse.core.resources.IProject) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) List(java.util.List)

Example 24 with IResourceDescriptions

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

the class ValidationJobScheduler method doScheduleInitialValidation.

protected void doScheduleInitialValidation(XtextDocument document) {
    URI uri = document.getResourceURI();
    if (uri == null)
        return;
    IResourceDescription documentDescription = resourceDescriptions.getResourceDescription(uri);
    if (documentDescription == null) {
        // resource was just created - build is likely to be running in background
        return;
    }
    if (dirtyStateManager instanceof IDirtyStateManagerExtension && builderStateProvider != null) {
        IResourceDescriptions persistedDescriptions = builderStateProvider.get();
        List<URI> dirtyResourceURIs = ((IDirtyStateManagerExtension) dirtyStateManager).getDirtyResourceURIs();
        for (URI dirtyResourceURI : dirtyResourceURIs) {
            IResourceDescription dirtyDescription = dirtyStateManager.getDirtyResourceDescription(dirtyResourceURI);
            if (dirtyDescription != null) {
                IResourceDescription persistedDescription = persistedDescriptions.getResourceDescription(dirtyResourceURI);
                // Shortcut to make sure we don't waste time with more involving haveEObjectDescriptionChanged computation
                ChangedResourceDescriptionDelta delta = new ChangedResourceDescriptionDelta(persistedDescription, dirtyDescription);
                if (resourceDescriptionManager.isAffected(delta, documentDescription)) {
                    document.checkAndUpdateAnnotations();
                    return;
                }
            }
        }
    } else {
        Set<URI> outgoingReferences = descriptionUtils.collectOutgoingReferences(documentDescription);
        for (URI outgoing : outgoingReferences) {
            if (isDirty(outgoing)) {
                document.checkAndUpdateAnnotations();
                return;
            }
        }
    }
}
Also used : IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) ChangedResourceDescriptionDelta(org.eclipse.xtext.resource.impl.ChangedResourceDescriptionDelta) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) URI(org.eclipse.emf.common.util.URI)

Example 25 with IResourceDescriptions

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

the class XtextGrammarQuickfixProvider method fixExternalImportedPackage.

@Fix(INVALID_PACKAGE_REFERENCE_EXTERNAL)
public void fixExternalImportedPackage(final Issue issue, IssueResolutionAcceptor acceptor) {
    if (issue.getData().length == 1)
        acceptor.accept(issue, "Update the imported package '" + issue.getData()[0] + "'", "Fix the bogus package import\n" + "import '" + issue.getData()[0] + "'", NULL_QUICKFIX_IMAGE, new IModification() {

            @Override
            public void apply(IModificationContext context) throws BadLocationException {
                String replaceString = valueConverterService.toString(issue.getData()[0], "STRING");
                IXtextDocument document = context.getXtextDocument();
                final List<String> importedPackages = document.priorityReadOnly(new IUnitOfWork<List<String>, XtextResource>() {

                    @Override
                    public List<String> exec(XtextResource state) throws Exception {
                        IResourceDescriptions descriptions = resourceDescriptionsProvider.getResourceDescriptions(state);
                        ResourceSet resourceSet = state.getResourceSet();
                        final Map<URI, URI> uriMap = Maps.newHashMap();
                        EPackage ePackage = loadPackageFromIndex(descriptions, resourceSet, uriMap, issue.getData()[0]);
                        if (ePackage != null) {
                            final Map<String, EPackage> packagePerNsURI = Maps.newHashMap();
                            packagePerNsURI.put(ePackage.getNsURI(), ePackage);
                            final Set<URI> updatedReferences = fixReferencesInPackages(ePackage, packagePerNsURI, uriMap, descriptions, resourceSet);
                            if (updatedReferences.isEmpty())
                                return null;
                            Iterator<EPackage> iterator = packagePerNsURI.values().iterator();
                            while (iterator.hasNext()) {
                                EPackage pack = iterator.next();
                                Resource resource = pack.eResource();
                                if (!resource.getURI().isPlatformResource()) {
                                    iterator.remove();
                                }
                            }
                            final List<String> result = Lists.newArrayList();
                            new WorkspaceModifyOperation() {

                                /* workspace lock */
                                @Override
                                protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {
                                    try {
                                        for (EPackage pack : packagePerNsURI.values()) {
                                            Resource resource = pack.eResource();
                                            resource.save(Collections.singletonMap(XMLResource.OPTION_URI_HANDLER, new URIHandlerImpl.PlatformSchemeAware() {

                                                @Override
                                                public URI deresolve(URI uri) {
                                                    // replace archive uris with platform:/plugin
                                                    if (!uri.isArchive() || !updatedReferences.contains(uri)) {
                                                        return super.deresolve(uri);
                                                    }
                                                    URI withoutFragment = uri.trimFragment();
                                                    if (uriMap.containsKey(withoutFragment)) {
                                                        withoutFragment = uriMap.get(withoutFragment);
                                                    }
                                                    return super.deresolve(withoutFragment.appendFragment(uri.fragment()));
                                                }
                                            }));
                                            result.add(resource.getURI().toString());
                                        }
                                    } catch (IOException ioe) {
                                        throw new InvocationTargetException(ioe);
                                    }
                                }
                            }.run(new NullProgressMonitor());
                            for (int i = resourceSet.getResources().size() - 1; i >= 0; i--) {
                                Resource resource = resourceSet.getResources().get(i);
                                if (!resource.getContents().isEmpty() && resource.getContents().get(0) instanceof GenModel) {
                                    resourceSet.getResources().remove(i);
                                }
                            }
                            return result;
                        }
                        return null;
                    }

                    private Set<URI> fixReferencesInPackages(EPackage ePackage, Map<String, EPackage> packagePerNsURI, Map<URI, URI> uriMap, IResourceDescriptions descriptions, ResourceSet resourceSet) {
                        Set<URI> result = Sets.newHashSet();
                        Map<EObject, Collection<Setting>> allReferences = EcoreUtil.CrossReferencer.find(Collections.singletonList(ePackage));
                        for (final Setting setting : Iterables.concat(allReferences.values())) {
                            if (setting.getEStructuralFeature().isChangeable()) {
                                final Object referenced = setting.get(true);
                                List<Object> references = null;
                                if (referenced instanceof EObject) {
                                    references = new AbstractList<Object>() {

                                        @Override
                                        public Object set(int index, Object element) {
                                            setting.set(element);
                                            return referenced;
                                        }

                                        @Override
                                        public Object get(int index) {
                                            return referenced;
                                        }

                                        @Override
                                        public int size() {
                                            return 1;
                                        }
                                    };
                                } else {
                                    @SuppressWarnings("unchecked") List<Object> casted = (List<Object>) referenced;
                                    references = casted;
                                }
                                for (int i = 0; i < references.size(); i++) {
                                    if (references.get(i) instanceof EObject) {
                                        EObject referencedEObject = (EObject) references.get(i);
                                        EPackage transitive = EcoreUtil2.getContainerOfType(referencedEObject, EPackage.class);
                                        if (isRegisteredPackage(transitive)) {
                                            if (referencedEObject instanceof EDataType)
                                                continue;
                                            if (referencedEObject == EcorePackage.Literals.EOBJECT)
                                                continue;
                                            EPackage fromWorkspace = packagePerNsURI.get(transitive.getNsURI());
                                            if (fromWorkspace == null && !packagePerNsURI.containsKey(transitive.getNsURI())) {
                                                fromWorkspace = loadPackageFromIndex(descriptions, resourceSet, uriMap, transitive.getNsURI());
                                                packagePerNsURI.put(transitive.getNsURI(), fromWorkspace);
                                            }
                                            if (fromWorkspace != null) {
                                                String fragment = transitive.eResource().getURIFragment(referencedEObject);
                                                EObject replacement = fromWorkspace.eResource().getEObject(fragment);
                                                if (replacement != null) {
                                                    result.add(EcoreUtil.getURI(replacement));
                                                    references.set(i, replacement);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        return result;
                    }

                    private boolean isRegisteredPackage(EPackage ePackage) {
                        return ePackage != null && (ePackage.eResource() == null || ePackage.getNsURI().equals(ePackage.eResource().getURI().toString()));
                    }

                    private EPackage loadPackageFromIndex(IResourceDescriptions descriptions, ResourceSet resourceSet, Map<URI, URI> uriMap, String nsURI) {
                        Iterable<IEObjectDescription> fixUs = descriptions.getExportedObjects(EcorePackage.Literals.EPACKAGE, QualifiedName.create(nsURI), false);
                        for (IEObjectDescription description : fixUs) {
                            if (description.getEObjectURI().isPlatformResource()) {
                                EObject result = resourceSet.getEObject(description.getEObjectURI(), true);
                                if (result instanceof EPackage) {
                                    return (EPackage) result;
                                }
                            }
                        }
                        URI genModelURI = EcorePlugin.getEPackageNsURIToGenModelLocationMap(false).get(nsURI);
                        if (genModelURI != null) {
                            Resource genmodelResource = resourceSet.getResource(genModelURI, true);
                            GenModel genModel = (GenModel) genmodelResource.getContents().get(0);
                            for (GenPackage genPackage : genModel.getGenPackages()) {
                                Object object = genPackage.eGet(GenModelPackage.Literals.GEN_PACKAGE__ECORE_PACKAGE, false);
                                if (object instanceof EObject) {
                                    EObject proxy = (EObject) object;
                                    URI proxyURI = EcoreUtil.getURI(proxy);
                                    URI resolvedProxyURI = proxyURI.resolve(genModelURI);
                                    if (nsURI.equals(genPackage.getEcorePackage().getNsURI())) {
                                        EPackage result = genPackage.getEcorePackage();
                                        uriMap.put(result.eResource().getURI(), resolvedProxyURI.trimFragment());
                                        return result;
                                    }
                                }
                            }
                        }
                        return null;
                    }
                });
                String delimiter = document.get(issue.getOffset(), 1);
                if (!replaceString.startsWith(delimiter)) {
                    replaceString = delimiter + replaceString.substring(1, replaceString.length() - 1) + delimiter;
                }
                document.replace(issue.getOffset(), issue.getLength(), replaceString);
                if (importedPackages != null && !importedPackages.isEmpty()) {
                    final Shell shell = workbench.getActiveWorkbenchWindow().getShell();
                    shell.getDisplay().asyncExec(new Runnable() {

                        @Override
                        public void run() {
                            String title = "Please update the Ecore2XtextDslProjectContributor that generates the language.";
                            String message = "Please make sure that the Ecore2XtextDslProjectContributor that generates the language is up-to date.\n" + "Especially important is the registration of the referenced packages.\n" + "Please refer to the reference documentation for details.";
                            MessageDialog dialog = new MessageDialog(shell, title, null, message, MessageDialog.INFORMATION, new String[] { "Open Documentation", "Close" }, 1);
                            if (dialog.open() == 0) {
                                try {
                                    workbench.getBrowserSupport().getExternalBrowser().openURL(new URL(GRAMMAR_LANG_DOC));
                                } catch (Exception e) {
                                // ignore
                                }
                            }
                        }
                    });
                }
            }
        });
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) Set(java.util.Set) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) WorkspaceModifyOperation(org.eclipse.ui.actions.WorkspaceModifyOperation) EDataType(org.eclipse.emf.ecore.EDataType) XtextResource(org.eclipse.xtext.resource.XtextResource) GenPackage(org.eclipse.emf.codegen.ecore.genmodel.GenPackage) URI(org.eclipse.emf.common.util.URI) URL(java.net.URL) EPackage(org.eclipse.emf.ecore.EPackage) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) Shell(org.eclipse.swt.widgets.Shell) EObject(org.eclipse.emf.ecore.EObject) Iterator(java.util.Iterator) AbstractList(java.util.AbstractList) List(java.util.List) MessageDialog(org.eclipse.jface.dialogs.MessageDialog) IModification(org.eclipse.xtext.ui.editor.model.edit.IModification) AbstractList(java.util.AbstractList) Setting(org.eclipse.emf.ecore.EStructuralFeature.Setting) Resource(org.eclipse.emf.ecore.resource.Resource) XMLResource(org.eclipse.emf.ecore.xmi.XMLResource) XtextResource(org.eclipse.xtext.resource.XtextResource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IOException(java.io.IOException) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) BadLocationException(org.eclipse.jface.text.BadLocationException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) GenModel(org.eclipse.emf.codegen.ecore.genmodel.GenModel) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) EObject(org.eclipse.emf.ecore.EObject) Map(java.util.Map) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Aggregations

IResourceDescriptions (org.eclipse.xtext.resource.IResourceDescriptions)46 IResourceDescription (org.eclipse.xtext.resource.IResourceDescription)23 URI (org.eclipse.emf.common.util.URI)18 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)15 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)13 Resource (org.eclipse.emf.ecore.resource.Resource)12 EObject (org.eclipse.emf.ecore.EObject)11 QualifiedName (org.eclipse.xtext.naming.QualifiedName)7 IContainer (org.eclipse.xtext.resource.IContainer)7 List (java.util.List)6 Test (org.junit.Test)6 IN4JSProject (org.eclipse.n4js.projectModel.IN4JSProject)5 IScope (org.eclipse.xtext.scoping.IScope)5 IProject (org.eclipse.core.resources.IProject)4 EClass (org.eclipse.emf.ecore.EClass)4 XtextResource (org.eclipse.xtext.resource.XtextResource)4 IFile (org.eclipse.core.resources.IFile)3 CoreException (org.eclipse.core.runtime.CoreException)3 EPackage (org.eclipse.emf.ecore.EPackage)3 TModule (org.eclipse.n4js.ts.types.TModule)3