private static void registerListeners(@NotNull PackageSet packageSet,
                                        @NotNull RefactoringElementListenerComposite result,
                                        @NotNull OldScopeDescriptor descriptor,
                                        String oldQualifiedName,
                                        @NotNull ReferenceKind referenceKind) {
    NamedScope oldScope = descriptor.getOldScope();
    PackageSet oldSet = oldScope.getValue();
    if (oldSet != null && packageSet.anyMatches(set -> set instanceof PatternBasedPackageSet && ((PatternBasedPackageSet)set).isOn(oldQualifiedName))) {
      result.addListener(new RefactoringElementAdapter() {
        @Override
        public void elementRenamedOrMoved(@NotNull PsiElement newElement) {
          LOG.assertTrue(newElement instanceof PsiQualifiedNamedElement || newElement instanceof PsiDirectory);
          String newName = getQualifiedName(newElement, referenceKind);
          if (newName != null) {
            PackageSet newSet = oldSet.map(set -> updateNameInPattern(set, oldQualifiedName, newName));
            if (newSet != oldSet) {
              String presentableName = oldScope.getPresentableName();
              descriptor.replaceScope(new NamedScope(oldScope.getScopeId(), () -> presentableName, oldScope.getIcon(), newSet));
            }
          }
        }

        @Override
        public void undoElementMovedOrRenamed(@NotNull PsiElement newElement, @NotNull String oldQualifiedName) {
          LOG.assertTrue(newElement instanceof PsiQualifiedNamedElement || newElement instanceof PsiDirectory);
          descriptor.replaceScope(oldScope);
        }
      });
    }
  }
private static void registerListeners(@NotNull PackageSet packageSet,
                                        @NotNull RefactoringElementListenerComposite result,
                                        @NotNull OldScopeDescriptor descriptor,
                                        String oldQualifiedName,
                                        @NotNull ReferenceKind referenceKind)