<commit_before>package com.intellij.codeInsight.daemon.impl;

import com.intellij.codeHighlighting.TextEditorHighlightingPass;
import com.intellij.codeInsight.CodeInsightSettings;
import com.intellij.codeInsight.CodeInsightUtil;
import com.intellij.codeInsight.completion.CompletionUtil;
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer;
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzerSettings;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.daemon.impl.actions.AddImportAction;
import com.intellij.codeInsight.daemon.impl.quickfix.QuickFixAction;
import com.intellij.codeInsight.hint.HintManager;
import com.intellij.codeInsight.hint.QuestionAction;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
import com.intellij.codeInsight.intention.impl.IntentionActionComposite;
import com.intellij.codeInsight.intention.impl.IntentionHintComponent;
import com.intellij.codeInsight.intention.impl.config.IntentionManagerSettings;
import com.intellij.codeInsight.lookup.LookupManager;
import com.intellij.codeInsight.template.impl.TemplateManagerImpl;
import com.intellij.codeInsight.template.impl.TemplateState;
import com.intellij.lang.annotation.HighlightSeverity;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.IdeActions;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.LogicalPosition;
import com.intellij.openapi.editor.impl.injected.EditorDelegate;
import com.intellij.openapi.keymap.KeymapUtil;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.packageDependencies.DependencyRule;
import com.intellij.packageDependencies.DependencyValidationManager;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.PsiShortNamesCache;
import com.intellij.psi.util.ClassUtil;
import com.intellij.psi.util.PsiUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class ShowIntentionsPass extends TextEditorHighlightingPass {
  private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.ShowIntentionsPass");
  private final Editor myEditor;
  private final IntentionAction[] myIntentionActions;

  private final PsiFile myFile;

  private int myStartOffset;
  private int myEndOffset;
  private final int myPassIdToShowIntentionsFor;

  ShowIntentionsPass(Project project, Editor editor, IntentionAction[] intentionActions, int passId) {
    super(project, editor.getDocument());
    myPassIdToShowIntentionsFor = passId;
    ApplicationManager.getApplication().assertIsDispatchThread();

    myEditor = editor;
    myIntentionActions = intentionActions;

    Rectangle visibleRect = myEditor.getScrollingModel().getVisibleArea();

    LogicalPosition startPosition = myEditor.xyToLogicalPosition(new Point(visibleRect.x, visibleRect.y));
    myStartOffset = myEditor.logicalPositionToOffset(startPosition);

    LogicalPosition endPosition = myEditor.xyToLogicalPosition(new Point(visibleRect.x + visibleRect.width, visibleRect.y + visibleRect.height));
    myEndOffset = myEditor.logicalPositionToOffset(new LogicalPosition(endPosition.line + 1, 0));

    myFile = PsiDocumentManager.getInstance(myProject).getPsiFile(myEditor.getDocument());
    LOG.assertTrue(myFile != null);
  }

  public void doCollectInformation(ProgressIndicator progress) {
  }

  public void doApplyInformationToEditor() {
    ApplicationManager.getApplication().assertIsDispatchThread();

    if (!myEditor.getContentComponent().hasFocus()) return;

    HighlightInfo[] visibleHighlights = getVisibleHighlights(myStartOffset, myEndOffset);

    PsiElement[] elements = new PsiElement[visibleHighlights.length];
    for (int i = 0; i < visibleHighlights.length; i++) {
      ProgressManager.getInstance().checkCanceled();

      HighlightInfo highlight = visibleHighlights[i];
      final PsiElement elementAt = myFile.findElementAt(highlight.startOffset);
      elements[i] = elementAt;
    }

    int caretOffset = myEditor.getCaretModel().getOffset();
    for (int i = visibleHighlights.length - 1; i >= 0; i--) {
      HighlightInfo info = visibleHighlights[i];
      if (elements[i] != null && info.startOffset <= caretOffset && showAddImportHint(info, elements[i])) return;
    }

    for (int i = 0; i < visibleHighlights.length; i++) {
      HighlightInfo info = visibleHighlights[i];
      if (elements[i] != null && info.startOffset > caretOffset && showAddImportHint(info, elements[i])) return;
    }

    if (!(myFile instanceof PsiCodeFragment)) {
      TemplateState state = TemplateManagerImpl.getTemplateState(myEditor);
      if (state == null || state.isFinished()) {
        showIntentionActions();
      }
    }
  }

  private void showIntentionActions() {
    DaemonCodeAnalyzerImpl codeAnalyzer = (DaemonCodeAnalyzerImpl)DaemonCodeAnalyzer.getInstance(myProject);
    if (LookupManager.getInstance(myProject).getActiveLookup() != null) return;

    // do not show intentions if caret is outside visible area
    LogicalPosition caretPos = myEditor.getCaretModel().getLogicalPosition();
    Rectangle visibleArea = myEditor.getScrollingModel().getVisibleArea();
    Point xy = myEditor.logicalPositionToXY(caretPos);
    if (!visibleArea.contains(xy)) return;
    final Editor injectedEditor = InjectedLanguageUtil.getEditorForInjectedLanguage(myEditor, myFile);
    final PsiFile injectedFile = injectedEditor instanceof EditorDelegate ? ((EditorDelegate)injectedEditor).getInjectedFile() : myFile;
    final PsiElement injectedElement = injectedFile.findElementAt(injectedEditor.getCaretModel().getOffset());

    List<HighlightInfo.IntentionActionDescriptor> intentionsToShow = new ArrayList<HighlightInfo.IntentionActionDescriptor>();
    List<HighlightInfo.IntentionActionDescriptor> fixesToShow = new ArrayList<HighlightInfo.IntentionActionDescriptor>();
    int offset = myEditor.getCaretModel().getOffset();
    HighlightInfo infoAtCursor = codeAnalyzer.findHighlightByOffset(myEditor.getDocument(), offset, true);
    for (IntentionAction action : myIntentionActions) {
      if (action instanceof IntentionActionComposite) {
        if (action instanceof QuickFixAction) {
          List<HighlightInfo.IntentionActionDescriptor> availableActions =
            ((IntentionActionComposite)action).getAvailableActions(myEditor, myFile, myPassIdToShowIntentionsFor);

          if (infoAtCursor == null || infoAtCursor.getSeverity() == HighlightSeverity.ERROR) {
            fixesToShow.addAll(availableActions);
          }
          else {
            intentionsToShow.addAll(availableActions);
          }
        }
      }
      else if (action instanceof PsiElementBaseIntentionAction && ((PsiElementBaseIntentionAction)action).isAvailable(myProject, injectedEditor, injectedElement)
               || action.isAvailable(myProject, injectedEditor, injectedFile)) {
        List<IntentionAction> enableDisableIntentionAction = new ArrayList<IntentionAction>();
        enableDisableIntentionAction.add(new IntentionHintComponent.EnableDisableIntentionAction(action));
        intentionsToShow.add(new HighlightInfo.IntentionActionDescriptor(action, enableDisableIntentionAction, null));
      }
    }

    if (!intentionsToShow.isEmpty() || !fixesToShow.isEmpty()) {
      boolean showBulb = false;
      for (HighlightInfo.IntentionActionDescriptor action : fixesToShow) {
        if (IntentionManagerSettings.getInstance().isShowLightBulb(action.getAction())) {
          showBulb = true;
          break;
        }
      }
      if (!showBulb) {
        for (HighlightInfo.IntentionActionDescriptor descriptor : intentionsToShow) {
          final IntentionAction action = descriptor.getAction();
          if (IntentionManagerSettings.getInstance().isShowLightBulb(action) && action.isAvailable(myProject, injectedEditor, injectedFile)) {
            showBulb = true;
            break;
          }
        }
      }

      if (showBulb) {
        IntentionHintComponent hintComponent = codeAnalyzer.getLastIntentionHint();

        if (hintComponent != null) {
          if (hintComponent.updateActions(fixesToShow, intentionsToShow)) {
            return;
          }
          hintComponent.hide();
          codeAnalyzer.setLastIntentionHint(null);
        }
        if (!HintManager.getInstance().hasShownHintsThatWillHideByOtherHint()) {
          hintComponent = IntentionHintComponent.showIntentionHint(myProject, injectedFile, injectedEditor, intentionsToShow, fixesToShow, false);
          codeAnalyzer.setLastIntentionHint(hintComponent);
        }
      }
    }
  }
<commit_msg>assertion added<commit_after>package com.intellij.codeInsight.daemon.impl;

import com.intellij.codeHighlighting.TextEditorHighlightingPass;
import com.intellij.codeInsight.CodeInsightSettings;
import com.intellij.codeInsight.CodeInsightUtil;
import com.intellij.codeInsight.completion.CompletionUtil;
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer;
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzerSettings;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.daemon.impl.actions.AddImportAction;
import com.intellij.codeInsight.daemon.impl.quickfix.QuickFixAction;
import com.intellij.codeInsight.hint.HintManager;
import com.intellij.codeInsight.hint.QuestionAction;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
import com.intellij.codeInsight.intention.impl.IntentionActionComposite;
import com.intellij.codeInsight.intention.impl.IntentionHintComponent;
import com.intellij.codeInsight.intention.impl.config.IntentionManagerSettings;
import com.intellij.codeInsight.lookup.LookupManager;
import com.intellij.codeInsight.template.impl.TemplateManagerImpl;
import com.intellij.codeInsight.template.impl.TemplateState;
import com.intellij.lang.annotation.HighlightSeverity;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.IdeActions;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.LogicalPosition;
import com.intellij.openapi.editor.impl.injected.EditorDelegate;
import com.intellij.openapi.keymap.KeymapUtil;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.packageDependencies.DependencyRule;
import com.intellij.packageDependencies.DependencyValidationManager;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.PsiShortNamesCache;
import com.intellij.psi.util.ClassUtil;
import com.intellij.psi.util.PsiUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class ShowIntentionsPass extends TextEditorHighlightingPass {
  private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.daemon.impl.ShowIntentionsPass");
  private final Editor myEditor;
  private final IntentionAction[] myIntentionActions;

  private final PsiFile myFile;

  private int myStartOffset;
  private int myEndOffset;
  private final int myPassIdToShowIntentionsFor;

  ShowIntentionsPass(Project project, Editor editor, IntentionAction[] intentionActions, int passId) {
    super(project, editor.getDocument());
    myPassIdToShowIntentionsFor = passId;
    ApplicationManager.getApplication().assertIsDispatchThread();

    myEditor = editor;
    myIntentionActions = intentionActions;

    Rectangle visibleRect = myEditor.getScrollingModel().getVisibleArea();

    LogicalPosition startPosition = myEditor.xyToLogicalPosition(new Point(visibleRect.x, visibleRect.y));
    myStartOffset = myEditor.logicalPositionToOffset(startPosition);

    LogicalPosition endPosition = myEditor.xyToLogicalPosition(new Point(visibleRect.x + visibleRect.width, visibleRect.y + visibleRect.height));
    myEndOffset = myEditor.logicalPositionToOffset(new LogicalPosition(endPosition.line + 1, 0));

    myFile = PsiDocumentManager.getInstance(myProject).getPsiFile(myEditor.getDocument());
    LOG.assertTrue(myFile != null);
  }

  public void doCollectInformation(ProgressIndicator progress) {
  }

  public void doApplyInformationToEditor() {
    ApplicationManager.getApplication().assertIsDispatchThread();

    if (!myEditor.getContentComponent().hasFocus()) return;

    HighlightInfo[] visibleHighlights = getVisibleHighlights(myStartOffset, myEndOffset);

    PsiElement[] elements = new PsiElement[visibleHighlights.length];
    for (int i = 0; i < visibleHighlights.length; i++) {
      ProgressManager.getInstance().checkCanceled();

      HighlightInfo highlight = visibleHighlights[i];
      final PsiElement elementAt = myFile.findElementAt(highlight.startOffset);
      elements[i] = elementAt;
    }

    int caretOffset = myEditor.getCaretModel().getOffset();
    for (int i = visibleHighlights.length - 1; i >= 0; i--) {
      HighlightInfo info = visibleHighlights[i];
      if (elements[i] != null && info.startOffset <= caretOffset && showAddImportHint(info, elements[i])) return;
    }

    for (int i = 0; i < visibleHighlights.length; i++) {
      HighlightInfo info = visibleHighlights[i];
      if (elements[i] != null && info.startOffset > caretOffset && showAddImportHint(info, elements[i])) return;
    }

    if (!(myFile instanceof PsiCodeFragment)) {
      TemplateState state = TemplateManagerImpl.getTemplateState(myEditor);
      if (state == null || state.isFinished()) {
        showIntentionActions();
      }
    }
  }

  private void showIntentionActions() {
    DaemonCodeAnalyzerImpl codeAnalyzer = (DaemonCodeAnalyzerImpl)DaemonCodeAnalyzer.getInstance(myProject);
    if (LookupManager.getInstance(myProject).getActiveLookup() != null) return;

    // do not show intentions if caret is outside visible area
    LogicalPosition caretPos = myEditor.getCaretModel().getLogicalPosition();
    Rectangle visibleArea = myEditor.getScrollingModel().getVisibleArea();
    Point xy = myEditor.logicalPositionToXY(caretPos);
    if (!visibleArea.contains(xy)) return;
    final Editor injectedEditor = InjectedLanguageUtil.getEditorForInjectedLanguage(myEditor, myFile);
    final PsiFile injectedFile = injectedEditor instanceof EditorDelegate ? ((EditorDelegate)injectedEditor).getInjectedFile() : myFile;
    final PsiElement injectedElement = injectedFile.findElementAt(injectedEditor.getCaretModel().getOffset());
    LOG.assertTrue(injectedElement == null || injectedElement.isValid(), injectedElement);

    List<HighlightInfo.IntentionActionDescriptor> intentionsToShow = new ArrayList<HighlightInfo.IntentionActionDescriptor>();
    List<HighlightInfo.IntentionActionDescriptor> fixesToShow = new ArrayList<HighlightInfo.IntentionActionDescriptor>();
    int offset = myEditor.getCaretModel().getOffset();
    HighlightInfo infoAtCursor = codeAnalyzer.findHighlightByOffset(myEditor.getDocument(), offset, true);
    for (IntentionAction action : myIntentionActions) {
      if (action instanceof IntentionActionComposite) {
        if (action instanceof QuickFixAction) {
          List<HighlightInfo.IntentionActionDescriptor> availableActions =
            ((IntentionActionComposite)action).getAvailableActions(myEditor, myFile, myPassIdToShowIntentionsFor);

          if (infoAtCursor == null || infoAtCursor.getSeverity() == HighlightSeverity.ERROR) {
            fixesToShow.addAll(availableActions);
          }
          else {
            intentionsToShow.addAll(availableActions);
          }
        }
      }
      else if (action instanceof PsiElementBaseIntentionAction && ((PsiElementBaseIntentionAction)action).isAvailable(myProject, injectedEditor, injectedElement)
               || action.isAvailable(myProject, injectedEditor, injectedFile)) {
        List<IntentionAction> enableDisableIntentionAction = new ArrayList<IntentionAction>();
        enableDisableIntentionAction.add(new IntentionHintComponent.EnableDisableIntentionAction(action));
        intentionsToShow.add(new HighlightInfo.IntentionActionDescriptor(action, enableDisableIntentionAction, null));
      }
    }

    if (!intentionsToShow.isEmpty() || !fixesToShow.isEmpty()) {
      boolean showBulb = false;
      for (HighlightInfo.IntentionActionDescriptor action : fixesToShow) {
        if (IntentionManagerSettings.getInstance().isShowLightBulb(action.getAction())) {
          showBulb = true;
          break;
        }
      }
      if (!showBulb) {
        for (HighlightInfo.IntentionActionDescriptor descriptor : intentionsToShow) {
          final IntentionAction action = descriptor.getAction();
          if (IntentionManagerSettings.getInstance().isShowLightBulb(action) && action.isAvailable(myProject, injectedEditor, injectedFile)) {
            showBulb = true;
            break;
          }
        }
      }

      if (showBulb) {
        IntentionHintComponent hintComponent = codeAnalyzer.getLastIntentionHint();

        if (hintComponent != null) {
          if (hintComponent.updateActions(fixesToShow, intentionsToShow)) {
            return;
          }
          codeAnalyzer.setLastIntentionHint(null);
        }
        if (!HintManager.getInstance().hasShownHintsThatWillHideByOtherHint()) {
          hintComponent = IntentionHintComponent.showIntentionHint(myProject, injectedFile, injectedEditor, intentionsToShow, fixesToShow, false);
          codeAnalyzer.setLastIntentionHint(hintComponent);
        }
      }
    }
  }

{'fraction_non_alphanumeric': 0.09552017771195853, 'fraction_numerical': 0.0006346855661924155, 'mean_word_length': 4.912445278298937, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3838013', 'n_tokens_mistral': 5368, 'n_tokens_neox': 5235, 'n_words': 876}