From b2ef103d5058958e7f095405afae3a54c13bb17e Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Tue, 26 Aug 2025 16:55:10 +0300 Subject: [PATCH] PY-83665 Stop excluding too short names from completion for importable names Originally, when PY-62208 was first implemented, importable variants were always suggested together with everything else, polluting the completion lookup with irrelevant "leaked" top-level elements, such as loop variables, temporary variables, etc. We first tried to exclude them by introducing this artificial threshold on "too short" names that presumably could be typed manually and then imported with a quick fix. It was possible to still complete them with a second invocation of the hotkey. Eventually, we stopped offering importable variants at all if there are variants from other completion contributors in PY-75580. In other words, they are suggested only when a sufficiently long prefix has been typed to distinguish it from built-in symbols and other names already available in scope. Now it seems that this filtering on name length is redundant and often gets in the way, preventing some common names, such as `Any`, `Enum` or `cast`, from being suggested. GitOrigin-RevId: e10444d5bbbdde365f89aca38bb6f8f85940dd78 --- .../jetbrains/python/PythonCommonCompletionTest.java | 12 ------------ .../completion/PyClassNameCompletionContributor.java | 7 ------- .../a.py | 1 - .../mod.py | 6 ------ .../noCaseOutsideMatchStatement.after.py | 1 - .../python/PythonKeywordCompletionTest.java | 8 +++++++- 6 files changed, 7 insertions(+), 28 deletions(-) delete mode 100644 python/testData/completion/tooShortImportableNamesSuggestedOnlyInExtendedCompletion/a.py delete mode 100644 python/testData/completion/tooShortImportableNamesSuggestedOnlyInExtendedCompletion/mod.py delete mode 100644 python/testData/keywordCompletion/noCaseOutsideMatchStatement.after.py diff --git a/python/python-common-tests/com/jetbrains/python/PythonCommonCompletionTest.java b/python/python-common-tests/com/jetbrains/python/PythonCommonCompletionTest.java index 50f985e046c6..331654f1909b 100644 --- a/python/python-common-tests/com/jetbrains/python/PythonCommonCompletionTest.java +++ b/python/python-common-tests/com/jetbrains/python/PythonCommonCompletionTest.java @@ -2253,18 +2253,6 @@ public abstract class PythonCommonCompletionTest extends PythonCommonTestCase { }); } - // PY-62208 - public void testTooShortImportableNamesSuggestedOnlyInExtendedCompletion() { - myFixture.copyDirectoryToProject(getTestName(true), ""); - myFixture.configureByFile("a.py"); - myFixture.complete(CompletionType.BASIC, 1); - List basicCompletionVariants = myFixture.getLookupElementStrings(); - assertDoesntContain(basicCompletionVariants, "c1", "c2"); - myFixture.complete(CompletionType.BASIC, 2); - List extendedCompletionVariants = myFixture.getLookupElementStrings(); - assertContainsElements(extendedCompletionVariants, "c1", "c2"); - } - private static void runWithImportableNamesInBasicCompletionDisabled(@NotNull Runnable action) { PyCodeInsightSettings settings = PyCodeInsightSettings.getInstance(); boolean old = settings.INCLUDE_IMPORTABLE_NAMES_IN_BASIC_COMPLETION; diff --git a/python/python-psi-impl/src/com/jetbrains/python/codeInsight/completion/PyClassNameCompletionContributor.java b/python/python-psi-impl/src/com/jetbrains/python/codeInsight/completion/PyClassNameCompletionContributor.java index b3a6f94370dd..6f877e6d3993 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/codeInsight/completion/PyClassNameCompletionContributor.java +++ b/python/python-psi-impl/src/com/jetbrains/python/codeInsight/completion/PyClassNameCompletionContributor.java @@ -52,7 +52,6 @@ public final class PyClassNameCompletionContributor extends PyImportableNameComp // See https://plugins.jetbrains.com/plugin/18465-sputnik private static final boolean TRACING_WITH_SPUTNIK_ENABLED = false; private static final Logger LOG = Logger.getInstance(PyClassNameCompletionContributor.class); - private static final int NAME_TOO_SHORT_FOR_BASIC_COMPLETION_THRESHOLD = 5; // See PY-73964, IJPL-265 private static final boolean RECURSIVE_INDEX_ACCESS_ALLOWED = false; @@ -129,10 +128,6 @@ public final class PyClassNameCompletionContributor extends PyImportableNameComp forEachPublicNameFromIndex(scope, elementName -> { ProgressManager.checkCanceled(); counters.scannedNames++; - if (elementName.length() < NAME_TOO_SHORT_FOR_BASIC_COMPLETION_THRESHOLD && !isExtendedCompletion) { - counters.tooShortNames++; - return true; - } if (!result.getPrefixMatcher().isStartMatch(elementName)) return true; return stubIndex.processElements(PyExportedModuleAttributeIndex.KEY, elementName, project, scope, PyElement.class, exported -> { ProgressManager.checkCanceled(); @@ -330,7 +325,6 @@ public final class PyClassNameCompletionContributor extends PyImportableNameComp private static class Counters { int scannedNames; int privateNames; - int tooShortNames; int notApplicableInContextNames; int totalVariants; @@ -339,7 +333,6 @@ public final class PyClassNameCompletionContributor extends PyImportableNameComp return "Counters{" + "scannedNames=" + scannedNames + ", privateNames=" + privateNames + - ", tooShortNames=" + tooShortNames + ", notApplicableInContextNames=" + notApplicableInContextNames + ", totalVariants=" + totalVariants + '}'; diff --git a/python/testData/completion/tooShortImportableNamesSuggestedOnlyInExtendedCompletion/a.py b/python/testData/completion/tooShortImportableNamesSuggestedOnlyInExtendedCompletion/a.py deleted file mode 100644 index 37118d6a3f04..000000000000 --- a/python/testData/completion/tooShortImportableNamesSuggestedOnlyInExtendedCompletion/a.py +++ /dev/null @@ -1 +0,0 @@ -c \ No newline at end of file diff --git a/python/testData/completion/tooShortImportableNamesSuggestedOnlyInExtendedCompletion/mod.py b/python/testData/completion/tooShortImportableNamesSuggestedOnlyInExtendedCompletion/mod.py deleted file mode 100644 index 19a9e895583e..000000000000 --- a/python/testData/completion/tooShortImportableNamesSuggestedOnlyInExtendedCompletion/mod.py +++ /dev/null @@ -1,6 +0,0 @@ -s = "foo" -for c1, c2 in zip(s, s[1:]): - pass - - - diff --git a/python/testData/keywordCompletion/noCaseOutsideMatchStatement.after.py b/python/testData/keywordCompletion/noCaseOutsideMatchStatement.after.py deleted file mode 100644 index 5b8988355bee..000000000000 --- a/python/testData/keywordCompletion/noCaseOutsideMatchStatement.after.py +++ /dev/null @@ -1 +0,0 @@ -cas \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PythonKeywordCompletionTest.java b/python/testSrc/com/jetbrains/python/PythonKeywordCompletionTest.java index da062daed52c..a4c1aab53db8 100644 --- a/python/testSrc/com/jetbrains/python/PythonKeywordCompletionTest.java +++ b/python/testSrc/com/jetbrains/python/PythonKeywordCompletionTest.java @@ -1,6 +1,7 @@ // Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.jetbrains.python; +import com.intellij.codeInsight.CodeInsightSettings; import com.intellij.codeInsight.completion.impl.CamelHumpMatcher; import com.jetbrains.python.codeInsight.completion.PyModuleNameCompletionContributor; import com.jetbrains.python.fixtures.PyTestCase; @@ -309,7 +310,12 @@ public class PythonKeywordCompletionTest extends PyTestCase { // PY-48039 public void testNoCaseOutsideMatchStatement() { - doTest(); + CodeInsightSettings.runWithTemporarySettings(settings -> { + settings.AUTOCOMPLETE_ON_CODE_COMPLETION = false; + List variants = doTestByTestName(); + assertDoesntContain(variants, PyNames.CASE); + return null; + }); } // PY-49728