Search in sources :

Example 1 with PyStatement

use of com.jetbrains.python.psi.PyStatement 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 2 with PyStatement

use of com.jetbrains.python.psi.PyStatement 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 3 with PyStatement

use of com.jetbrains.python.psi.PyStatement in project intellij-community by JetBrains.

the class PySmartEnterProcessor method collectAllElements.

private static void collectAllElements(final PsiElement element, @NotNull final List<PsiElement> result, boolean recurse) {
    result.add(0, element);
    if (doNotStep(element)) {
        if (!recurse) {
            return;
        }
        recurse = false;
    }
    final PsiElement[] children = element.getChildren();
    for (final PsiElement child : children) {
        if (element instanceof PyStatement && child instanceof PyStatement) {
            continue;
        }
        collectAllElements(child, result, recurse);
    }
}
Also used : PyStatement(com.jetbrains.python.psi.PyStatement) PsiElement(com.intellij.psi.PsiElement)

Example 4 with PyStatement

use of com.jetbrains.python.psi.PyStatement in project intellij-community by JetBrains.

the class PyCodeBlockProvider method getCodeBlockRange.

@Nullable
@Override
public TextRange getCodeBlockRange(Editor editor, PsiFile psiFile) {
    int caretOffset = editor.getCaretModel().getOffset();
    PsiElement element = psiFile.findElementAt(caretOffset);
    if (element == null) {
        return null;
    }
    while (caretOffset > 0 && element instanceof PsiWhiteSpace) {
        caretOffset--;
        element = psiFile.findElementAt(caretOffset);
    }
    PyStatement statement = PsiTreeUtil.getParentOfType(element, PyStatement.class);
    if (statement != null) {
        PyStatementList statementList = PsiTreeUtil.findChildOfType(statement, PyStatementList.class);
        // that statement list
        if (statementList == null) {
            statementList = PsiTreeUtil.getParentOfType(statement, PyStatementList.class);
            if (statementList != null) {
                statement = PsiTreeUtil.getParentOfType(statementList, PyStatement.class);
            }
        }
        if (statement != null) {
            // if we're in the beginning of the statement already, pressing Ctrl-[ again should move the caret one statement higher
            final int statementStart = statement.getTextRange().getStartOffset();
            int statementEnd = statement.getTextRange().getEndOffset();
            while (statementEnd > statementStart && psiFile.findElementAt(statementEnd) instanceof PsiWhiteSpace) {
                statementEnd--;
            }
            if (caretOffset == statementStart || caretOffset == statementEnd) {
                final PyStatement statementAbove = PsiTreeUtil.getParentOfType(statement, PyStatement.class);
                if (statementAbove != null) {
                    if (caretOffset == statementStart) {
                        return new TextRange(statementAbove.getTextRange().getStartOffset(), statementEnd);
                    } else {
                        return new TextRange(statementStart, statementAbove.getTextRange().getEndOffset());
                    }
                }
            }
            return statement.getTextRange();
        }
    }
    return null;
}
Also used : PyStatement(com.jetbrains.python.psi.PyStatement) TextRange(com.intellij.openapi.util.TextRange) PyStatementList(com.jetbrains.python.psi.PyStatementList) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with PyStatement

use of com.jetbrains.python.psi.PyStatement in project intellij-community by JetBrains.

the class PyStatementListImpl method deleteChildInternal.

@Override
public void deleteChildInternal(@NotNull ASTNode child) {
    final PsiElement childElement = child.getPsi();
    if (childElement instanceof PyStatement && getStatements().length == 1) {
        childElement.replace(PyElementGenerator.getInstance(getProject()).createPassStatement());
        return;
    }
    super.deleteChildInternal(child);
}
Also used : PyStatement(com.jetbrains.python.psi.PyStatement) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PyStatement (com.jetbrains.python.psi.PyStatement)9 PsiElement (com.intellij.psi.PsiElement)8 PyFile (com.jetbrains.python.psi.PyFile)3 Sdk (com.intellij.openapi.projectRoots.Sdk)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiDirectory (com.intellij.psi.PsiDirectory)2 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)2 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)2 PyStatementList (com.jetbrains.python.psi.PyStatementList)2 Nullable (org.jetbrains.annotations.Nullable)2 Location (com.intellij.execution.Location)1 ASTNode (com.intellij.lang.ASTNode)1 Lexer (com.intellij.lexer.Lexer)1 Module (com.intellij.openapi.module.Module)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiManager (com.intellij.psi.PsiManager)1 CodeStyleManager (com.intellij.psi.codeStyle.CodeStyleManager)1 PythonIndentingLexer (com.jetbrains.python.lexer.PythonIndentingLexer)1 File (java.io.File)1