Search in sources :

Example 41 with URI

use of org.eclipse.emf.common.util.URI in project xtext-xtend by eclipse.

the class XtendFileRenameParticipant method createRenameElementContexts.

@Override
protected List<? extends IRenameElementContext> createRenameElementContexts(Object element) {
    if (super.getNewName().endsWith(".xtend")) {
        IFile file = (IFile) element;
        final IPath filePath = file.getFullPath();
        final IPath newPath = file.getFullPath().removeLastSegments(1).append(getNewName() + ".xtend");
        String className = trimFileExtension(file.getName());
        if (className != null) {
            ResourceSet resourceSet = resourceSetProvider.get(file.getProject());
            URI resourceURI = URI.createPlatformResourceURI(file.getFullPath().toString(), true);
            Resource resource = resourceSet.getResource(resourceURI, true);
            if (resource != null && !resource.getContents().isEmpty()) {
                for (XtendTypeDeclaration type : EcoreUtil2.eAllOfType(resource.getContents().get(0), XtendTypeDeclaration.class)) {
                    if (equal(className, type.getName())) {
                        IRenameElementContext renameElementContext = renameContextFactory.createRenameElementContext(type, null, null, (XtextResource) resource);
                        if (renameElementContext instanceof IChangeRedirector.Aware)
                            ((IChangeRedirector.Aware) renameElementContext).setChangeRedirector(new IChangeRedirector() {

                                @Override
                                public IPath getRedirectedPath(IPath source) {
                                    return source.equals(filePath) ? newPath : source;
                                }
                            });
                        return singletonList(renameElementContext);
                    }
                }
            }
        }
    }
    return super.createRenameElementContexts(element);
}
Also used : IFile(org.eclipse.core.resources.IFile) IChangeRedirector(org.eclipse.xtext.ui.refactoring.IChangeRedirector) IPath(org.eclipse.core.runtime.IPath) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) IRenameElementContext(org.eclipse.xtext.ui.refactoring.ui.IRenameElementContext) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URI(org.eclipse.emf.common.util.URI)

Example 42 with URI

use of org.eclipse.emf.common.util.URI in project xtext-xtend by eclipse.

the class XtendUIValidator method getExpectedPackageName.

protected String getExpectedPackageName(XtendFile xtendFile) {
    URI fileURI = xtendFile.eResource().getURI();
    for (Pair<IStorage, IProject> storage : storage2UriMapper.getStorages(fileURI)) {
        if (storage.getFirst() instanceof IFile) {
            IPath fileWorkspacePath = storage.getFirst().getFullPath();
            IJavaProject javaProject = JavaCore.create(storage.getSecond());
            if (javaProject != null && javaProject.exists() && javaProject.isOpen()) {
                try {
                    for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
                        if (!root.isArchive() && !root.isExternal()) {
                            IResource resource = root.getResource();
                            if (resource != null) {
                                IPath sourceFolderPath = resource.getFullPath();
                                if (sourceFolderPath.isPrefixOf(fileWorkspacePath)) {
                                    IPath claspathRelativePath = fileWorkspacePath.makeRelativeTo(sourceFolderPath);
                                    return claspathRelativePath.removeLastSegments(1).toString().replace("/", ".");
                                }
                            }
                        }
                    }
                } catch (JavaModelException e) {
                    LOG.error("Error resolving expected path for XtendFile", e);
                }
            }
        }
    }
    return null;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) IJavaProject(org.eclipse.jdt.core.IJavaProject) IPath(org.eclipse.core.runtime.IPath) IStorage(org.eclipse.core.resources.IStorage) URI(org.eclipse.emf.common.util.URI) IProject(org.eclipse.core.resources.IProject) IResource(org.eclipse.core.resources.IResource) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 43 with URI

use of org.eclipse.emf.common.util.URI in project xtext-xtend by eclipse.

the class UIResourceChangeRegistry method readState.

public void readState(final InputStream in) {
    try {
        final DataInputStream reader = new DataInputStream(in);
        for (final HashMultimap<String, URI> map : Collections.<HashMultimap<String, URI>>unmodifiableList(CollectionLiterals.<HashMultimap<String, URI>>newArrayList(this.existsListeners, this.charsetListeners, this.childrenListeners, this.contentsListeners))) {
            {
                final int urisForExists = reader.readInt();
                for (int i = 0; (i < urisForExists); i++) {
                    {
                        final String path = reader.readUTF();
                        final String uri = reader.readUTF();
                        map.put(path, URI.createURI(uri));
                    }
                }
            }
        }
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : DataInputStream(java.io.DataInputStream) URI(org.eclipse.emf.common.util.URI) HashMultimap(com.google.common.collect.HashMultimap)

Example 44 with URI

use of org.eclipse.emf.common.util.URI in project xtext-xtend by eclipse.

the class XtendUIResourceDescriptionManager method isAffected.

@Override
public boolean isAffected(Collection<Delta> deltas, IResourceDescription candidate, IResourceDescriptions context) {
    // skip collecting the outgoing references since we don't index references anyway
    // Set<URI> outgoingReferences = getDescriptionUtils().collectOutgoingReferences(candidate);
    // if (!outgoingReferences.isEmpty()) {
    // for (IResourceDescription.Delta delta : deltas)
    // if (hasChanges(delta, candidate) && outgoingReferences.contains(delta.getUri()))
    // return true;
    // }
    // this is a tradeoff - we could either check whether a given delta uri is contained
    // in a reachable container and check for intersecting names afterwards, or we can do
    // the other way round
    // unfortunately there is no way to decide reliably which algorithm scales better
    // note that this method is called for each description so we have something like a
    // number of deltas x number of resources which is not really nice
    List<IContainer> containers = null;
    Collection<QualifiedName> importedNames = getImportedNames(candidate);
    Map<String, Boolean> checkedProjects = Maps.newHashMap();
    for (IResourceDescription.Delta delta : deltas) {
        if (hasChanges(delta, candidate)) {
            // not a java resource - delta's resource should be contained in a visible container
            // as long as we did not delete the resource
            URI uri = delta.getUri();
            if ((uri.isPlatform() || uri.isArchive()) && delta.getNew() != null) {
                if (containers == null)
                    containers = getContainerManager().getVisibleContainers(candidate, context);
                boolean descriptionIsContained = false;
                for (int i = 0; i < containers.size() && !descriptionIsContained; i++) {
                    descriptionIsContained = containers.get(i).hasResourceDescription(uri);
                }
                if (!descriptionIsContained && !isProjectDependency(uri, candidate.getURI(), checkedProjects)) {
                    return false;
                }
            }
            if (isAffected(importedNames, delta.getNew()) || isAffected(importedNames, delta.getOld())) {
                return true;
            }
        }
    }
    return false;
}
Also used : IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) QualifiedName(org.eclipse.xtext.naming.QualifiedName) Delta(org.eclipse.xtext.resource.IResourceDescription.Delta) IContainer(org.eclipse.xtext.resource.IContainer) URI(org.eclipse.emf.common.util.URI)

Example 45 with URI

use of org.eclipse.emf.common.util.URI in project xtext-xtend by eclipse.

the class UIResourceChangeRegistryTest method testConcurrentDiscard.

@Test
public void testConcurrentDiscard() throws Exception {
    try {
        final Consumer<Integer> _function = (Integer it) -> {
            this.resourceChangeRegistry.registerCreateOrModify("/foo", this.uri.appendSegment(it.toString()));
        };
        new IntegerRange(1, 10000).forEach(_function);
        final Runnable _function_1 = () -> {
            final SecureRandom random = new SecureRandom(new byte[] { ((byte) 1) });
            final Consumer<Integer> _function_2 = (Integer it) -> {
                final URI removedURI = this.uri.appendSegment(Integer.valueOf(random.nextInt(10000)).toString());
                this.resourceChangeRegistry.discardCreateOrModifyInformation(removedURI);
            };
            new IntegerRange(1, 1000).forEach(_function_2);
        };
        final Runnable r = _function_1;
        final ExecutorService executorService = Executors.newCachedThreadPool();
        try {
            final Future<?> future1 = executorService.submit(r);
            final Future<?> future2 = executorService.submit(r);
            final Future<?> future3 = executorService.submit(r);
            final Future<?> future4 = executorService.submit(r);
            future1.get();
            future2.get();
            future3.get();
            future4.get();
        } catch (final Throwable _t) {
            if (_t instanceof ExecutionException) {
                final ExecutionException e = (ExecutionException) _t;
                throw e.getCause();
            } else {
                throw Exceptions.sneakyThrow(_t);
            }
        } finally {
            executorService.shutdown();
        }
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : IntegerRange(org.eclipse.xtext.xbase.lib.IntegerRange) Consumer(java.util.function.Consumer) ExecutorService(java.util.concurrent.ExecutorService) SecureRandom(java.security.SecureRandom) ExecutionException(java.util.concurrent.ExecutionException) URI(org.eclipse.emf.common.util.URI) Test(org.junit.Test)

Aggregations

URI (org.eclipse.emf.common.util.URI)305 Resource (org.eclipse.emf.ecore.resource.Resource)85 Test (org.junit.Test)79 EObject (org.eclipse.emf.ecore.EObject)55 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)37 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)33 XtextResource (org.eclipse.xtext.resource.XtextResource)32 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)26 BuildRequest (org.eclipse.xtext.build.BuildRequest)23 File (java.io.File)20 AbstractIncrementalBuilderTest (org.eclipse.xtext.testing.builder.AbstractIncrementalBuilderTest)20 ResourceSetImpl (org.eclipse.emf.ecore.resource.impl.ResourceSetImpl)19 IOException (java.io.IOException)18 InternalEObject (org.eclipse.emf.ecore.InternalEObject)18 EPackage (org.eclipse.emf.ecore.EPackage)15 IResourceDescription (org.eclipse.xtext.resource.IResourceDescription)15 InputStream (java.io.InputStream)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 ArrayList (java.util.ArrayList)13 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)13