if postfix template added
authorLiana Bakradze <liana.bakradze@jetbrains.com>
Fri, 15 Jan 2016 11:39:51 +0000 (14:39 +0300)
committerliana.bakradze <liana.bakradze@jetbrains.com>
Thu, 11 Feb 2016 20:35:57 +0000 (23:35 +0300)
(cherry picked from commit 30e3fe9)

14 files changed:
python/resources/postfixTemplates/PyIfPostfixTemplate/after.py.template [new file with mode: 0644]
python/resources/postfixTemplates/PyIfPostfixTemplate/before.py.template [new file with mode: 0644]
python/resources/postfixTemplates/PyIfPostfixTemplate/description.html [new file with mode: 0644]
python/src/com/jetbrains/python/codeInsight/postfix/PyIfPostfixTemplate.java [new file with mode: 0644]
python/src/com/jetbrains/python/codeInsight/postfix/PyPostfixTemplateProvider.java
python/testData/postfix/if/complexExpression.py [new file with mode: 0644]
python/testData/postfix/if/complexExpression_after.py [new file with mode: 0644]
python/testData/postfix/if/function.py [new file with mode: 0644]
python/testData/postfix/if/function_after.py [new file with mode: 0644]
python/testData/postfix/if/nonApplicable.py [new file with mode: 0644]
python/testData/postfix/if/nonApplicable_after.py [new file with mode: 0644]
python/testData/postfix/if/topLevel.py [new file with mode: 0644]
python/testData/postfix/if/topLevel_after.py [new file with mode: 0644]
python/testSrc/com/jetbrains/python/postfix/PyIfPostfixTemplateTest.java [new file with mode: 0644]

diff --git a/python/resources/postfixTemplates/PyIfPostfixTemplate/after.py.template b/python/resources/postfixTemplates/PyIfPostfixTemplate/after.py.template
new file mode 100644 (file)
index 0000000..ae09d09
--- /dev/null
@@ -0,0 +1,3 @@
+def f(a):
+  if a:
+    <spot></spot>
\ 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 (file)
index 0000000..4c3ad81
--- /dev/null
@@ -0,0 +1,2 @@
+def f(a):
+  <spot>a</spot>.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 (file)
index 0000000..247b98e
--- /dev/null
@@ -0,0 +1,5 @@
+<html>
+<body>
+Uses the expression as condition in 'if' statement.
+</body>
+</html>
\ 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 (file)
index 0000000..6406179
--- /dev/null
@@ -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;
+    }
+  }
+}
index abc890b6a4dd542531247f2287defcc65c960c4b..54d7ca3d3ae5a25284595fc1d21372f20d8c902b 100644 (file)
@@ -30,7 +30,8 @@ public class PyPostfixTemplateProvider implements PostfixTemplateProvider {
   public Set<PostfixTemplate> getTemplates() {
     return ContainerUtil.<PostfixTemplate>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 (file)
index 0000000..f9ca6fa
--- /dev/null
@@ -0,0 +1,2 @@
+def f(a, b, c):
+    (a + b) * c.if<caret>
\ 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 (file)
index 0000000..0e4ee68
--- /dev/null
@@ -0,0 +1,3 @@
+def f(a, b, c):
+    if (a + b) * c:
+        <caret>
\ 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 (file)
index 0000000..29d82b9
--- /dev/null
@@ -0,0 +1,2 @@
+def f(a):
+    a.if<caret>
\ 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 (file)
index 0000000..df40cb8
--- /dev/null
@@ -0,0 +1,3 @@
+def f(a):
+    if a:
+        <caret>
\ 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 (file)
index 0000000..8a60f1d
--- /dev/null
@@ -0,0 +1 @@
+a = 1.if<caret>
\ 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 (file)
index 0000000..5f0e550
--- /dev/null
@@ -0,0 +1 @@
+a = 1.if    <caret>
\ 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 (file)
index 0000000..9037d23
--- /dev/null
@@ -0,0 +1 @@
+True.if<caret>
\ 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 (file)
index 0000000..8fa4b7a
--- /dev/null
@@ -0,0 +1,2 @@
+if True:
+    <caret>
\ 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 (file)
index 0000000..233862c
--- /dev/null
@@ -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/";
+  }
+}