<commit_before>/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Created by IntelliJ IDEA.
 * User: cdr
 * Date: Nov 13, 2002
 * Time: 3:26:50 PM
 * To change this template use Options | File Templates.
 */
package com.intellij.codeInsight.daemon.impl.quickfix;

import com.intellij.codeInsight.CodeInsightUtilBase;
import com.intellij.codeInsight.TargetElementUtil;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
import com.intellij.codeInsight.daemon.impl.analysis.HighlightUtil;
import com.intellij.codeInsight.intention.HighPriorityAction;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.find.FindManager;
import com.intellij.find.findUsages.FindUsagesHandler;
import com.intellij.find.findUsages.FindUsagesManager;
import com.intellij.find.findUsages.JavaMethodFindUsagesOptions;
import com.intellij.find.impl.FindManagerImpl;
import com.intellij.ide.util.SuperMethodWarningUtil;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.undo.UndoUtil;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.codeStyle.SuggestedNameInfo;
import com.intellij.psi.codeStyle.VariableKind;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.refactoring.RefactoringBundle;
import com.intellij.refactoring.changeSignature.ChangeSignatureProcessor;
import com.intellij.refactoring.changeSignature.JavaChangeSignatureDialog;
import com.intellij.refactoring.changeSignature.ParameterInfoImpl;
import com.intellij.refactoring.util.RefactoringUtil;
import com.intellij.usageView.UsageInfo;
import com.intellij.util.ArrayUtil;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.Processor;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;

public class ChangeMethodSignatureFromUsageFix implements IntentionAction, HighPriorityAction {
  private final PsiMethod myTargetMethod;
  private final PsiExpression[] myExpressions;
  private final PsiSubstitutor mySubstitutor;
  private final PsiElement myContext;
  private final boolean myChangeAllUsages;
  private final int myMinUsagesNumberToShowDialog;
  private ParameterInfoImpl[] myNewParametersInfo;
  private static final Logger LOG = Logger.getInstance("#" + ChangeMethodSignatureFromUsageFix.class.getName());

  ChangeMethodSignatureFromUsageFix(@NotNull PsiMethod targetMethod,
                                    @NotNull PsiExpression[] expressions,
                                    @NotNull PsiSubstitutor substitutor,
                                    @NotNull PsiElement context,
                                    boolean changeAllUsages, int minUsagesNumberToShowDialog) {
    myTargetMethod = targetMethod;
    myExpressions = expressions;
    mySubstitutor = substitutor;
    myContext = context;
    myChangeAllUsages = changeAllUsages;
    myMinUsagesNumberToShowDialog = minUsagesNumberToShowDialog;
  }

  @NotNull
  public String getText() {
    final String shortText = getShortText();
    if (shortText != null) return shortText;
    return QuickFixBundle.message("change.method.signature.from.usage.text",
                                  HighlightUtil.formatMethod(myTargetMethod),
                                  myTargetMethod.getName(),
                                  formatTypesList(myNewParametersInfo, myContext));
  }

  @Nullable
  private String getShortText() {
    if (myNewParametersInfo.length > 4 || myTargetMethod.getParameterList().getParametersCount() > 4) {

      final StringBuilder buf = new StringBuilder();
      final HashSet<ParameterInfoImpl> newParams = new HashSet<ParameterInfoImpl>();
      final HashSet<ParameterInfoImpl> removedParams = new HashSet<ParameterInfoImpl>();
      final HashSet<ParameterInfoImpl> changedParams = new HashSet<ParameterInfoImpl>();
      getNewParametersInfo(myExpressions, myTargetMethod, mySubstitutor, buf, newParams, removedParams, changedParams);

      final String targetMethodName = myTargetMethod.getName();
      if (myTargetMethod.getContainingClass().findMethodsByName(targetMethodName, true).length == 1) {
        if (newParams.size() == 1) {
          final ParameterInfoImpl p = newParams.iterator().next();
          return "Add " + p.getTypeText() + " as " + ArrayUtil.find(myNewParametersInfo, p) + " parameter to method " + targetMethodName;
        }
        if (removedParams.size() == 1) {
          final ParameterInfoImpl p = removedParams.iterator().next();
          return "Remove " + (p.getOldIndex() + 1) + " parameter from method " + targetMethodName;
        }
        if (changedParams.size() == 1) {
          final ParameterInfoImpl p = changedParams.iterator().next();
          return "Change " + (p.getOldIndex() + 1)+ " parameter type of method " +targetMethodName + " from " + myTargetMethod.getParameterList().getParameters()[p.getOldIndex()].getType().getPresentableText() + " to " + p.getTypeText();
        }
      }
      return "<html> Change signature of " + targetMethodName + "(" + buf.toString() + ")</html>";
    }
    return null;
  }

  private static String formatTypesList(ParameterInfoImpl[] infos, PsiElement context) {
    if (infos == null) return null;
    String result = "";
    try {
      for (ParameterInfoImpl info : infos) {
        PsiType type = info.createType(context, context.getManager());
        if (result.length() != 0) {
          result += ", ";
        }
        LOG.assertTrue(type != null, "old idx: " + info.getOldIndex() + "; " + info.getClass().getName());
        result += type.getPresentableText();
      }
    }
    catch (IncorrectOperationException e) {
      return null;
    }<commit_msg>change signature fix: correct message<commit_after>/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Created by IntelliJ IDEA.
 * User: cdr
 * Date: Nov 13, 2002
 * Time: 3:26:50 PM
 * To change this template use Options | File Templates.
 */
package com.intellij.codeInsight.daemon.impl.quickfix;

import com.intellij.codeInsight.CodeInsightUtilBase;
import com.intellij.codeInsight.TargetElementUtil;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
import com.intellij.codeInsight.daemon.impl.analysis.HighlightUtil;
import com.intellij.codeInsight.intention.HighPriorityAction;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.find.FindManager;
import com.intellij.find.findUsages.FindUsagesHandler;
import com.intellij.find.findUsages.FindUsagesManager;
import com.intellij.find.findUsages.JavaMethodFindUsagesOptions;
import com.intellij.find.impl.FindManagerImpl;
import com.intellij.ide.util.SuperMethodWarningUtil;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.undo.UndoUtil;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.codeStyle.SuggestedNameInfo;
import com.intellij.psi.codeStyle.VariableKind;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.refactoring.RefactoringBundle;
import com.intellij.refactoring.changeSignature.ChangeSignatureProcessor;
import com.intellij.refactoring.changeSignature.JavaChangeSignatureDialog;
import com.intellij.refactoring.changeSignature.ParameterInfoImpl;
import com.intellij.refactoring.util.RefactoringUtil;
import com.intellij.usageView.UsageInfo;
import com.intellij.util.ArrayUtil;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.Processor;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;

public class ChangeMethodSignatureFromUsageFix implements IntentionAction, HighPriorityAction {
  private final PsiMethod myTargetMethod;
  private final PsiExpression[] myExpressions;
  private final PsiSubstitutor mySubstitutor;
  private final PsiElement myContext;
  private final boolean myChangeAllUsages;
  private final int myMinUsagesNumberToShowDialog;
  private ParameterInfoImpl[] myNewParametersInfo;
  private static final Logger LOG = Logger.getInstance("#" + ChangeMethodSignatureFromUsageFix.class.getName());

  ChangeMethodSignatureFromUsageFix(@NotNull PsiMethod targetMethod,
                                    @NotNull PsiExpression[] expressions,
                                    @NotNull PsiSubstitutor substitutor,
                                    @NotNull PsiElement context,
                                    boolean changeAllUsages, int minUsagesNumberToShowDialog) {
    myTargetMethod = targetMethod;
    myExpressions = expressions;
    mySubstitutor = substitutor;
    myContext = context;
    myChangeAllUsages = changeAllUsages;
    myMinUsagesNumberToShowDialog = minUsagesNumberToShowDialog;
  }

  @NotNull
  public String getText() {
    final String shortText = getShortText();
    if (shortText != null) return shortText;
    return QuickFixBundle.message("change.method.signature.from.usage.text",
                                  HighlightUtil.formatMethod(myTargetMethod),
                                  myTargetMethod.getName(),
                                  formatTypesList(myNewParametersInfo, myContext));
  }

  @Nullable
  private String getShortText() {
    if (myNewParametersInfo.length > 4 || myTargetMethod.getParameterList().getParametersCount() > 4) {

      final StringBuilder buf = new StringBuilder();
      final HashSet<ParameterInfoImpl> newParams = new HashSet<ParameterInfoImpl>();
      final HashSet<ParameterInfoImpl> removedParams = new HashSet<ParameterInfoImpl>();
      final HashSet<ParameterInfoImpl> changedParams = new HashSet<ParameterInfoImpl>();
      getNewParametersInfo(myExpressions, myTargetMethod, mySubstitutor, buf, newParams, removedParams, changedParams);

      final String targetMethodName = myTargetMethod.getName();
      if (myTargetMethod.getContainingClass().findMethodsByName(targetMethodName, true).length == 1) {
        if (newParams.size() == 1) {
          final ParameterInfoImpl p = newParams.iterator().next();
          return "Add " + p.getTypeText() + " as " + (ArrayUtil.find(myNewParametersInfo, p) + 1) + "nd parameter to method " + targetMethodName;
        }
        if (removedParams.size() == 1) {
          final ParameterInfoImpl p = removedParams.iterator().next();
          return "Remove " + (p.getOldIndex() + 1) + "nd parameter from method " + targetMethodName;
        }
        if (changedParams.size() == 1) {
          final ParameterInfoImpl p = changedParams.iterator().next();
          return "Change " + (p.getOldIndex() + 1)+ "nd parameter type of method " +targetMethodName + " from " + myTargetMethod.getParameterList().getParameters()[p.getOldIndex()].getType().getPresentableText() + " to " + p.getTypeText();
        }
      }
      return "<html> Change signature of " + targetMethodName + "(" + buf.toString() + ")</html>";
    }
    return null;
  }

  private static String formatTypesList(ParameterInfoImpl[] infos, PsiElement context) {
    if (infos == null) return null;
    String result = "";
    try {
      for (ParameterInfoImpl info : infos) {
        PsiType type = info.createType(context, context.getManager());
        if (result.length() != 0) {
          result += ", ";
        }
        LOG.assertTrue(type != null, "old idx: " + info.getOldIndex() + "; " + info.getClass().getName());
        result += type.getPresentableText();
      }
    }
    catch (IncorrectOperationException e) {
      return null;
    }
{'fraction_non_alphanumeric': 0.09817964786630856, 'fraction_numerical': 0.004849298716800955, 'mean_word_length': 3.865698729582577, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7595428', 'n_tokens_mistral': 3818, 'n_tokens_neox': 3719, 'n_words': 842}