RefactoringElementListener (PsiElement element) { if (scriptPath == null || !scriptPath.equals(getPathByElement(element))) { return null; } final PsiClass classToRun = GroovyRunnerPsiUtil.getRunningClass(element); if (element instanceof GroovyFile) { return new RefactoringElementAdapter() { @Override protected void elementRenamedOrMoved(@NotNull PsiElement newElement) { if (newElement instanceof GroovyFile file) { setScriptPath(ScriptFileUtil.getScriptFilePath(file.getVirtualFile())); } } @Override public void undoElementMovedOrRenamed(@NotNull PsiElement newElement, @NotNull String oldQualifiedName) { elementRenamedOrMoved(newElement); } }; } else if (element instanceof PsiClass && element.getManager().areElementsEquivalent(element, classToRun)) { return new RefactoringElementAdapter() { @Override protected void elementRenamedOrMoved(@NotNull PsiElement newElement) { setName(((PsiClass)newElement).getName()); } @Override public void undoElementMovedOrRenamed(@NotNull PsiElement newElement, @NotNull String oldQualifiedName) { elementRenamedOrMoved(newElement); } }; } return null; }