<commit_before>package com.jetbrains.python.refactoring.move;

import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.refactoring.BaseRefactoringProcessor;
import com.intellij.refactoring.RefactoringBundle;
import com.intellij.refactoring.move.MoveCallback;
import com.intellij.refactoring.move.MoveHandlerDelegate;
import com.intellij.refactoring.util.CommonRefactoringUtil;
import com.intellij.util.IncorrectOperationException;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.codeInsight.controlflow.ScopeOwner;
import com.jetbrains.python.codeInsight.dataflow.scope.ScopeUtil;
import com.jetbrains.python.psi.PyClass;
import com.jetbrains.python.psi.PyFunction;
import com.jetbrains.python.psi.impl.PyPsiUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * @author vlan
 */
public class PyMoveClassOrFunctionDelegate extends MoveHandlerDelegate {

  @Override
  public void doMove(Project project,
                     PsiElement[] elements,
                     @Nullable PsiElement targetContainer,
                     @Nullable MoveCallback callback) {
    PsiNamedElement[] elementsToMove = new PsiNamedElement[elements.length];
    for (int i = 0; i < elements.length; i++) {
      final PsiNamedElement e = getElementToMove(elements[i]);
      if (e == null) {
        return;
      }
      elementsToMove[i] = e;
    }
    String initialDestination = null;
    if (targetContainer instanceof PsiFile) {
      final VirtualFile virtualFile = ((PsiFile)targetContainer).getVirtualFile();
      if (virtualFile != null) {
        initialDestination = FileUtil.toSystemDependentName(virtualFile.getPath());
      }
    }
    final PyMoveClassOrFunctionDialog dialog = new PyMoveClassOrFunctionDialog(project, elementsToMove, initialDestination);
    dialog.show();
    if (!dialog.isOK()) {
      return;
    }
    final String destination = dialog.getTargetPath();
    final boolean previewUsages = dialog.isPreviewUsages();
    try {
      final BaseRefactoringProcessor processor = new PyMoveClassOrFunctionProcessor(project, elementsToMove, destination, previewUsages);
      processor.run();
    }
    catch (IncorrectOperationException e) {
      CommonRefactoringUtil.showErrorMessage(RefactoringBundle.message("error.title"), e.getMessage(),
                                             null, project);
    }
  }

  @Override
  public boolean tryToMove(@NotNull PsiElement element,
                           @NotNull Project project,
                           @Nullable DataContext dataContext,
                           @Nullable PsiReference reference,
                           @Nullable Editor editor) {
    final PsiNamedElement e = getElementToMove(element);
    if (e instanceof PyClass || e instanceof PyFunction) {
      if (PyPsiUtils.isTopLevel(e)) {
        PsiElement targetContainer = null;
        if (editor != null) {
          final Document document = editor.getDocument();
          targetContainer = PsiDocumentManager.getInstance(project).getPsiFile(document);
        }
        doMove(project, new PsiElement[] {e}, targetContainer, null);
      }
      else {
        CommonRefactoringUtil.showErrorHint(project, editor, PyBundle.message("refactoring.move.class.or.function.error.selection"),
                                            RefactoringBundle.message("error.title"), null);
      }
      return true;
    }
    return false;
  }

  @Nullable
  public static PsiNamedElement getElementToMove(@NotNull PsiElement element) {
    final ScopeOwner owner = (element instanceof ScopeOwner) ? (ScopeOwner)element : ScopeUtil.getScopeOwner(element);
    return (owner instanceof PsiNamedElement) ? (PsiNamedElement)owner : null;
  }
}<commit_msg>Select any named element for move refactoring, not only scope owner (PY-6461)<commit_after>package com.jetbrains.python.refactoring.move;

import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.refactoring.BaseRefactoringProcessor;
import com.intellij.refactoring.RefactoringBundle;
import com.intellij.refactoring.move.MoveCallback;
import com.intellij.refactoring.move.MoveHandlerDelegate;
import com.intellij.refactoring.util.CommonRefactoringUtil;
import com.intellij.util.IncorrectOperationException;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.psi.PyClass;
import com.jetbrains.python.psi.PyFunction;
import com.jetbrains.python.psi.impl.PyPsiUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * @author vlan
 */
public class PyMoveClassOrFunctionDelegate extends MoveHandlerDelegate {

  @Override
  public void doMove(Project project,
                     PsiElement[] elements,
                     @Nullable PsiElement targetContainer,
                     @Nullable MoveCallback callback) {
    PsiNamedElement[] elementsToMove = new PsiNamedElement[elements.length];
    for (int i = 0; i < elements.length; i++) {
      final PsiNamedElement e = getElementToMove(elements[i]);
      if (e == null) {
        return;
      }
      elementsToMove[i] = e;
    }
    String initialDestination = null;
    if (targetContainer instanceof PsiFile) {
      final VirtualFile virtualFile = ((PsiFile)targetContainer).getVirtualFile();
      if (virtualFile != null) {
        initialDestination = FileUtil.toSystemDependentName(virtualFile.getPath());
      }
    }
    final PyMoveClassOrFunctionDialog dialog = new PyMoveClassOrFunctionDialog(project, elementsToMove, initialDestination);
    dialog.show();
    if (!dialog.isOK()) {
      return;
    }
    final String destination = dialog.getTargetPath();
    final boolean previewUsages = dialog.isPreviewUsages();
    try {
      final BaseRefactoringProcessor processor = new PyMoveClassOrFunctionProcessor(project, elementsToMove, destination, previewUsages);
      processor.run();
    }
    catch (IncorrectOperationException e) {
      CommonRefactoringUtil.showErrorMessage(RefactoringBundle.message("error.title"), e.getMessage(),
                                             null, project);
    }
  }

  @Override
  public boolean tryToMove(@NotNull PsiElement element,
                           @NotNull Project project,
                           @Nullable DataContext dataContext,
                           @Nullable PsiReference reference,
                           @Nullable Editor editor) {
    final PsiNamedElement e = getElementToMove(element);
    if (e instanceof PyClass || e instanceof PyFunction) {
      if (PyPsiUtils.isTopLevel(e)) {
        PsiElement targetContainer = null;
        if (editor != null) {
          final Document document = editor.getDocument();
          targetContainer = PsiDocumentManager.getInstance(project).getPsiFile(document);
        }
        doMove(project, new PsiElement[] {e}, targetContainer, null);
      }
      else {
        CommonRefactoringUtil.showErrorHint(project, editor, PyBundle.message("refactoring.move.class.or.function.error.selection"),
                                            RefactoringBundle.message("error.title"), null);
      }
      return true;
    }
    return false;
  }

  @Nullable
  public static PsiNamedElement getElementToMove(@NotNull PsiElement element) {
    if (element instanceof PsiNamedElement) {
      return (PsiNamedElement)element;
    }
    return null;
  }
}
{'fraction_non_alphanumeric': 0.09230184856925804, 'fraction_numerical': 0.0007596859964547987, 'mean_word_length': 3.2240641711229947, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '262862', 'n_tokens_mistral': 2159, 'n_tokens_neox': 2126, 'n_words': 435}