From fb11ee327cb65d70390cc1251f4babc8e208291a Mon Sep 17 00:00:00 2001 From: Daniil Kalinin Date: Mon, 7 Mar 2022 16:56:23 +0300 Subject: [PATCH] PY-53200 do not autocomplete parameter list if method already have one Test for PY-53200 Better tests for PY-53200 PY-53200 Removed unnecessary test. `testMethodNamesDoNotDuplicateParameterList` renamed and moved closer to the corresponding ones Removed test for method names in suggestions restored GitOrigin-RevId: 51b0721190718c44810c16e198bad583c2af67d0 --- .../python/PythonCommonCompletionTest.java | 15 ++++++++++ .../PySuperMethodCompletionContributor.java | 30 +++++++++++-------- .../a.py | 10 +++++++ ...erMethodWithExistingParameterList.after.py | 7 +++++ .../superMethodWithExistingParameterList.py | 7 +++++ 5 files changed, 56 insertions(+), 13 deletions(-) create mode 100644 python/testData/completion/methodNamesSuggestedWithoutParameterListIfItIsAlreadyExist/a.py create mode 100644 python/testData/completion/superMethodWithExistingParameterList.after.py create mode 100644 python/testData/completion/superMethodWithExistingParameterList.py diff --git a/python/python-common-tests/com/jetbrains/python/PythonCommonCompletionTest.java b/python/python-common-tests/com/jetbrains/python/PythonCommonCompletionTest.java index 93d8299e4a99..2a486c9bde2a 100644 --- a/python/python-common-tests/com/jetbrains/python/PythonCommonCompletionTest.java +++ b/python/python-common-tests/com/jetbrains/python/PythonCommonCompletionTest.java @@ -335,6 +335,11 @@ public abstract class PythonCommonCompletionTest extends PythonCommonTestCase { doTest(); } + // PY-53200 + public void testSuperMethodWithExistingParameterList() { + doTest(); + } + public void testLocalVarInDictKey() { // PY-2558 doTest(); } @@ -1880,6 +1885,16 @@ public abstract class PythonCommonCompletionTest extends PythonCommonTestCase { }); } + // PY-53200 + public void testMethodNamesSuggestedWithoutParameterListIfItIsAlreadyExist() { + myFixture.copyDirectoryToProject(getTestName(true), ""); + myFixture.configureByFile("a.py"); + myFixture.completeBasic(); + List suggested = myFixture.getLookupElementStrings(); + assertNotNull(suggested); + assertContainsElements(suggested, "something_a", "something_b"); + } + private void doTestHasattrContributor(String[] inList, String[] notInList) { doTestHasattrContributor("hasattrCompletion/" + getTestName(true) + ".py", inList, notInList); } diff --git a/python/python-psi-impl/src/com/jetbrains/python/codeInsight/completion/PySuperMethodCompletionContributor.java b/python/python-psi-impl/src/com/jetbrains/python/codeInsight/completion/PySuperMethodCompletionContributor.java index 800bcfd6da90..f486f3d4c392 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/codeInsight/completion/PySuperMethodCompletionContributor.java +++ b/python/python-psi-impl/src/com/jetbrains/python/codeInsight/completion/PySuperMethodCompletionContributor.java @@ -29,6 +29,7 @@ import com.jetbrains.python.PyTokenTypes; import com.jetbrains.python.psi.LanguageLevel; import com.jetbrains.python.psi.PyClass; import com.jetbrains.python.psi.PyFunction; +import com.jetbrains.python.psi.PyParameterList; import org.jetbrains.annotations.NotNull; import java.util.HashSet; @@ -48,6 +49,7 @@ public class PySuperMethodCompletionContributor extends CompletionContributor im @NotNull CompletionResultSet result) { PsiElement position = parameters.getOriginalPosition(); PyClass containingClass = PsiTreeUtil.getParentOfType(position, PyClass.class); + PsiElement nextElement = position != null ? position.getNextSibling() : null; if (containingClass == null && position instanceof PsiWhiteSpace) { position = PsiTreeUtil.prevLeaf(position); containingClass = PsiTreeUtil.getParentOfType(position, PyClass.class); @@ -65,19 +67,21 @@ public class PySuperMethodCompletionContributor extends CompletionContributor im for (PyFunction superMethod : ancestor.getMethods()) { if (!seenNames.contains(superMethod.getName())) { StringBuilder builder = new StringBuilder(); - builder.append(superMethod.getName()) - .append(superMethod.getParameterList().getText()); - if (superMethod.getAnnotation() != null) { - builder.append(" ") - .append(superMethod.getAnnotation().getText()) - .append(":"); - } - else if (superMethod.getTypeComment() != null) { - builder.append(": ") - .append(superMethod.getTypeComment().getText()); - } - else { - builder.append(":"); + builder.append(superMethod.getName()); + if (!(nextElement instanceof PyParameterList)) { + builder.append(superMethod.getParameterList().getText()); + if (superMethod.getAnnotation() != null) { + builder.append(" ") + .append(superMethod.getAnnotation().getText()) + .append(":"); + } + else if (superMethod.getTypeComment() != null) { + builder.append(": ") + .append(superMethod.getTypeComment().getText()); + } + else { + builder.append(":"); + } } LookupElementBuilder element = LookupElementBuilder.create(builder.toString()); result.addElement(TailTypeDecorator.withTail(element, TailType.NONE)); diff --git a/python/testData/completion/methodNamesSuggestedWithoutParameterListIfItIsAlreadyExist/a.py b/python/testData/completion/methodNamesSuggestedWithoutParameterListIfItIsAlreadyExist/a.py new file mode 100644 index 000000000000..8ef939554d59 --- /dev/null +++ b/python/testData/completion/methodNamesSuggestedWithoutParameterListIfItIsAlreadyExist/a.py @@ -0,0 +1,10 @@ +class Me: + def something_a(self): + ... + + def something_b(self): + ... + +class Stub(Me): + def something_(self): + ... \ No newline at end of file diff --git a/python/testData/completion/superMethodWithExistingParameterList.after.py b/python/testData/completion/superMethodWithExistingParameterList.after.py new file mode 100644 index 000000000000..5cbce6c4fe04 --- /dev/null +++ b/python/testData/completion/superMethodWithExistingParameterList.after.py @@ -0,0 +1,7 @@ +class Me: + def something_a(self): + ... + +class Stub(Me): + def something_a(self): + ... \ No newline at end of file diff --git a/python/testData/completion/superMethodWithExistingParameterList.py b/python/testData/completion/superMethodWithExistingParameterList.py new file mode 100644 index 000000000000..6c33878aa938 --- /dev/null +++ b/python/testData/completion/superMethodWithExistingParameterList.py @@ -0,0 +1,7 @@ +class Me: + def something_a(self): + ... + +class Stub(Me): + def something_(self): + ... \ No newline at end of file