Java Code Examples for com.intellij.psi.PsiDirectory#getVirtualFile()

The following examples show how to use com.intellij.psi.PsiDirectory#getVirtualFile() . 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 File: PackageViewPane.java    From consulo with Apache License 2.0 6 votes vote down vote up
private Module[] getModulesFor(PsiDirectory dir) {
  final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
  final VirtualFile vFile = dir.getVirtualFile();
  final Set<Module> modules = new HashSet<Module>();
  final Module module = fileIndex.getModuleForFile(vFile);
  if (module != null) {
    modules.add(module);
  }
  if (fileIndex.isInLibrarySource(vFile) || fileIndex.isInLibraryClasses(vFile)) {
    final List<OrderEntry> orderEntries = fileIndex.getOrderEntriesForFile(vFile);
    if (orderEntries.isEmpty()) {
      return Module.EMPTY_ARRAY;
    }
    for (OrderEntry entry : orderEntries) {
      modules.add(entry.getOwnerModule());
    }
  }
  return modules.toArray(new Module[modules.size()]);
}
 
Example 2
Source File: BlazeCreateXmlResourcePanel.java    From intellij with Apache License 2.0 6 votes vote down vote up
private void setupResourceDirectoryCombo() {
  Project project = myModule.getProject();
  if (myContextFile != null) {
    // Try to figure out res/ dir from context
    // (e.g., while refactoring an xml file that's a child of a res/ directory).
    // We currently take the parent and hide the combo box.
    PsiManager manager = PsiManager.getInstance(project);
    VirtualFile virtualDirectory =
        BlazeCreateResourceUtils.getResDirFromDataContext(myContextFile);
    PsiDirectory directory =
        virtualDirectory != null ? manager.findDirectory(virtualDirectory) : null;
    if (directory != null) {
      myResDirectory = directory.getVirtualFile();
    } else {
      // As a last resort, if we have poor context,
      // e.g., quick fix from within a .java file, set up the UI
      // based on the deps of the .java file.
      BlazeCreateResourceUtils.setupResDirectoryChoices(
          project, myContextFile, myResDirLabel, myResDirCombo);
    }
  } else {
    // As a last resort, if we have no context at all, set up some UI.
    BlazeCreateResourceUtils.setupResDirectoryChoices(
        project, null, myResDirLabel, myResDirCombo);
  }
}
 
Example 3
Source File: CSharpCreateFromTemplateHandler.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Nullable
@RequiredReadAction
public static Module findModuleByPsiDirectory(final PsiDirectory directory)
{
	LightVirtualFile l = new LightVirtualFile("test.cs", CSharpFileType.INSTANCE, "")
	{
		@Override
		public VirtualFile getParent()
		{
			return directory.getVirtualFile();
		}

		@Nonnull
		@Override
		public VirtualFileSystem getFileSystem()
		{
			return LocalFileSystem.getInstance();
		}
	};
	return CreateFileFromTemplateAction.ModuleResolver.EP_NAME.composite().resolveModule(directory, CSharpFileType.INSTANCE);
}
 
Example 4
Source File: PsiDirectoryNode.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public boolean contains(@Nonnull VirtualFile file) {
  final PsiDirectory value = getValue();
  if (value == null) {
    return false;
  }

  VirtualFile directory = value.getVirtualFile();
  if (directory.getFileSystem() instanceof LocalFileSystem) {
    file = PathUtil.getLocalFile(file);
  }

  if (!VfsUtilCore.isAncestor(directory, file, false)) {
    return false;
  }

  return !FileTypeRegistry.getInstance().isFileIgnored(file);
}
 
Example 5
Source File: SimpleCoverageAnnotator.java    From consulo with Apache License 2.0 6 votes vote down vote up
@javax.annotation.Nullable
protected DirCoverageInfo getDirCoverageInfo(@Nonnull final PsiDirectory directory,
                                             @Nonnull final CoverageSuitesBundle currentSuite) {
  final VirtualFile dir = directory.getVirtualFile();

  final ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(directory.getProject()).getFileIndex();
  //final Module module = projectFileIndex.getModuleForFile(dir);

  final boolean isInTestContent = projectFileIndex.isInTestSourceContent(dir);
  if (!currentSuite.isTrackTestFolders() && isInTestContent) {
    return null;
  }

  final String path = normalizeFilePath(dir.getPath());

  return isInTestContent ? myTestDirCoverageInfos.get(path) : myDirCoverageInfos.get(path);
}
 
Example 6
Source File: TYPO3ExtensionUtil.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
/**
 * Traverses the given directories and returns the first valid
 * extension definition that's applicable.
 *
 * @param directories List of directories to analyze
 */
public static TYPO3ExtensionDefinition findContainingExtension(PsiDirectory[] directories) {
    for (PsiDirectory directory : directories) {
        VirtualDirectoryImpl virtualFile = (VirtualDirectoryImpl) directory.getVirtualFile();

        while (!isExtensionRootDirectory(virtualFile)) {
            if (virtualFile.getParent() == null) {
                return null;
            }

            virtualFile = virtualFile.getParent();
        }

        TYPO3ExtensionDefinition extensionDefinition = ExtensionDefinitionFactory.fromDirectory(virtualFile);
        if (extensionDefinition != null) {
            return extensionDefinition;
        }
    }

    return null;
}
 
Example 7
Source File: ContentRootDecoration.java    From SVNToolBox with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isForMe(ProjectViewNode node) {
    if (node instanceof PsiDirectoryNode) {
        PsiDirectoryNode dirNode = (PsiDirectoryNode) node;            
        final PsiDirectory psiDirectory = dirNode.getValue();
        if (psiDirectory != null) {
            final Project project = dirNode.getProject();
            final VirtualFile directoryFile = psiDirectory.getVirtualFile();
            return ProjectRootsUtil.isModuleContentRoot(directoryFile, project)
                    || ProjectRootsUtil.isInSource(directoryFile, project);
        }
    }
    return false;
}
 
Example 8
Source File: NavBarPanel.java    From consulo with Apache License 2.0 5 votes vote down vote up
static Object expandDirsWithJustOneSubdir(Object target) {
  if (target instanceof PsiElement && !((PsiElement)target).isValid()) return target;
  if (target instanceof PsiDirectory) {
    PsiDirectory directory = (PsiDirectory)target;
    for (VirtualFile file = directory.getVirtualFile(), next; ; file = next) {
      VirtualFile[] children = file.getChildren();
      VirtualFile child = children.length == 1 ? children[0] : null;
      //noinspection AssignmentToForLoopParameter
      next = child != null && child.isDirectory() && !child.is(VFileProperty.SYMLINK) ? child : null;
      if (next == null) return ObjectUtils.notNull(directory.getManager().findDirectory(file), directory);
    }
  }
  return target;
}
 
Example 9
Source File: TodoDirNode.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected void setupIcon(PresentationData data, PsiDirectory psiDirectory) {
  final VirtualFile virtualFile = psiDirectory.getVirtualFile();
  if (ProjectRootsUtil.isModuleContentRoot(virtualFile, psiDirectory.getProject())) {
    data.setIcon(IconDescriptorUpdaters.getIcon(psiDirectory, 0));
  }
  else {
    super.setupIcon(data, psiDirectory);
  }
}
 
Example 10
Source File: FileTreeModelBuilder.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public PackageDependenciesNode addDirNode(PsiDirectory dir) {
  final VirtualFile vFile = dir.getVirtualFile();
  if (myMarker == null) return null;
  final boolean[] isMarked = new boolean[]{myMarker.isMarked(vFile)};
  VirtualFile dirToReload = vFile.getParent();
  PackageDependenciesNode rootToReload = myModuleDirNodes.get(dirToReload);
  if (rootToReload == null && myFlattenPackages) {
    final Module module = myFileIndex.getModuleForFile(vFile);
    final boolean moduleNodeExist = myModuleNodes.get(module) != null;
    rootToReload = getModuleNode(module);
    if (!moduleNodeExist) {
      rootToReload = null; //need to reload from parent / mostly for problems view
    }
  }
  else {
    while (rootToReload == null && dirToReload != null) {
      dirToReload = dirToReload.getParent();
      rootToReload = myModuleDirNodes.get(dirToReload);
    }
  }
  myFileIndex.iterateContentUnderDirectory(vFile, new MyContentIterator() {
    @Override
    public boolean processFile(VirtualFile fileOrDir) {
      isMarked[0] |= myMarker.isMarked(fileOrDir);
      return super.processFile(fileOrDir);
    }
  });
  if (!isMarked[0]) return null;

  getModuleDirNode(vFile, myFileIndex.getModuleForFile(vFile), null);
  return rootToReload != null ? rootToReload : myRoot;
}
 
Example 11
Source File: FileUtil.java    From idea-php-advanced-autocomplete with MIT License 5 votes vote down vote up
public static String[] getRelativeFiles(PsiFile baseFile)
    {
        final List<String> files = new ArrayList<String>();
        Project project = baseFile.getProject();
        final VirtualFile projectDirectory = project.getBaseDir();
        PsiDirectory directory = baseFile.getContainingDirectory();

        if(directory == null)
            return null;

        final VirtualFile originDirectory = directory.getVirtualFile();

        ProjectFileIndex fileIndex = ProjectFileIndex.SERVICE.getInstance(project);

/*
        for(VirtualFile file : originDirectory.getChildren()) {
            files.add(file.getPath().replace(projectDirectory.getPath() + "/", ""));
        }
*/

        fileIndex.iterateContentUnderDirectory(originDirectory, new ContentIterator() {
                @Override
                public boolean processFile(VirtualFile file) {
                    //files.add(file.getName());
                    if(!file.isDirectory()) {
                        files.add(file.getPath().replace(originDirectory.getPath() + "/", ""));
                    }

                    return true;
                }
            }
        );

        return files.toArray(new String[files.size()]);
    }
 
Example 12
Source File: PackageElementNode.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public VirtualFile[] getVirtualFiles() {
  final PackageElement value = getValue();
  if (value == null) {
    return VirtualFile.EMPTY_ARRAY;
  }
  final PsiDirectory[] directories = PackageNodeUtil
    .getDirectories(value.getPackage(), getProject(), value.getModule(), isLibraryElement());
  final VirtualFile[] result = new VirtualFile[directories.length];
  for (int i = 0; i < directories.length; i++) {
    PsiDirectory directory = directories[i];
    result[i] = directory.getVirtualFile();
  }
  return result;
}
 
Example 13
Source File: BlazeCreateXmlResourcePanel.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Override
public VirtualFile getResourceDirectory() {
  if (myResDirectory != null) {
    return myResDirectory;
  }
  if (myResDirCombo.isVisible()) {
    PsiDirectory directory =
        BlazeCreateResourceUtils.getResDirFromUI(myModule.getProject(), myResDirCombo);
    return directory != null ? directory.getVirtualFile() : null;
  }
  return null;
}
 
Example 14
Source File: PsiDirectoryNode.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
protected void updateImpl(PresentationData data) {
  final Project project = getProject();
  final PsiDirectory psiDirectory = getValue();
  final VirtualFile directoryFile = psiDirectory.getVirtualFile();

  final Object parentValue = getParentValue();
  if (ProjectRootsUtil.isModuleContentRoot(directoryFile, project)) {
    ProjectFileIndex fi = ProjectRootManager.getInstance(project).getFileIndex();
    Module module = fi.getModuleForFile(directoryFile);

    data.setPresentableText(directoryFile.getName());
    if (module != null) {
      if (!(parentValue instanceof Module)) {
        if (Comparing.equal(module.getName(), directoryFile.getName())) {
          data.addText(directoryFile.getName(), SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES);
        }
        else {
          data.addText(directoryFile.getName() + " ", SimpleTextAttributes.REGULAR_ATTRIBUTES);
          data.addText("[" + module.getName() + "]", SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES);
        }
      }
      else {
        data.addText(directoryFile.getName(), SimpleTextAttributes.REGULAR_ATTRIBUTES);
      }

      if (parentValue instanceof Module || parentValue instanceof Project) {
        final String location = FileUtil.getLocationRelativeToUserHome(directoryFile.getPresentableUrl());
        data.addText(" (" + location + ")", SimpleTextAttributes.GRAYED_ATTRIBUTES);
      }
      else if (ProjectRootsUtil.isSourceOrTestRoot(directoryFile, project)) {
        if (ProjectRootsUtil.isInTestSource(directoryFile, project)) {
          data.addText(" (test source root)", SimpleTextAttributes.GRAY_ATTRIBUTES);
        }
        else {
          data.addText(" (source root)",  SimpleTextAttributes.GRAY_ATTRIBUTES);
        }
      }

      setupIcon(data, psiDirectory);

      return;
    }
  }

  final String name = parentValue instanceof Project
                      ? psiDirectory.getVirtualFile().getPresentableUrl()
                      : BaseProjectViewDirectoryHelper.getNodeName(getSettings(), parentValue, psiDirectory);
  if (name == null) {
    setValue(null);
    return;
  }

  data.setPresentableText(name);
  if (ProjectRootsUtil.isLibraryRoot(directoryFile, project)) {
    data.setLocationString("library home");
  }
  else {
    data.setLocationString(BaseProjectViewDirectoryHelper.getLocationString(psiDirectory));
  }

  setupIcon(data, psiDirectory);
}
 
Example 15
Source File: GlobalSearchScopesCore.java    From consulo with Apache License 2.0 4 votes vote down vote up
private DirectoryScope(@Nonnull PsiDirectory psiDirectory, final boolean withSubdirectories) {
  super(psiDirectory.getProject());
  myWithSubdirectories = withSubdirectories;
  myDirectory = psiDirectory.getVirtualFile();
}
 
Example 16
Source File: DirElementInfo.java    From consulo with Apache License 2.0 4 votes vote down vote up
DirElementInfo(@Nonnull PsiDirectory directory) {
  myProject = directory.getProject();
  myVirtualFile = directory.getVirtualFile();
}
 
Example 17
Source File: PsiDirectoryNode.java    From consulo with Apache License 2.0 4 votes vote down vote up
protected void setupIcon(PresentationData data, PsiDirectory psiDirectory) {
  final VirtualFile virtualFile = psiDirectory.getVirtualFile();
  data.setIcon(VfsIconUtil.getIcon(virtualFile, 0, myProject));
}
 
Example 18
Source File: UnityNamespaceGeneratePolicy.java    From consulo-unity3d with Apache License 2.0 4 votes vote down vote up
@RequiredReadAction
@Nullable
@Override
public String calculateDirtyNamespace(@Nonnull PsiDirectory psiDirectory)
{
	Project project = psiDirectory.getProject();
	VirtualFile baseDir = project.getBaseDir();
	if(baseDir == null)
	{
		return myNamespacePrefix;
	}

	VirtualFile currentDirectory = psiDirectory.getVirtualFile();

	VirtualFile assetsDirectory = baseDir.findChild(Unity3dProjectImportUtil.ASSETS_DIRECTORY);
	if(assetsDirectory != null)
	{
		VirtualFile targetDirectory = assetsDirectory;

		VirtualFile temp = currentDirectory;
		while(temp != null && !temp.equals(targetDirectory))
		{
			if("Editor".equals(temp.getName()))
			{
				targetDirectory = temp;
			}
			temp = temp.getParent();
		}

		// if path is not changed
		if(targetDirectory.equals(assetsDirectory))
		{
			temp = currentDirectory;
			while(temp != null && !temp.equals(targetDirectory))
			{
				if("Scripts".equals(temp.getName()))
				{
					targetDirectory = temp;
				}
				temp = temp.getParent();
			}
		}

		// if path is not changed
		if(targetDirectory.equals(assetsDirectory))
		{
			for(String path : Unity3dProjectImportUtil.FIRST_PASS_PATHS)
			{
				VirtualFile child = baseDir.findFileByRelativePath(path);
				if(child != null && VfsUtil.isAncestor(child, currentDirectory, false))
				{
					targetDirectory = child;
					break;
				}
			}
		}

		String relativePath = VfsUtil.getRelativePath(currentDirectory, targetDirectory, '.');
		if(relativePath != null)
		{
			if(!StringUtil.isEmpty(myNamespacePrefix))
			{
				if(StringUtil.isEmpty(relativePath))
				{
					return myNamespacePrefix;
				}
				return myNamespacePrefix + "." + relativePath;
			}
			return relativePath;
		}
	}
	return myNamespacePrefix;
}
 
Example 19
Source File: GraphQLEditConfigAction.java    From js-graphql-intellij-plugin with MIT License 4 votes vote down vote up
VirtualFile getSelectedDirectory() {
    final PsiDirectory selectedItem = (PsiDirectory) comboBox.getSelectedItem();
    return selectedItem != null ? selectedItem.getVirtualFile() : null;
}
 
Example 20
Source File: DirectoryUrl.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static DirectoryUrl create(PsiDirectory directory) {
  Project project = directory.getProject();
  final VirtualFile virtualFile = directory.getVirtualFile();
  final Module module = ModuleUtil.findModuleForFile(virtualFile, project);
  return new DirectoryUrl(virtualFile.getUrl(), module != null ? module.getName() : null);
}