added while postfix template
authorLiana Bakradze <liana.bakradze@jetbrains.com>
Fri, 15 Jan 2016 14:05:19 +0000 (17:05 +0300)
committerliana.bakradze <liana.bakradze@jetbrains.com>
Thu, 11 Feb 2016 20:36:43 +0000 (23:36 +0300)
(cherry picked from commit 24bba05)

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

diff --git a/python/resources/postfixTemplates/PyWhilePostfixTemplate/after.py.template b/python/resources/postfixTemplates/PyWhilePostfixTemplate/after.py.template
new file mode 100644 (file)
index 0000000..652b69b
--- /dev/null
@@ -0,0 +1,3 @@
+def f(a):
+  while a:
+    <spot></spot>
\ No newline at end of file
diff --git a/python/resources/postfixTemplates/PyWhilePostfixTemplate/before.py.template b/python/resources/postfixTemplates/PyWhilePostfixTemplate/before.py.template
new file mode 100644 (file)
index 0000000..44a6c65
--- /dev/null
@@ -0,0 +1,2 @@
+def f(a):
+  <spot>a</spot>.while
\ No newline at end of file
diff --git a/python/resources/postfixTemplates/PyWhilePostfixTemplate/description.html b/python/resources/postfixTemplates/PyWhilePostfixTemplate/description.html
new file mode 100644 (file)
index 0000000..2e548f1
--- /dev/null
@@ -0,0 +1,5 @@
+<html>
+<body>
+Uses the expression as condition in 'while' statement.
+</body>
+</html>
\ No newline at end of file
diff --git a/python/src/com/jetbrains/python/codeInsight/postfix/PyExpressionAsConditionSurrounder.java b/python/src/com/jetbrains/python/codeInsight/postfix/PyExpressionAsConditionSurrounder.java
new file mode 100644 (file)
index 0000000..7011a5f
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * 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.openapi.editor.Editor;
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.util.TextRange;
+import com.intellij.psi.PsiDocumentManager;
+import com.intellij.psi.codeStyle.CodeStyleManager;
+import com.intellij.util.IncorrectOperationException;
+import com.jetbrains.python.psi.*;
+import com.jetbrains.python.refactoring.surround.surrounders.expressions.PyExpressionSurrounder;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+public abstract class PyExpressionAsConditionSurrounder extends PyExpressionSurrounder {
+
+  abstract protected String getTextToGenerate();
+
+  @Nullable
+  abstract protected PyExpression getCondition(PyStatement statement);
+
+  @Nullable
+  abstract protected PyStatementListContainer getStatementListContainer(PyStatement statement);
+
+  @Override
+  public TextRange surroundExpression(@NotNull Project project, @NotNull Editor editor, @NotNull PyExpression expression)
+    throws IncorrectOperationException {
+    TextRange currentCaretPosition = TextRange.from(editor.getCaretModel().getOffset(), 0);
+    PyStatement statement = PyElementGenerator.getInstance(project).
+      createFromText(LanguageLevel.getDefault(), PyStatement.class, getTextToGenerate());
+    final PyExpression condition = getCondition(statement);
+    if (condition == null) {
+      return currentCaretPosition;
+    }
+    condition.replace(expression);
+    statement = (PyStatement)CodeStyleManager.getInstance(project).reformat(statement);
+    statement = (PyStatement)expression.getParent().replace(statement);
+    PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
+    PyStatementListContainer statementListContainer = getStatementListContainer(statement);
+    if (statementListContainer == null) {
+      return currentCaretPosition;
+    }
+    PyStatementList statementList = statementListContainer.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 boolean isApplicable(@NotNull PyExpression expr) {
+    return true;
+  }
+}
index 309f6bbf57fff65453414f9bb47675c15f93c5b5..f15aec0b2683e5c8d549e834ca711a5d2e2c8985 100644 (file)
@@ -17,14 +17,10 @@ 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.codeStyle.CodeStyleManager;
-import com.intellij.util.IncorrectOperationException;
-import com.jetbrains.python.psi.*;
-import com.jetbrains.python.refactoring.surround.surrounders.expressions.PyExpressionSurrounder;
+import com.jetbrains.python.psi.PyExpression;
+import com.jetbrains.python.psi.PyIfStatement;
+import com.jetbrains.python.psi.PyStatement;
+import com.jetbrains.python.psi.PyStatementListContainer;
 import org.jetbrains.annotations.NotNull;
 
 public class PyIfPostfixTemplate extends SurroundPostfixTemplateBase {
@@ -41,30 +37,28 @@ public class PyIfPostfixTemplate extends SurroundPostfixTemplateBase {
     return new PyIfSurrounder();
   }
 
-  private static class PyIfSurrounder extends PyExpressionSurrounder {
+
+  private static class PyIfSurrounder extends PyExpressionAsConditionSurrounder {
+
+    @Override
+    protected String getTextToGenerate() {
+      return "if a:\n pass";
+    }
+
     @Override
-    public boolean isApplicable(@NotNull PyExpression expr) {
-      return true;
+    protected PyExpression getCondition(PyStatement statement) {
+      if (statement instanceof PyIfStatement) {
+        return ((PyIfStatement)statement).getIfPart().getCondition();
+      }
+      return null;
     }
 
     @Override
-    public TextRange surroundExpression(@NotNull Project project, @NotNull Editor editor, @NotNull PyExpression expression)
-      throws IncorrectOperationException {
-      String text = "if a:\n pass";
-      PyIfStatement ifStatement = PyElementGenerator.getInstance(project).
-        createFromText(LanguageLevel.getDefault(), PyIfStatement.class, text);
-      final PyExpression condition = ifStatement.getIfPart().getCondition();
-      if (condition != null) {
-        condition.replace(expression);
+    protected PyStatementListContainer getStatementListContainer(PyStatement statement) {
+      if (statement instanceof PyIfStatement) {
+        return ((PyIfStatement)statement).getIfPart();
       }
-      ifStatement = (PyIfStatement)CodeStyleManager.getInstance(project).reformat(ifStatement);
-      ifStatement = (PyIfStatement)expression.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);
+      return null;
     }
 
     @Override
index 54d7ca3d3ae5a25284595fc1d21372f20d8c902b..4531f6a1d75bf80faab851c25dc634ac9ea987b1 100644 (file)
@@ -31,7 +31,8 @@ public class PyPostfixTemplateProvider implements PostfixTemplateProvider {
     return ContainerUtil.<PostfixTemplate>newHashSet(new PyNotPostfixTemplate(),
                                                      new PyParenthesizedExpressionPostfixTemplate(),
                                                      new PyReturnPostfixTemplate(),
-                                                     new PyIfPostfixTemplate());
+                                                     new PyIfPostfixTemplate(),
+                                                     new PyWhilePostfixTemplate());
   }
 
   @Override
diff --git a/python/src/com/jetbrains/python/codeInsight/postfix/PyWhilePostfixTemplate.java b/python/src/com/jetbrains/python/codeInsight/postfix/PyWhilePostfixTemplate.java
new file mode 100644 (file)
index 0000000..ca7c59e
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * 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.jetbrains.python.psi.PyExpression;
+import com.jetbrains.python.psi.PyStatement;
+import com.jetbrains.python.psi.PyStatementListContainer;
+import com.jetbrains.python.psi.PyWhileStatement;
+import org.jetbrains.annotations.NotNull;
+
+public class PyWhilePostfixTemplate extends SurroundPostfixTemplateBase {
+
+  public static final String TEMPLATE_DESCRIPTION = "while expr";
+
+  public PyWhilePostfixTemplate() {
+    super("while", TEMPLATE_DESCRIPTION, PyPostfixUtils.PY_PSI_INFO, PyPostfixUtils.selectorTopmost());
+  }
+
+  @NotNull
+  @Override
+  protected Surrounder getSurrounder() {
+    return new PyWhileSurrounder();
+  }
+
+  private static class PyWhileSurrounder extends PyExpressionAsConditionSurrounder {
+
+    @Override
+    protected String getTextToGenerate() {
+      return "while a:\n pass";
+    }
+
+    @Override
+    protected PyExpression getCondition(PyStatement statement) {
+      if (statement instanceof PyWhileStatement) {
+        return ((PyWhileStatement)statement).getWhilePart().getCondition();
+      }
+      return null;
+    }
+
+    @Override
+    protected PyStatementListContainer getStatementListContainer(PyStatement statement) {
+      if (statement instanceof PyWhileStatement) {
+        return ((PyWhileStatement)statement).getWhilePart();
+      }
+      return null;
+    }
+
+    @Override
+    public String getTemplateDescription() {
+      return TEMPLATE_DESCRIPTION;
+    }
+  }
+}
diff --git a/python/testData/postfix/while/complexExpression.py b/python/testData/postfix/while/complexExpression.py
new file mode 100644 (file)
index 0000000..ff135bf
--- /dev/null
@@ -0,0 +1,2 @@
+def f(a, b, c):
+    (2*(a + b)) * c.while<caret>
\ No newline at end of file
diff --git a/python/testData/postfix/while/complexExpression_after.py b/python/testData/postfix/while/complexExpression_after.py
new file mode 100644 (file)
index 0000000..c421c94
--- /dev/null
@@ -0,0 +1,3 @@
+def f(a, b, c):
+    while (2 * (a + b)) * c:
+        <caret>
\ No newline at end of file
diff --git a/python/testData/postfix/while/function.py b/python/testData/postfix/while/function.py
new file mode 100644 (file)
index 0000000..19ce485
--- /dev/null
@@ -0,0 +1,2 @@
+def f(a):
+    a.while<caret>
\ No newline at end of file
diff --git a/python/testData/postfix/while/function_after.py b/python/testData/postfix/while/function_after.py
new file mode 100644 (file)
index 0000000..6ca733e
--- /dev/null
@@ -0,0 +1,3 @@
+def f(a):
+    while a:
+        <caret>
\ No newline at end of file
diff --git a/python/testData/postfix/while/nonApplicable.py b/python/testData/postfix/while/nonApplicable.py
new file mode 100644 (file)
index 0000000..88e5915
--- /dev/null
@@ -0,0 +1 @@
+a = 1.while<caret>
\ No newline at end of file
diff --git a/python/testData/postfix/while/nonApplicable_after.py b/python/testData/postfix/while/nonApplicable_after.py
new file mode 100644 (file)
index 0000000..fd3e80e
--- /dev/null
@@ -0,0 +1 @@
+a = 1.while <caret>
\ No newline at end of file
diff --git a/python/testData/postfix/while/topLevel.py b/python/testData/postfix/while/topLevel.py
new file mode 100644 (file)
index 0000000..7a5756d
--- /dev/null
@@ -0,0 +1 @@
+True.while<caret>
\ No newline at end of file
diff --git a/python/testData/postfix/while/topLevel_after.py b/python/testData/postfix/while/topLevel_after.py
new file mode 100644 (file)
index 0000000..064cfb2
--- /dev/null
@@ -0,0 +1,2 @@
+while True:
+    <caret>
\ No newline at end of file
diff --git a/python/testSrc/com/jetbrains/python/postfix/PyWhilePostfixTemplateTest.java b/python/testSrc/com/jetbrains/python/postfix/PyWhilePostfixTemplateTest.java
new file mode 100644 (file)
index 0000000..4800cb9
--- /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 PyWhilePostfixTemplateTest extends PyPostfixTemplateTestCase{
+
+  public void testTopLevel() {
+    doTest();
+  }
+
+  public void testFunction() {
+    doTest();
+  }
+
+  public void testNonApplicable() {
+    doTest();
+  }
+
+  public void testComplexExpression() {
+    doTest();
+  }
+
+  @Override
+  protected String getTestDataDir() {
+    return "while/";
+  }
+}