IDEA-CR-49176: inline function doesn't copy docstring, but keeps the first-line comment (PY-36469, PY-36491)

GitOrigin-RevId: db1fbb5178892db955678dfcec684a5af7f987c6
This commit is contained in:
Aleksei Kniazev
2019-07-08 18:05:36 +03:00
committed by intellij-monorepo-bot
parent af8d542c7d
commit 72e24eef45
6 changed files with 51 additions and 3 deletions
@@ -121,6 +121,7 @@ class PyInlineFunctionProcessor(project: Project,
val functionScope = ControlFlowCache.getScope(myFunction)
PyClassRefactoringUtil.rememberNamedReferences(myFunction)
val hasDocstring = myFunction.docStringExpression != null
references.forEach { usage ->
val reference = usage.element as PyReferenceExpression
@@ -231,11 +232,19 @@ class PyInlineFunctionProcessor(project: Project,
CodeStyleManager.getInstance(myProject).reformat(replacementFunction, true)
declarations.forEach { containingStatement.parent.addBefore(it, containingStatement) }
val insertElement = { elem: PsiElement -> containingStatement.parent.addBefore(elem, containingStatement) }
declarations.forEach { insertElement(it) }
if (replacementFunction.firstChild != null) {
val statements = replacementFunction.statements
if (replacementFunction.firstChild is PsiComment) {
SyntaxTraverser.psiApi().children(replacementFunction)
.takeWhile { it is PsiComment || it is PsiWhiteSpace }
.filterIsInstance<PsiComment>()
.forEach { insertElement(it) }
}
val statements = if (hasDocstring) replacementFunction.statements.drop(1) else replacementFunction.statements.toList()
statements.asSequence()
.map { containingStatement.parent.addBefore(it, containingStatement) }
.map { insertElement(it) }
.forEach { PyClassRefactoringUtil.restoreNamedReferences(it) }
}
@@ -0,0 +1,9 @@
def foo():
# comment on the first line
print(42)
return 42
# comment on the first line
print(42)
res = 42
@@ -0,0 +1,7 @@
def foo():
# comment on the first line
print(42)
return 42
res = fo<caret>o()
@@ -0,0 +1,11 @@
def foo():
"""
Docstring for the function
:return: int
"""
print(42)
return 42
print(42)
res = 42
@@ -0,0 +1,10 @@
def foo():
"""
Docstring for the function
:return: int
"""
print(42)
return 42
res = fo<caret>o()
@@ -73,6 +73,8 @@ class PyInlineFunctionTest : PyTestCase() {
fun testCallFromStaticMethod() = doTest()
fun testCallFromClassMethod() = doTest()
fun testComplexQualifier() = doTest()
fun testRemovingDocstring() = doTest()
fun testKeepFirstComment() = doTest()
//fun testInlineImportedAs() = doTest(inlineThis = false)
fun testRemoveFunctionWithStub() {
doTest(inlineThis = false, remove = true)