<commit_before>/*
 * Copyright 2003-2018 <NAME>, <NAME>
 *
 * 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.
 */
package com.siyeh.ig.controlflow;

import com.intellij.codeInsight.BlockUtils;
import com.intellij.codeInspection.*;
import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.search.LocalSearchScope;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.psi.util.PsiTreeUtil;
import com.siyeh.ig.psiutils.*;
import one.util.streamex.StreamEx;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.*;
import java.util.stream.Collectors;

import static com.intellij.util.ObjectUtils.tryCast;

// Not really with identical branches, but also common parts
public class IfStatementWithIdenticalBranchesInspection extends AbstractBaseJavaLocalInspectionTool {
  public boolean myHighlightWhenLastStatementIsCall = true;

  private static final List<IfStatementInspector> ourInspectors = new ArrayList<>(Arrays.asList(
    ImplicitElse::inspect,
    ThenElse::inspect,
    ElseIf::inspect
  ));


  @Nullable
  @Override
  public JComponent createOptionsPanel() {
    return new SingleCheckboxOptionsPanel(InspectionsBundle.message("inspection.common.if.parts.settings.highlight.when.tail.call"), this, "myHighlightWhenLastStatementIsCall");
  }

  @NotNull
  @Override
  public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
    return new JavaElementVisitor() {
      @Override
      public void visitIfStatement(PsiIfStatement ifStatement) {
        PsiStatement[] thenStatements = unwrap(ifStatement.getThenBranch());
        PsiStatement[] elseStatements = unwrap(ifStatement.getElseBranch());
        for (IfStatementInspector inspector : ourInspectors) {
          IfInspectionResult result = inspector.inspect(ifStatement, thenStatements, elseStatements, isOnTheFly, myHighlightWhenLastStatementIsCall);
          if (result != null) {
            ProblemHighlightType highlightType;
            if (result.myIsWarning) {
              highlightType = ProblemHighlightType.GENERIC_ERROR_OR_WARNING;
            }
            else {
              if (!isOnTheFly) return;
              highlightType = ProblemHighlightType.INFORMATION;
            }
            LocalQuickFix[] fixes;
            if (myHighlightWhenLastStatementIsCall) {
              fixes = new LocalQuickFix[]{
                new SetInspectionOptionFix(
                  IfStatementWithIdenticalBranchesInspection.this,
                  "myHighlightWhenLastStatementIsCall",
                  InspectionsBundle.message("inspection.common.if.parts.disable.highlight.tail.call"),
                  false
                ),
                result.myFix
              };
            } else {
              fixes = new LocalQuickFix[] {
                result.myFix
              };
            }
            holder.registerProblem(result.myElementToHighlight, result.myMessage, highlightType, fixes);
          }
        }
      }
    };
  }

  @FunctionalInterface
  private interface IfStatementInspector {
    @Nullable IfInspectionResult inspect(@NotNull PsiIfStatement ifStatement,
                                         @NotNull PsiStatement[] thenBranch,
                                         @NotNull PsiStatement[] elseBranch,
                                         boolean isOnTheFly,
                                         boolean highlightWhenLastStatementIsCall);
  }

  private static class IfInspectionResult {
    private final @NotNull PsiElement myElementToHighlight;
    private final boolean myIsWarning;
    private final @NotNull LocalQuickFix myFix;
    private final @NotNull String myMessage;

    IfInspectionResult(@NotNull PsiElement elementToHighlight,
                              boolean isWarning,
                              @NotNull LocalQuickFix fix,
                              @NotNull String message) {
      myElementToHighlight = elementToHighlight;
      myIsWarning = isWarning;
      myFix = fix;
      myMessage = message;
    }
  }

  private static class MergeElseIfsFix implements LocalQuickFix {
    @Nls
    @NotNull
    @Override
    public String getName() {
      return InspectionsBundle.message("inspection.common.if.parts.family.else.if");
    }

    @Nls
    @NotNull
    @Override
    public String getFamilyName() {
      return InspectionsBundle.message("inspection.common.if.parts.family.else.if");
    }

    @Override<commit_msg>make MergeElseIfsFix unavailable if there is nothing to merge (IDEA-170081)<commit_after>/*
 * Copyright 2003-2018 <NAME>, <NAME>
 *
 * 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.
 */
package com.siyeh.ig.controlflow;

import com.intellij.codeInsight.BlockUtils;
import com.intellij.codeInspection.*;
import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.search.LocalSearchScope;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.psi.util.PsiTreeUtil;
import com.siyeh.ig.psiutils.*;
import one.util.streamex.StreamEx;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.*;
import java.util.stream.Collectors;

import static com.intellij.util.ObjectUtils.tryCast;

// Not really with identical branches, but also common parts
public class IfStatementWithIdenticalBranchesInspection extends AbstractBaseJavaLocalInspectionTool {
  public boolean myHighlightWhenLastStatementIsCall = true;

  private static final List<IfStatementInspector> ourInspectors = new ArrayList<>(Arrays.asList(
    ImplicitElse::inspect,
    ThenElse::inspect,
    ElseIf::inspect
  ));


  @Nullable
  @Override
  public JComponent createOptionsPanel() {
    return new SingleCheckboxOptionsPanel(InspectionsBundle.message("inspection.common.if.parts.settings.highlight.when.tail.call"), this, "myHighlightWhenLastStatementIsCall");
  }

  @NotNull
  @Override
  public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
    return new JavaElementVisitor() {
      @Override
      public void visitIfStatement(PsiIfStatement ifStatement) {
        PsiStatement[] thenStatements = unwrap(ifStatement.getThenBranch());
        PsiStatement[] elseStatements = unwrap(ifStatement.getElseBranch());
        for (IfStatementInspector inspector : ourInspectors) {
          IfInspectionResult result = inspector.inspect(ifStatement, thenStatements, elseStatements, isOnTheFly, myHighlightWhenLastStatementIsCall);
          if (result != null) {
            ProblemHighlightType highlightType;
            if (result.myIsWarning) {
              highlightType = ProblemHighlightType.GENERIC_ERROR_OR_WARNING;
            }
            else {
              if (!isOnTheFly) return;
              highlightType = ProblemHighlightType.INFORMATION;
            }
            LocalQuickFix[] fixes;
            if (myHighlightWhenLastStatementIsCall) {
              fixes = new LocalQuickFix[]{
                new SetInspectionOptionFix(
                  IfStatementWithIdenticalBranchesInspection.this,
                  "myHighlightWhenLastStatementIsCall",
                  InspectionsBundle.message("inspection.common.if.parts.disable.highlight.tail.call"),
                  false
                ),
                result.myFix
              };
            } else {
              fixes = new LocalQuickFix[] {
                result.myFix
              };
            }
            holder.registerProblem(result.myElementToHighlight, result.myMessage, highlightType, fixes);
          }
        }
      }
    };
  }

  @FunctionalInterface
  private interface IfStatementInspector {
    @Nullable IfInspectionResult inspect(@NotNull PsiIfStatement ifStatement,
                                         @NotNull PsiStatement[] thenBranch,
                                         @NotNull PsiStatement[] elseBranch,
                                         boolean isOnTheFly,
                                         boolean highlightWhenLastStatementIsCall);
  }

  private static class IfInspectionResult {
    private final @NotNull PsiElement myElementToHighlight;
    private final boolean myIsWarning;
    private final @NotNull LocalQuickFix myFix;
    private final @NotNull String myMessage;

    IfInspectionResult(@NotNull PsiElement elementToHighlight,
                              boolean isWarning,
                              @NotNull LocalQuickFix fix,
                              @NotNull String message) {
      myElementToHighlight = elementToHighlight;
      myIsWarning = isWarning;
      myFix = fix;
      myMessage = message;
    }
  }

  private static class MergeElseIfsFix implements LocalQuickFix {
    @Nls
    @NotNull
    @Override
    public String getName() {
      return InspectionsBundle.message("inspection.common.if.parts.family.else.if");
    }

    @Nls
    @NotNull
    @Override
    public String getFamilyName() {
      return InspectionsBundle.message("inspection.common.if.parts.family.else.if");
    }

    @Override
    public boolean isAvailable(@NotNull ProblemDescriptor descriptor) {
      PsiIfStatement ifStatement = tryCast(descriptor.getStartElement().getParent(), PsiIfStatement.class);
      if (ifStatement == null || ifStatement.getCondition() == null) {
        return false;
      }
      return ElseIf.from(ifStatement, unwrap(ifStatement.getThenBranch())) != null;
    }

    @Override
{'fraction_non_alphanumeric': 0.0779757531227039, 'fraction_numerical': 0.002755326965466569, 'mean_word_length': 2.794076655052265, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3879349', 'n_tokens_mistral': 2888, 'n_tokens_neox': 2759, 'n_words': 661}