mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-CR-49176: inline function is made possible for functions with lambdas (PY-36373)
GitOrigin-RevId: 4f2603447f760d959d31d5033944bae85ad68bf8
This commit is contained in:
committed by
intellij-monorepo-bot
parent
05f36652c9
commit
3749eba8e1
@@ -43,7 +43,7 @@ class PyInlineFunctionHandler : InlineActionHandler() {
|
||||
isOverride(element, project) -> "refactoring.inline.function.overridden"
|
||||
functionScope.hasGlobals() -> "refactoring.inline.function.global"
|
||||
functionScope.hasNonLocals() -> "refactoring.inline.function.nonlocal"
|
||||
functionScope.hasNestedScopes() -> "refactoring.inline.function.nested"
|
||||
hasNestedFunction(element) -> "refactoring.inline.function.nested"
|
||||
hasNonExhaustiveIfs(element) -> "refactoring.inline.function.interrupts.flow"
|
||||
else -> null
|
||||
}
|
||||
@@ -56,6 +56,8 @@ class PyInlineFunctionHandler : InlineActionHandler() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun hasNestedFunction(function: PyFunction): Boolean = SyntaxTraverser.psiTraverser(function.statementList).traverse().any { it is PyFunction }
|
||||
|
||||
private fun hasNonExhaustiveIfs(function: PyFunction): Boolean {
|
||||
val returns = mutableListOf<PyReturnStatement>()
|
||||
|
||||
|
||||
@@ -181,9 +181,12 @@ class PyInlineFunctionProcessor(project: Project,
|
||||
replacementFunction.accept(object : PyRecursiveElementVisitor() {
|
||||
override fun visitPyReferenceExpression(node: PyReferenceExpression) {
|
||||
if (!node.isQualified) {
|
||||
when (val name = node.name) {
|
||||
in mappedArguments -> argumentReplacements[node] = mappedArguments[name]!!
|
||||
in nameClashes -> nameClashRefs.putValue(name!!, node)
|
||||
val parentLambda = PsiTreeUtil.getParentOfType(node, PyLambdaExpression::class.java)
|
||||
if (parentLambda == null || parentLambda.parameterList.parameters.none { it.name == node.name }) {
|
||||
when (val name = node.name) {
|
||||
in mappedArguments -> argumentReplacements[node] = mappedArguments[name]!!
|
||||
in nameClashes -> nameClashRefs.putValue(name!!, node)
|
||||
}
|
||||
}
|
||||
}
|
||||
super.visitPyReferenceExpression(node)
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
def foo(lst):
|
||||
a = 1
|
||||
res = map(lambda x: x + a + 1, lst)
|
||||
return list(res)
|
||||
|
||||
|
||||
a = 2
|
||||
|
||||
lst = [1, 2]
|
||||
a1 = 1
|
||||
res = map(lambda x: x + a1 + 1, lst)
|
||||
x = list(res)
|
||||
@@ -0,0 +1,9 @@
|
||||
def foo(lst):
|
||||
a = 1
|
||||
res = map(lambda x: x + a + 1, lst)
|
||||
return list(res)
|
||||
|
||||
|
||||
a = 2
|
||||
|
||||
x = fo<caret>o([1, 2])
|
||||
@@ -73,6 +73,7 @@ class PyInlineFunctionTest : PyTestCase() {
|
||||
fun testCallFromStaticMethod() = doTest()
|
||||
fun testCallFromClassMethod() = doTest()
|
||||
fun testComplexQualifier() = doTest()
|
||||
fun testFunctionWithLambda() = doTest()
|
||||
fun testRemovingDocstring() = doTest()
|
||||
fun testKeepFirstComment() = doTest()
|
||||
//fun testInlineImportedAs() = doTest(inlineThis = false)
|
||||
|
||||
Reference in New Issue
Block a user