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;
  }
 public boolean isLibraryClassFile(@NotNull VirtualFile file) {
   if (file.isDirectory()) return false;
   if (myFileTypeRegistry.isFileIgnored(file)) return false;
   VirtualFile parent = file.getParent();
   DirectoryInfo parentInfo = getInfoForDirectory(parent);
   return parentInfo != null && parentInfo.libraryClassRoot != null;
 }
  @NotNull
  @Override
  public PsiElement createFromTemplate(
      final Project project,
      final PsiDirectory directory,
      String fileName,
      final FileTemplate template,
      final String templateText,
      @NotNull final Map<String, Object> props)
      throws IncorrectOperationException {
    fileName = checkAppendExtension(fileName, template);

    if (FileTypeManager.getInstance().isFileIgnored(fileName)) {
      throw new IncorrectOperationException(
          "This filename is ignored (Settings | Editor | File Types | Ignore files and folders)");
    }

    directory.checkCreateFile(fileName);
    FileType type = FileTypeRegistry.getInstance().getFileTypeByFileName(fileName);
    PsiFile file =
        PsiFileFactory.getInstance(project).createFileFromText(fileName, type, templateText);

    if (template.isReformatCode()) {
      CodeStyleManager.getInstance(project).reformat(file);
    }

    file = (PsiFile) directory.add(file);
    return file;
  }
  public boolean isIgnored(@NotNull VirtualFile file) {
    if (myFileTypeRegistry.isFileIgnored(file)) return true;
    if (myFileExclusionManager != null && myFileExclusionManager.isExcluded(file)) return true;
    VirtualFile dir = file.isDirectory() ? file : file.getParent();
    if (dir == null) return false;

    DirectoryInfo info = getInfoForDirectory(dir);
    if (info != null) return false;
    if (myDirectoryIndex.isProjectExcludeRoot(dir)) return true;

    VirtualFile parent = dir.getParent();
    while (true) {
      if (parent == null) return false;
      DirectoryInfo parentInfo = getInfoForDirectory(parent);
      if (parentInfo != null) return true;
      if (myDirectoryIndex.isProjectExcludeRoot(parent)) return true;
      parent = parent.getParent();
    }
  }
 public boolean isContentSourceFile(@NotNull VirtualFile file) {
   return !file.isDirectory()
       && !myFileTypeRegistry.isFileIgnored(file)
       && isInSourceContent(file);
 }
Exemplo n.º 6
0
  @Override
  @Nullable
  public VirtualFileSystemEntry findRoot(
      @NotNull String basePath, @NotNull NewVirtualFileSystem fs) {
    if (basePath.isEmpty()) {
      LOG.error("Invalid root, fs=" + fs);
      return null;
    }

    String rootUrl = normalizeRootUrl(basePath, fs);

    myRootsLock.readLock().lock();
    try {
      VirtualFileSystemEntry root = myRoots.get(rootUrl);
      if (root != null) return root;
    } finally {
      myRootsLock.readLock().unlock();
    }

    final VirtualFileSystemEntry newRoot;
    int rootId = FSRecords.findRootRecord(rootUrl);

    VfsData.Segment segment = VfsData.getSegment(rootId, true);
    VfsData.DirectoryData directoryData = new VfsData.DirectoryData();
    if (fs instanceof ArchiveFileSystem) {
      String parentPath =
          basePath.substring(0, basePath.indexOf(ArchiveFileSystem.ARCHIVE_SEPARATOR));
      VirtualFile parentFile = LocalFileSystem.getInstance().findFileByPath(parentPath);
      if (parentFile == null) return null;
      FileType type = FileTypeRegistry.getInstance().getFileTypeByFileName(parentFile.getName());
      if (!(type instanceof ArchiveFileType)) return null;
      newRoot = new ArchiveRoot(fs, rootId, segment, directoryData, parentFile);
    } else {
      newRoot = new FsRoot(fs, rootId, segment, directoryData, basePath);
    }

    FileAttributes attributes =
        fs.getAttributes(
            new StubVirtualFile() {
              @NotNull
              @Override
              public String getPath() {
                return newRoot.getPath();
              }

              @Nullable
              @Override
              public VirtualFile getParent() {
                return null;
              }
            });
    if (attributes == null || !attributes.isDirectory()) {
      return null;
    }

    boolean mark = false;

    myRootsLock.writeLock().lock();
    try {
      VirtualFileSystemEntry root = myRoots.get(rootUrl);
      if (root != null) return root;

      VfsData.initFile(rootId, segment, -1, directoryData);
      mark = writeAttributesToRecord(rootId, 0, newRoot, fs, attributes);

      myRoots.put(rootUrl, newRoot);
      myRootsById.put(rootId, newRoot);
    } finally {
      myRootsLock.writeLock().unlock();
    }

    if (!mark && attributes.lastModified != FSRecords.getTimestamp(rootId)) {
      newRoot.markDirtyRecursively();
    }

    LOG.assertTrue(
        rootId == newRoot.getId(),
        "root=" + newRoot + " expected=" + rootId + " actual=" + newRoot.getId());

    return newRoot;
  }
 protected boolean isIgnored() {
   final VirtualFile file = getVirtualFile();
   if (file instanceof LightVirtualFile) return false;
   if (myExclusionManager != null && myExclusionManager.isExcluded(file)) return true;
   return FileTypeRegistry.getInstance().isFileIgnored(file);
 }
 private static boolean isIgnored(@NotNull VirtualFile dir) {
   return FileTypeRegistry.getInstance().isFileIgnored(dir);
 }