Search in sources :

Example 1 with BadDataException

use of com.jetbrains.python.refactoring.classes.membersManager.vp.BadDataException in project intellij-community by JetBrains.

the class PyExtractSuperclassPresenterImpl method validateView.

@Override
protected void validateView() throws BadDataException {
    super.validateView();
    final Project project = myClassUnderRefactoring.getProject();
    if (!myNamesValidator.isIdentifier(myView.getSuperClassName(), project)) {
        throw new BadDataException(PyBundle.message("refactoring.extract.super.name.0.must.be.ident", myView.getSuperClassName()));
    }
    boolean rootFound = false;
    final File moduleFile = new File(myView.getModuleFile());
    try {
        final String targetDir = FileUtil.toSystemIndependentName(moduleFile.getCanonicalPath());
        for (final VirtualFile file : ProjectRootManager.getInstance(project).getContentRoots()) {
            if (StringUtil.startsWithIgnoreCase(targetDir, file.getPath())) {
                rootFound = true;
                break;
            }
        }
    } catch (final IOException ignore) {
    }
    if (!rootFound) {
        throw new BadDataException(PyBundle.message("refactoring.extract.super.target.path.outside.roots"));
    }
    // TODO: Cover with test. It can't be done for now, because testFixture reports root path incorrectly
    // PY-12173
    myView.getModuleFile();
    final VirtualFile moduleVirtualFile = LocalFileSystem.getInstance().findFileByIoFile(moduleFile);
    if (moduleVirtualFile != null) {
        final PsiFile psiFile = PsiManager.getInstance(project).findFile(moduleVirtualFile);
        if (psiFile instanceof PyFile) {
            if (((PyFile) psiFile).findTopLevelClass(myView.getSuperClassName()) != null) {
                throw new BadDataException(PyBundle.message("refactoring.extract.super.target.class.already.exists", myView.getSuperClassName()));
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) BadDataException(com.jetbrains.python.refactoring.classes.membersManager.vp.BadDataException) PsiFile(com.intellij.psi.PsiFile) IOException(java.io.IOException) PyFile(com.jetbrains.python.psi.PyFile) PyFile(com.jetbrains.python.psi.PyFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File)

Aggregations

Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiFile (com.intellij.psi.PsiFile)1 PyFile (com.jetbrains.python.psi.PyFile)1 BadDataException (com.jetbrains.python.refactoring.classes.membersManager.vp.BadDataException)1 File (java.io.File)1 IOException (java.io.IOException)1