@SuppressWarnings("ConstantConditions") @Override public boolean isMyFileType(@NotNull final VirtualFile file) { if (file instanceof StubVirtualFile) { return false; // Helps New -> File get correct file type } return recursionGuard.doPreventingRecursion(GUARD_ID, true, () -> { if (file.isValid()) { final String extension = file.getExtension(); if ("yml".equalsIgnoreCase(extension) || "yaml".equalsIgnoreCase(extension)) { try (InputStream inputStream = file.getInputStream()) { final byte[] bytes = new byte[BYTES_TO_READ]; final int n = inputStream.read(bytes, 0, BYTES_TO_READ); return n > 0 && isKubernetesYaml(bytes); } catch (final IOException e) { logger.info("Error while determining file type.", e); } } } return false; }); }
@Override @NotNull public FileType getFileTypeByFile(@NotNull VirtualFile file) { FileType fileType = getOrDetectByFile(file); if (fileType == null) { fileType = file instanceof StubVirtualFile ? UnknownFileType.INSTANCE : getOrDetectFromContent(file); } return fileType; }
private VirtualFile createVirtualFile(String path) { VirtualFile stub = new StubVirtualFile() { @Override public boolean isValid() { return true; } }; when(mockFileSystem.findFileByIoFile(new File(path))).thenReturn(stub); return stub; }
@Override @Nonnull public FileType getFileTypeByFile(@Nonnull VirtualFile file) { FileType fileType = getOrDetectByFile(file); if (fileType == null) { fileType = file instanceof StubVirtualFile ? UnknownFileType.INSTANCE : getOrDetectFromContent(file); } return fileType; }