Search in sources :

Example 41 with PsiDirectory

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

the class CreateTestAction method invoke.

@Override
public void invoke(@NotNull final Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
    final PyFunction srcFunction = PsiTreeUtil.getParentOfType(element, PyFunction.class);
    final PyClass srcClass = PsiTreeUtil.getParentOfType(element, PyClass.class);
    if (srcClass == null && srcFunction == null)
        return;
    final PsiDirectory dir = element.getContainingFile().getContainingDirectory();
    final CreateTestDialog d = new CreateTestDialog(project);
    if (srcClass != null) {
        d.setClassName("Test" + StringUtil.capitalize(srcClass.getName()));
        d.setFileName("test_" + StringUtil.decapitalize(srcClass.getName()) + ".py");
        if (dir != null)
            d.setTargetDir(dir.getVirtualFile().getPath());
        if (srcFunction != null) {
            d.methodsSize(1);
            d.addMethod("test_" + srcFunction.getName(), 0);
        } else {
            final List<PyFunction> methods = Lists.newArrayList();
            srcClass.visitMethods(pyFunction -> {
                if (pyFunction.getName() != null && !pyFunction.getName().startsWith("__"))
                    methods.add(pyFunction);
                return true;
            }, false, null);
            d.methodsSize(methods.size());
            int i = 0;
            for (PyFunction f : methods) {
                d.addMethod("test_" + f.getName(), i);
                ++i;
            }
        }
    } else {
        d.setClassName("Test" + StringUtil.capitalize(srcFunction.getName()));
        d.setFileName("test_" + StringUtil.decapitalize(srcFunction.getName()) + ".py");
        if (dir != null)
            d.setTargetDir(dir.getVirtualFile().getPath());
        d.methodsSize(1);
        d.addMethod("test_" + srcFunction.getName(), 0);
    }
    if (!d.showAndGet()) {
        return;
    }
    CommandProcessor.getInstance().executeCommand(project, () -> {
        PsiFile e = PyTestCreator.generateTestAndNavigate(project, d);
        final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
        documentManager.commitAllDocuments();
    }, CodeInsightBundle.message("intention.create.test"), this);
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyFunction(com.jetbrains.python.psi.PyFunction) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 42 with PsiDirectory

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

the class PyToxConfigurationProducer method setupConfigurationFromContext.

@Override
protected boolean setupConfigurationFromContext(final PyToxConfiguration configuration, final ConfigurationContext context, final Ref<PsiElement> sourceElement) {
    final PsiFile file = sourceElement.get().getContainingFile();
    if (file == null) {
        return false;
    }
    final String envName = PyToxTestLocator.getEnvNameFromElement(file);
    if (envName != null) {
        configuration.setRunOnlyEnvs(envName);
        configuration.setName(String.format("Tox: run on %s", envName));
        return true;
    }
    final PsiDirectory directory = file.getContainingDirectory();
    if (directory == null) {
        return false;
    }
    configuration.setWorkingDirectory(directory.getVirtualFile().getCanonicalPath());
    if (TOX_FILE_NAME.equals(file.getName())) {
        configuration.setName("Tox");
        return true;
    }
    return false;
}
Also used : PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile)

Example 43 with PsiDirectory

use of com.intellij.psi.PsiDirectory 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 44 with PsiDirectory

use of com.intellij.psi.PsiDirectory 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 45 with PsiDirectory

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

the class PropertiesImplUtil method createByUrl.

@Nullable
public static ResourceBundle createByUrl(@NotNull final String url, @NotNull final Project project) {
    final int idx = url.lastIndexOf('/');
    if (idx == -1)
        return null;
    final String baseDirectoryName = url.substring(0, idx);
    final String baseName = url.substring(idx + 1);
    final VirtualFile baseDirectoryVirtualFile = VirtualFileManager.getInstance().findFileByUrl(baseDirectoryName);
    if (baseDirectoryVirtualFile == null) {
        return null;
    }
    final PsiDirectory baseDirectory = PsiManager.getInstance(project).findDirectory(baseDirectoryVirtualFile);
    if (baseDirectory == null) {
        return null;
    }
    final ResourceBundleManager bundleBaseNameManager = ResourceBundleManager.getInstance(project);
    for (PsiFile file : baseDirectory.getFiles()) {
        final PropertiesFile propertiesFile = getPropertiesFile(file);
        if (propertiesFile == null)
            continue;
        final String currBaseName = bundleBaseNameManager.getBaseName(file);
        if (currBaseName.equals(baseName)) {
            return getResourceBundle(propertiesFile);
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiDirectory (com.intellij.psi.PsiDirectory)321 VirtualFile (com.intellij.openapi.vfs.VirtualFile)122 PsiFile (com.intellij.psi.PsiFile)111 PsiElement (com.intellij.psi.PsiElement)79 Project (com.intellij.openapi.project.Project)73 Module (com.intellij.openapi.module.Module)68 Nullable (org.jetbrains.annotations.Nullable)62 NotNull (org.jetbrains.annotations.NotNull)50 IdeView (com.intellij.ide.IdeView)31 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)24 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)24 PsiManager (com.intellij.psi.PsiManager)24 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)20 ArrayList (java.util.ArrayList)20 PsiPackage (com.intellij.psi.PsiPackage)17 File (java.io.File)15 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)11 IncorrectOperationException (com.intellij.util.IncorrectOperationException)11 Course (com.jetbrains.edu.learning.courseFormat.Course)11 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)10