Search in sources :

Example 41 with IStorage

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

the class WorkspaceEncodingProvider method getEncoding.

@Override
public String getEncoding(URI uri) {
    if (workspace != null) {
        if (uri != null) {
            if (uri.isPlatformResource()) {
                IFile file = workspace.getRoot().getFile(new Path(uri.toPlatformString(true)));
                try {
                    return file.getCharset(true);
                } catch (CoreException e) {
                    LOG.error("Error getting file encoding for " + uri, e);
                }
            } else {
                Iterator<Pair<IStorage, IProject>> storages = storage2UriMapper.getStorages(uri).iterator();
                if (storages.hasNext()) {
                    Pair<IStorage, IProject> next = storages.next();
                    IStorage storage = next.getFirst();
                    if (storage instanceof IEncodedStorage) {
                        try {
                            return ((IEncodedStorage) storage).getCharset();
                        } catch (CoreException e) {
                            LOG.error("Error getting file encoding for " + uri, e);
                        }
                    } else {
                        try {
                            String result = next.getSecond().getDefaultCharset(true);
                            return result;
                        } catch (CoreException e) {
                            LOG.error("Error getting file encoding for " + uri, e);
                        }
                    }
                }
            }
        }
        try {
            return workspace.getRoot().getDefaultCharset();
        } catch (CoreException e) {
            LOG.error("Error getting file encoding for " + uri, e);
        }
    }
    // fallback to runtime encoding provider
    return runtimeEncodingProvider.getEncoding(uri);
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) IEncodedStorage(org.eclipse.core.resources.IEncodedStorage) IStorage(org.eclipse.core.resources.IStorage) IProject(org.eclipse.core.resources.IProject) Pair(org.eclipse.xtext.util.Pair)

Example 42 with IStorage

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

the class CompareInputResourceProvider method getResource.

protected IResource getResource(ITypedElement typedElement) {
    if (typedElement == null) {
        return null;
    }
    IResource result = null;
    if (typedElement instanceof IResourceProvider) {
        IResourceProvider resourceProvider = (IResourceProvider) typedElement;
        result = resourceProvider.getResource();
    } else if (typedElement instanceof org.eclipse.team.internal.ui.StorageTypedElement) {
        org.eclipse.team.internal.ui.StorageTypedElement storageTypedElement = (org.eclipse.team.internal.ui.StorageTypedElement) typedElement;
        IStorage bufferedStorage = storageTypedElement.getBufferedStorage();
        result = getExistingFile(bufferedStorage != null ? bufferedStorage.getFullPath() : Path.EMPTY);
    }
    if (result == null) {
        IProject projectFromInput = getProjectFromInput();
        List<String> path = getPath(typedElement);
        for (int i = 0; i < path.size() && result == null; i++) {
            IProject project = getWorkspaceRoot().getProject(path.get(i));
            String subPath = IPath.SEPARATOR + Joiner.on(IPath.SEPARATOR).join(path.subList(i, path.size()));
            if (project.exists()) {
                result = getExistingFile(new Path(subPath));
            } else if (projectFromInput != null) {
                String pathInProject = IPath.SEPARATOR + projectFromInput.getName() + subPath;
                result = getExistingFile(new Path(pathInProject));
            }
        }
    }
    return result;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IStorage(org.eclipse.core.resources.IStorage) IProject(org.eclipse.core.resources.IProject) IResourceProvider(org.eclipse.compare.IResourceProvider) IResource(org.eclipse.core.resources.IResource)

Example 43 with IStorage

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

the class JavaProjectsStateHelper method getJarWithEntry.

protected IPackageFragmentRoot getJarWithEntry(URI uri) {
    Iterable<Pair<IStorage, IProject>> storages = getStorages(uri);
    IPackageFragmentRoot result = null;
    for (Pair<IStorage, IProject> storage2Project : storages) {
        IStorage storage = storage2Project.getFirst();
        if (storage instanceof IJarEntryResource) {
            IPackageFragmentRoot fragmentRoot = ((IJarEntryResource) storage).getPackageFragmentRoot();
            if (fragmentRoot != null) {
                // IPackageFragmentRoot has some unexpected caching - it may return a different project
                // thus we use the one that was used to record the IPackageFragmentRoot
                IProject actualProject = storage2Project.getSecond();
                IJavaProject javaProject = JavaCore.create(actualProject);
                if (!javaProject.exists()) {
                    javaProject = fragmentRoot.getJavaProject();
                }
                if (isAccessibleXtextProject(javaProject.getProject())) {
                    // if both projects are the same - fine
                    if (javaProject.equals(fragmentRoot.getJavaProject()))
                        return fragmentRoot;
                    // otherwise re-obtain the fragment root from the real project
                    if (fragmentRoot.isExternal()) {
                        IPackageFragmentRoot actualRoot = javaProject.getPackageFragmentRoot(fragmentRoot.getPath().toString());
                        if (actualProject.exists()) {
                            return actualRoot;
                        }
                    } else {
                        IPackageFragmentRoot actualRoot = javaProject.getPackageFragmentRoot(fragmentRoot.getResource());
                        if (actualRoot.exists()) {
                            return actualRoot;
                        }
                    }
                    result = fragmentRoot;
                }
                if (result == null)
                    result = fragmentRoot;
            }
        }
    }
    return result;
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) IJarEntryResource(org.eclipse.jdt.core.IJarEntryResource) IStorage(org.eclipse.core.resources.IStorage) IProject(org.eclipse.core.resources.IProject) Pair(org.eclipse.xtext.util.Pair) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 44 with IStorage

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

the class JavaSearchHelper method accept.

protected void accept(IReferenceDescription referenceDescription) {
    URI sourceResourceURI = referenceDescription.getSourceEObjectUri().trimFragment();
    Iterable<Pair<IStorage, IProject>> storages = storage2UriMapper.getStorages(sourceResourceURI);
    Iterator<Pair<IStorage, IProject>> iterator = storages.iterator();
    while (iterator.hasNext()) {
        Pair<IStorage, IProject> pair = iterator.next();
        IStorage storage = pair.getFirst();
        IProject project = pair.getSecond();
        if (project != null && !project.isHidden()) {
            ResourceSet resourceSet = getResourceSet(project);
            EObject sourceEObject = resourceSet.getEObject(referenceDescription.getSourceEObjectUri(), true);
            if (sourceEObject != null) {
                ITextRegion region = getLocation(sourceEObject, referenceDescription.getEReference(), referenceDescription.getIndexInList());
                acceptMatch(storage, region);
            } else {
                acceptMatch(referenceDescription, null);
            }
        }
    }
}
Also used : ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) 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) Pair(org.eclipse.xtext.util.Pair)

Example 45 with IStorage

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

the class XbaseEditor method selectAndReveal.

@Override
protected void selectAndReveal(final int selectionStart, final int selectionLength, final int revealStart, final int revealLength) {
    try {
        reentrantCallFromSelf++;
        if (expectJavaSelection > 0) {
            try {
                ITrace traceToSource = getTraceStorage();
                if (traceToSource != null) {
                    IResource javaResource = typeRoot.getResource();
                    if (expectLineSelection && javaResource instanceof IStorage) {
                        if (isCompiledWithJSR45()) {
                            try (InputStream contents = ((IStorage) javaResource).getContents()) {
                                String string = Files.readStreamIntoString(contents);
                                Document javaDocument = new Document(string);
                                int line = getLineInJavaDocument(javaDocument, selectionStart, selectionLength);
                                if (line != -1) {
                                    int startOffsetOfContents = getStartOffsetOfContentsInJava(javaDocument, line);
                                    if (startOffsetOfContents != -1) {
                                        ILocationInResource bestSelection = traceToSource.getBestAssociatedLocation(new TextRegion(startOffsetOfContents, 0));
                                        if (bestSelection != null) {
                                            final ITextRegionWithLineInformation textRegion = bestSelection.getTextRegion();
                                            if (textRegion != null) {
                                                int lineToSelect = textRegion.getLineNumber();
                                                try {
                                                    IRegion lineInfo = getDocument().getLineInformation(lineToSelect);
                                                    super.selectAndReveal(lineInfo.getOffset(), lineInfo.getLength(), lineInfo.getOffset(), lineInfo.getLength());
                                                    return;
                                                } catch (BadLocationException e) {
                                                    log.error(e.getMessage(), e);
                                                }
                                            }
                                        }
                                    }
                                }
                            } catch (BadLocationException | CoreException | IOException e) {
                            // do nothing
                            }
                        }
                    } else if (selectionStart >= 0 && selectionLength >= 0) {
                        ILocationInResource bestSelection = traceToSource.getBestAssociatedLocation(new TextRegion(selectionStart, selectionLength));
                        if (bestSelection != null) {
                            ILocationInResource bestReveal = bestSelection;
                            if (selectionStart != revealStart || selectionLength != revealLength) {
                                bestReveal = traceToSource.getBestAssociatedLocation(new TextRegion(revealStart, revealLength));
                                if (bestReveal == null) {
                                    bestReveal = bestSelection;
                                }
                            }
                            ITextRegion fixedSelection = bestSelection.getTextRegion();
                            if (fixedSelection != null) {
                                ITextRegion fixedReveal = bestReveal.getTextRegion();
                                if (fixedReveal == null) {
                                    fixedReveal = fixedSelection;
                                }
                                super.selectAndReveal(fixedSelection.getOffset(), fixedSelection.getLength(), fixedReveal.getOffset(), fixedReveal.getLength());
                                return;
                            }
                        }
                    }
                }
            } finally {
                expectLineSelection = false;
                expectJavaSelection--;
            }
        }
        super.selectAndReveal(selectionStart, selectionLength, revealStart, revealLength);
    } finally {
        reentrantCallFromSelf--;
    }
}
Also used : 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) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) ILocationInResource(org.eclipse.xtext.generator.trace.ILocationInResource) IRegion(org.eclipse.jface.text.IRegion) ITextRegionWithLineInformation(org.eclipse.xtext.util.ITextRegionWithLineInformation) CoreException(org.eclipse.core.runtime.CoreException) ITextRegion(org.eclipse.xtext.util.ITextRegion) ITrace(org.eclipse.xtext.generator.trace.ITrace) IResource(org.eclipse.core.resources.IResource) BadLocationException(org.eclipse.jface.text.BadLocationException)

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