mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
added (PY-1242) Refactoring: Convert lambda to function
This commit is contained in:
@@ -148,6 +148,11 @@
|
||||
<category>Python</category>
|
||||
</intentionAction>
|
||||
|
||||
<intentionAction>
|
||||
<className>com.jetbrains.python.codeInsight.intentions.PyConvertLambdaToFunctionIntention</className>
|
||||
<category>Python</category>
|
||||
</intentionAction>
|
||||
|
||||
<stubElementTypeHolder class="com.jetbrains.python.PyElementTypes"/>
|
||||
<inspectionToolProvider implementation="com.jetbrains.python.inspections.PythonInspectionToolProvider"/>
|
||||
|
||||
|
||||
@@ -130,7 +130,9 @@ INTN.convert.dict.literal.to.dict.constructor=Convert dict literal to dict const
|
||||
|
||||
INTN.quoted.string=Convert between single-quoted and double-quoted strings
|
||||
INTN.quoted.string.single.to.double=Convert single-quoted string to double-quoted string
|
||||
INTN.quoted.string.double.to.single=Convert double-quoted string to sungle-quoted string
|
||||
INTN.quoted.string.double.to.single=Convert double-quoted string to single-quoted string
|
||||
|
||||
INTN.convert.lambda.to.function=Convert lambda to function
|
||||
|
||||
# Conflict checker
|
||||
CONFLICT.name.$0.obscured=Name ''{0}'' obscured by local definitions
|
||||
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
package com.jetbrains.python.codeInsight.intentions;
|
||||
|
||||
import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
|
||||
import com.intellij.openapi.application.Application;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.jetbrains.python.PyBundle;
|
||||
import com.jetbrains.python.psi.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* User: catherine
|
||||
* Intention to convert lambda to function
|
||||
*/
|
||||
public class PyConvertLambdaToFunctionIntention extends BaseIntentionAction {
|
||||
|
||||
@NotNull
|
||||
public String getFamilyName() {
|
||||
return PyBundle.message("INTN.convert.lambda.to.function");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getText() {
|
||||
return PyBundle.message("INTN.convert.lambda.to.function");
|
||||
}
|
||||
|
||||
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
|
||||
PyLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyLambdaExpression.class);
|
||||
if (lambdaExpression != null) {
|
||||
if (lambdaExpression.getBody() != null)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
|
||||
PyLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyLambdaExpression.class);
|
||||
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
|
||||
if (lambdaExpression != null) {
|
||||
String name;
|
||||
PsiElement parent = lambdaExpression.getParent();
|
||||
if (parent instanceof PyAssignmentStatement) {
|
||||
name = ((PyAssignmentStatement)parent).getLeftHandSideExpression().getText();
|
||||
}
|
||||
else {
|
||||
Application application = ApplicationManager.getApplication();
|
||||
if (application != null && !application.isUnitTestMode()) {
|
||||
AskNameDialog dialog = new AskNameDialog(project);
|
||||
dialog.setTitle("Enter function name");
|
||||
dialog.show();
|
||||
if (!dialog.isOK()) return; // 'Cancel' button cancels everything
|
||||
name = dialog.getAlias();
|
||||
if (name.isEmpty()) return;
|
||||
}
|
||||
else
|
||||
name = "function";
|
||||
}
|
||||
|
||||
PyExpression body = lambdaExpression.getBody();
|
||||
PyParameter[] parameters = lambdaExpression.getParameterList().getParameters();
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
stringBuilder.append("def ");
|
||||
stringBuilder.append(name);
|
||||
stringBuilder.append("(");
|
||||
int size = parameters.length;
|
||||
for (int i = 0; i != size; ++i) {
|
||||
PyParameter parameter = parameters[i];
|
||||
stringBuilder.append(parameter.getName());
|
||||
if ( i != size - 1)
|
||||
stringBuilder.append(",");
|
||||
}
|
||||
stringBuilder.append("):\n return ");
|
||||
stringBuilder.append(body.getText());
|
||||
|
||||
PyFunction function = elementGenerator.createFromText(LanguageLevel.forElement(lambdaExpression),
|
||||
PyFunction.class, stringBuilder.toString());
|
||||
|
||||
PyFunction pyFunction = PsiTreeUtil.getParentOfType(lambdaExpression, PyFunction.class);
|
||||
if (pyFunction != null) {
|
||||
pyFunction.getStatementList().addBefore(function, pyFunction.getStatementList().getStatements()[0]);
|
||||
}
|
||||
else {
|
||||
PyStatement statement = PsiTreeUtil.getParentOfType(lambdaExpression, PyStatement.class);
|
||||
file.addBefore(function, statement);
|
||||
}
|
||||
if (parent instanceof PyAssignmentStatement) {
|
||||
parent.delete();
|
||||
}
|
||||
else {
|
||||
lambdaExpression.replace(elementGenerator.createFromText(LanguageLevel.forElement(lambdaExpression), PyExpression.class,
|
||||
name));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
def func(seq):
|
||||
def function(x, y):
|
||||
return (x + y) / y
|
||||
|
||||
newlist = reduce(function
|
||||
, seq)
|
||||
@@ -0,0 +1,2 @@
|
||||
def func(seq):
|
||||
newlist = reduce(lambda x<caret>, y: (x+y)/y, seq)
|
||||
@@ -124,4 +124,8 @@ public class PyIntentionTest extends PyLightFixtureTestCase {
|
||||
public void testQuotedString() {
|
||||
doTest(PyBundle.message("INTN.quoted.string.double.to.single"));
|
||||
}
|
||||
|
||||
public void testConvertLambdaToFunction() {
|
||||
doTest(PyBundle.message("INTN.convert.lambda.to.function"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user