From 1f258240d05b8e6384f0575327800fd130deffbf Mon Sep 17 00:00:00 2001 From: Semyon Proshev Date: Sat, 6 Jun 2020 01:10:18 +0300 Subject: [PATCH] Disable suggested refactorings for everything inside stubs or having such stubs (PY-42285) Since we can't correctly apply refactorings to stubs and implementations. GitOrigin-RevId: a4ac64f93d9c894908eb0d72fd1e9c9624d7094b --- .../suggested/PySuggestedRefactoringSupport.kt | 13 +++++++++++-- .../suggested/PySuggestedRefactoringTest.kt | 9 +++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/python/src/com/jetbrains/python/refactoring/suggested/PySuggestedRefactoringSupport.kt b/python/src/com/jetbrains/python/refactoring/suggested/PySuggestedRefactoringSupport.kt index dc42ea6d2019..b06ab80e65f5 100644 --- a/python/src/com/jetbrains/python/refactoring/suggested/PySuggestedRefactoringSupport.kt +++ b/python/src/com/jetbrains/python/refactoring/suggested/PySuggestedRefactoringSupport.kt @@ -9,6 +9,7 @@ import com.intellij.psi.util.hasErrorElementInRange import com.intellij.refactoring.suggested.* import com.jetbrains.python.PyNames import com.jetbrains.python.PyTokenTypes +import com.jetbrains.python.psi.PyElement import com.jetbrains.python.psi.PyFunction import com.jetbrains.python.psi.PyParameter import com.jetbrains.python.psi.PyParameterList @@ -22,7 +23,7 @@ class PySuggestedRefactoringSupport : SuggestedRefactoringSupport { return element is PyFunction && element.name.let { it != null && PyNames.isIdentifier(it) } && element.property == null && - !PyiUtil.isOverload(element, TypeEvalContext.codeAnalysis(element.project, element.containingFile)) + !shouldBeSuppressed(element) } internal fun defaultValue(parameter: SuggestedRefactoringSupport.Parameter): String? { @@ -33,7 +34,15 @@ class PySuggestedRefactoringSupport : SuggestedRefactoringSupport { return element is PsiNameIdentifierOwner && element.name.let { it != null && PyNames.isIdentifier(it) } && (element !is PyParameter || containingFunction(element).let { it != null && !isAvailableForChangeSignature(it) }) && - !PyiUtil.isOverload(element, TypeEvalContext.codeAnalysis(element.project, element.containingFile)) + !shouldBeSuppressed(element) + } + + private fun shouldBeSuppressed(element: PsiElement): Boolean { + if (PyiUtil.isInsideStub(element)) return true + if (element is PyElement && PyiUtil.getPythonStub(element) != null) return true + if (PyiUtil.isOverload(element, TypeEvalContext.codeAnalysis(element.project, element.containingFile))) return true + + return false } private fun containingFunction(parameter: PyParameter): PyFunction? { diff --git a/python/testSrc/com/jetbrains/python/refactoring/suggested/PySuggestedRefactoringTest.kt b/python/testSrc/com/jetbrains/python/refactoring/suggested/PySuggestedRefactoringTest.kt index 9d253d1b4479..09db4b3f9bf6 100644 --- a/python/testSrc/com/jetbrains/python/refactoring/suggested/PySuggestedRefactoringTest.kt +++ b/python/testSrc/com/jetbrains/python/refactoring/suggested/PySuggestedRefactoringTest.kt @@ -766,16 +766,13 @@ class PySuggestedRefactoringTest : PyTestCase() { myFixture.copyFileToProject("$testDataPathPrefix/$source", source) - doChangeSignatureTest( + doNoIntentionTest( """ def foo(p1): print(p1) """.trimIndent(), - """ - def foo(p12): - print(p12) - """.trimIndent(), - { myFixture.type("2") } + { myFixture.type("2") }, + intention = changeSignatureIntention() ) }