From f3de4b7f1fbbae8308e269d48919f44b6c74a2d5 Mon Sep 17 00:00:00 2001 From: "liana.bakradze" Date: Wed, 13 Jan 2016 15:54:27 +0300 Subject: [PATCH] return postfix template added (cherry picked from commit b0094ed) --- .../PyReturnPostfixTemplate/after.py.template | 3 ++ .../before.py.template | 3 ++ .../PyReturnPostfixTemplate/description.html | 5 +++ .../postfix/PyPostfixTemplateProvider.java | 3 +- .../codeInsight/postfix/PyPostfixUtils.java | 17 ++++++++ .../postfix/PyReturnPostfixTemplate.java | 34 ++++++++++++++++ .../postfix/return/complexExpression.py | 3 ++ .../postfix/return/complexExpression_after.py | 4 ++ python/testData/postfix/return/if.py | 5 +++ python/testData/postfix/return/if_after.py | 5 +++ .../testData/postfix/return/notApplicable.py | 3 ++ .../postfix/return/notApplicable_after.py | 3 ++ python/testData/postfix/return/number.py | 2 + .../testData/postfix/return/number_after.py | 2 + .../postfix/PyNotPostfixTemplateTest.java | 4 +- ...thesizedExpressionPostfixTemplateTest.java | 4 +- .../postfix/PyPostfixTemplateTestCase.java | 4 +- .../postfix/PyReturnPostfixTemplateTest.java | 40 +++++++++++++++++++ 18 files changed, 138 insertions(+), 6 deletions(-) create mode 100644 python/resources/postfixTemplates/PyReturnPostfixTemplate/after.py.template create mode 100644 python/resources/postfixTemplates/PyReturnPostfixTemplate/before.py.template create mode 100644 python/resources/postfixTemplates/PyReturnPostfixTemplate/description.html create mode 100644 python/src/com/jetbrains/python/codeInsight/postfix/PyReturnPostfixTemplate.java create mode 100644 python/testData/postfix/return/complexExpression.py create mode 100644 python/testData/postfix/return/complexExpression_after.py create mode 100644 python/testData/postfix/return/if.py create mode 100644 python/testData/postfix/return/if_after.py create mode 100644 python/testData/postfix/return/notApplicable.py create mode 100644 python/testData/postfix/return/notApplicable_after.py create mode 100644 python/testData/postfix/return/number.py create mode 100644 python/testData/postfix/return/number_after.py create mode 100644 python/testSrc/com/jetbrains/python/postfix/PyReturnPostfixTemplateTest.java diff --git a/python/resources/postfixTemplates/PyReturnPostfixTemplate/after.py.template b/python/resources/postfixTemplates/PyReturnPostfixTemplate/after.py.template new file mode 100644 index 000000000000..c9c072b3364a --- /dev/null +++ b/python/resources/postfixTemplates/PyReturnPostfixTemplate/after.py.template @@ -0,0 +1,3 @@ +def f(): + a = 1 + return a \ No newline at end of file diff --git a/python/resources/postfixTemplates/PyReturnPostfixTemplate/before.py.template b/python/resources/postfixTemplates/PyReturnPostfixTemplate/before.py.template new file mode 100644 index 000000000000..1653a1ef32dc --- /dev/null +++ b/python/resources/postfixTemplates/PyReturnPostfixTemplate/before.py.template @@ -0,0 +1,3 @@ +def f(): + a = 1 + a.return \ No newline at end of file diff --git a/python/resources/postfixTemplates/PyReturnPostfixTemplate/description.html b/python/resources/postfixTemplates/PyReturnPostfixTemplate/description.html new file mode 100644 index 000000000000..24afbda38224 --- /dev/null +++ b/python/resources/postfixTemplates/PyReturnPostfixTemplate/description.html @@ -0,0 +1,5 @@ + + +Returns value from containing method. + + \ 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 9604810921a9..abc890b6a4dd 100644 --- a/python/src/com/jetbrains/python/codeInsight/postfix/PyPostfixTemplateProvider.java +++ b/python/src/com/jetbrains/python/codeInsight/postfix/PyPostfixTemplateProvider.java @@ -29,7 +29,8 @@ public class PyPostfixTemplateProvider implements PostfixTemplateProvider { @Override public Set getTemplates() { return ContainerUtil.newHashSet(new PyNotPostfixTemplate(), - new PyParenthesizedExpressionPostfixTemplate()); + new PyParenthesizedExpressionPostfixTemplate(), + new PyReturnPostfixTemplate()); } @Override diff --git a/python/src/com/jetbrains/python/codeInsight/postfix/PyPostfixUtils.java b/python/src/com/jetbrains/python/codeInsight/postfix/PyPostfixUtils.java index dfce12170bac..3ded51893d76 100644 --- a/python/src/com/jetbrains/python/codeInsight/postfix/PyPostfixUtils.java +++ b/python/src/com/jetbrains/python/codeInsight/postfix/PyPostfixUtils.java @@ -22,7 +22,9 @@ import com.intellij.openapi.editor.Document; import com.intellij.openapi.util.Condition; import com.intellij.openapi.util.Conditions; import com.intellij.psi.PsiElement; +import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.PsiUtilCore; +import com.intellij.util.containers.ContainerUtil; import com.jetbrains.python.psi.*; import org.jetbrains.annotations.NotNull; @@ -75,4 +77,19 @@ public class PyPostfixUtils { public static PostfixTemplateExpressionSelector selectorAllExpressionsWithCurrentOffset() { return selectorAllExpressionsWithCurrentOffset(Conditions.alwaysTrue()); } + + public static PostfixTemplateExpressionSelector selectorTopmost() { + return selectorTopmost(Conditions.alwaysTrue()); + } + + public static PostfixTemplateExpressionSelector selectorTopmost(Condition additionalFilter) { + return new PostfixTemplateExpressionSelectorBase(additionalFilter) { + @Override + protected List getNonFilteredExpressions(@NotNull PsiElement context, @NotNull Document document, int offset) { + PyExpressionStatement exprStatement = PsiTreeUtil.getNonStrictParentOfType(context, PyExpressionStatement.class); + PyExpression statement = exprStatement != null ? PsiTreeUtil.getChildOfType(exprStatement, PyExpression.class) : null; + return ContainerUtil.createMaybeSingletonList((statement)); + } + }; + } } diff --git a/python/src/com/jetbrains/python/codeInsight/postfix/PyReturnPostfixTemplate.java b/python/src/com/jetbrains/python/codeInsight/postfix/PyReturnPostfixTemplate.java new file mode 100644 index 000000000000..b68a840bf9ad --- /dev/null +++ b/python/src/com/jetbrains/python/codeInsight/postfix/PyReturnPostfixTemplate.java @@ -0,0 +1,34 @@ +/* + * 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.StringBasedPostfixTemplate; +import com.intellij.psi.PsiElement; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class PyReturnPostfixTemplate extends StringBasedPostfixTemplate { + public PyReturnPostfixTemplate() { + super("return", "return expr", PyPostfixUtils.selectorTopmost()); + } + + + @Nullable + @Override + public String getTemplateString(@NotNull PsiElement element) { + return "return $expr$$END$"; + } +} diff --git a/python/testData/postfix/return/complexExpression.py b/python/testData/postfix/return/complexExpression.py new file mode 100644 index 000000000000..d0d097a9c416 --- /dev/null +++ b/python/testData/postfix/return/complexExpression.py @@ -0,0 +1,3 @@ +def f(a): + b = a + max((a + (a + b)), b).return \ No newline at end of file diff --git a/python/testData/postfix/return/complexExpression_after.py b/python/testData/postfix/return/complexExpression_after.py new file mode 100644 index 000000000000..f930d1d12441 --- /dev/null +++ b/python/testData/postfix/return/complexExpression_after.py @@ -0,0 +1,4 @@ +def f(a): + b = a + return max((a + (a + b)), b) + \ No newline at end of file diff --git a/python/testData/postfix/return/if.py b/python/testData/postfix/return/if.py new file mode 100644 index 000000000000..c2a0b86f465a --- /dev/null +++ b/python/testData/postfix/return/if.py @@ -0,0 +1,5 @@ +def d(a): + if a: + 1.return + else: + return 2 \ No newline at end of file diff --git a/python/testData/postfix/return/if_after.py b/python/testData/postfix/return/if_after.py new file mode 100644 index 000000000000..1d22d47a00d2 --- /dev/null +++ b/python/testData/postfix/return/if_after.py @@ -0,0 +1,5 @@ +def d(a): + if a: + return 1 + else: + return 2 \ No newline at end of file diff --git a/python/testData/postfix/return/notApplicable.py b/python/testData/postfix/return/notApplicable.py new file mode 100644 index 000000000000..35e5bf521d3d --- /dev/null +++ b/python/testData/postfix/return/notApplicable.py @@ -0,0 +1,3 @@ +def f(a): + b = a.return + return max((a + (a + b)), b) \ No newline at end of file diff --git a/python/testData/postfix/return/notApplicable_after.py b/python/testData/postfix/return/notApplicable_after.py new file mode 100644 index 000000000000..af9da787b86b --- /dev/null +++ b/python/testData/postfix/return/notApplicable_after.py @@ -0,0 +1,3 @@ +def f(a): + b = a.return + return max((a + (a + b)), b) \ No newline at end of file diff --git a/python/testData/postfix/return/number.py b/python/testData/postfix/return/number.py new file mode 100644 index 000000000000..95079218288b --- /dev/null +++ b/python/testData/postfix/return/number.py @@ -0,0 +1,2 @@ +def f(): + 1.return \ No newline at end of file diff --git a/python/testData/postfix/return/number_after.py b/python/testData/postfix/return/number_after.py new file mode 100644 index 000000000000..b8595995dba3 --- /dev/null +++ b/python/testData/postfix/return/number_after.py @@ -0,0 +1,2 @@ +def f(): + return 1 diff --git a/python/testSrc/com/jetbrains/python/postfix/PyNotPostfixTemplateTest.java b/python/testSrc/com/jetbrains/python/postfix/PyNotPostfixTemplateTest.java index d1dfe1c04098..728e937ddcbb 100644 --- a/python/testSrc/com/jetbrains/python/postfix/PyNotPostfixTemplateTest.java +++ b/python/testSrc/com/jetbrains/python/postfix/PyNotPostfixTemplateTest.java @@ -30,7 +30,7 @@ public class PyNotPostfixTemplateTest extends PyPostfixTemplateTestCase { } @Override - protected String getTestDataPath() { - return super.getTestDataPath() + "not/"; + protected String getTestDataDir() { + return "not/"; } } diff --git a/python/testSrc/com/jetbrains/python/postfix/PyParenthesizedExpressionPostfixTemplateTest.java b/python/testSrc/com/jetbrains/python/postfix/PyParenthesizedExpressionPostfixTemplateTest.java index 1efdf9529f4c..f5beebe21793 100644 --- a/python/testSrc/com/jetbrains/python/postfix/PyParenthesizedExpressionPostfixTemplateTest.java +++ b/python/testSrc/com/jetbrains/python/postfix/PyParenthesizedExpressionPostfixTemplateTest.java @@ -30,7 +30,7 @@ public class PyParenthesizedExpressionPostfixTemplateTest extends PyPostfixTempl } @Override - protected String getTestDataPath() { - return super.getTestDataPath() + "par/"; + protected String getTestDataDir() { + return "par/"; } } diff --git a/python/testSrc/com/jetbrains/python/postfix/PyPostfixTemplateTestCase.java b/python/testSrc/com/jetbrains/python/postfix/PyPostfixTemplateTestCase.java index e3bd52578e7e..5f2571f7545f 100644 --- a/python/testSrc/com/jetbrains/python/postfix/PyPostfixTemplateTestCase.java +++ b/python/testSrc/com/jetbrains/python/postfix/PyPostfixTemplateTestCase.java @@ -26,9 +26,11 @@ public abstract class PyPostfixTemplateTestCase extends PyTestCase { myFixture.checkResultByFile(getTestName(true) + "_after" + ".py", true); } + abstract protected String getTestDataDir(); + @Override @NonNls protected String getTestDataPath() { - return PythonTestUtil.getTestDataPath() + "/postfix/"; + return PythonTestUtil.getTestDataPath() + "/postfix/" + getTestDataDir(); } } diff --git a/python/testSrc/com/jetbrains/python/postfix/PyReturnPostfixTemplateTest.java b/python/testSrc/com/jetbrains/python/postfix/PyReturnPostfixTemplateTest.java new file mode 100644 index 000000000000..a166a4fdeb95 --- /dev/null +++ b/python/testSrc/com/jetbrains/python/postfix/PyReturnPostfixTemplateTest.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 PyReturnPostfixTemplateTest extends PyPostfixTemplateTestCase { + + public void testNumber() { + doTest(); + } + + public void testComplexExpression() { + doTest(); + } + + public void testNotApplicable() { + doTest(); + } + + public void testIf() { + doTest(); + } + + @Override + protected String getTestDataDir() { + return "return/"; + } +} \ No newline at end of file