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