Search in sources :

Example 21 with IStorage

use of org.eclipse.core.resources.IStorage 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 22 with IStorage

use of org.eclipse.core.resources.IStorage in project xtext-xtend by eclipse.

the class DerivedSourceView method computeInput.

@Override
protected String computeInput(IWorkbenchPartSelection workbenchPartSelection) {
    openEditorAction.setInputFile(null);
    openEditorAction.setSelectedRegion(null);
    IEclipseTrace trace = traceInformation.getTraceToTarget(getEditorResource(workbenchPartSelection));
    if (trace != null) {
        if (workbenchPartSelection instanceof DerivedSourceSelection) {
            DerivedSourceSelection derivedSourceSelection = (DerivedSourceSelection) workbenchPartSelection;
            selectedSource = derivedSourceSelection.getStorage();
        } else {
            derivedSources = Sets.newHashSet();
            TextRegion localRegion = mapTextRegion(workbenchPartSelection);
            Iterable<IStorage> transform = Iterables.filter(transform(trace.getAllAssociatedLocations(localRegion), new Function<ILocationInEclipseResource, IStorage>() {

                @Override
                public IStorage apply(ILocationInEclipseResource input) {
                    return input.getPlatformResource();
                }
            }), Predicates.notNull());
            addAll(derivedSources, transform);
            ILocationInEclipseResource bestAssociatedLocation = trace.getBestAssociatedLocation(localRegion);
            if (bestAssociatedLocation != null) {
                selectedSource = bestAssociatedLocation.getPlatformResource();
            } else if (!derivedSources.isEmpty()) {
                selectedSource = derivedSources.iterator().next();
            }
        }
    }
    IFile file = getSelectedFile();
    if (file != null) {
        try {
            file.refreshLocal(1, new NullProgressMonitor());
            if (file.exists()) {
                openEditorAction.setInputFile(file);
                try (InputStream contents = file.getContents()) {
                    return Files.readStreamIntoString(contents);
                }
            }
        } catch (CoreException | IOException e) {
            throw new WrappedRuntimeException(e);
        }
    }
    return null;
}
Also used : ILocationInEclipseResource(org.eclipse.xtext.ui.generator.trace.ILocationInEclipseResource) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) InputStream(java.io.InputStream) IOException(java.io.IOException) IStorage(org.eclipse.core.resources.IStorage) Function(com.google.common.base.Function) CoreException(org.eclipse.core.runtime.CoreException) IEclipseTrace(org.eclipse.xtext.ui.generator.trace.IEclipseTrace) WrappedRuntimeException(org.eclipse.core.internal.utils.WrappedRuntimeException)

Example 23 with IStorage

use of org.eclipse.core.resources.IStorage in project xtext-eclipse by eclipse.

the class IntegrationTest method testIgnoreFilesInOutputFolder.

@Test
public void testIgnoreFilesInOutputFolder() throws Exception {
    IJavaProject javaProject = createJavaProjectWithRootSrc("foo");
    createFile("foo/src/foo" + F_EXT, "object Foo ");
    createFile("foo/bar" + F_EXT, "object Bar references Foo");
    build();
    IProject project = javaProject.getProject();
    IResource resourceFromBin = project.findMember(new Path("/bin/foo" + F_EXT));
    assertNotNull(resourceFromBin);
    assertTrue(resourceFromBin instanceof IStorage);
    assertTrue(resourceFromBin.exists());
    URI fakeBinURI = URI.createPlatformResourceURI("/" + project.getName() + "/bin/foo" + F_EXT, true);
    assertFalse(serviceProvider.canHandle(fakeBinURI, (IStorage) resourceFromBin));
    assertTrue(serviceProvider.canHandle(fakeBinURI));
    IResource resourceFromRoot = project.findMember(new Path("/bar" + F_EXT));
    assertNotNull(resourceFromRoot);
    assertTrue(resourceFromRoot instanceof IStorage);
    URI fromRootURI = URI.createPlatformResourceURI("/" + project.getName() + "/bar" + F_EXT, true);
    assertFalse(serviceProvider.canHandle(fromRootURI, (IStorage) resourceFromBin));
    assertTrue(serviceProvider.canHandle(fromRootURI));
}
Also used : Path(org.eclipse.core.runtime.Path) IJavaProject(org.eclipse.jdt.core.IJavaProject) 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) Test(org.junit.Test)

Example 24 with IStorage

use of org.eclipse.core.resources.IStorage in project xtext-eclipse by eclipse.

the class LoadingResourceAccess method readOnly.

@Override
public <R> R readOnly(URI targetURI, IUnitOfWork<R, ResourceSet> work) {
    URI resourceURI = targetURI.trimFragment();
    Iterable<Pair<IStorage, IProject>> storages = storage2UriMapper.getStorages(resourceURI);
    Iterator<Pair<IStorage, IProject>> iterator = storages.iterator();
    while (iterator.hasNext()) {
        Pair<IStorage, IProject> pair = iterator.next();
        IProject project = pair.getSecond();
        if (project != null) {
            ResourceSet resourceSet = resourceSetProvider.get(project);
            if (resourceSet != null)
                resourceSet.getResource(resourceURI, true);
            try {
                return work.exec(resourceSet);
            } catch (Exception e) {
                throw new WrappedException(e);
            }
        }
    }
    return null;
}
Also used : WrappedException(org.eclipse.emf.common.util.WrappedException) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IStorage(org.eclipse.core.resources.IStorage) URI(org.eclipse.emf.common.util.URI) IProject(org.eclipse.core.resources.IProject) WrappedException(org.eclipse.emf.common.util.WrappedException) Pair(org.eclipse.xtext.util.Pair)

Example 25 with IStorage

use of org.eclipse.core.resources.IStorage in project xtext-eclipse by eclipse.

the class Storage2UriMapperJavaImpl method getStorages.

/* @NonNull */
@Override
public Iterable<Pair<IStorage, IProject>> getStorages(/* @NonNull */
URI uri) {
    List<Pair<IStorage, IProject>> result = newArrayListWithCapacity(1);
    List<PackageFragmentRootData> packageFragmentRootDatas;
    synchronized (cachedPackageFragmentRootData) {
        packageFragmentRootDatas = newArrayList(cachedPackageFragmentRootData.values());
    }
    Iterator<PackageFragmentRootData> iterator = packageFragmentRootDatas.iterator();
    while (iterator.hasNext()) {
        PackageFragmentRootData data = iterator.next();
        if (data.exists()) {
            if (data.uriPrefix == null || uri.toString().startsWith(data.uriPrefix.toString())) {
                IStorage storage = data.uri2Storage.get(uri);
                if (storage != null) {
                    for (IPackageFragmentRoot root : data.associatedRoots.values()) {
                        result.add(Tuples.create(storage, root.getJavaProject().getProject()));
                    }
                }
            }
        } else {
            iterator.remove();
        }
    }
    if (result.isEmpty() && uri.isArchive()) {
        String authority = uri.authority();
        authority = authority.substring(0, authority.length() - 1);
        URI archiveURI = URI.createURI(authority);
        if (archiveURI.isFile() || archiveURI.isPlatformResource()) {
            IPath archivePath = new Path(archiveURI.isPlatformResource() ? archiveURI.toPlatformString(true) : archiveURI.toFileString());
            for (PackageFragmentRootData data : packageFragmentRootDatas) {
                if (data.uriPrefix != null && archivePath.equals(data.getPath())) {
                    // prefixes have an empty last segment.
                    URI prefix = data.uriPrefix.lastSegment().length() == 0 ? data.uriPrefix.trimSegments(1) : data.uriPrefix;
                    URI expectedURI = prefix.appendSegments(uri.segments());
                    IStorage storage = data.uri2Storage.get(expectedURI);
                    if (storage != null) {
                        for (IPackageFragmentRoot root : data.associatedRoots.values()) {
                            result.add(Tuples.create(storage, root.getJavaProject().getProject()));
                        }
                    }
                }
            }
        }
    }
    return result;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) IStorage(org.eclipse.core.resources.IStorage) URI(org.eclipse.emf.common.util.URI) Pair(org.eclipse.xtext.util.Pair) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Aggregations

IStorage (org.eclipse.core.resources.IStorage)96 URI (org.eclipse.emf.common.util.URI)32 IFile (org.eclipse.core.resources.IFile)31 IProject (org.eclipse.core.resources.IProject)27 CoreException (org.eclipse.core.runtime.CoreException)25 IResource (org.eclipse.core.resources.IResource)20 Pair (org.eclipse.xtext.util.Pair)16 Test (org.junit.Test)15 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)13 IStorageEditorInput (org.eclipse.ui.IStorageEditorInput)12 IPath (org.eclipse.core.runtime.IPath)11 IJavaProject (org.eclipse.jdt.core.IJavaProject)10 IOException (java.io.IOException)7 WrappedException (org.eclipse.emf.common.util.WrappedException)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)6 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)6 GitSynchronizeData (org.eclipse.egit.core.synchronize.dto.GitSynchronizeData)6 GitSynchronizeDataSet (org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet)6 Storage2UriMapperJavaImpl (org.eclipse.xtext.ui.resource.Storage2UriMapperJavaImpl)6