@Override
 public boolean acceptInput(@NotNull final VirtualFile file) {
   return ourEnabled
       && findDuplicatesProfile(file.getFileType()) != null
       && file.isInLocalFileSystem() // skip library sources
   ;
 }
 private FileInfo[] createFileInfos(VirtualFile[] files) {
   List<FileInfo> fileInfos = new ArrayList<FileInfo>();
   for (final VirtualFile file : files) {
     if (file != null && !file.isWritable() && file.isInLocalFileSystem()) {
       fileInfos.add(new FileInfo(file, myProject));
     }
   }
   return fileInfos.toArray(new FileInfo[fileInfos.size()]);
 }
 @NotNull
 private static VirtualFile[] filterByReadOnliness(@NotNull VirtualFile[] files) {
   List<VirtualFile> result = new ArrayList<VirtualFile>();
   for (VirtualFile file : files) {
     if (file.isInLocalFileSystem()) {
       result.add(file);
     }
   }
   return VfsUtilCore.toVirtualFileArray(result);
 }
 static VirtualFile[] getFiles(DataContext dataContext) {
   ArrayList<VirtualFile> filesList = new ArrayList<VirtualFile>();
   VirtualFile[] files = CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(dataContext);
   for (int i = 0; files != null && i < files.length; i++) {
     VirtualFile file = files[i];
     if (file.isInLocalFileSystem()) {
       filesList.add(file);
     }
   }
   return VfsUtil.toVirtualFileArray(filesList);
 }
 public static boolean isTestngXML(final VirtualFile virtualFile) {
   if ("xml".equalsIgnoreCase(virtualFile.getExtension())
       && virtualFile.isInLocalFileSystem()
       && virtualFile.isValid()) {
     final String result = NanoXmlUtil.parseHeader(virtualFile).getRootTagLocalName();
     if (result != null && result.equals(SUITE_TAG_NAME)) {
       return true;
     }
   }
   return false;
 }
  private static PsiFile createFileCopy(PsiFile file, long caret, long selEnd) {
    final VirtualFile virtualFile = file.getVirtualFile();
    boolean mayCacheCopy =
        file.isPhysical()
            &&
            // we don't want to cache code fragment copies even if they appear to be physical
            virtualFile != null
            && virtualFile.isInLocalFileSystem();
    long combinedOffsets = caret + (selEnd << 32);
    if (mayCacheCopy) {
      final Trinity<PsiFile, Document, Long> cached =
          SoftReference.dereference(file.getUserData(FILE_COPY_KEY));
      if (cached != null
          && cached.first.getClass().equals(file.getClass())
          && isCopyUpToDate(cached.second, cached.first)) {
        final PsiFile copy = cached.first;
        if (copy.getViewProvider().getModificationStamp()
                > file.getViewProvider().getModificationStamp()
            && cached.third.longValue() != combinedOffsets) {
          // the copy PSI might have some caches that are not cleared on its modification because
          // there are no events in the copy
          //   so, clear all the caches
          // hopefully it's a rare situation that the user invokes completion in different parts of
          // the file
          //   without modifying anything physical in between
          ((PsiModificationTrackerImpl) file.getManager().getModificationTracker()).incCounter();
        }
        final Document document = cached.second;
        assert document != null;
        file.putUserData(
            FILE_COPY_KEY,
            new SoftReference<Trinity<PsiFile, Document, Long>>(
                Trinity.create(copy, document, combinedOffsets)));

        Document originalDocument = file.getViewProvider().getDocument();
        assert originalDocument != null;
        assert originalDocument.getTextLength() == file.getTextLength() : originalDocument;
        document.setText(originalDocument.getImmutableCharSequence());
        return copy;
      }
    }

    final PsiFile copy = (PsiFile) file.copy();
    if (mayCacheCopy) {
      final Document document = copy.getViewProvider().getDocument();
      assert document != null;
      file.putUserData(
          FILE_COPY_KEY,
          new SoftReference<Trinity<PsiFile, Document, Long>>(
              Trinity.create(copy, document, combinedOffsets)));
    }
    return copy;
  }
  @SuppressWarnings("UnsafeVfsRecursion")
  public static void assertDirectoriesEqual(
      VirtualFile dirAfter, VirtualFile dirBefore, @Nullable VirtualFileFilter fileFilter)
      throws IOException {
    FileDocumentManager.getInstance().saveAllDocuments();

    VirtualFile[] childrenAfter = dirAfter.getChildren();

    if (dirAfter.isInLocalFileSystem()
        && dirAfter.getFileSystem() != TempFileSystem.getInstance()) {
      File[] ioAfter = new File(dirAfter.getPath()).listFiles();
      shallowCompare(childrenAfter, ioAfter);
    }

    VirtualFile[] childrenBefore = dirBefore.getChildren();
    if (dirBefore.isInLocalFileSystem()
        && dirBefore.getFileSystem() != TempFileSystem.getInstance()) {
      File[] ioBefore = new File(dirBefore.getPath()).listFiles();
      shallowCompare(childrenBefore, ioBefore);
    }

    HashMap<String, VirtualFile> mapAfter = buildNameToFileMap(childrenAfter, fileFilter);
    HashMap<String, VirtualFile> mapBefore = buildNameToFileMap(childrenBefore, fileFilter);

    Set<String> keySetAfter = mapAfter.keySet();
    Set<String> keySetBefore = mapBefore.keySet();
    assertEquals(dirAfter.getPath(), keySetAfter, keySetBefore);

    for (String name : keySetAfter) {
      VirtualFile fileAfter = mapAfter.get(name);
      VirtualFile fileBefore = mapBefore.get(name);
      if (fileAfter.isDirectory()) {
        assertDirectoriesEqual(fileAfter, fileBefore, fileFilter);
      } else {
        assertFilesEqual(fileAfter, fileBefore);
      }
    }
  }
  @Override
  public void update(AnActionEvent e) {
    if (myUpdating) {
      return;
    }
    VirtualFile virtualFile = e.getData(PlatformDataKeys.VIRTUAL_FILE);
    VirtualFile[] files = e.getData(PlatformDataKeys.VIRTUAL_FILE_ARRAY);
    if (files != null && files.length > 1) {
      virtualFile = null;
    }
    if (virtualFile != null) {
      Navigatable navigatable = e.getData(PlatformDataKeys.NAVIGATABLE);
      if (navigatable instanceof OpenFileDescriptor) {
        // prefer source to the class file
        virtualFile = ((OpenFileDescriptor) navigatable).getFile();
      }
    }
    if (virtualFile != null && !virtualFile.isInLocalFileSystem()) {
      virtualFile = null;
    }

    Editor editor = e.getData(PlatformDataKeys.EDITOR);
    boolean enabled =
        encodingActionsPair.areActionsEnabled(
            null,
            editor,
            editor == null ? null : editor.getComponent(),
            virtualFile,
            getEventProject(e));
    removeAll();
    if (enabled) {
      addAll(encodingActionsPair.createActionGroup());
    }

    myUpdating = true;
    try {
      e.getPresentation().setText("File encoding");
      // updating the enabled state of the action can trigger the menuSelected handler, which
      // updates the action group again
      e.getPresentation().setEnabled(enabled);
    } finally {
      myUpdating = false;
    }
  }
  @Nullable
  private static VirtualFile findServiceDirectoryImpl(Project project) {
    for (VirtualFile file : ProjectRootManager.getInstance(project).getContentRoots()) {
      if (file.isInLocalFileSystem() && file.isDirectory()) {
        VirtualFile modules = file.findChild("node_modules");
        if (modules != null) {
          VirtualFile angularPackage = modules.findChild("@angular");
          if (angularPackage != null) {
            VirtualFile serviceDirectory = angularPackage.findChild("language-service");
            if (serviceDirectory != null) {
              return serviceDirectory;
            }
          }
        }
      }
    }

    return null;
  }
  @Override
  public void before(@NotNull List<? extends VFileEvent> events) {
    if (myForbid) return;
    final FileAndDirsCollector dirtyFilesAndDirs = new FileAndDirsCollector();
    // collect files and directories - sources of events
    for (VFileEvent event : events) {
      final VirtualFile file = getFileForEvent(event);
      if (file == null) {
        continue;
      }

      if (event instanceof VFileDeleteEvent) {
        if (!file.isInLocalFileSystem()) {
          continue;
        }
        dirtyFilesAndDirs.add(file, true);
      } else if (event instanceof VFileMoveEvent || event instanceof VFilePropertyChangeEvent) {
        dirtyFilesAndDirs.add(file, true);
      }
    }
    // and notify VCSDirtyScopeManager
    markDirtyOnPooled(dirtyFilesAndDirs);
  }
Example #11
0
  /**
   * Here we check if a given file belongs to our plugin. We take this road because we need the
   * actual file and not a filename to check files without extension.
   *
   * <p>A file is checked according to the rules defined in the facet settings. A file can be set to
   * ignored, accepted or auto. Auto means that the content is checked.
   *
   * @param file The file to check
   * @return True if BashSupport wants to take that file
   */
  public boolean isMyFileType(VirtualFile file) {
    if (file == null) {
      return false;
    }

    if (file.isDirectory()) {
      return false;
    }

    if (extensionList.contains(file.getExtension())) {
      return true;
    } else if (!file.isInLocalFileSystem()) {
      return false;
    } else if (StringUtils.isEmpty(file.getExtension())) {
      BashFacet facet = null;
      try {
        // no extensions, special checks (looking at the content, etc)

        // guess project
        Project project = ProjectUtil.guessProjectForFile(file);
        if (project == null) {
          return false;
        }

        DumbServiceImpl dumbService = DumbServiceImpl.getInstance(project);
        if (dumbService == null || dumbService.isDumb()) {
          return false;
        }

        DirectoryIndex directoryIndex = DirectoryIndex.getInstance(project);
        if (directoryIndex == null || !directoryIndex.isInitialized()) {
          return false;
        }

        Module module = ModuleUtil.findModuleForFile(file, project);
        if (module == null) {
          return false;
        }

        facet = BashFacet.getInstance(module);
        if (facet == null) {
          return false;
        }

        BashFacetConfiguration config = facet.getConfiguration();
        FileMode mode = config.findMode(file);

        if (mode == FileMode.accept()) {
          return true;
        } else if (mode == FileMode.ignore()) {
          return false;
        } else if (mode == FileMode.auto()) {
          return BashContentUtil.isProbablyBashFile(
              VfsUtil.virtualToIoFile(file),
              MIN_FILE_PROBABILIY,
              ProjectUtil.guessProjectForFile(file));
        }
      } catch (Exception e) {
        // ignore this
        LOG.warn("Could not check the file type due to exception", e);
      }
    }

    return false;
  }