Search in sources :

Example 76 with PsiManager

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

the class CreateHtmlDescriptionFix method createDescription.

private void createDescription(VirtualFile root) {
    if (!root.isDirectory())
        return;
    final PsiManager psiManager = PsiManager.getInstance(myModule.getProject());
    final PsiDirectory psiRoot = psiManager.findDirectory(root);
    if (psiRoot == null)
        return;
    PsiDirectory descrRoot = StreamEx.of(psiRoot.getSubdirectories()).findFirst(dir -> getDescriptionFolderName().equals(dir.getName())).orElse(null);
    try {
        descrRoot = descrRoot == null ? psiRoot.createSubdirectory(getDescriptionFolderName()) : descrRoot;
        if (isFixedDescriptionFilename()) {
            PsiDirectory dir = descrRoot.findSubdirectory(myFilename);
            if (dir == null) {
                descrRoot = descrRoot.createSubdirectory(myFilename);
            }
        }
        final FileTemplate descrTemplate = FileTemplateManager.getInstance(myModule.getProject()).getJ2eeTemplate(TEMPLATE_NAME);
        final PsiElement template = FileTemplateUtil.createFromTemplate(descrTemplate, getNewFileName(), null, descrRoot);
        if (template instanceof PsiFile) {
            final VirtualFile file = ((PsiFile) template).getVirtualFile();
            if (file != null) {
                FileEditorManager.getInstance(myModule.getProject()).openFile(file, true);
            }
        }
    } catch (Exception e) {
    //
    }
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) Arrays(java.util.Arrays) AllIcons(com.intellij.icons.AllIcons) ArrayUtil(com.intellij.util.ArrayUtil) VirtualFile(com.intellij.openapi.vfs.VirtualFile) NonNls(org.jetbrains.annotations.NonNls) DescriptionCheckerUtil(org.jetbrains.idea.devkit.inspections.DescriptionCheckerUtil) PsiManager(com.intellij.psi.PsiManager) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) JBList(com.intellij.ui.components.JBList) VfsUtilCore(com.intellij.openapi.vfs.VfsUtilCore) JavaResourceRootType(org.jetbrains.jps.model.java.JavaResourceRootType) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager) DescriptionType(org.jetbrains.idea.devkit.inspections.DescriptionType) Editor(com.intellij.openapi.editor.Editor) JBPopup(com.intellij.openapi.ui.popup.JBPopup) Iconable(com.intellij.openapi.util.Iconable) File(java.io.File) DevKitBundle(org.jetbrains.idea.devkit.DevKitBundle) List(java.util.List) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) JBPopupFactory(com.intellij.openapi.ui.popup.JBPopupFactory) StreamEx(one.util.streamex.StreamEx) FileTemplateUtil(com.intellij.ide.fileTemplates.FileTemplateUtil) LayeredIcon(com.intellij.ui.LayeredIcon) ApplicationManager(com.intellij.openapi.application.ApplicationManager) PsiDirectory(com.intellij.psi.PsiDirectory) NotNull(org.jetbrains.annotations.NotNull) JavaModuleSourceRootTypes(org.jetbrains.jps.model.java.JavaModuleSourceRootTypes) javax.swing(javax.swing) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiDirectory(com.intellij.psi.PsiDirectory) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) PsiManager(com.intellij.psi.PsiManager) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

Example 77 with PsiManager

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

the class PackageTreeExpansionMonitor method install.

public static TreeExpansionMonitor<PackageDependenciesNode> install(final JTree tree, final Project project) {
    return new TreeExpansionMonitor<PackageDependenciesNode>(tree) {

        @Override
        protected TreePath findPathByNode(final PackageDependenciesNode node) {
            if (node.getPsiElement() == null) {
                return new TreePath(node.getPath());
            }
            PsiManager manager = PsiManager.getInstance(project);
            Enumeration enumeration = ((DefaultMutableTreeNode) tree.getModel().getRoot()).breadthFirstEnumeration();
            while (enumeration.hasMoreElements()) {
                final Object nextElement = enumeration.nextElement();
                if (nextElement instanceof PackageDependenciesNode) {
                    //do not include root
                    PackageDependenciesNode child = (PackageDependenciesNode) nextElement;
                    if (manager.areElementsEquivalent(child.getPsiElement(), node.getPsiElement())) {
                        return new TreePath(child.getPath());
                    }
                }
            }
            return null;
        }
    };
}
Also used : Enumeration(java.util.Enumeration) TreePath(javax.swing.tree.TreePath) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) PsiManager(com.intellij.psi.PsiManager)

Example 78 with PsiManager

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

the class DirectoryChooser method fillList.

private void fillList(PsiDirectory[] directories, @Nullable PsiDirectory defaultSelection, Project project, String postfixToShow, Map<PsiDirectory, String> postfixes) {
    if (myView.getItemsSize() > 0) {
        myView.clearItems();
    }
    if (defaultSelection == null) {
        defaultSelection = getDefaultSelection(directories, project);
        if (defaultSelection == null && directories.length > 0) {
            defaultSelection = directories[0];
        }
    }
    int selectionIndex = -1;
    for (int i = 0; i < directories.length; i++) {
        PsiDirectory directory = directories[i];
        if (directory.equals(defaultSelection)) {
            selectionIndex = i;
            break;
        }
    }
    if (selectionIndex < 0 && directories.length == 1) {
        selectionIndex = 0;
    }
    if (selectionIndex < 0) {
        // find source root corresponding to defaultSelection
        final PsiManager manager = PsiManager.getInstance(project);
        VirtualFile[] sourceRoots = ProjectRootManager.getInstance(project).getContentSourceRoots();
        for (VirtualFile sourceRoot : sourceRoots) {
            if (sourceRoot.isDirectory()) {
                PsiDirectory directory = manager.findDirectory(sourceRoot);
                if (directory != null && isParent(defaultSelection, directory)) {
                    defaultSelection = directory;
                    break;
                }
            }
        }
    }
    int existingIdx = 0;
    for (int i = 0; i < directories.length; i++) {
        PsiDirectory directory = directories[i];
        final String postfixForDirectory;
        if (postfixes == null) {
            postfixForDirectory = postfixToShow;
        } else {
            postfixForDirectory = postfixes.get(directory);
        }
        final ItemWrapper itemWrapper = new ItemWrapper(directory, postfixForDirectory);
        myItems.add(itemWrapper);
        if (myFilterExisting) {
            if (selectionIndex == i)
                selectionIndex = -1;
            if (postfixForDirectory != null && directory.getVirtualFile().findFileByRelativePath(StringUtil.trimStart(postfixForDirectory, File.separator)) == null) {
                if (isParent(directory, defaultSelection)) {
                    myDefaultSelection = directory;
                }
                continue;
            }
        }
        myView.addItem(itemWrapper);
        if (selectionIndex < 0 && isParent(directory, defaultSelection)) {
            selectionIndex = existingIdx;
        }
        existingIdx++;
    }
    buildFragments();
    myView.listFilled();
    if (myView.getItemsSize() > 0) {
        if (selectionIndex != -1) {
            myView.selectItemByIndex(selectionIndex);
        } else {
            myView.selectItemByIndex(0);
        }
    } else {
        myView.clearSelection();
    }
    enableButtons();
    myView.getComponent().repaint();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiDirectory(com.intellij.psi.PsiDirectory) PsiManager(com.intellij.psi.PsiManager)

Example 79 with PsiManager

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

the class PlatformPackageUtil method findLongestExistingPackage.

@Nullable
private static String findLongestExistingPackage(Project project, String packageName, GlobalSearchScope scope) {
    final PsiManager manager = PsiManager.getInstance(project);
    DirectoryIndex index = DirectoryIndex.getInstance(project);
    String nameToMatch = packageName;
    while (true) {
        Query<VirtualFile> vFiles = index.getDirectoriesByPackageName(nameToMatch, false);
        PsiDirectory directory = getWritableModuleDirectory(vFiles, scope, manager);
        if (directory != null)
            return index.getPackageName(directory.getVirtualFile());
        int lastDotIndex = nameToMatch.lastIndexOf('.');
        if (lastDotIndex >= 0) {
            nameToMatch = nameToMatch.substring(0, lastDotIndex);
        } else {
            return null;
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DirectoryIndex(com.intellij.openapi.roots.impl.DirectoryIndex) PsiDirectory(com.intellij.psi.PsiDirectory) PsiManager(com.intellij.psi.PsiManager) Nullable(org.jetbrains.annotations.Nullable)

Example 80 with PsiManager

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

the class ChangeUtil method copyElement.

public static TreeElement copyElement(TreeElement original, final PsiElement context, CharTable table) {
    final TreeElement element = (TreeElement) original.clone();
    final PsiManager manager = original.getManager();
    DummyHolderFactory.createHolder(manager, element, context, table).getTreeElement();
    encodeInformation(element, original);
    TreeUtil.clearCaches(element);
    saveIndentationToCopy(original, element);
    return element;
}
Also used : PsiManager(com.intellij.psi.PsiManager)

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