From c5ce062f81a0dd2d82612679d80325fd54832ed2 Mon Sep 17 00:00:00 2001 From: Liana Bakradze Date: Tue, 24 May 2016 12:25:44 +0300 Subject: [PATCH] PY-19607 print postfix template --- .../PyPrintPostfixTemplate/after.py.template | 1 + .../PyPrintPostfixTemplate/before.py.template | 1 + .../PyPrintPostfixTemplate/description.html | 5 ++ .../postfix/PyPostfixTemplateProvider.java | 3 +- .../postfix/PyPrintPostfixTemplate.java | 73 +++++++++++++++++++ python/testData/postfix/print/number2.py | 1 + .../testData/postfix/print/number2_after.py | 1 + python/testData/postfix/print/number3.py | 1 + .../testData/postfix/print/number3_after.py | 1 + python/testData/postfix/print/tuple.py | 1 + python/testData/postfix/print/tuple_after.py | 1 + .../postfix/PyPrintPostfixTemplateTest.java | 42 +++++++++++ 12 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 python/resources/postfixTemplates/PyPrintPostfixTemplate/after.py.template create mode 100644 python/resources/postfixTemplates/PyPrintPostfixTemplate/before.py.template create mode 100644 python/resources/postfixTemplates/PyPrintPostfixTemplate/description.html create mode 100644 python/src/com/jetbrains/python/codeInsight/postfix/PyPrintPostfixTemplate.java create mode 100644 python/testData/postfix/print/number2.py create mode 100644 python/testData/postfix/print/number2_after.py create mode 100644 python/testData/postfix/print/number3.py create mode 100644 python/testData/postfix/print/number3_after.py create mode 100644 python/testData/postfix/print/tuple.py create mode 100644 python/testData/postfix/print/tuple_after.py create mode 100644 python/testSrc/com/jetbrains/python/postfix/PyPrintPostfixTemplateTest.java diff --git a/python/resources/postfixTemplates/PyPrintPostfixTemplate/after.py.template b/python/resources/postfixTemplates/PyPrintPostfixTemplate/after.py.template new file mode 100644 index 000000000000..e9d5b1efe37b --- /dev/null +++ b/python/resources/postfixTemplates/PyPrintPostfixTemplate/after.py.template @@ -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 index 000000000000..f5cf0d9282f0 --- /dev/null +++ b/python/resources/postfixTemplates/PyPrintPostfixTemplate/before.py.template @@ -0,0 +1 @@ +a.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 index 000000000000..c2b72917eb3b --- /dev/null +++ b/python/resources/postfixTemplates/PyPrintPostfixTemplate/description.html @@ -0,0 +1,5 @@ + + +Surrounds expression with print function for Python 3 or with print statement for Python 2. + + \ No newline at end of file diff --git a/python/src/com/jetbrains/python/codeInsight/postfix/PyPostfixTemplateProvider.java b/python/src/com/jetbrains/python/codeInsight/postfix/PyPostfixTemplateProvider.java index 6cbceea1a1b7..3bc82e3ea58b 100644 --- a/python/src/com/jetbrains/python/codeInsight/postfix/PyPostfixTemplateProvider.java +++ b/python/src/com/jetbrains/python/codeInsight/postfix/PyPostfixTemplateProvider.java @@ -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 index 000000000000..0ba997e74a31 --- /dev/null +++ b/python/src/com/jetbrains/python/codeInsight/postfix/PyPrintPostfixTemplate.java @@ -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 index 000000000000..522b2b929ecc --- /dev/null +++ b/python/testData/postfix/print/number2.py @@ -0,0 +1 @@ +1.print \ 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 index 000000000000..a6757d52f004 --- /dev/null +++ b/python/testData/postfix/print/number2_after.py @@ -0,0 +1 @@ +print 1 \ 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 index 000000000000..522b2b929ecc --- /dev/null +++ b/python/testData/postfix/print/number3.py @@ -0,0 +1 @@ +1.print \ 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 index 000000000000..c732eb7321f6 --- /dev/null +++ b/python/testData/postfix/print/number3_after.py @@ -0,0 +1 @@ +print(1) \ 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 index 000000000000..73a9504ab03e --- /dev/null +++ b/python/testData/postfix/print/tuple.py @@ -0,0 +1 @@ +(1, 1).print \ 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 index 000000000000..c3726c9c27c9 --- /dev/null +++ b/python/testData/postfix/print/tuple_after.py @@ -0,0 +1 @@ +print((1, 1)) \ 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 index 000000000000..4c329cf6eb73 --- /dev/null +++ b/python/testSrc/com/jetbrains/python/postfix/PyPrintPostfixTemplateTest.java @@ -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/"; + } +}