PY-19607 print postfix template
authorLiana Bakradze <liana.bakradze@jetbrains.com>
Tue, 24 May 2016 09:25:44 +0000 (12:25 +0300)
committerAndrey Vlasovskikh <andrey.vlasovskikh@gmail.com>
Wed, 1 Jun 2016 09:10:10 +0000 (02:10 -0700)
12 files changed:
python/resources/postfixTemplates/PyPrintPostfixTemplate/after.py.template [new file with mode: 0644]
python/resources/postfixTemplates/PyPrintPostfixTemplate/before.py.template [new file with mode: 0644]
python/resources/postfixTemplates/PyPrintPostfixTemplate/description.html [new file with mode: 0644]
python/src/com/jetbrains/python/codeInsight/postfix/PyPostfixTemplateProvider.java
python/src/com/jetbrains/python/codeInsight/postfix/PyPrintPostfixTemplate.java [new file with mode: 0644]
python/testData/postfix/print/number2.py [new file with mode: 0644]
python/testData/postfix/print/number2_after.py [new file with mode: 0644]
python/testData/postfix/print/number3.py [new file with mode: 0644]
python/testData/postfix/print/number3_after.py [new file with mode: 0644]
python/testData/postfix/print/tuple.py [new file with mode: 0644]
python/testData/postfix/print/tuple_after.py [new file with mode: 0644]
python/testSrc/com/jetbrains/python/postfix/PyPrintPostfixTemplateTest.java [new file with mode: 0644]

diff --git a/python/resources/postfixTemplates/PyPrintPostfixTemplate/after.py.template b/python/resources/postfixTemplates/PyPrintPostfixTemplate/after.py.template
new file mode 100644 (file)
index 0000000..e9d5b1e
--- /dev/null
@@ -0,0 +1 @@
+print(a)
\ No newline at end of file
diff --git a/python/resources/postfixTemplates/PyPrintPostfixTemplate/before.py.template b/python/resources/postfixTemplates/PyPrintPostfixTemplate/before.py.template
new file mode 100644 (file)
index 0000000..f5cf0d9
--- /dev/null
@@ -0,0 +1 @@
+<spot>a</spot>.print
\ No newline at end of file
diff --git a/python/resources/postfixTemplates/PyPrintPostfixTemplate/description.html b/python/resources/postfixTemplates/PyPrintPostfixTemplate/description.html
new file mode 100644 (file)
index 0000000..c2b7291
--- /dev/null
@@ -0,0 +1,5 @@
+<html>
+<body>
+Surrounds expression with print function for Python 3 or with print statement for Python 2.
+</body>
+</html>
\ No newline at end of file
index 6cbceea1a1b7978e6afbf1afd4f47dc04191e2b1..3bc82e3ea58b5ad691ec209585ff5c6f61817431 100644 (file)
@@ -34,7 +34,8 @@ public class PyPostfixTemplateProvider implements PostfixTemplateProvider {
                                                      new PyIfPostfixTemplate(),
                                                      new PyWhilePostfixTemplate(),
                                                      new PyIsNonePostfixTemplate(),
-                                                     new PyIsNotNonePostfixTemplate());
+                                                     new PyIsNotNonePostfixTemplate(),
+                                                     new PyPrintPostfixTemplate());
   }
 
   @Override
diff --git a/python/src/com/jetbrains/python/codeInsight/postfix/PyPrintPostfixTemplate.java b/python/src/com/jetbrains/python/codeInsight/postfix/PyPrintPostfixTemplate.java
new file mode 100644 (file)
index 0000000..0ba997e
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * 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.codeStyle.CodeStyleManager;
+import com.intellij.psi.util.PsiTreeUtil;
+import com.intellij.util.IncorrectOperationException;
+import com.jetbrains.python.psi.*;
+import com.jetbrains.python.refactoring.surround.surrounders.expressions.PyExpressionSurrounder;
+import org.jetbrains.annotations.NotNull;
+
+public class PyPrintPostfixTemplate extends SurroundPostfixTemplateBase {
+
+  public static final String DESCR = "print(expr)";
+
+  protected PyPrintPostfixTemplate() {
+    super("print", DESCR, PyPostfixUtils.PY_PSI_INFO, PyPostfixUtils.selectorTopmost());
+  }
+
+  @NotNull
+  @Override
+  protected Surrounder getSurrounder() {
+    return new PyExpressionSurrounder() {
+      @Override
+      public boolean isApplicable(@NotNull PyExpression expr) {
+        return true;
+      }
+
+      @Override
+      public TextRange surroundExpression(@NotNull Project project, @NotNull Editor editor, @NotNull PyExpression expression)
+        throws IncorrectOperationException {
+        LanguageLevel level = LanguageLevel.forElement(expression);
+        String textToGenerate = level.isPy3K() ? "print(a)" : "print a";
+        PyStatement pyStatement = PyElementGenerator.getInstance(project).createFromText(level, PyStatement.class, textToGenerate);
+        if (!level.isPy3K()) {
+          pyStatement.getLastChild().replace(expression);
+        } else {
+          PyArgumentList argumentList = PsiTreeUtil.findChildOfType(pyStatement, PyArgumentList.class);
+          if (argumentList == null) {
+            return null;
+          }
+          argumentList.getArguments()[0].replace(expression);
+        }
+        pyStatement = (PyStatement)CodeStyleManager.getInstance(project).reformat(pyStatement);
+        pyStatement = (PyStatement)expression.getParent().replace(pyStatement);
+        return TextRange.from(pyStatement.getTextRange().getEndOffset(), 0);
+      }
+
+      @Override
+      public String getTemplateDescription() {
+        return DESCR;
+      }
+    };
+  }
+}
diff --git a/python/testData/postfix/print/number2.py b/python/testData/postfix/print/number2.py
new file mode 100644 (file)
index 0000000..522b2b9
--- /dev/null
@@ -0,0 +1 @@
+1.print<caret>
\ No newline at end of file
diff --git a/python/testData/postfix/print/number2_after.py b/python/testData/postfix/print/number2_after.py
new file mode 100644 (file)
index 0000000..a6757d5
--- /dev/null
@@ -0,0 +1 @@
+print 1<caret>
\ No newline at end of file
diff --git a/python/testData/postfix/print/number3.py b/python/testData/postfix/print/number3.py
new file mode 100644 (file)
index 0000000..522b2b9
--- /dev/null
@@ -0,0 +1 @@
+1.print<caret>
\ No newline at end of file
diff --git a/python/testData/postfix/print/number3_after.py b/python/testData/postfix/print/number3_after.py
new file mode 100644 (file)
index 0000000..c732eb7
--- /dev/null
@@ -0,0 +1 @@
+print(1)<caret>
\ No newline at end of file
diff --git a/python/testData/postfix/print/tuple.py b/python/testData/postfix/print/tuple.py
new file mode 100644 (file)
index 0000000..73a9504
--- /dev/null
@@ -0,0 +1 @@
+(1, 1).print<caret>
\ No newline at end of file
diff --git a/python/testData/postfix/print/tuple_after.py b/python/testData/postfix/print/tuple_after.py
new file mode 100644 (file)
index 0000000..c3726c9
--- /dev/null
@@ -0,0 +1 @@
+print((1, 1))<caret>
\ No newline at end of file
diff --git a/python/testSrc/com/jetbrains/python/postfix/PyPrintPostfixTemplateTest.java b/python/testSrc/com/jetbrains/python/postfix/PyPrintPostfixTemplateTest.java
new file mode 100644 (file)
index 0000000..4c329cf
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * 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;
+
+import com.jetbrains.python.psi.LanguageLevel;
+import com.jetbrains.python.psi.impl.PythonLanguageLevelPusher;
+
+public class PyPrintPostfixTemplateTest extends PyPostfixTemplateTestCase {
+
+  public void testNumber2() {
+    PythonLanguageLevelPusher.setForcedLanguageLevel(myFixture.getProject(), LanguageLevel.PYTHON27);
+    doTest();
+  }
+
+  public void testNumber3() {
+    PythonLanguageLevelPusher.setForcedLanguageLevel(myFixture.getProject(), LanguageLevel.PYTHON35);
+    doTest();
+  }
+
+  public void testTuple() {
+    PythonLanguageLevelPusher.setForcedLanguageLevel(myFixture.getProject(), LanguageLevel.PYTHON35);
+    doTest();
+  }
+
+  @Override
+  protected String getTestDataDir() {
+    return "print/";
+  }
+}