Java Code Examples for com.intellij.openapi.vfs.VirtualFile#isValid()

The following examples show how to use com.intellij.openapi.vfs.VirtualFile#isValid() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: consulo   File: FileManagerImpl.java    License: Apache License 2.0 6 votes vote down vote up
private boolean shouldResurrect(@Nonnull FileViewProvider viewProvider, @Nonnull VirtualFile file) {
  if (!file.isValid()) return false;

  Map<VirtualFile, FileViewProvider> tempProviders = myTempProviders.get();
  if (tempProviders.containsKey(file)) {
    LOG.error(new StackOverflowPreventedException("isValid leads to endless recursion in " + viewProvider.getClass() + ": " + new ArrayList<>(viewProvider.getLanguages())));
  }
  tempProviders.put(file, null);
  try {
    FileViewProvider recreated = createFileViewProvider(file, true);
    tempProviders.put(file, recreated);
    return areViewProvidersEquivalent(viewProvider, recreated) && ((AbstractFileViewProvider)viewProvider).getCachedPsiFiles().stream().noneMatch(f -> hasInvalidOriginal(f));
  }
  finally {
    FileViewProvider temp = tempProviders.remove(file);
    if (temp != null) {
      DebugUtil.performPsiModification("invalidate temp view provider", () -> ((AbstractFileViewProvider)temp).markInvalidated());
    }
  }
}
 
Example 2
Source Project: consulo   File: VcsDescriptor.java    License: Apache License 2.0 6 votes vote down vote up
public boolean probablyUnderVcs(final VirtualFile file) {
  if (file == null || (! file.isDirectory()) || (! file.isValid())) return false;
  if (myAdministrativePattern == null) return false;
  return ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {
    @Override
    public Boolean compute() {
      if (checkFileForBeingAdministrative(file)) return true;
      if (myCrawlUpToCheckUnderVcs) {
        VirtualFile current = file.getParent();
        while (current != null) {
          if (checkFileForBeingAdministrative(current)) return true;
          current = current.getParent();
        }
      }
      return false;
    }
  });
}
 
Example 3
Source Project: consulo   File: UndoRedo.java    License: Apache License 2.0 6 votes vote down vote up
private Collection<VirtualFile> collectReadOnlyAffectedFiles() {
  Collection<VirtualFile> readOnlyFiles = new ArrayList<>();
  for (UndoableAction action : myUndoableGroup.getActions()) {
    if (action instanceof MentionOnlyUndoableAction) continue;

    DocumentReference[] refs = action.getAffectedDocuments();
    if (refs == null) continue;

    for (DocumentReference ref : refs) {
      VirtualFile file = ref.getFile();
      if ((file != null) && file.isValid() && !file.isWritable()) {
        readOnlyFiles.add(file);
      }
    }
  }
  return readOnlyFiles;
}
 
Example 4
Source Project: consulo   File: FileBasedIndexImpl.java    License: Apache License 2.0 5 votes vote down vote up
static boolean belongsToScope(VirtualFile file, VirtualFile restrictedTo, GlobalSearchScope filter) {
  if (!(file instanceof VirtualFileWithId) || !file.isValid()) {
    return false;
  }

  return (restrictedTo == null || Comparing.equal(file, restrictedTo)) && (filter == null || restrictedTo != null || filter.accept(file));
}
 
Example 5
Source Project: consulo   File: PerFileMappingsBase.java    License: Apache License 2.0 5 votes vote down vote up
private void cleanup() {
  for (Iterator<VirtualFile> i = myMappings.keySet().iterator(); i.hasNext();) {
    VirtualFile file = i.next();
    if (file != null /* PROJECT, top-level */ && !file.isValid()) {
      i.remove();
    }
  }
}
 
Example 6
Source Project: consulo   File: PsiNavigateUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static void navigate(@Nullable final PsiElement psiElement) {
  if (psiElement != null && psiElement.isValid()) {
    final PsiElement navigationElement = psiElement.getNavigationElement();
    final int offset = navigationElement instanceof PsiFile ? -1 : navigationElement.getTextOffset();
    final VirtualFile virtualFile = navigationElement.getContainingFile().getVirtualFile();
    if (virtualFile != null && virtualFile.isValid()) {
      new OpenFileDescriptor(navigationElement.getProject(), virtualFile, offset).navigate(true);
    }
  }
}
 
Example 7
Source Project: bxfs   File: BxCore.java    License: MIT License 5 votes vote down vote up
public VirtualFile[] getSiteTemplatesComponents(String templateComponent) {
	List<VirtualFile> templates = new ArrayList<VirtualFile>();
	for (String bitrixPath : BitrixPaths) {
		VirtualFile templatesDir = project.getBaseDir().findFileByRelativePath(bitrixPath + "/templates"); if (templatesDir != null && templatesDir.isDirectory()) {
			for (VirtualFile templateDir : templatesDir.getChildren()) if (templateDir != null && templateDir.isDirectory()) {
				VirtualFile component = templateDir.findChild(templateComponent); if (component != null && !component.isDirectory() && component.isValid())
					templates.add(component);
			}
		}
	}
	return templates.toArray(new VirtualFile[templates.size()]);
}
 
Example 8
@Nullable
private Result<PsiFile> getSourceFileResult(ClsFileImpl clsFile, VirtualFile root) {
  // This code is adapted from JavaPsiImplementationHelperImpl#getClsFileNavigationElement
  PsiClass[] classes = clsFile.getClasses();
  if (classes.length == 0) {
    return null;
  }

  String sourceFileName = ((ClsClassImpl) classes[0]).getSourceFileName();
  String packageName = clsFile.getPackageName();
  String relativePath =
      packageName.isEmpty()
          ? sourceFileName
          : packageName.replace('.', '/') + '/' + sourceFileName;

  VirtualFile source = root.findFileByRelativePath(relativePath);
  if (source != null && source.isValid()) {
    // Since we have an actual source jar tracked down, use that source jar as the modification
    // tracker. This means the result will continue to be cached unless that source jar changes.
    // If we didn't find a source jar, we use a modification tracker that invalidates on every
    // Blaze sync, which is less efficient.
    PsiFile psiSource = clsFile.getManager().findFile(source);
    if (psiSource instanceof PsiClassOwner) {
      return Result.create(psiSource, source);
    }
    return Result.create(null, source);
  }

  return null;
}
 
Example 9
Source Project: consulo   File: TogglePopupHintsPanel.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
private PsiFile getCurrentFile() {
  VirtualFile virtualFile = getSelectedFile();
  if (virtualFile != null && virtualFile.isValid()) {
    return PsiManager.getInstance(getProject()).findFile(virtualFile);
  }
  return null;
}
 
Example 10
Source Project: consulo   File: PsiFileUrl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Object[] createPath(final Project project) {
  final VirtualFile file = VirtualFileManager.getInstance().findFileByUrl(url);
  if (file == null || !file.isValid()){
    return null;
  }
  return new Object[]{PsiManager.getInstance(project).findFile(file)};
}
 
Example 11
@Override
public void computeIncrementalCompilerInstructions(@Nonnull IncrementalCompilerInstructionCreator creator,
                                                   @Nonnull PackagingElementResolvingContext resolvingContext,
                                                   @Nonnull ArtifactIncrementalCompilerContext compilerContext,
                                                   @Nonnull ArtifactType artifactType) {
  final VirtualFile file = findFile();
  if (file != null && file.isValid() && file.isDirectory()) {
    creator.addDirectoryCopyInstructions(file);
  }
}
 
Example 12
Source Project: consulo   File: DocumentImpl.java    License: Apache License 2.0 5 votes vote down vote up
private void beforeChangedUpdate(DocumentEvent event, DelayedExceptions exceptions) {
  Application app = ApplicationManager.getApplication();
  if (app != null) {
    FileDocumentManager manager = FileDocumentManager.getInstance();
    VirtualFile file = manager.getFile(this);
    if (file != null && !file.isValid()) {
      LOG.error("File of this document has been deleted: " + file);
    }
  }
  assertInsideCommand();

  getLineSet(); // initialize line set to track changed lines

  if (!ShutDownTracker.isShutdownHookRunning()) {
    DocumentListener[] listeners = getListeners();
    for (int i = listeners.length - 1; i >= 0; i--) {
      try {
        listeners[i].beforeDocumentChange(event);
      }
      catch (Throwable e) {
        exceptions.register(e);
      }
    }
  }

  myEventsHandling = true;
}
 
Example 13
Source Project: consulo   File: PathUtil.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
public static String getLocalPath(@Nullable VirtualFile file) {
  if (file == null || !file.isValid()) {
    return null;
  }
  if (file.getFileSystem().getProtocol().equals(ArchiveFileSystem.ARCHIVE_SEPARATOR) && file.getParent() != null) {
    return null;
  }
  return getLocalPath(file.getPath());
}
 
Example 14
private void updateNodesContaining(@Nonnull Collection<VirtualFile> filesToRefresh, @Nonnull DefaultMutableTreeNode rootNode) {
  if (!(rootNode.getUserObject() instanceof ProjectViewNode)) {
    return;
  }
  ProjectViewNode node = (ProjectViewNode) rootNode.getUserObject();
  Collection<VirtualFile> containingFiles = null;
  for (VirtualFile virtualFile : filesToRefresh) {
    if (!virtualFile.isValid()) {
      addSubtreeToUpdate(rootNode); // file must be deleted
      return;
    }
    if (node.contains(virtualFile)) {
      if (containingFiles == null) {
        containingFiles = new SmartList<VirtualFile>();
      }
      containingFiles.add(virtualFile);
    }
  }
  if (containingFiles != null) {
    updateNode(rootNode);
    Enumeration children = rootNode.children();
    while (children.hasMoreElements()) {
      DefaultMutableTreeNode child = (DefaultMutableTreeNode) children.nextElement();
      updateNodesContaining(containingFiles, child);
    }
  }
}
 
Example 15
Source Project: consulo   File: CompileContextImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void recalculateOutputDirs() {
  final Module[] allModules = ModuleManager.getInstance(myProject).getModules();

  final Set<VirtualFile> allDirs = new OrderedSet<VirtualFile>();
  final Set<VirtualFile> testOutputDirs = new java.util.HashSet<VirtualFile>();
  final Set<VirtualFile> productionOutputDirs = new java.util.HashSet<VirtualFile>();

  for (Module module : allModules) {
    ModuleCompilerPathsManager moduleCompilerPathsManager = ModuleCompilerPathsManager.getInstance(module);

    final VirtualFile output = moduleCompilerPathsManager.getCompilerOutput(ProductionContentFolderTypeProvider.getInstance());
    if (output != null && output.isValid()) {
      allDirs.add(output);
      productionOutputDirs.add(output);
    }

    final VirtualFile testsOutput = moduleCompilerPathsManager.getCompilerOutput(TestContentFolderTypeProvider.getInstance());
    if (testsOutput != null && testsOutput.isValid()) {
      allDirs.add(testsOutput);
      testOutputDirs.add(testsOutput);
    }
  }
  myOutputDirectories = VfsUtil.toVirtualFileArray(allDirs);
  // need this to ensure that the sent contains only _dedicated_ test output dirs
  // Directories that are configured for both test and production classes must not be added in the resulting set
  testOutputDirs.removeAll(productionOutputDirs);
  myTestOutputDirectories = Collections.unmodifiableSet(testOutputDirs);
}
 
Example 16
Source Project: floobits-intellij   File: FactoryImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public IFile findFileByPath(String path) {
    String absPath = context.absPath(path);
    if (absPath == null) {
        return null;
    }
    VirtualFile fileByPath = instance.findFileByPath(absPath);
    if (fileByPath != null && fileByPath.isValid()) {
        return new FileImpl(fileByPath);
    }
    return null;
}
 
Example 17
Source Project: consulo   File: PsiDocumentManagerBase.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public PsiFile getCachedPsiFile(@Nonnull Document document) {
  final VirtualFile virtualFile = FileDocumentManager.getInstance().getFile(document);
  if (virtualFile == null || !virtualFile.isValid()) return null;
  return getCachedPsiFile(virtualFile);
}
 
Example 18
Source Project: consulo   File: FileChooserDescriptor.java    License: Apache License 2.0 4 votes vote down vote up
@Nonnull
protected static Image dressIcon(final VirtualFile file, final Image baseIcon) {
  return file.isValid() && file.is(VFileProperty.SYMLINK) ? ImageEffects.layered(baseIcon, AllIcons.Nodes.Symlink) : baseIcon;
}
 
Example 19
Source Project: consulo   File: FileElement.java    License: Apache License 2.0 4 votes vote down vote up
public static boolean isFileHidden(@Nullable VirtualFile file) {
  return file != null &&
         file.isValid() &&
         file.isInLocalFileSystem() &&
         (file.is(VFileProperty.HIDDEN) || SystemInfo.isUnix && file.getName().startsWith("."));
}
 
Example 20
Source Project: consulo   File: FileTreeModel.java    License: Apache License 2.0 4 votes vote down vote up
private static boolean isValid(VirtualFile file) {
  return file != null && file.isValid();
}