private static Language calcBaseLanguage(@NotNull VirtualFile file, @NotNull Project project) {
    if (file instanceof LightVirtualFile) {
      final Language language = ((LightVirtualFile) file).getLanguage();
      if (language != null) {
        return language;
      }
    }

    FileType fileType = file.getFileType();
    // Do not load content
    if (fileType == UnknownFileType.INSTANCE) {
      fileType = FileTypeRegistry.getInstance().detectFileTypeFromContent(file);
    }
    if (fileType.isBinary()) return Language.ANY;
    if (isTooLarge(file)) return PlainTextLanguage.INSTANCE;

    if (fileType instanceof LanguageFileType) {
      return LanguageSubstitutors.INSTANCE.substituteLanguage(
          ((LanguageFileType) fileType).getLanguage(), file, project);
    }

    final ContentBasedFileSubstitutor[] processors =
        Extensions.getExtensions(ContentBasedFileSubstitutor.EP_NAME);
    for (ContentBasedFileSubstitutor processor : processors) {
      Language language = processor.obtainLanguageForFile(file);
      if (language != null) return language;
    }

    return PlainTextLanguage.INSTANCE;
  }
  @Nullable
  protected PsiFile createFile(
      @NotNull Project project, @NotNull VirtualFile file, @NotNull FileType fileType) {
    if (fileType.isBinary() || file.isSpecialFile()) {
      return new PsiBinaryFileImpl((PsiManagerImpl) getManager(), this);
    }
    if (!isTooLarge(file)) {
      final PsiFile psiFile = createFile(getBaseLanguage());
      if (psiFile != null) return psiFile;
    }

    return new PsiPlainTextFileImpl(this);
  }
  private PsiFile createFile() {
    try {
      final VirtualFile vFile = getVirtualFile();
      if (vFile.isDirectory()) return null;
      if (isIgnored()) return null;

      final Project project = myManager.getProject();
      if (isPhysical()) { // check directories consistency
        final VirtualFile parent = vFile.getParent();
        if (parent == null) return null;
        final PsiDirectory psiDir = getManager().findDirectory(parent);
        if (psiDir == null) return null;
      }

      FileType fileType = vFile.getFileType();
      PsiFile file = null;
      if (fileType.isBinary() || vFile.isSpecialFile()) {
        // TODO check why ClsFileImpl doesn't created automatically with create method
        file = new ClsFileImpl((PsiManagerImpl) getManager(), this);
        // file = new PsiBinaryFileImpl((PsiManagerImpl) getManager(), this);
      } else {
        if (!isTooLarge(vFile)) {
          final PsiFile psiFile = createFile(getBaseLanguage());
          if (psiFile != null) file = psiFile;
        } else {
          file = new PsiPlainTextFileImpl(this);
        }
      }
      return file;
    } catch (ProcessCanceledException e) {
      e.printStackTrace();
      throw e;

    } catch (Throwable e) {
      e.printStackTrace();
      LOG.error(e);
      return null;
    }
  }