Search in sources :

Example 1 with PyStatementList

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

the class PyMainPostfixTemplate method getSurrounder.

@NotNull
@Override
protected Surrounder getSurrounder() {
    return new PyStatementSurrounder() {

        @Nullable
        @Override
        protected TextRange surroundStatement(@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement[] elements) throws IncorrectOperationException {
            PyIfStatement ifStatement = PyElementGenerator.getInstance(project).createFromText(LanguageLevel.forElement(elements[0]), PyIfStatement.class, "if __name__ == '__main__':\n expr");
            ifStatement = (PyIfStatement) CodeStyleManager.getInstance(project).reformat(ifStatement);
            final PsiElement parent = elements[0].getParent();
            ifStatement = (PyIfStatement) parent.addBefore(ifStatement, elements[0]);
            final PyStatementList statementList = ifStatement.getIfPart().getStatementList();
            statementList.addRange(elements[0], elements[elements.length - 1]);
            statementList.getFirstChild().delete();
            parent.deleteChildRange(elements[0], elements[elements.length - 1]);
            return TextRange.from(statementList.getTextRange().getEndOffset(), 0);
        }

        @Override
        public String getTemplateDescription() {
            return DESCR;
        }
    };
}
Also used : PyStatementSurrounder(com.jetbrains.python.refactoring.surround.surrounders.statements.PyStatementSurrounder) Project(com.intellij.openapi.project.Project) PyIfStatement(com.jetbrains.python.psi.PyIfStatement) PyStatementList(com.jetbrains.python.psi.PyStatementList) Editor(com.intellij.openapi.editor.Editor) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PyStatementList

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

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

the class PyWithIfElseSurrounder method surroundStatement.

@Override
@Nullable
protected TextRange surroundStatement(@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement[] elements) throws IncorrectOperationException {
    PyIfStatement ifStatement = PyElementGenerator.getInstance(project).createFromText(LanguageLevel.getDefault(), PyIfStatement.class, "if True:\n    pass\nelse:    pass\n");
    final PsiElement parent = elements[0].getParent();
    final PyStatementList statementList = ifStatement.getIfPart().getStatementList();
    statementList.addRange(elements[0], elements[elements.length - 1]);
    statementList.deleteChildRange(statementList.getFirstChild(), statementList.getFirstChild());
    ifStatement = (PyIfStatement) parent.addBefore(ifStatement, elements[0]);
    parent.deleteChildRange(elements[0], elements[elements.length - 1]);
    ifStatement = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(ifStatement);
    if (ifStatement == null) {
        return null;
    }
    return ifStatement.getTextRange();
}
Also used : PyIfStatement(com.jetbrains.python.psi.PyIfStatement) PyStatementList(com.jetbrains.python.psi.PyStatementList) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with PyStatementList

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

the class PyWithTryFinallySurrounder method getResultRange.

@Override
protected TextRange getResultRange(PyTryExceptStatement tryStatement) {
    final PyFinallyPart finallyPart = tryStatement.getFinallyPart();
    assert finallyPart != null;
    final PyStatementList statementList = finallyPart.getStatementList();
    return statementList.getTextRange();
}
Also used : PyFinallyPart(com.jetbrains.python.psi.PyFinallyPart) PyStatementList(com.jetbrains.python.psi.PyStatementList)

Example 5 with PyStatementList

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

the class PySmartEnterProcessor method getStatementAtCaret.

@Nullable
protected PsiElement getStatementAtCaret(Editor editor, PsiFile psiFile) {
    PsiElement statementAtCaret = super.getStatementAtCaret(editor, psiFile);
    if (statementAtCaret instanceof PsiWhiteSpace) {
        return null;
    }
    if (statementAtCaret == null) {
        return null;
    }
    final PyStatementList statementList = PsiTreeUtil.getParentOfType(statementAtCaret, PyStatementList.class, false);
    if (statementList != null) {
        for (PyStatement statement : statementList.getStatements()) {
            if (PsiTreeUtil.isAncestor(statement, statementAtCaret, true)) {
                return statement;
            }
        }
    } else {
        final PyFile file = PsiTreeUtil.getParentOfType(statementAtCaret, PyFile.class, false);
        if (file != null) {
            for (PyStatement statement : file.getStatements()) {
                if (PsiTreeUtil.isAncestor(statement, statementAtCaret, true)) {
                    return statement;
                }
            }
        }
    }
    return null;
}
Also used : PyStatement(com.jetbrains.python.psi.PyStatement) PyStatementList(com.jetbrains.python.psi.PyStatementList) PyFile(com.jetbrains.python.psi.PyFile) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PyStatementList (com.jetbrains.python.psi.PyStatementList)6 PsiElement (com.intellij.psi.PsiElement)4 Nullable (org.jetbrains.annotations.Nullable)3 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)2 PyIfStatement (com.jetbrains.python.psi.PyIfStatement)2 PyStatement (com.jetbrains.python.psi.PyStatement)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 Editor (com.intellij.openapi.editor.Editor)1 Project (com.intellij.openapi.project.Project)1 TextRange (com.intellij.openapi.util.TextRange)1 PyElementGenerator (com.jetbrains.python.psi.PyElementGenerator)1 PyFile (com.jetbrains.python.psi.PyFile)1 PyFinallyPart (com.jetbrains.python.psi.PyFinallyPart)1 PyFunction (com.jetbrains.python.psi.PyFunction)1 PyStatementSurrounder (com.jetbrains.python.refactoring.surround.surrounders.statements.PyStatementSurrounder)1 NotNull (org.jetbrains.annotations.NotNull)1