PY-36374 Don't pass the name to ImportCandidateHolder.getPresentableText

This name is now accessible directly as ImportCandidateHolder.getImportableName.

GitOrigin-RevId: d217b401e4bffbffd6f8d9b8d3179ea9db3bc295
This commit is contained in:
Mikhail Golubev
2021-01-07 17:25:32 +00:00
committed by intellij-monorepo-bot
parent 57251de36e
commit 46e8bf3f76
5 changed files with 25 additions and 30 deletions
@@ -121,7 +121,7 @@ public class AutoImportQuickFix extends LocalQuickFixOnPsiElement implements Hig
public String getText() {
if (myUseQualifiedImport) return PyPsiBundle.message("ACT.qualify.with.module");
else if (myImports.size() == 1) {
return PyPsiBundle.message("QFIX.auto.import.import.name", myImports.get(0).getPresentableText(myInitialName));
return PyPsiBundle.message("QFIX.auto.import.import.name", myImports.get(0).getPresentableText());
}
else {
return PyPsiBundle.message("QFIX.auto.import.import.this.name");
@@ -126,10 +126,10 @@ public class ImportCandidateHolder implements Comparable<ImportCandidateHolder>
}
@NotNull
public @NlsSafe String getPresentableText(@NotNull String myName) {
public @NlsSafe String getPresentableText() {
PyImportElement importElement = getImportElement();
PsiElement importable = getImportable();
final StringBuilder sb = new StringBuilder(getQualifiedName(myName, myPath, importElement));
final StringBuilder sb = new StringBuilder(getQualifiedName(getImportableName(), myPath, importElement));
PsiElement parent = null;
if (importElement != null) {
parent = importElement.getParent();
@@ -30,12 +30,12 @@ public class PyImportChooser implements ImportChooser {
// GUI part
DataManager.getInstance().getDataContextFromFocus().doWhenDone((Consumer<DataContext>)dataContext -> JBPopupFactory.getInstance()
.createPopupChooserBuilder(sources)
.setRenderer(new CellRenderer(name))
.setRenderer(new CellRenderer())
.setTitle(useQualifiedImport ? PyPsiBundle.message("ACT.qualify.with.module") : PyPsiBundle.message("ACT.from.some.module.import"))
.setItemChosenCallback(item -> {
result.setResult(item);
})
.setNamerForFiltering(o -> o.getPresentableText(name))
.setNamerForFiltering(o -> o.getPresentableText())
.createPopup()
.showInBestPositionFor(dataContext));
@@ -45,10 +45,8 @@ public class PyImportChooser implements ImportChooser {
// Stolen from FQNameCellRenderer
private static class CellRenderer extends SimpleColoredComponent implements ListCellRenderer<ImportCandidateHolder> {
private final Font FONT;
private final String myName;
CellRenderer(String name) {
myName = name;
CellRenderer() {
EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
FONT = new Font(scheme.getEditorFontName(), Font.PLAIN, scheme.getEditorFontSize());
setOpaque(true);
@@ -66,7 +64,7 @@ public class PyImportChooser implements ImportChooser {
if (importable != null) {
setIcon(importable.getIcon(0));
}
String item_name = value.getPresentableText(myName);
String item_name = value.getPresentableText();
append(item_name, SimpleTextAttributes.REGULAR_ATTRIBUTES);
setFont(FONT);
@@ -570,7 +570,7 @@ public abstract class PyTestCase extends UsefulTestCase {
if (expected.length > 0) {
T prev = expected[0];
int prevIndex = actualList.indexOf(prev);
assertTrue(prevIndex >= 0);
assertTrue(prev + " is not found in " + actualList, prevIndex >= 0);
for (int i = 1; i < expected.length; i++) {
final T next = expected[i];
final int nextIndex = actualList.indexOf(next);
@@ -19,10 +19,8 @@ import com.intellij.lang.injection.InjectedLanguageManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFileSystemItem;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.QualifiedName;
import com.intellij.util.ObjectUtils;
import com.intellij.util.Processor;
import com.intellij.util.containers.ContainerUtil;
@@ -34,7 +32,6 @@ import com.jetbrains.python.formatter.PyCodeStyleSettings;
import com.jetbrains.python.inspections.unresolvedReference.PyUnresolvedReferencesInspection;
import com.jetbrains.python.psi.LanguageLevel;
import com.jetbrains.python.psi.PyReferenceExpression;
import com.jetbrains.python.psi.resolve.QualifiedNameFinder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -61,7 +58,7 @@ public class PyAddImportQuickFixTest extends PyQuickFixTestCase {
Consumer<VirtualFile> fileConsumer = file -> {
doMultiFileAutoImportTest("Import", fix -> {
final List<ImportCandidateHolder> candidates = fix.getCandidates();
final List<String> names = ContainerUtil.map(candidates, c -> c.getPresentableText("join"));
final List<String> names = ContainerUtil.map(candidates, c -> c.getPresentableText());
assertSameElements(names, "os.path.join()");
return true;
});
@@ -90,7 +87,7 @@ public class PyAddImportQuickFixTest extends PyQuickFixTestCase {
public void testCanonicalNamesFromHigherLevelPackage() {
doMultiFileAutoImportTest("Import", fix -> {
final List<ImportCandidateHolder> candidates = fix.getCandidates();
final List<String> names = ContainerUtil.map(candidates, c -> c.getPresentableText("MyClass"));
final List<String> names = ContainerUtil.map(candidates, c -> c.getPresentableText());
assertOrderedEquals(names, "bar.MyClass", "foo.MyClass");
return true;
});
@@ -137,7 +134,7 @@ public class PyAddImportQuickFixTest extends PyQuickFixTestCase {
// PY-16176
public void testAllVariantsSuggestedWhenExistingNonProjectImportFits() {
doMultiFileAutoImportTest("Import", quickfix -> {
final List<String> candidates = ContainerUtil.map(quickfix.getCandidates(), c -> c.getPresentableText("time"));
final List<String> candidates = ContainerUtil.map(quickfix.getCandidates(), c -> c.getPresentableText());
assertOrderedEquals(candidates, "time from datetime", "time");
return false;
});
@@ -146,7 +143,7 @@ public class PyAddImportQuickFixTest extends PyQuickFixTestCase {
// PY-16176
public void testExistingImportsAlwaysSuggestedFirstEvenIfLonger() {
doMultiFileAutoImportTest("Import", quickfix -> {
final List<String> candidates = ContainerUtil.map(quickfix.getCandidates(), c -> c.getPresentableText("ClassB"));
final List<String> candidates = ContainerUtil.map(quickfix.getCandidates(), c -> c.getPresentableText());
assertOrderedEquals(candidates, "ClassB from long.pkg.path", "short.ClassB");
return false;
});
@@ -155,7 +152,7 @@ public class PyAddImportQuickFixTest extends PyQuickFixTestCase {
// PY-16176
public void testExistingImportsAlwaysSuggestedFirstEvenIfNonProject() {
doMultiFileAutoImportTest("Import", quickfix -> {
final List<String> candidates = ContainerUtil.map(quickfix.getCandidates(), c -> c.getPresentableText("datetime"));
final List<String> candidates = ContainerUtil.map(quickfix.getCandidates(), c -> c.getPresentableText());
assertOrderedEquals(candidates, "datetime(date) from datetime", "mod.datetime");
return false;
});
@@ -191,8 +188,8 @@ public class PyAddImportQuickFixTest extends PyQuickFixTestCase {
runWithAdditionalFileInLibDir(
"os/path.py",
"",
(___) -> doTestProposedImportsOrdering("path",
"path from sys", "first.path", "first.second.path()", "os.path", "first._third.path")
(___) -> doTestProposedImportsOrdering(
"path from sys", "first.path", "first.second.path()", "os.path", "first._third.path")
)
);
}
@@ -202,7 +199,7 @@ public class PyAddImportQuickFixTest extends PyQuickFixTestCase {
runWithAdditionalFileInLibDir(
"sys.py",
"path = 10",
(__) -> doTestProposedImportsOrdering("path", "pkg.path", "sys.path")
(__) -> doTestProposedImportsOrdering("pkg.path", "sys.path")
);
}
@@ -211,23 +208,23 @@ public class PyAddImportQuickFixTest extends PyQuickFixTestCase {
runWithAdditionalFileInLibDir(
"sys.py",
"path = 10",
(__) -> doTestProposedImportsOrdering("path", "first.second.path", "sys.path", "_private.path")
(__) -> doTestProposedImportsOrdering("first.second.path", "sys.path", "_private.path")
);
}
// PY-20976
public void testOrderingSymbolBeforeModule() {
doTestProposedImportsOrdering("foo", "first.module.foo()", "first.a.foo");
doTestProposedImportsOrdering("first.module.foo()", "first.a.foo");
}
// PY-20976
public void testOrderingModuleBeforePackage() {
doTestProposedImportsOrdering("foo", "b.foo", "a.foo");
doTestProposedImportsOrdering("b.foo", "a.foo");
}
// PY-20976
public void testOrderingPathComponentsNumber() {
doTestProposedImportsOrdering("foo", "c.foo", "b.c.foo", "a.b.c.foo");
doTestProposedImportsOrdering("c.foo", "b.c.foo", "a.b.c.foo");
}
// PY-20976
@@ -239,7 +236,7 @@ public class PyAddImportQuickFixTest extends PyQuickFixTestCase {
runWithAdditionalFileInLibDir(
"os/path.py",
"",
(___) -> doTestProposedImportsOrdering("path", "path from sys", "src.path", "os.path")
(___) -> doTestProposedImportsOrdering("path from sys", "src.path", "os.path")
)
);
}
@@ -281,8 +278,8 @@ public class PyAddImportQuickFixTest extends PyQuickFixTestCase {
return dir != null && dir.getName().equals("numpy") && "np".equals(c.getAsName());
});
assertNotNull(importNumpyAsNpVariant);
List<String> candidateText = ContainerUtil.map(fix.getCandidates(), c -> c.getPresentableText("np"));
assertContainsInRelativeOrder(candidateText, "np", "pandas.np");
List<String> candidateText = ContainerUtil.map(fix.getCandidates(), c -> c.getPresentableText());
assertContainsInRelativeOrder(candidateText, "numpy", "pandas.np");
return true;
});
}
@@ -291,9 +288,9 @@ public class PyAddImportQuickFixTest extends PyQuickFixTestCase {
doMultiFileAutoImportTest("Import");
}
private void doTestProposedImportsOrdering(@NotNull String text, String @NotNull ... expected) {
private void doTestProposedImportsOrdering(String @NotNull ... expected) {
doMultiFileAutoImportTest("Import", fix -> {
final List<String> candidates = ContainerUtil.map(fix.getCandidates(), c -> c.getPresentableText(text));
final List<String> candidates = ContainerUtil.map(fix.getCandidates(), c -> c.getPresentableText());
assertNotNull(candidates);
assertContainsInRelativeOrder(candidates, expected);
return false;