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;
}
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;
}
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));
}
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;
}
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;
}
Aggregations