Java Code Examples for com.intellij.psi.PsiFile#getContainingDirectory()

The following examples show how to use com.intellij.psi.PsiFile#getContainingDirectory() . 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: BlazeResourcesDomFileDescription.java    From intellij with Apache License 2.0 6 votes vote down vote up
/** Only check that the file is under res/values or res/values-*. */
private static boolean isBlazeResourcesFile(PsiFile file) {
  if (!Blaze.isBlazeProject(file.getProject())) {
    return false;
  }
  file = file.getOriginalFile();
  PsiDirectory parent = file.getContainingDirectory();
  if (parent == null) {
    return false;
  }
  String parentName = parent.getName();
  if (!parentName.equals(FD_RES_VALUES) && !parentName.startsWith(FD_RES_VALUES + '-')) {
    return false;
  }
  PsiDirectory grandParent = parent.getParentDirectory();
  return grandParent != null && grandParent.getName().equals(FD_RES);
}
 
Example 2
Source File: YamlElementPatternHelper.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Override
public boolean accepts(@NotNull PsiFile psiFile, ProcessingContext processingContext) {
    if (psiFile.getFileType() != YAMLFileType.YML) {
        return false;
    }

    if (psiFile.getName().matches("(security|config).*\\.(yml|yaml)")) {
        return true;
    }

    // psiFile.virtualFile is empty; check via folder structure
    PsiDirectory containingDirectory = psiFile.getContainingDirectory();
    if (containingDirectory == null) {
        return false;
    }

    if ("packages".equals(containingDirectory.getName())) {
        return true;
    }

    VirtualFile virtualDirectoryFile = containingDirectory.getVirtualFile();

    String relativePath = VfsExUtil.getRelativeProjectPath(psiFile.getProject(), virtualDirectoryFile);
    return relativePath != null && relativePath.contains("config/packages/");
}
 
Example 3
Source File: FileResourceUtil.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
/**
 * Gives targets to files which relative to current file directory
 */
@NotNull
public static Collection<PsiFile> getFileResourceTargetsInDirectoryScope(@NotNull PsiFile psiFile, @NotNull String content) {

    // bundle scope
    if(content.startsWith("@")) {
        return Collections.emptyList();
    }

    PsiDirectory containingDirectory = psiFile.getContainingDirectory();
    if(containingDirectory == null) {
        return Collections.emptyList();
    }

    VirtualFile relativeFile = VfsUtil.findRelativeFile(content, containingDirectory.getVirtualFile());
    if(relativeFile == null) {
        return Collections.emptyList();
    }

    PsiFile targetFile = PsiElementUtils.virtualFileToPsiFile(psiFile.getProject(), relativeFile);
    if(targetFile == null) {
        return Collections.emptyList();
    }

    return Collections.singletonList(targetFile);
}
 
Example 4
Source File: NeosUtil.java    From intellij-neos with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Collect all parent directories until a directory named "Fusion" is found.
 * If none is found, return an empty array.
 */
public static ArrayList<PsiDirectory> getParentFusionDirectories(PsiDirectory currentDirectory) {
    if (currentDirectory == null) {
        return null;
    }

    PsiFile composerManifest = ComposerUtil.getComposerManifest(currentDirectory);
    PsiDirectory packageDirectory = null;
    if (composerManifest != null) {
        packageDirectory = composerManifest.getContainingDirectory();
    }

    ArrayList<PsiDirectory> parentDirectories = new ArrayList<>();
    boolean fusionDirectoryExists = false;

    do {
        parentDirectories.add(currentDirectory);
        currentDirectory = currentDirectory.getParentDirectory();

        if (currentDirectory != null && currentDirectory.getName().equals("Fusion")) {
            fusionDirectoryExists = true;
        }
    } while (currentDirectory != null
            && !currentDirectory.getName().equals("Fusion")
            && !currentDirectory.equals(packageDirectory)
            && !currentDirectory.equals(guessProjectDir(currentDirectory.getProject())));

    if (!fusionDirectoryExists) {
        return new ArrayList<>();
    }

    return parentDirectories;
}
 
Example 5
Source File: YamlCompletionContributor.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Override
protected void addCompletions(@NotNull CompletionParameters completionParameters, final ProcessingContext processingContext, @NotNull final CompletionResultSet completionResultSet) {

    PsiFile originalFile = completionParameters.getOriginalFile();
    if(!Symfony2ProjectComponent.isEnabled(originalFile)) {
        return;
    }

    final PsiDirectory containingDirectory = originalFile.getContainingDirectory();
    if (containingDirectory == null) {
        return;
    }

    final VirtualFile containingDirectoryFiles = containingDirectory.getVirtualFile();
    VfsUtil.visitChildrenRecursively(containingDirectoryFiles, new VirtualFileVisitor() {
        @Override
        public boolean visitFile(@NotNull VirtualFile file) {

            String relativePath = VfsUtil.getRelativePath(file, containingDirectoryFiles, '/');
            if (relativePath == null) {
                return super.visitFile(file);
            }

            completionResultSet.addElement(LookupElementBuilder.create(relativePath).withIcon(file.getFileType().getIcon()));

            return super.visitFile(file);
        }
    });

}
 
Example 6
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 7
Source File: DirectoryCoverageViewExtension.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public PsiElement getParentElement(PsiElement element) {
  final PsiFile containingFile = element.getContainingFile();
  if (containingFile != null) {
    return containingFile.getContainingDirectory();
  }
  return null;
}
 
Example 8
Source File: DirectoryNode.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean canSelectInLeftTree(final Map<PsiFile, Set<PsiFile>> deps) {
  Set<PsiFile> files = deps.keySet();
  for (PsiFile file : files) {
    if (file.getContainingDirectory() == getPsiDirectory()) {
      return true;
    }
  }
  return false;
}
 
Example 9
Source File: CopyElementAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(AnActionEvent e) {
  final DataContext dataContext = e.getDataContext();
  final Project project = dataContext.getData(CommonDataKeys.PROJECT);
  if (project == null) {
    return;
  }

  CommandProcessor.getInstance().executeCommand(project, new Runnable() {
    @Override
    public void run() {
      PsiDocumentManager.getInstance(project).commitAllDocuments();
    }
  }, "", null);
  final Editor editor = dataContext.getData(PlatformDataKeys.EDITOR);
  PsiElement[] elements;

  PsiDirectory defaultTargetDirectory;
  if (editor != null) {
    PsiElement aElement = getTargetElement(editor, project);
    PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
    if (file == null) return;
    elements = new PsiElement[]{aElement};
    if (aElement == null || !CopyHandler.canCopy(elements)) {
      elements = new PsiElement[]{file};
    }
    defaultTargetDirectory = file.getContainingDirectory();
  }
  else {
    PsiElement element = dataContext.getData(LangDataKeys.TARGET_PSI_ELEMENT);
    defaultTargetDirectory = element instanceof PsiDirectory ? (PsiDirectory)element : null;
    elements = dataContext.getData(LangDataKeys.PSI_ELEMENT_ARRAY);
  }
  doCopy(elements, defaultTargetDirectory);
}
 
Example 10
Source File: PlatformPackageUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static PsiDirectory getDirectory(@Nullable PsiElement element) {
  if (element == null) return null;
  // handle injection and fragment editor
  PsiFile file = FileContextUtil.getContextFile(element);
  return file == null ? null : file.getContainingDirectory();
}
 
Example 11
Source File: DirectoryChooser.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public PsiDirectory getSelectedDirectory() {
  if (mySelection != null) {
    final PsiFile file = mySelection.getContainingFile();
    if (file != null){
      return file.getContainingDirectory();
    }
  }
  ItemWrapper wrapper = myView.getSelectedItem();
  if (wrapper == null) return null;
  return wrapper.myDirectory;
}