// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.refactoring.util;

import com.intellij.codeInsight.lookup.LookupManager;
import com.intellij.lang.findUsages.DescriptiveNameUtil;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.ex.FocusChangeListener;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.ElementDescriptionUtil;
import com.intellij.psi.PsiElement;
import com.intellij.refactoring.RefactoringBundle;
import com.intellij.ui.EditorSettingsProvider;
import com.intellij.usageView.UsageViewUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;

import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author yole
 */
public final class RefactoringUIUtil {
  private RefactoringUIUtil() { }

  public static @Nls String getDescription(@NotNull PsiElement element, boolean includeParent) {
    RefactoringDescriptionLocation location = includeParent ? RefactoringDescriptionLocation.WITH_PARENT : RefactoringDescriptionLocation.WITHOUT_PARENT;
    return ElementDescriptionUtil.getElementDescription(element, location);
  }

  public static void processIncorrectOperation(Project project, IncorrectOperationException e) {
    String message = e.getMessage();
    int index = message != null ? message.indexOf("java.io.IOException") : -1;
    if (index > 0) {
      message = message.substring(index + "java.io.IOException".length());
    }

    String s = message;
    ApplicationManager.getApplication().invokeLater(
      () -> Messages.showMessageDialog(project, s, RefactoringBundle.message("error.title"), Messages.getErrorIcon()));
  }

  public static @NotNull String calculatePsiElementDescriptionList(@NotNull PsiElement @NotNull [] elements) {
    return Stream.of(elements)
      .filter(PsiElement::isValid)
      .map(e -> UsageViewUtil.getType(e) + ' ' + DescriptiveNameUtil.getDescriptiveName(e))
      .distinct()
      .collect(Collectors.joining(", "));
  }

  public static final EditorSettingsProvider SELECT_ALL_ON_FOCUS = editor -> editor.addFocusListener(new FocusChangeListener() {
    @Override
    public void focusGained(@NotNull Editor editor) {
      if (LookupManager.getActiveLookup(editor) == null) {
        editor.getSelectionModel().setSelection(0, editor.getDocument().getTextLength());
      }
    }
  });
}
@@ -533,16 +533,66 @@
 ssages;%0A
+import com.intellij.openapi.util.text.StringUtil;%0A
 import c
@@ -627,24 +627,24 @@
 iptionUtil;%0A
-
 import com.i
@@ -806,24 +806,59 @@
 geViewUtil;%0A
+import com.intellij.util.Function;%0A
 import com.i
@@ -895,24 +895,75 @@
 nException;%0A
+import com.intellij.util.containers.ContainerUtil;%0A
 import org.j
@@ -1052,57 +1052,14 @@
 til.
-stream.Collectors;%0Aimport java.util.stream.Stream
+Arrays
 ;%0A%0A/
@@ -2124,81 +2124,55 @@
     
-return Stream.of(elements)%0A      .filter(PsiElement::isValid)%0A      .map(
+Function%3CPsiElement, String%3E presentationFun = 
 e -%3E
@@ -2250,68 +2250,113 @@
 e(e)
-)
+;
 %0A    
-  .distinct()%0A      .collect(Collectors.joining(
+return StringUtil.join(ContainerUtil.map2LinkedSet(Arrays.asList(elements), presentationFun), 
 %22, %22)
-)
 ;%0A  

Revert (IDEA-CR-65329)