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
This commit is contained in:
Mikhail Golubev
2025-09-05 13:12:34 +00:00
committed by intellij-monorepo-bot
parent b1b8c766df
commit b2ef103d50
6 changed files with 7 additions and 28 deletions
@@ -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<String> basicCompletionVariants = myFixture.getLookupElementStrings();
assertDoesntContain(basicCompletionVariants, "c1", "c2");
myFixture.complete(CompletionType.BASIC, 2);
List<String> 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;
@@ -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 +
'}';
@@ -1,6 +0,0 @@
s = "foo"
for c1, c2 in zip(s, s[1:]):
pass
@@ -1 +0,0 @@
cas<caret>
@@ -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<String> variants = doTestByTestName();
assertDoesntContain(variants, PyNames.CASE);
return null;
});
}
// PY-49728