LAB-31 move add field/function quick fixes to python-psi-impl
authorSergey Stupin <sergey.stupin@jetbrains.com>
Thu, 13 Aug 2020 13:32:23 +0000 (15:32 +0200)
committerintellij-monorepo-bot <intellij-monorepo-bot-no-reply@jetbrains.com>
Thu, 13 Aug 2020 13:39:07 +0000 (13:39 +0000)
GitOrigin-RevId: 59951f875689d397320a9dab3cced4c7160f12fb

python/pluginResources/messages/PyBundle.properties
python/python-psi-impl/resources/messages/PyPsiBundle.properties
python/python-psi-impl/src/com/jetbrains/python/inspections/quickfix/AddFunctionQuickFix.java [moved from python/src/com/jetbrains/python/inspections/quickfix/AddFunctionQuickFix.java with 95% similarity]
python/python-psi-impl/src/com/jetbrains/python/inspections/unresolvedReference/PyUnresolvedReferencesVisitor.java
python/src/com/jetbrains/python/inspections/unresolvedReference/PyUnresolvedReferencesInspection.java

index d2cc06174d280b27a630c75253425eeeef1ecb35..e30463871e6e9f82dbd5d3cb0bd8f691570b8091 100644 (file)
@@ -33,7 +33,6 @@ QFIX.change.signature.of=Change signature of {0}
 
 QFIX.NAME.rename.element=Rename element
 
-QFIX.create.function.in.module=Create function in module
 QFIX.ignore.unresolved.reference=Ignore unresolved reference
 QFIX.ignore.unresolved.reference.0=Ignore unresolved reference ''{0}''
 QFIX.mark.all.unresolved.attributes.of.0.as.ignored=Mark all unresolved attributes of ''{0}'' as ignored
index 4aa6f1d70e8d406dfe44385b3e068ad77e6a5f86..3da7b8abe0df94a2770b3bc572327afd027be749 100644 (file)
@@ -332,6 +332,7 @@ QFIX.NAME.add.exception.base=Add Exception base class
 QFIX.add.encoding=Add encoding declaration
 QFIX.remove.trailing.suffix=Remove trailing suffix
 QFIX.action.failed=Action failed
+QFIX.create.function.in.module=Create function in module
 
 
 #PyRemoveUnderscoresInNumericLiteralsQuickFix
similarity index 95%
rename from python/src/com/jetbrains/python/inspections/quickfix/AddFunctionQuickFix.java
rename to python/python-psi-impl/src/com/jetbrains/python/inspections/quickfix/AddFunctionQuickFix.java
index c09ae35b2c8e8e14ab0991cea9015cfd948ca19f..83a3e0cacb4b3a225ddbfe9efcb23e19eb14594a 100644 (file)
@@ -12,14 +12,13 @@ import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.fileEditor.FileEditorManager;
 import com.intellij.openapi.fileEditor.OpenFileDescriptor;
 import com.intellij.openapi.project.Project;
-import com.intellij.openapi.ui.MessageType;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.psi.PsiElement;
 import com.intellij.psi.PsiFile;
 import com.intellij.psi.util.PsiTreeUtil;
 import com.intellij.util.IncorrectOperationException;
-import com.jetbrains.python.PyBundle;
 import com.jetbrains.python.PyPsiBundle;
+import com.jetbrains.python.PythonUiService;
 import com.jetbrains.python.inspections.PyInspectionExtension;
 import com.jetbrains.python.psi.*;
 import com.jetbrains.python.psi.impl.ParamHelper;
@@ -27,7 +26,6 @@ import com.jetbrains.python.psi.impl.PyFunctionBuilder;
 import com.jetbrains.python.psi.types.PyModuleType;
 import com.jetbrains.python.psi.types.PyType;
 import com.jetbrains.python.psi.types.TypeEvalContext;
-import com.jetbrains.python.ui.PyUiUtil;
 import org.jetbrains.annotations.NotNull;
 
 import java.util.List;
@@ -59,7 +57,7 @@ public class AddFunctionQuickFix  implements LocalQuickFix {
   @Override
   @NotNull
   public String getFamilyName() {
-    return PyBundle.message("QFIX.create.function.in.module");
+    return PyPsiBundle.message("QFIX.create.function.in.module");
   }
 
   @Override
@@ -128,7 +126,7 @@ public class AddFunctionQuickFix  implements LocalQuickFix {
     }
     catch (IncorrectOperationException ignored) {
       // we failed. tell about this
-      PyUiUtil.showBalloon(project, PyPsiBundle.message("QFIX.failed.to.add.function"), MessageType.ERROR);
+      PythonUiService.getInstance().showBalloonError(project, PyPsiBundle.message("QFIX.failed.to.add.function"));
     }
   }
 
index aa6a241f33be44bfbbf78370645b246e6de6fa56..afd06c8856f18eac65de82d1a8b7bf2369ccaf90 100644 (file)
@@ -39,6 +39,9 @@ import com.jetbrains.python.inspections.PyInspection;
 import com.jetbrains.python.inspections.PyInspectionExtension;
 import com.jetbrains.python.inspections.PyInspectionVisitor;
 import com.jetbrains.python.inspections.PyInspectionsUtil;
+import com.jetbrains.python.inspections.quickfix.AddFieldQuickFix;
+import com.jetbrains.python.inspections.quickfix.AddFunctionQuickFix;
+import com.jetbrains.python.inspections.quickfix.AddMethodQuickFix;
 import com.jetbrains.python.psi.*;
 import com.jetbrains.python.psi.impl.PyBuiltinCache;
 import com.jetbrains.python.psi.impl.PyImportStatementNavigator;
@@ -342,7 +345,7 @@ public abstract class PyUnresolvedReferencesVisitor extends PyInspectionVisitor
           if (ignoreUnresolvedMemberForType(type, reference, refName) || isDeclaredInSlots(type, refName)) {
             return;
           }
-          ContainerUtil.addAll(fixes, getCreateMemberFromUsageFixes(myTypeEvalContext, type, reference, refText));
+          ContainerUtil.addAll(fixes, getCreateMemberFromUsageFixes(type, reference, refText));
           if (type instanceof PyClassType) {
             final PyClassType classType = (PyClassType)type;
             if (reference instanceof PyOperatorReference) {
@@ -896,12 +899,29 @@ public abstract class PyUnresolvedReferencesVisitor extends PyInspectionVisitor
     return null;
   }
 
-  Iterable<LocalQuickFix> getCreateMemberFromUsageFixes(
-    TypeEvalContext typeEvalContext, PyType type, PsiReference reference, String refText
-  ) {
-    return Collections.emptyList();
+  Iterable<LocalQuickFix> getCreateMemberFromUsageFixes(PyType type, PsiReference reference, String refText) {
+    List<LocalQuickFix> result = new ArrayList<>();
+    PsiElement element = reference.getElement();
+    if (type instanceof PyClassTypeImpl) {
+      PyClass cls = ((PyClassType)type).getPyClass();
+      if (!PyBuiltinCache.getInstance(element).isBuiltin(cls)) {
+        if (element.getParent() instanceof PyCallExpression) {
+          result.add(new AddMethodQuickFix(refText, cls.getName(), true));
+        }
+        else if (!(reference instanceof PyOperatorReference)) {
+          result.add(new AddFieldQuickFix(refText, "None", type.getName(), true));
+        }
+      }
+    }
+    else if (type instanceof PyModuleType) {
+      PyFile file = ((PyModuleType)type).getModule();
+      result.add(new AddFunctionQuickFix(refText, file.getName()));
+      getCreateClassFix(myTypeEvalContext, refText, element);
+    }
+    return result;
   }
 
+
   Iterable<LocalQuickFix> getAddSelfFixes(TypeEvalContext typeEvalContext, PyElement node, PyReferenceExpression expr) {
     return Collections.emptyList();
   }
index 5d0990cb49d7448191253c4426d16eae9c4fbacd..7ae97ec95dda6da956b287481abaf870914567cf 100644 (file)
@@ -12,7 +12,10 @@ import com.intellij.openapi.util.Comparing;
 import com.intellij.openapi.util.Key;
 import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.profile.codeInspection.InspectionProjectProfileManager;
-import com.intellij.psi.*;
+import com.intellij.psi.PsiElement;
+import com.intellij.psi.PsiElementVisitor;
+import com.intellij.psi.PsiFile;
+import com.intellij.psi.PsiReference;
 import com.intellij.psi.util.PsiTreeUtil;
 import com.intellij.psi.util.QualifiedName;
 import com.intellij.util.PlatformUtils;
@@ -33,10 +36,11 @@ import com.jetbrains.python.packaging.PyPackageUtil;
 import com.jetbrains.python.packaging.PyRequirement;
 import com.jetbrains.python.packaging.PyRequirementsKt;
 import com.jetbrains.python.psi.*;
-import com.jetbrains.python.psi.impl.PyBuiltinCache;
 import com.jetbrains.python.psi.impl.references.PyImportReference;
-import com.jetbrains.python.psi.impl.references.PyOperatorReference;
-import com.jetbrains.python.psi.types.*;
+import com.jetbrains.python.psi.types.PyClassTypeImpl;
+import com.jetbrains.python.psi.types.PyModuleType;
+import com.jetbrains.python.psi.types.PyType;
+import com.jetbrains.python.psi.types.TypeEvalContext;
 import com.jetbrains.python.sdk.PythonSdkUtil;
 import com.jetbrains.python.sdk.skeletons.PySkeletonRefresher;
 import one.util.streamex.StreamEx;
@@ -252,31 +256,6 @@ public class PyUnresolvedReferencesInspection extends PyInspection {
     }
 
     @Override
-    public Iterable<LocalQuickFix> getCreateMemberFromUsageFixes(
-      TypeEvalContext typeEvalContext, PyType type, PsiReference reference, String refText
-    ) {
-      List<LocalQuickFix> result = new ArrayList<>();
-      PsiElement element = reference.getElement();
-      if (type instanceof PyClassTypeImpl) {
-        PyClass cls = ((PyClassType)type).getPyClass();
-        if (!PyBuiltinCache.getInstance(element).isBuiltin(cls)) {
-          if (element.getParent() instanceof PyCallExpression) {
-            result.add(new AddMethodQuickFix(refText, cls.getName(), true));
-          }
-          else if (!(reference instanceof PyOperatorReference)) {
-            result.add(new AddFieldQuickFix(refText, "None", type.getName(), true));
-          }
-        }
-      }
-      else if (type instanceof PyModuleType) {
-        PyFile file = ((PyModuleType)type).getModule();
-        result.add(new AddFunctionQuickFix(refText, file.getName()));
-        getCreateClassFix(typeEvalContext, refText, element);
-      }
-      return result;
-    }
-
-    @Override
     public Iterable<LocalQuickFix> getAddSelfFixes(TypeEvalContext typeEvalContext, PyElement node, PyReferenceExpression expr) {
       List<LocalQuickFix> result = new ArrayList<>();
       final PyClass containedClass = PsiTreeUtil.getParentOfType(node, PyClass.class);