Search in sources :

Example 6 with PsiFileSystemItem

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

the class PyTestConfigurationProducer method setupConfigurationFromContext.

@Override
protected boolean setupConfigurationFromContext(AbstractPythonLegacyTestRunConfiguration<PyTestRunConfiguration> configuration, ConfigurationContext context, Ref<PsiElement> sourceElement) {
    final PsiElement element = sourceElement.get();
    final Module module = ModuleUtilCore.findModuleForPsiElement(element);
    if (!(configuration instanceof PyTestRunConfiguration)) {
        return false;
    }
    if (module == null) {
        return false;
    }
    if (!(TestRunnerService.getInstance(module).getProjectConfiguration().equals(PythonTestConfigurationsModel.PY_TEST_NAME))) {
        return false;
    }
    final PsiFileSystemItem file = element instanceof PsiDirectory ? (PsiDirectory) element : element.getContainingFile();
    if (file == null) {
        return false;
    }
    final VirtualFile virtualFile = file.getVirtualFile();
    if (virtualFile == null) {
        return false;
    }
    if (file instanceof PyFile || file instanceof PsiDirectory) {
        final List<PyStatement> testCases = PyTestUtil.getPyTestCasesFromFile(file, TypeEvalContext.userInitiated(element.getProject(), element.getContainingFile()));
        if (testCases.isEmpty()) {
            return false;
        }
    } else {
        return false;
    }
    final Sdk sdk = PythonSdkType.findPythonSdk(context.getModule());
    if (sdk == null) {
        return false;
    }
    configuration.setUseModuleSdk(true);
    configuration.setModule(ModuleUtilCore.findModuleForPsiElement(element));
    ((PyTestRunConfiguration) configuration).setTestToRun(virtualFile.getPath());
    final String keywords = getKeywords(element, sdk);
    if (keywords != null) {
        ((PyTestRunConfiguration) configuration).useKeyword(true);
        ((PyTestRunConfiguration) configuration).setKeywords(keywords);
        configuration.setName("py.test in " + keywords);
    } else {
        configuration.setName("py.test in " + file.getName());
    }
    return true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PyStatement(com.jetbrains.python.psi.PyStatement) PsiDirectory(com.intellij.psi.PsiDirectory) PyFile(com.jetbrains.python.psi.PyFile) Sdk(com.intellij.openapi.projectRoots.Sdk) Module(com.intellij.openapi.module.Module) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) PsiElement(com.intellij.psi.PsiElement)

Example 7 with PsiFileSystemItem

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

the class PyTestConfigurationProducer method isConfigurationFromContext.

@Override
public boolean isConfigurationFromContext(AbstractPythonLegacyTestRunConfiguration configuration, ConfigurationContext context) {
    final Location location = context.getLocation();
    if (location == null)
        return false;
    if (!(configuration instanceof PyTestRunConfiguration))
        return false;
    final PsiElement element = location.getPsiElement();
    final PsiFileSystemItem file = element instanceof PsiDirectory ? (PsiDirectory) element : element.getContainingFile();
    if (file == null)
        return false;
    final VirtualFile virtualFile = file.getVirtualFile();
    if (virtualFile == null)
        return false;
    if (file instanceof PyFile || file instanceof PsiDirectory) {
        final List<PyStatement> testCases = PyTestUtil.getPyTestCasesFromFile(file, TypeEvalContext.userInitiated(element.getProject(), element.getContainingFile()));
        if (testCases.isEmpty())
            return false;
    } else {
        return false;
    }
    final Sdk sdk = PythonSdkType.findPythonSdk(context.getModule());
    if (sdk == null)
        return false;
    final String keywords = getKeywords(element, sdk);
    final String scriptName = ((PyTestRunConfiguration) configuration).getTestToRun();
    final String workingDirectory = configuration.getWorkingDirectory();
    final String path = virtualFile.getPath();
    final boolean isTestFileEquals = scriptName.equals(path) || path.equals(new File(workingDirectory, scriptName).getAbsolutePath());
    final String configurationKeywords = ((PyTestRunConfiguration) configuration).getKeywords();
    return isTestFileEquals && (configurationKeywords.equals(keywords) || StringUtil.isEmptyOrSpaces(((PyTestRunConfiguration) configuration).getKeywords()) && keywords == null);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PyFile(com.jetbrains.python.psi.PyFile) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) PyStatement(com.jetbrains.python.psi.PyStatement) PsiDirectory(com.intellij.psi.PsiDirectory) Sdk(com.intellij.openapi.projectRoots.Sdk) PyFile(com.jetbrains.python.psi.PyFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) PsiElement(com.intellij.psi.PsiElement) Location(com.intellij.execution.Location)

Example 8 with PsiFileSystemItem

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

the class PyFindUsagesHandlerFactory method createFindUsagesHandler.

@Nullable
@Override
public FindUsagesHandler createFindUsagesHandler(@NotNull PsiElement element, boolean forHighlightUsages) {
    if (element instanceof PyImportedModule) {
        final PsiElement resolved = ((PyImportedModule) element).resolve();
        if (resolved != null) {
            element = resolved;
        }
    }
    if (element instanceof PsiFileSystemItem) {
        return new PyModuleFindUsagesHandler((PsiFileSystemItem) element);
    }
    if (element instanceof PyFunction) {
        if (!forHighlightUsages) {
            TypeEvalContext context = TypeEvalContext.userInitiated(element.getProject(), null);
            final Collection<PsiElement> superMethods = PySuperMethodsSearch.search((PyFunction) element, true, context).findAll();
            if (superMethods.size() > 0) {
                final PsiElement next = superMethods.iterator().next();
                // TODO should do this for Jython functions overriding Java methods too
                if (next instanceof PyFunction && !isInObject((PyFunction) next)) {
                    int rc = Messages.showYesNoDialog(element.getProject(), "Method " + ((PyFunction) element).getName() + " overrides method of class " + ((PyFunction) next).getContainingClass().getName() + ".\nDo you want to find usages of the base method?", "Find Usages", Messages.getQuestionIcon());
                    if (rc == Messages.YES) {
                        List<PsiElement> allMethods = new ArrayList<>();
                        allMethods.add(element);
                        allMethods.addAll(superMethods);
                        return new PyFunctionFindUsagesHandler(element, allMethods);
                    } else {
                        return new PyFunctionFindUsagesHandler(element);
                    }
                }
            }
        }
        return new PyFunctionFindUsagesHandler(element);
    }
    if (element instanceof PyClass) {
        return new PyClassFindUsagesHandler((PyClass) element);
    }
    if (element instanceof PyTargetExpression) {
        return new PyTargetExpressionFindUsagesHandler(((PyTargetExpression) element));
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) PyImportedModule(com.jetbrains.python.psi.impl.PyImportedModule) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with PsiFileSystemItem

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

the class DissociateResourceBundleAction method dissociate.

public static void dissociate(final Collection<ResourceBundle> resourceBundles, final Project project) {
    final Set<PsiFileSystemItem> toUpdateInProjectView = new HashSet<>();
    for (ResourceBundle resourceBundle : resourceBundles) {
        for (final PropertiesFile propertiesFile : resourceBundle.getPropertiesFiles()) {
            PsiDirectory containingDirectory = propertiesFile.getContainingFile().getContainingDirectory();
            if (containingDirectory != null) {
                toUpdateInProjectView.add(containingDirectory);
            }
        }
        ResourceBundleManager.getInstance(project).dissociateResourceBundle(resourceBundle);
    }
    AbstractProjectViewPane currentProjectViewPane = ProjectView.getInstance(project).getCurrentProjectViewPane();
    if (currentProjectViewPane == null) {
        return;
    }
    AbstractTreeBuilder treeBuilder = currentProjectViewPane.getTreeBuilder();
    if (treeBuilder != null) {
        for (PsiFileSystemItem item : toUpdateInProjectView) {
            treeBuilder.queueUpdateFrom(item, false);
        }
    }
}
Also used : PsiDirectory(com.intellij.psi.PsiDirectory) AbstractTreeBuilder(com.intellij.ide.util.treeView.AbstractTreeBuilder) ResourceBundle(com.intellij.lang.properties.ResourceBundle) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) AbstractProjectViewPane(com.intellij.ide.projectView.impl.AbstractProjectViewPane) HashSet(com.intellij.util.containers.HashSet)

Example 10 with PsiFileSystemItem

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

the class CreatePackageAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    final IdeView view = e.getData(LangDataKeys.IDE_VIEW);
    if (view == null) {
        return;
    }
    final Project project = e.getData(CommonDataKeys.PROJECT);
    final PsiDirectory directory = DirectoryChooserUtil.getOrChooseDirectory(view);
    if (directory == null)
        return;
    final CreateDirectoryOrPackageHandler validator = new CreateDirectoryOrPackageHandler(project, directory, false, ".") {

        @Override
        protected void createDirectories(String subDirName) {
            super.createDirectories(subDirName);
            PsiFileSystemItem element = getCreatedElement();
            if (element instanceof PsiDirectory) {
                createInitPyInHierarchy((PsiDirectory) element, directory);
            }
        }
    };
    Messages.showInputDialog(project, IdeBundle.message("prompt.enter.new.package.name"), IdeBundle.message("title.new.package"), Messages.getQuestionIcon(), "", validator);
    final PsiFileSystemItem result = validator.getCreatedElement();
    if (result != null) {
        view.selectElement(result);
    }
}
Also used : Project(com.intellij.openapi.project.Project) PsiDirectory(com.intellij.psi.PsiDirectory) CreateDirectoryOrPackageHandler(com.intellij.ide.actions.CreateDirectoryOrPackageHandler) IdeView(com.intellij.ide.IdeView) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem)

Aggregations

PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)60 VirtualFile (com.intellij.openapi.vfs.VirtualFile)31 Nullable (org.jetbrains.annotations.Nullable)20 PsiElement (com.intellij.psi.PsiElement)18 PsiFile (com.intellij.psi.PsiFile)18 Project (com.intellij.openapi.project.Project)14 PsiDirectory (com.intellij.psi.PsiDirectory)14 NotNull (org.jetbrains.annotations.NotNull)8 Module (com.intellij.openapi.module.Module)7 File (java.io.File)6 PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)5 ArrayList (java.util.ArrayList)5 PsiManager (com.intellij.psi.PsiManager)4 IProperty (com.intellij.lang.properties.IProperty)3 LookupElement (com.intellij.codeInsight.lookup.LookupElement)2 Sdk (com.intellij.openapi.projectRoots.Sdk)2 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)2 SearchScope (com.intellij.psi.search.SearchScope)2 QualifiedName (com.intellij.psi.util.QualifiedName)2 XmlFile (com.intellij.psi.xml.XmlFile)2