Skip to content

Commit

Permalink
refactor ContentTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Apr 2, 2024
1 parent 2099e0e commit 95e2bde
Showing 1 changed file with 70 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,97 @@
import static org.eclipse.core.filebuffers.FileBuffers.getTextFileBufferManager;
import static org.eclipse.core.runtime.Platform.getContentTypeManager;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.core.filebuffers.ITextFileBuffer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IPathEditorInput;

import net.sf.jstuff.core.functional.ThrowingSupplier;

/**
* @author Sebastian Thomschke
*/
public abstract class ContentTypes {

private static final IContentType[] EMPTY = {};

public static @Nullable IContentType findById(final String contentTypeId) {
return getContentTypeManager().getContentType(contentTypeId);
}

public static IContentType[] getAll(final @Nullable IDocument doc) {
public static List<IContentType> of(final @Nullable IDocument doc) {
if (doc == null)
return EMPTY;
return Collections.emptyList();

final var buff = getTextFileBufferManager().getTextFileBuffer(doc);
if (buff == null)
return of(null, () -> new ByteArrayInputStream(doc.get().getBytes()));

return getAll(getTextFileBufferManager().getTextFileBuffer(doc));
return of(buff);
}

public static IContentType[] getAll(final IEditorInput input) {
return getContentTypeManager().findContentTypesFor(input.getName());
public static List<IContentType> of(final IEditorInput input) {
if (input instanceof final IFileEditorInput fInput)
return of(fInput.getName(), () -> fInput.getFile().getContents());
if (input instanceof final IPathEditorInput pInput)
return of(pInput.getPath().toFile().toPath());
return List.of(getContentTypeManager().findContentTypesFor(input.getName()));
}

public static IContentType[] getAll(final @Nullable ITextFileBuffer buff) {
if (buff == null)
return EMPTY;
public static List<IContentType> of(final IFile file) {
return of(file.getName(), file::getContents);
}

public static List<IContentType> of(final ITextFileBuffer buff) {
final String fileName = buff.getLocation().lastSegment();
final var result = new ArrayList<>( //
of(fileName, () -> new ByteArrayInputStream(buff.getDocument().get().getBytes())) //
);

final var fileName = buff.getLocation().lastSegment();
if (fileName == null)
return EMPTY;
try {
final IContentType primaryContentType = buff.getContentType();
if (primaryContentType != null) {
switch (result.indexOf(primaryContentType)) {
case -1:
result.add(0, primaryContentType);
break;
case 0: // nothing to do, already at first position
break;
default:
result.remove(primaryContentType);
result.add(0, primaryContentType);
}
}
} catch (final CoreException e) {
// ignore
}
return result;
}

public static List<IContentType> of(final Path path) {
return of(path.getFileName().toString(), () -> Files.newInputStream(path));
}

return getContentTypeManager().findContentTypesFor(fileName);
private static List<IContentType> of(final @Nullable String fileName,
final ThrowingSupplier<@Nullable InputStream, Exception> inputStreamFactory) {
try (var in = inputStreamFactory.get()) {
if (in != null)
return List.of(getContentTypeManager().findContentTypesFor(in, fileName));
} catch (final Exception ex) {
// ignore
}
if (fileName != null)
return List.of(getContentTypeManager().findContentTypesFor(fileName));
return Collections.emptyList();
}
}

0 comments on commit 95e2bde

Please sign in to comment.