Search in sources :

Example 11 with PsiManager

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

the class MvcModuleStructureSynchronizer method initComponent.

@Override
public void initComponent() {
    final MessageBusConnection connection = myProject.getMessageBus().connect();
    connection.subscribe(ProjectTopics.PROJECT_ROOTS, new ModuleRootListener() {

        @Override
        public void rootsChanged(ModuleRootEvent event) {
            myModificationTracker.incModificationCount();
            queue(SyncAction.SyncLibrariesInPluginsModule, myProject);
            queue(SyncAction.UpgradeFramework, myProject);
            queue(SyncAction.CreateAppStructureIfNeeded, myProject);
            queue(SyncAction.UpdateProjectStructure, myProject);
            queue(SyncAction.EnsureRunConfigurationExists, myProject);
            updateProjectViewVisibility();
        }
    });
    connection.subscribe(ProjectTopics.MODULES, new ModuleListener() {

        @Override
        public void moduleAdded(@NotNull Project project, @NotNull Module module) {
            queue(SyncAction.UpdateProjectStructure, module);
            queue(SyncAction.CreateAppStructureIfNeeded, module);
        }
    });
    connection.subscribe(VirtualFileManager.VFS_CHANGES, new BulkVirtualFileListenerAdapter(new VirtualFileAdapter() {

        @Override
        public void fileCreated(@NotNull final VirtualFileEvent event) {
            myModificationTracker.incModificationCount();
            final VirtualFile file = event.getFile();
            final String fileName = event.getFileName();
            if (MvcModuleStructureUtil.APPLICATION_PROPERTIES.equals(fileName) || isApplicationDirectoryName(fileName)) {
                queue(SyncAction.UpdateProjectStructure, file);
                queue(SyncAction.EnsureRunConfigurationExists, file);
            } else if (isLibDirectory(file) || isLibDirectory(event.getParent())) {
                queue(SyncAction.UpdateProjectStructure, file);
            } else {
                if (!myProject.isInitialized())
                    return;
                final Module module = ProjectRootManager.getInstance(myProject).getFileIndex().getModuleForFile(file);
                if (module == null) {
                    // Maybe it is creation of a plugin in plugin directory.
                    if (file.isDirectory()) {
                        if (myPluginRoots.contains(file.getParent())) {
                            queue(SyncAction.UpdateProjectStructure, myProject);
                            return;
                        }
                        if (!myOutOfModuleDirectoryCreatedActionAdded) {
                            queue(SyncAction.OutOfModuleDirectoryCreated, myProject);
                            myOutOfModuleDirectoryCreatedActionAdded = true;
                        }
                    }
                    return;
                }
                if (!MvcConsole.isUpdatingVfsByConsoleProcess(module))
                    return;
                final MvcFramework framework = MvcFramework.getInstance(module);
                if (framework == null)
                    return;
                if (framework.isToReformatOnCreation(file) || file.isDirectory()) {
                    ApplicationManager.getApplication().invokeLater(() -> {
                        if (!file.isValid())
                            return;
                        if (!framework.hasSupport(module))
                            return;
                        final List<VirtualFile> files = new ArrayList<>();
                        if (file.isDirectory()) {
                            ModuleRootManager.getInstance(module).getFileIndex().iterateContentUnderDirectory(file, new ContentIterator() {

                                @Override
                                public boolean processFile(VirtualFile fileOrDir) {
                                    if (!fileOrDir.isDirectory() && framework.isToReformatOnCreation(fileOrDir)) {
                                        files.add(file);
                                    }
                                    return true;
                                }
                            });
                        } else {
                            files.add(file);
                        }
                        PsiManager manager = PsiManager.getInstance(myProject);
                        for (VirtualFile virtualFile : files) {
                            PsiFile psiFile = manager.findFile(virtualFile);
                            if (psiFile != null) {
                                new ReformatCodeProcessor(myProject, psiFile, null, false).run();
                            }
                        }
                    }, module.getDisposed());
                }
            }
        }

        @Override
        public void fileDeleted(@NotNull VirtualFileEvent event) {
            myModificationTracker.incModificationCount();
            final VirtualFile file = event.getFile();
            if (isLibDirectory(file) || isLibDirectory(event.getParent())) {
                queue(SyncAction.UpdateProjectStructure, file);
            }
        }

        @Override
        public void contentsChanged(@NotNull VirtualFileEvent event) {
            final String fileName = event.getFileName();
            if (MvcModuleStructureUtil.APPLICATION_PROPERTIES.equals(fileName)) {
                queue(SyncAction.UpdateProjectStructure, event.getFile());
            }
        }

        @Override
        public void fileMoved(@NotNull VirtualFileMoveEvent event) {
            myModificationTracker.incModificationCount();
        }

        @Override
        public void propertyChanged(@NotNull VirtualFilePropertyEvent event) {
            if (VirtualFile.PROP_NAME.equals(event.getPropertyName())) {
                myModificationTracker.incModificationCount();
            }
        }
    }));
}
Also used : MessageBusConnection(com.intellij.util.messages.MessageBusConnection) ModuleListener(com.intellij.openapi.project.ModuleListener) PsiManager(com.intellij.psi.PsiManager) ReformatCodeProcessor(com.intellij.codeInsight.actions.ReformatCodeProcessor) NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) BulkVirtualFileListenerAdapter(com.intellij.openapi.vfs.impl.BulkVirtualFileListenerAdapter) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module)

Example 12 with PsiManager

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

the class MvcModuleStructureUtil method findApplicationProperties.

@Nullable
public static PropertiesFile findApplicationProperties(@NotNull Module module, MvcFramework framework) {
    VirtualFile root = framework.findAppRoot(module);
    if (root == null)
        return null;
    VirtualFile appChild = root.findChild(APPLICATION_PROPERTIES);
    if (appChild == null || !appChild.isValid())
        return null;
    PsiManager manager = PsiManager.getInstance(module.getProject());
    PsiFile psiFile = manager.findFile(appChild);
    if (psiFile instanceof PropertiesFile) {
        return (PropertiesFile) psiFile;
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiManager(com.intellij.psi.PsiManager) PsiFile(com.intellij.psi.PsiFile) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) Nullable(org.jetbrains.annotations.Nullable)

Example 13 with PsiManager

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

the class FileAssociationsManagerImpl method getAssociationsFor.

public PsiFile[] getAssociationsFor(PsiFile file, FileType... fileTypes) {
    final VirtualFile virtualFile = file.getVirtualFile();
    if (virtualFile == null)
        return PsiFile.EMPTY_ARRAY;
    for (VirtualFilePointer pointer : myAssociations.keySet()) {
        if (pointer.isValid() && pointer.getUrl().equals(virtualFile.getUrl())) {
            final VirtualFilePointerContainer container = myAssociations.get(pointer);
            if (container != null) {
                final VirtualFile[] files = container.getFiles();
                final Set<PsiFile> list = new HashSet<>();
                final PsiManager psiManager = PsiManager.getInstance(myProject);
                for (VirtualFile assoc : files) {
                    final PsiFile psiFile = psiManager.findFile(assoc);
                    if (psiFile != null && (fileTypes.length == 0 || matchesFileType(psiFile, fileTypes))) {
                        list.add(psiFile);
                    }
                }
                return PsiUtilCore.toPsiFileArray(list);
            } else {
                return PsiFile.EMPTY_ARRAY;
            }
        }
    }
    return PsiFile.EMPTY_ARRAY;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiManager(com.intellij.psi.PsiManager) PsiFile(com.intellij.psi.PsiFile) VirtualFilePointer(com.intellij.openapi.vfs.pointers.VirtualFilePointer) VirtualFilePointerContainer(com.intellij.openapi.vfs.pointers.VirtualFilePointerContainer)

Example 14 with PsiManager

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

the class XmlChooseColorIntentionAction method chooseColor.

public static void chooseColor(JComponent editorComponent, PsiElement element) {
    String caption = CodeInsightBundle.message("intention.color.chooser.dialog");
    final XmlAttributeValue literal = PsiTreeUtil.getParentOfType(element, XmlAttributeValue.class, false);
    if (literal == null)
        return;
    final String text = StringUtil.unquoteString(literal.getValue());
    Color oldColor;
    try {
        oldColor = Color.decode(text);
    } catch (NumberFormatException e) {
        oldColor = JBColor.GRAY;
    }
    Color color = ColorChooser.chooseColor(editorComponent, caption, oldColor, true);
    if (color == null)
        return;
    if (!Comparing.equal(color, oldColor)) {
        if (!FileModificationService.getInstance().preparePsiElementForWrite(element))
            return;
        final String newText = "#" + ColorUtil.toHex(color);
        final PsiManager manager = literal.getManager();
        final XmlAttribute newAttribute = XmlElementFactory.getInstance(manager.getProject()).createAttribute("name", newText, element);
        final Runnable replaceRunnable = () -> {
            final XmlAttributeValue valueElement = newAttribute.getValueElement();
            assert valueElement != null;
            literal.replace(valueElement);
        };
        new WriteCommandAction(element.getProject(), caption) {

            @Override
            protected void run(@NotNull Result result) throws Throwable {
                replaceRunnable.run();
            }
        }.execute();
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) XmlAttribute(com.intellij.psi.xml.XmlAttribute) JBColor(com.intellij.ui.JBColor) PsiManager(com.intellij.psi.PsiManager) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Result(com.intellij.openapi.application.Result)

Example 15 with PsiManager

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

the class FetchExtResourceAction method fetchDtd.

private void fetchDtd(final Project project, final String dtdUrl, final String url, final ProgressIndicator indicator) throws IOException {
    final String extResourcesPath = getExternalResourcesPath();
    final File extResources = new File(extResourcesPath);
    LOG.assertTrue(extResources.mkdirs() || extResources.exists(), extResources);
    final PsiManager psiManager = PsiManager.getInstance(project);
    ApplicationManager.getApplication().invokeAndWait(() -> {
        WriteAction.run(() -> {
            final String path = FileUtil.toSystemIndependentName(extResources.getAbsolutePath());
            final VirtualFile vFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(path);
            LOG.assertTrue(vFile != null, path);
        });
    });
    final List<String> downloadedResources = new LinkedList<>();
    final List<String> resourceUrls = new LinkedList<>();
    final IOException[] nestedException = new IOException[1];
    try {
        final String resPath = fetchOneFile(indicator, url, project, extResourcesPath, null);
        if (resPath == null)
            return;
        resourceUrls.add(dtdUrl);
        downloadedResources.add(resPath);
        VirtualFile virtualFile = findFileByPath(resPath, dtdUrl);
        Set<String> linksToProcess = new HashSet<>();
        Set<String> processedLinks = new HashSet<>();
        Map<String, String> baseUrls = new HashMap<>();
        VirtualFile contextFile = virtualFile;
        linksToProcess.addAll(extractEmbeddedFileReferences(virtualFile, null, psiManager, url));
        while (!linksToProcess.isEmpty()) {
            String s = linksToProcess.iterator().next();
            linksToProcess.remove(s);
            processedLinks.add(s);
            final boolean absoluteUrl = s.startsWith(HTTP_PROTOCOL);
            String resourceUrl;
            if (absoluteUrl) {
                resourceUrl = s;
            } else {
                String baseUrl = baseUrls.get(s);
                if (baseUrl == null)
                    baseUrl = url;
                resourceUrl = baseUrl.substring(0, baseUrl.lastIndexOf('/') + 1) + s;
            }
            String resourcePath;
            String refname = s.substring(s.lastIndexOf('/') + 1);
            if (absoluteUrl)
                refname = Integer.toHexString(s.hashCode()) + "_" + refname;
            try {
                resourcePath = fetchOneFile(indicator, resourceUrl, project, extResourcesPath, refname);
            } catch (IOException e) {
                nestedException[0] = new FetchingResourceIOException(e, resourceUrl);
                break;
            }
            if (resourcePath == null)
                break;
            virtualFile = findFileByPath(resourcePath, absoluteUrl ? s : null);
            downloadedResources.add(resourcePath);
            if (absoluteUrl) {
                resourceUrls.add(s);
            }
            final Set<String> newLinks = extractEmbeddedFileReferences(virtualFile, contextFile, psiManager, resourceUrl);
            for (String u : newLinks) {
                baseUrls.put(u, resourceUrl);
                if (!processedLinks.contains(u))
                    linksToProcess.add(u);
            }
        }
    } catch (IOException ex) {
        nestedException[0] = ex;
    }
    if (nestedException[0] != null) {
        cleanup(resourceUrls, downloadedResources);
        throw nestedException[0];
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiManager(com.intellij.psi.PsiManager) IOException(java.io.IOException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File)

Aggregations

PsiManager (com.intellij.psi.PsiManager)97 VirtualFile (com.intellij.openapi.vfs.VirtualFile)69 PsiFile (com.intellij.psi.PsiFile)51 NotNull (org.jetbrains.annotations.NotNull)25 PsiDirectory (com.intellij.psi.PsiDirectory)24 Project (com.intellij.openapi.project.Project)17 Module (com.intellij.openapi.module.Module)16 Nullable (org.jetbrains.annotations.Nullable)16 PsiElement (com.intellij.psi.PsiElement)15 ArrayList (java.util.ArrayList)15 File (java.io.File)14 XmlFile (com.intellij.psi.xml.XmlFile)11 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)10 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)8 PsiClass (com.intellij.psi.PsiClass)6 PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)4 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)4 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)4 IOException (java.io.IOException)4 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)4