diff --git a/python/resources/postfixTemplates/PyIfPostfixTemplate/after.py.template b/python/resources/postfixTemplates/PyIfPostfixTemplate/after.py.template
new file mode 100644
index 000000000000..ae09d099e2d6
--- /dev/null
+++ b/python/resources/postfixTemplates/PyIfPostfixTemplate/after.py.template
@@ -0,0 +1,3 @@
+def f(a):
+ if a:
+
\ No newline at end of file
diff --git a/python/resources/postfixTemplates/PyIfPostfixTemplate/before.py.template b/python/resources/postfixTemplates/PyIfPostfixTemplate/before.py.template
new file mode 100644
index 000000000000..4c3ad81dba6b
--- /dev/null
+++ b/python/resources/postfixTemplates/PyIfPostfixTemplate/before.py.template
@@ -0,0 +1,2 @@
+def f(a):
+ a.if
\ No newline at end of file
diff --git a/python/resources/postfixTemplates/PyIfPostfixTemplate/description.html b/python/resources/postfixTemplates/PyIfPostfixTemplate/description.html
new file mode 100644
index 000000000000..247b98e3e3b5
--- /dev/null
+++ b/python/resources/postfixTemplates/PyIfPostfixTemplate/description.html
@@ -0,0 +1,5 @@
+
+
+Uses the expression as condition in 'if' statement.
+
+
\ No newline at end of file
diff --git a/python/src/com/jetbrains/python/codeInsight/postfix/PyIfPostfixTemplate.java b/python/src/com/jetbrains/python/codeInsight/postfix/PyIfPostfixTemplate.java
new file mode 100644
index 000000000000..64061799f81c
--- /dev/null
+++ b/python/src/com/jetbrains/python/codeInsight/postfix/PyIfPostfixTemplate.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2000-2016 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.jetbrains.python.codeInsight.postfix;
+
+import com.intellij.codeInsight.template.postfix.templates.SurroundPostfixTemplateBase;
+import com.intellij.lang.surroundWith.Surrounder;
+import com.intellij.openapi.editor.Editor;
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.util.TextRange;
+import com.intellij.psi.PsiDocumentManager;
+import com.intellij.psi.PsiElement;
+import com.intellij.psi.codeStyle.CodeStyleManager;
+import com.intellij.util.IncorrectOperationException;
+import com.jetbrains.python.psi.*;
+import com.jetbrains.python.refactoring.surround.surrounders.statements.PyStatementSurrounder;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+public class PyIfPostfixTemplate extends SurroundPostfixTemplateBase {
+
+ public static final String TEMPLATE_DESCRIPTION = "if expr";
+
+ public PyIfPostfixTemplate() {
+ super("if", TEMPLATE_DESCRIPTION, PyPostfixUtils.PY_PSI_INFO, PyPostfixUtils.selectorTopmost());
+ }
+
+ @NotNull
+ @Override
+ protected Surrounder getSurrounder() {
+ return new PyIfSurrounder();
+ }
+
+ private static class PyIfSurrounder extends PyStatementSurrounder {
+
+ @Nullable
+ @Override
+ protected TextRange surroundStatement(@NotNull Project project, @NotNull final Editor editor, @NotNull PsiElement[] elements)
+ throws IncorrectOperationException {
+ String text = "if a:\n pass";
+ PyIfStatement ifStatement = PyElementGenerator.getInstance(project).
+ createFromText(LanguageLevel.getDefault(), PyIfStatement.class, text);
+ final PsiElement element = elements[0];
+ final PyExpression condition = ifStatement.getIfPart().getCondition();
+ if (condition != null) {
+ condition.replace(element);
+ }
+ ifStatement = (PyIfStatement)CodeStyleManager.getInstance(project).reformat(ifStatement);
+ ifStatement = (PyIfStatement)element.getParent().replace(ifStatement);
+ PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
+ PyStatementList statementList = ifStatement.getIfPart().getStatementList();
+ PyStatement[] statements = statementList.getStatements();
+ final TextRange range = statements[0].getTextRange();
+ editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
+ return TextRange.from(range.getStartOffset(), 0);
+ }
+
+ @Override
+ public String getTemplateDescription() {
+ return TEMPLATE_DESCRIPTION;
+ }
+ }
+}
diff --git a/python/src/com/jetbrains/python/codeInsight/postfix/PyPostfixTemplateProvider.java b/python/src/com/jetbrains/python/codeInsight/postfix/PyPostfixTemplateProvider.java
index abc890b6a4dd..54d7ca3d3ae5 100644
--- a/python/src/com/jetbrains/python/codeInsight/postfix/PyPostfixTemplateProvider.java
+++ b/python/src/com/jetbrains/python/codeInsight/postfix/PyPostfixTemplateProvider.java
@@ -30,7 +30,8 @@ public class PyPostfixTemplateProvider implements PostfixTemplateProvider {
public Set getTemplates() {
return ContainerUtil.newHashSet(new PyNotPostfixTemplate(),
new PyParenthesizedExpressionPostfixTemplate(),
- new PyReturnPostfixTemplate());
+ new PyReturnPostfixTemplate(),
+ new PyIfPostfixTemplate());
}
@Override
diff --git a/python/testData/postfix/if/complexExpression.py b/python/testData/postfix/if/complexExpression.py
new file mode 100644
index 000000000000..f9ca6fae42f6
--- /dev/null
+++ b/python/testData/postfix/if/complexExpression.py
@@ -0,0 +1,2 @@
+def f(a, b, c):
+ (a + b) * c.if
\ No newline at end of file
diff --git a/python/testData/postfix/if/complexExpression_after.py b/python/testData/postfix/if/complexExpression_after.py
new file mode 100644
index 000000000000..0e4ee687afbf
--- /dev/null
+++ b/python/testData/postfix/if/complexExpression_after.py
@@ -0,0 +1,3 @@
+def f(a, b, c):
+ if (a + b) * c:
+
\ No newline at end of file
diff --git a/python/testData/postfix/if/function.py b/python/testData/postfix/if/function.py
new file mode 100644
index 000000000000..29d82b9cdc12
--- /dev/null
+++ b/python/testData/postfix/if/function.py
@@ -0,0 +1,2 @@
+def f(a):
+ a.if
\ No newline at end of file
diff --git a/python/testData/postfix/if/function_after.py b/python/testData/postfix/if/function_after.py
new file mode 100644
index 000000000000..df40cb876881
--- /dev/null
+++ b/python/testData/postfix/if/function_after.py
@@ -0,0 +1,3 @@
+def f(a):
+ if a:
+
\ No newline at end of file
diff --git a/python/testData/postfix/if/nonApplicable.py b/python/testData/postfix/if/nonApplicable.py
new file mode 100644
index 000000000000..8a60f1d7aa67
--- /dev/null
+++ b/python/testData/postfix/if/nonApplicable.py
@@ -0,0 +1 @@
+a = 1.if
\ No newline at end of file
diff --git a/python/testData/postfix/if/nonApplicable_after.py b/python/testData/postfix/if/nonApplicable_after.py
new file mode 100644
index 000000000000..5f0e550f286b
--- /dev/null
+++ b/python/testData/postfix/if/nonApplicable_after.py
@@ -0,0 +1 @@
+a = 1.if
\ No newline at end of file
diff --git a/python/testData/postfix/if/topLevel.py b/python/testData/postfix/if/topLevel.py
new file mode 100644
index 000000000000..9037d2387580
--- /dev/null
+++ b/python/testData/postfix/if/topLevel.py
@@ -0,0 +1 @@
+True.if
\ No newline at end of file
diff --git a/python/testData/postfix/if/topLevel_after.py b/python/testData/postfix/if/topLevel_after.py
new file mode 100644
index 000000000000..8fa4b7a0a3d0
--- /dev/null
+++ b/python/testData/postfix/if/topLevel_after.py
@@ -0,0 +1,2 @@
+if True:
+
\ No newline at end of file
diff --git a/python/testSrc/com/jetbrains/python/postfix/PyIfPostfixTemplateTest.java b/python/testSrc/com/jetbrains/python/postfix/PyIfPostfixTemplateTest.java
new file mode 100644
index 000000000000..233862c98070
--- /dev/null
+++ b/python/testSrc/com/jetbrains/python/postfix/PyIfPostfixTemplateTest.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2000-2016 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.jetbrains.python.postfix;
+
+public class PyIfPostfixTemplateTest extends PyPostfixTemplateTestCase {
+
+ public void testTopLevel() {
+ doTest();
+ }
+
+ public void testFunction() {
+ doTest();
+ }
+
+ public void testNonApplicable() {
+ doTest();
+ }
+
+ public void testComplexExpression() {
+ doTest();
+ }
+
+ @Override
+ protected String getTestDataDir() {
+ return "if/";
+ }
+}