コード例 #1
0
 @Override
 protected void moveOffsetAfter(boolean success) {
   if (success && (myPanel != null && myPanel.getInitPlace() != InitPlace.SAME_METHOD)
       || myOperation.getInplaceInitPlace() != InitPlace.SAME_METHOD) {
     final AccessToken accessToken =
         ApplicationManager.getApplication().acquireWriteActionLock(getClass());
     try {
       final PyAssignmentStatement initializer =
           PsiTreeUtil.getParentOfType(myTarget, PyAssignmentStatement.class);
       assert initializer != null;
       final Function<String, PyStatement> callback =
           FunctionUtil.<String, PyStatement>constant(initializer);
       final PyClass pyClass = PyUtil.getContainingClassOrSelf(initializer);
       InitPlace initPlace =
           myPanel != null ? myPanel.getInitPlace() : myOperation.getInplaceInitPlace();
       if (initPlace == InitPlace.CONSTRUCTOR) {
         AddFieldQuickFix.addFieldToInit(myProject, pyClass, "", callback);
       } else if (initPlace == InitPlace.SET_UP) {
         addFieldToSetUp(pyClass, callback);
       }
       if (myOperation.getOccurrences().size() > 0) {
         initializer.delete();
       } else {
         final PyExpression copy =
             PyElementGenerator.getInstance(myProject)
                 .createExpressionFromText(
                     LanguageLevel.forElement(myTarget), myTarget.getText());
         initializer.replace(copy);
       }
       initializer.delete();
     } finally {
       accessToken.finish();
     }
   }
 }
コード例 #2
0
 private static boolean inConstructor(@NotNull PsiElement expression) {
   final PsiElement expr = expression instanceof PyClass ? expression : expression.getParent();
   PyClass clazz = PyUtil.getContainingClassOrSelf(expr);
   final ScopeOwner current = ScopeUtil.getScopeOwner(expression);
   if (clazz != null && current != null && current instanceof PyFunction) {
     PyFunction init = clazz.findMethodByName(PyNames.INIT, false, null);
     if (current == init) {
       return true;
     }
   }
   return false;
 }
コード例 #3
0
 @Nullable
 private static Boolean isOverrides(final PyFunction pyFunction) {
   final PyClass clazz = PyUtil.getContainingClassOrSelf(pyFunction);
   assert clazz != null : "Refactoring called on function, not method: " + pyFunction;
   for (final PyClass parentClass : clazz.getSuperClasses()) {
     final PyFunction parentMethod = parentClass.findMethodByName(pyFunction.getName(), true);
     if (parentMethod != null) {
       return true;
     }
   }
   return null;
 }
コード例 #4
0
 @Override
 protected boolean checkEnabled(IntroduceOperation operation) {
   if (PyUtil.getContainingClassOrSelf(operation.getElement()) == null) {
     CommonRefactoringUtil.showErrorHint(
         operation.getProject(),
         operation.getEditor(),
         "Cannot introduce field: not in class",
         myDialogTitle,
         getHelpId());
     return false;
   }
   if (dependsOnLocalScopeValues(operation.getElement())) {
     operation.removeAvailableInitPlace(InitPlace.CONSTRUCTOR);
     operation.removeAvailableInitPlace(InitPlace.SET_UP);
   }
   return true;
 }
コード例 #5
0
 private static boolean isTestClass(PsiFile file, Editor editor) {
   PsiElement element1 = null;
   final SelectionModel selectionModel = editor.getSelectionModel();
   if (selectionModel.hasSelection()) {
     element1 = file.findElementAt(selectionModel.getSelectionStart());
   } else {
     final CaretModel caretModel = editor.getCaretModel();
     final Document document = editor.getDocument();
     int lineNumber = document.getLineNumber(caretModel.getOffset());
     if ((lineNumber >= 0) && (lineNumber < document.getLineCount())) {
       element1 = file.findElementAt(document.getLineStartOffset(lineNumber));
     }
   }
   if (element1 != null) {
     final PyClass clazz = PyUtil.getContainingClassOrSelf(element1);
     if (clazz != null && PythonUnitTestUtil.isTestCaseClass(clazz, null)) return true;
   }
   return false;
 }
コード例 #6
0
 @Nullable
 @Override
 protected PsiElement addDeclaration(
     @NotNull PsiElement expression,
     @NotNull PsiElement declaration,
     @NotNull IntroduceOperation operation) {
   final PsiElement expr = expression instanceof PyClass ? expression : expression.getParent();
   PsiElement anchor = PyUtil.getContainingClassOrSelf(expr);
   assert anchor instanceof PyClass;
   final PyClass clazz = (PyClass) anchor;
   final Project project = anchor.getProject();
   if (operation.getInitPlace() == InitPlace.CONSTRUCTOR && !inConstructor(expression)) {
     return AddFieldQuickFix.addFieldToInit(
         project, clazz, "", new AddFieldDeclaration(declaration));
   } else if (operation.getInitPlace() == InitPlace.SET_UP) {
     return addFieldToSetUp(clazz, new AddFieldDeclaration(declaration));
   }
   return PyIntroduceVariableHandler.doIntroduceVariable(
       expression, declaration, operation.getOccurrences(), operation.isReplaceAll());
 }