return postfix template added

(cherry picked from commit b0094ed)
This commit is contained in:
liana.bakradze
2016-02-11 23:35:39 +03:00
parent 02214ca394
commit f3de4b7f1f
18 changed files with 138 additions and 6 deletions
@@ -0,0 +1,3 @@
def f():
a = 1
return a
@@ -0,0 +1,3 @@
def f():
a = 1
<spot>a</spot>.return
@@ -0,0 +1,5 @@
<html>
<body>
Returns value from containing method.
</body>
</html>
@@ -29,7 +29,8 @@ public class PyPostfixTemplateProvider implements PostfixTemplateProvider {
@Override
public Set<PostfixTemplate> getTemplates() {
return ContainerUtil.<PostfixTemplate>newHashSet(new PyNotPostfixTemplate(),
new PyParenthesizedExpressionPostfixTemplate());
new PyParenthesizedExpressionPostfixTemplate(),
new PyReturnPostfixTemplate());
}
@Override
@@ -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.<PsiElement>alwaysTrue());
}
public static PostfixTemplateExpressionSelector selectorTopmost() {
return selectorTopmost(Conditions.<PsiElement>alwaysTrue());
}
public static PostfixTemplateExpressionSelector selectorTopmost(Condition<PsiElement> additionalFilter) {
return new PostfixTemplateExpressionSelectorBase(additionalFilter) {
@Override
protected List<PsiElement> 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.<PsiElement>createMaybeSingletonList((statement));
}
};
}
}
@@ -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$";
}
}
@@ -0,0 +1,3 @@
def f(a):
b = a
max((a + (a + b)), b).return<caret>
@@ -0,0 +1,4 @@
def f(a):
b = a
return max((a + (a + b)), b)<caret>
+5
View File
@@ -0,0 +1,5 @@
def d(a):
if a:
1.return<caret>
else:
return 2
@@ -0,0 +1,5 @@
def d(a):
if a:
return 1
else:
return 2
@@ -0,0 +1,3 @@
def f(a):
b = a.return<caret>
return max((a + (a + b)), b)
@@ -0,0 +1,3 @@
def f(a):
b = a.return <caret>
return max((a + (a + b)), b)
+2
View File
@@ -0,0 +1,2 @@
def f():
1.return<caret>
@@ -0,0 +1,2 @@
def f():
return 1
@@ -30,7 +30,7 @@ public class PyNotPostfixTemplateTest extends PyPostfixTemplateTestCase {
}
@Override
protected String getTestDataPath() {
return super.getTestDataPath() + "not/";
protected String getTestDataDir() {
return "not/";
}
}
@@ -30,7 +30,7 @@ public class PyParenthesizedExpressionPostfixTemplateTest extends PyPostfixTempl
}
@Override
protected String getTestDataPath() {
return super.getTestDataPath() + "par/";
protected String getTestDataDir() {
return "par/";
}
}
@@ -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();
}
}
@@ -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/";
}
}