From 5d3a9d440e5f64f7c3c8b9c7db38dc01156d02bc Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Wed, 6 Jan 2021 13:59:19 +0300 Subject: [PATCH] PY-36374 Clean up in ImportFromExistingAction and ImportCandidateHolder * Remove some obsolete comments * Simplify a redundant PECS signature * Fix visibility of the fields in ImportFromExistingAction * Make a precondition violation an IllegalArgumentException instead of assertion GitOrigin-RevId: e37ea9974ac56f385f1993d6b953c522f564efe5 --- .../codeInsight/imports/ImportCandidateHolder.java | 7 ++++--- .../codeInsight/imports/ImportFromExistingAction.java | 10 +++++----- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/python/python-psi-impl/src/com/jetbrains/python/codeInsight/imports/ImportCandidateHolder.java b/python/python-psi-impl/src/com/jetbrains/python/codeInsight/imports/ImportCandidateHolder.java index 4cf3972e15ea..c36a65e260c0 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/codeInsight/imports/ImportCandidateHolder.java +++ b/python/python-psi-impl/src/com/jetbrains/python/codeInsight/imports/ImportCandidateHolder.java @@ -28,9 +28,9 @@ import java.util.List; * * @author dcheryasov */ -// visibility is intentionally package-level public class ImportCandidateHolder implements Comparable { private static final Logger LOG = Logger.getInstance(ImportCandidateHolder.class); + @NotNull private final SmartPsiElementPointer myImportable; @Nullable private final SmartPsiElementPointer myImportElement; @NotNull private final SmartPsiElementPointer myFile; @@ -49,10 +49,12 @@ public class ImportCandidateHolder implements Comparable * For top-level imported symbols it's qualified name of containing module (or package for __init__.py). * For modules and packages it should be qualified name of their parental package * (empty for modules and packages located at source roots). - * */ public ImportCandidateHolder(@NotNull PsiNamedElement importable, @NotNull PsiFileSystemItem file, @Nullable PyImportElement importElement, @Nullable QualifiedName path, @Nullable String asName) { + if (importElement == null && path == null) { + throw new IllegalArgumentException("Either an import path or an existing import should be provided for " + importable); + } SmartPointerManager pointerManager = SmartPointerManager.getInstance(importable.getProject()); myFile = pointerManager.createSmartPsiElementPointer(file); myImportable = pointerManager.createSmartPsiElementPointer(importable); @@ -63,7 +65,6 @@ public class ImportCandidateHolder implements Comparable myAsName = asName; myRelevance = PyCompletionUtilsKt.computeCompletionWeight(importable, myImportableName, myPath, null, false); LOG.debug("Computed relevance for import item ", myImportableName, ": ", myRelevance); - assert importElement != null || path != null; // one of these must be present } public ImportCandidateHolder(@NotNull PsiNamedElement importable, @NotNull PsiFileSystemItem file, diff --git a/python/python-psi-impl/src/com/jetbrains/python/codeInsight/imports/ImportFromExistingAction.java b/python/python-psi-impl/src/com/jetbrains/python/codeInsight/imports/ImportFromExistingAction.java index 1f52d18a7a9c..09c1a6db0aef 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/codeInsight/imports/ImportFromExistingAction.java +++ b/python/python-psi-impl/src/com/jetbrains/python/codeInsight/imports/ImportFromExistingAction.java @@ -27,10 +27,10 @@ import static com.jetbrains.python.psi.PyUtil.as; * @author dcheryasov */ public class ImportFromExistingAction implements QuestionAction { - PsiElement myTarget; - List mySources; // list of - String myName; - boolean myUseQualifiedImport; + private final PsiElement myTarget; + private final List mySources; + private final String myName; + private final boolean myUseQualifiedImport; private Runnable myOnDoneCallback; private final boolean myImportLocally; @@ -40,7 +40,7 @@ public class ImportFromExistingAction implements QuestionAction { * @param name relevant name ot the target element (e.g. of identifier in an expression). * @param useQualified if True, use qualified "import modulename" instead of "from modulename import ...". */ - public ImportFromExistingAction(@NotNull PsiElement target, @NotNull List sources, @NotNull String name, + public ImportFromExistingAction(@NotNull PsiElement target, @NotNull List sources, @NotNull String name, boolean useQualified, boolean importLocally) { myTarget = target; mySources = sources;