Search in sources :

Example 1 with PyIfStatement

use of com.jetbrains.python.psi.PyIfStatement 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 PyIfStatement

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

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

the class PyElseUnwrapper method collectElementsToIgnore.

@Override
public void collectElementsToIgnore(PsiElement element, Set<PsiElement> result) {
    PsiElement parent = element.getParent();
    while (parent instanceof PyIfStatement) {
        result.add(parent);
        parent = parent.getParent();
    }
}
Also used : PyIfStatement(com.jetbrains.python.psi.PyIfStatement) PsiElement(com.intellij.psi.PsiElement)

Example 4 with PyIfStatement

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

the class PyElseUnwrapperBase method doUnwrap.

@Override
protected void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException {
    PyElement elseBranch;
    if (element instanceof PyIfStatement && ((PyIfStatement) element).getElsePart() != null) {
        elseBranch = ((PyIfStatement) element).getElsePart();
    } else {
        elseBranch = (PyElement) element;
    }
    unwrapElseBranch(elseBranch, element.getParent(), context);
}
Also used : PyIfStatement(com.jetbrains.python.psi.PyIfStatement) PyElement(com.jetbrains.python.psi.PyElement)

Aggregations

PyIfStatement (com.jetbrains.python.psi.PyIfStatement)4 PsiElement (com.intellij.psi.PsiElement)3 PyStatementList (com.jetbrains.python.psi.PyStatementList)2 Editor (com.intellij.openapi.editor.Editor)1 Project (com.intellij.openapi.project.Project)1 PyElement (com.jetbrains.python.psi.PyElement)1 PyStatementSurrounder (com.jetbrains.python.refactoring.surround.surrounders.statements.PyStatementSurrounder)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1