Search in sources :

Example 96 with PsiManager

use of com.intellij.psi.PsiManager in project intellij-community by JetBrains.

the class PackageViewProjectNode method getChildren.

@Override
@NotNull
public Collection<AbstractTreeNode> getChildren() {
    if (getSettings().isShowModules()) {
        final List<Module> allModules = new ArrayList<>(Arrays.asList(ModuleManager.getInstance(getProject()).getModules()));
        for (Iterator<Module> it = allModules.iterator(); it.hasNext(); ) {
            final Module module = it.next();
            final VirtualFile[] sourceRoots = ModuleRootManager.getInstance(module).getSourceRoots();
            if (sourceRoots.length == 0) {
                // do not show modules with no source roots configured
                it.remove();
            }
        }
        return modulesAndGroups(allModules.toArray(new Module[allModules.size()]));
    } else {
        final ProjectRootManager projectRootManager = ProjectRootManager.getInstance(myProject);
        final PsiManager psiManager = PsiManager.getInstance(myProject);
        final List<AbstractTreeNode> children = new ArrayList<>();
        final Set<PsiPackage> topLevelPackages = new HashSet<>();
        for (final VirtualFile root : projectRootManager.getContentSourceRoots()) {
            final PsiDirectory directory = psiManager.findDirectory(root);
            if (directory == null) {
                continue;
            }
            final PsiPackage directoryPackage = JavaDirectoryService.getInstance().getPackage(directory);
            if (directoryPackage == null || PackageUtil.isPackageDefault(directoryPackage)) {
                // add subpackages
                final PsiDirectory[] subdirectories = directory.getSubdirectories();
                for (PsiDirectory subdirectory : subdirectories) {
                    final PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(subdirectory);
                    if (aPackage != null && !PackageUtil.isPackageDefault(aPackage)) {
                        topLevelPackages.add(aPackage);
                    }
                }
                // add non-dir items
                children.addAll(ProjectViewDirectoryHelper.getInstance(myProject).getDirectoryChildren(directory, getSettings(), false));
            } else {
                // this is the case when a source root has pakage prefix assigned
                topLevelPackages.add(directoryPackage);
            }
        }
        for (final PsiPackage psiPackage : topLevelPackages) {
            PackageUtil.addPackageAsChild(children, psiPackage, null, getSettings(), false);
        }
        if (getSettings().isShowLibraryContents()) {
            children.add(new PackageViewLibrariesNode(getProject(), null, getSettings()));
        }
        return children;
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiManager(com.intellij.psi.PsiManager) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) PsiPackage(com.intellij.psi.PsiPackage) PsiDirectory(com.intellij.psi.PsiDirectory) Module(com.intellij.openapi.module.Module) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) NotNull(org.jetbrains.annotations.NotNull)

Example 97 with PsiManager

use of com.intellij.psi.PsiManager in project intellij-community by JetBrains.

the class RelaxSymbolIndex method getSymbolsByName.

public static NavigationItem[] getSymbolsByName(final String name, Project project, boolean includeNonProjectItems) {
    final GlobalSearchScope scope = includeNonProjectItems ? GlobalSearchScope.allScope(project) : GlobalSearchScope.projectScope(project);
    final Collection<NavigationItem> result = new ArrayList<>();
    PsiManager psiManager = PsiManager.getInstance(project);
    for (VirtualFile file : FileBasedIndex.getInstance().getContainingFiles(NAME, name, scope)) {
        final PsiFile psiFile = psiManager.findFile(file);
        if (psiFile instanceof XmlFile) {
            final Grammar grammar = GrammarFactory.getGrammar((XmlFile) psiFile);
            if (grammar != null) {
                grammar.acceptChildren(new CommonElement.Visitor() {

                    @Override
                    public void visitDefine(Define define) {
                        if (name.equals(define.getName())) {
                            final PsiElement psi = define.getPsiElement();
                            if (psi != null) {
                                MyNavigationItem.add((NavigationItem) define.getPsiElement(), result);
                            }
                        }
                    }
                });
            }
        }
    }
    return result.toArray(new NavigationItem[result.size()]);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlFile(com.intellij.psi.xml.XmlFile) PsiManager(com.intellij.psi.PsiManager) Grammar(org.intellij.plugins.relaxNG.model.Grammar) CommonElement(org.intellij.plugins.relaxNG.model.CommonElement) NavigationItem(com.intellij.navigation.NavigationItem) PsiElementNavigationItem(com.intellij.navigation.PsiElementNavigationItem) Define(org.intellij.plugins.relaxNG.model.Define) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

Example 98 with PsiManager

use of com.intellij.psi.PsiManager in project intellij-community by JetBrains.

the class RelaxIncludeIndex method processRelatedFiles.

private static boolean processRelatedFiles(PsiFile file, VirtualFile[] files, PsiElementProcessor<XmlFile> processor) {
    Project project = file.getProject();
    final PsiManager psiManager = PsiManager.getInstance(project);
    final PsiFile[] psiFiles = ContainerUtil.map2Array(files, PsiFile.class, (NullableFunction<VirtualFile, PsiFile>) file1 -> psiManager.findFile(file1));
    for (final PsiFile psiFile : psiFiles) {
        if (!processFile(psiFile, processor)) {
            return false;
        }
    }
    return true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileIncludeManager(com.intellij.psi.impl.include.FileIncludeManager) RncFileType(org.intellij.plugins.relaxNG.compact.RncFileType) XmlFileType(com.intellij.ide.highlighter.XmlFileType) XmlFile(com.intellij.psi.xml.XmlFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) NullableFunction(com.intellij.util.NullableFunction) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) FileType(com.intellij.openapi.fileTypes.FileType) ContainerUtil(com.intellij.util.containers.ContainerUtil) PsiManager(com.intellij.psi.PsiManager) RngGrammar(org.intellij.plugins.relaxNG.xml.dom.RngGrammar) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) DomManager(com.intellij.util.xml.DomManager) NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) PsiManager(com.intellij.psi.PsiManager) PsiFile(com.intellij.psi.PsiFile)

Example 99 with PsiManager

use of com.intellij.psi.PsiManager in project intellij-community by JetBrains.

the class RncFileReferenceManipulator method handleContentChange.

@Override
public RncFileReference handleContentChange(@NotNull RncFileReference element, @NotNull TextRange range, String newContent) throws IncorrectOperationException {
    final ASTNode node = element.getNode();
    assert node != null;
    final ASTNode literal = node.findChildByType(RncTokenTypes.LITERAL);
    if (literal != null) {
        assert range.equals(element.getReferenceRange());
        final PsiManager manager = element.getManager();
        final ASTNode newChild = RenameUtil.createLiteralNode(manager, newContent);
        literal.getTreeParent().replaceChild(literal, newChild);
    }
    return element;
}
Also used : ASTNode(com.intellij.lang.ASTNode) PsiManager(com.intellij.psi.PsiManager)

Example 100 with PsiManager

use of com.intellij.psi.PsiManager in project intellij-community by JetBrains.

the class PyTokenSeparatorGenerator method generateWhitespaceBetweenTokens.

public ASTNode generateWhitespaceBetweenTokens(ASTNode left, ASTNode right) {
    PsiManager manager = right.getTreeParent().getPsi().getManager();
    if (left.getElementType() == PyTokenTypes.END_OF_LINE_COMMENT) {
        return createLineBreak(manager);
    }
    if (left.getPsi().isValid() && right.getPsi().isValid()) {
        final PsiElement commonParent = PsiTreeUtil.findCommonParent(left.getPsi(), right.getPsi());
        if (commonParent == null)
            return null;
        final PsiElement leftPrevAncestor = PsiTreeUtil.findPrevParent(commonParent, left.getPsi());
        final PsiElement rightPrevAncestor = PsiTreeUtil.findPrevParent(commonParent, right.getPsi());
        if (isStatementOrFunction(leftPrevAncestor) && isStatementOrFunction(rightPrevAncestor)) {
            int leftIndent = PyPsiUtils.getElementIndentation(leftPrevAncestor);
            int rightIndent = PyPsiUtils.getElementIndentation(rightPrevAncestor);
            int maxIndent = Math.max(leftIndent, rightIndent);
            return createWhitespace(manager, "\n" + StringUtil.repeatSymbol(' ', maxIndent));
        }
    }
    if (right.getElementType() == PyTokenTypes.DEF_KEYWORD || right.getElementType() == PyTokenTypes.CLASS_KEYWORD) {
        return createLineBreak(manager);
    }
    if (left.getElementType() == TokenType.WHITE_SPACE || right.getElementType() == TokenType.WHITE_SPACE) {
        return null;
    }
    final PyStatement leftStatement = PsiTreeUtil.getParentOfType(left.getPsi(), PyStatement.class);
    if (leftStatement != null && !PsiTreeUtil.isAncestor(leftStatement, right.getPsi(), false)) {
        return createLineBreak(manager);
    }
    final Lexer lexer = new PythonIndentingLexer();
    if (LanguageUtil.canStickTokensTogetherByLexer(left, right, lexer) == ParserDefinition.SpaceRequirements.MUST) {
        return createSpace(manager);
    }
    return null;
}
Also used : PyStatement(com.jetbrains.python.psi.PyStatement) Lexer(com.intellij.lexer.Lexer) PythonIndentingLexer(com.jetbrains.python.lexer.PythonIndentingLexer) PythonIndentingLexer(com.jetbrains.python.lexer.PythonIndentingLexer) PsiManager(com.intellij.psi.PsiManager) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiManager (com.intellij.psi.PsiManager)120 VirtualFile (com.intellij.openapi.vfs.VirtualFile)84 PsiFile (com.intellij.psi.PsiFile)61 PsiDirectory (com.intellij.psi.PsiDirectory)31 NotNull (org.jetbrains.annotations.NotNull)26 Project (com.intellij.openapi.project.Project)20 File (java.io.File)19 PsiElement (com.intellij.psi.PsiElement)18 ArrayList (java.util.ArrayList)18 Module (com.intellij.openapi.module.Module)17 Nullable (org.jetbrains.annotations.Nullable)16 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)11 XmlFile (com.intellij.psi.xml.XmlFile)11 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)9 PsiClass (com.intellij.psi.PsiClass)7 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)6 HashSet (java.util.HashSet)6 Nullable (javax.annotation.Nullable)5 PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)4 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)4