Search in sources :

Example 6 with PsiManager

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

the class PyExtractSuperclassTest method testMultifileAppend.

public void testMultifileAppend() {
    // this is half-copy-paste of testMultifileNew. generalization won't make either easier to follow.
    String baseName = "/refactoring/extractsuperclass/multifile/";
    myFixture.configureByFiles(baseName + "source.py", baseName + "a/__init__.py", baseName + "a/b/__init__.py", baseName + "a/b/foo.py");
    final String className = "Foo";
    final String superclassName = "Suppa";
    final PyClass clazz = findClass(className);
    final List<PyMemberInfo<PyElement>> members = new ArrayList<>();
    final PyElement member = findMember(className, ".foo");
    members.add(MembersManager.findMember(clazz, member));
    final VirtualFile base_dir = myFixture.getFile().getVirtualFile().getParent();
    new WriteCommandAction.Simple(myFixture.getProject()) {

        @Override
        protected void run() throws Throwable {
            //TODO: Test via presenter
            //noinspection ConstantConditions
            final String path = base_dir.getPath() + "/a/b";
            PyExtractSuperclassHelper.extractSuperclass(clazz, members, superclassName, path + "/foo.py");
        }
    }.execute();
    final PsiManager psi_mgr = PsiManager.getInstance(myFixture.getProject());
    VirtualFile vfile = base_dir.findChild("a");
    assertTrue(vfile.isDirectory());
    vfile = vfile.findChild(PyNames.INIT_DOT_PY);
    assertNotNull(vfile);
    vfile = base_dir.findChild("a").findChild("b");
    assertTrue(vfile.isDirectory());
    assertNotNull(vfile.findChild(PyNames.INIT_DOT_PY));
    vfile = vfile.findChild("foo.py");
    assertNotNull(vfile);
    PsiFile psi_file = psi_mgr.findFile(vfile);
    String result = psi_file.getText().trim();
    File expected_file = new File(getTestDataPath() + baseName, "target.append.py");
    String expected = psi_mgr.findFile(LocalFileSystem.getInstance().findFileByIoFile(expected_file)).getText().trim();
    assertEquals(expected, result);
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) VirtualFile(com.intellij.openapi.vfs.VirtualFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) ArrayList(java.util.ArrayList) PyMemberInfo(com.jetbrains.python.refactoring.classes.membersManager.PyMemberInfo) PsiManager(com.intellij.psi.PsiManager) PsiFile(com.intellij.psi.PsiFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) PsiFile(com.intellij.psi.PsiFile) PyElement(com.jetbrains.python.psi.PyElement)

Example 7 with PsiManager

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

the class InsertComponentProcessor method checkAddDependencyOnInsert.

private boolean checkAddDependencyOnInsert(final ComponentItem item) {
    if (item.getClassName().equals(HSpacer.class.getName()) || item.getClassName().equals(VSpacer.class.getName())) {
        // this is mostly required for IDEA developers, so that developers don't receive prompt to offer ui-designer-impl dependency
        return true;
    }
    PsiManager manager = PsiManager.getInstance(myEditor.getProject());
    final GlobalSearchScope projectScope = GlobalSearchScope.allScope(myEditor.getProject());
    final GlobalSearchScope moduleScope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(myEditor.getModule());
    final PsiClass componentClass = JavaPsiFacade.getInstance(manager.getProject()).findClass(item.getClassName(), projectScope);
    if (componentClass != null && JavaPsiFacade.getInstance(manager.getProject()).findClass(item.getClassName(), moduleScope) == null) {
        final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myEditor.getProject()).getFileIndex();
        List<OrderEntry> entries = fileIndex.getOrderEntriesForFile(componentClass.getContainingFile().getVirtualFile());
        if (entries.size() > 0) {
            if (entries.get(0) instanceof ModuleSourceOrderEntry) {
                if (!checkAddModuleDependency(item, (ModuleSourceOrderEntry) entries.get(0)))
                    return false;
            } else if (entries.get(0) instanceof LibraryOrderEntry) {
                if (!checkAddLibraryDependency(item, (LibraryOrderEntry) entries.get(0)))
                    return false;
            }
        }
    }
    return true;
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiClass(com.intellij.psi.PsiClass) PsiManager(com.intellij.psi.PsiManager)

Example 8 with PsiManager

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

the class Java15FormInspection method checkComponentProperties.

protected void checkComponentProperties(Module module, final IComponent component, final FormErrorCollector collector) {
    final GlobalSearchScope scope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module);
    final PsiManager psiManager = PsiManager.getInstance(module.getProject());
    final PsiClass aClass = JavaPsiFacade.getInstance(psiManager.getProject()).findClass(component.getComponentClassName(), scope);
    if (aClass == null) {
        return;
    }
    for (final IProperty prop : component.getModifiedProperties()) {
        final PsiMethod getter = PropertyUtil.findPropertyGetter(aClass, prop.getName(), false, true);
        if (getter == null)
            continue;
        final LanguageLevel languageLevel = LanguageLevelUtil.getEffectiveLanguageLevel(module);
        if (Java15APIUsageInspection.getLastIncompatibleLanguageLevel(getter, languageLevel) != null) {
            registerError(component, collector, prop, "@since " + Java15APIUsageInspection.getShortName(languageLevel));
        }
    }
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) IProperty(com.intellij.uiDesigner.lw.IProperty) PsiMethod(com.intellij.psi.PsiMethod) LanguageLevel(com.intellij.pom.java.LanguageLevel) PsiClass(com.intellij.psi.PsiClass) PsiManager(com.intellij.psi.PsiManager)

Example 9 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 10 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)

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