RefactoringElementListener (PsiElement element) { if (element instanceof PsiDirectory) { VirtualFile vFile = ((PsiDirectory)element).getVirtualFile(); if ((myTestType == TestType.TEST_FOLDER && pathsEqual(vFile, myFolderName)) || pathsEqual(vFile, getExpandedWorkingDir(this))) { return new RefactoringElementAdapter() { @Override protected void elementRenamedOrMoved(@NotNull PsiElement newElement) { String newPath = FileUtil.toSystemDependentName(((PsiDirectory)newElement).getVirtualFile().getPath()); setWorkingDirectory(newPath); if (myTestType == TestType.TEST_FOLDER) { myFolderName = newPath; } } @Override public void undoElementMovedOrRenamed(@NotNull PsiElement newElement, @NotNull String oldQualifiedName) { final String systemDependant = FileUtil.toSystemDependentName(oldQualifiedName); setWorkingDirectory(systemDependant); if (myTestType == TestType.TEST_FOLDER) { myFolderName = systemDependant; } } }; } return null; } if (myTestType == TestType.TEST_FOLDER) { return null; } File scriptFile = new File(myScriptName); if (!scriptFile.isAbsolute()) { scriptFile = new File(getExpandedWorkingDir(this), myScriptName); } PsiFile containingFile = element.getContainingFile(); VirtualFile vFile = containingFile == null ? null : containingFile.getVirtualFile(); if (vFile != null && Objects.equals(new File(vFile.getPath()).getAbsolutePath(), scriptFile.getAbsolutePath())) { if (element instanceof PsiFile) { return new RefactoringElementAdapter() { @Override protected void elementRenamedOrMoved(@NotNull PsiElement newElement) { VirtualFile virtualFile = ((PsiFile)newElement).getVirtualFile(); if (virtualFile != null) { myScriptName = FileUtil.toSystemDependentName(virtualFile.getPath()); } } @Override public void undoElementMovedOrRenamed(@NotNull PsiElement newElement, @NotNull String oldQualifiedName) { myScriptName = FileUtil.toSystemDependentName(oldQualifiedName); } }; } if (element instanceof PyClass && (myTestType == TestType.TEST_CLASS || myTestType == TestType.TEST_METHOD) && Objects.equals(((PyClass)element).getName(), myClassName)) { return new RefactoringElementAdapter() { @Override protected void elementRenamedOrMoved(@NotNull PsiElement newElement) { myClassName = ((PyClass)newElement).getName(); } @Override public void undoElementMovedOrRenamed(@NotNull PsiElement newElement, @NotNull String oldQualifiedName) { myClassName = oldQualifiedName; } }; } if (element instanceof PyFunction && Objects.equals(((PyFunction)element).getName(), myMethodName)) { ScopeOwner scopeOwner = PsiTreeUtil.getParentOfType(element, ScopeOwner.class); if ((myTestType == TestType.TEST_FUNCTION && scopeOwner instanceof PyFile) || (myTestType == TestType.TEST_METHOD && scopeOwner instanceof PyClass && Objects.equals(scopeOwner.getName(), myClassName))) { return new RefactoringElementAdapter() { @Override protected void elementRenamedOrMoved(@NotNull PsiElement newElement) { myMethodName = ((PyFunction)newElement).getName(); } @Override public void undoElementMovedOrRenamed(@NotNull PsiElement newElement, @NotNull String oldQualifiedName) { final int methodIdx = oldQualifiedName.indexOf("#") + 1; if (methodIdx > 0 && methodIdx < oldQualifiedName.length()) { myMethodName = oldQualifiedName.substring(methodIdx); } } }; } } } return null; }