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

import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.codeInsight.CodeInsightUtil;
import com.intellij.codeInsight.ExternalAnnotationsManager;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.lang.findUsages.FindUsagesProvider;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;

/**
 * @author ven
 */
public class AddAnnotationFix implements IntentionAction, LocalQuickFix {
  private final String myFQN;
  private final PsiModifierListOwner myModifierListOwner;
  private static final Logger LOG = Logger.getInstance("#" + AddAnnotationFix.class.getName());


  public AddAnnotationFix(String fqn, PsiModifierListOwner modifierListOwner) {
    myFQN = fqn;
    myModifierListOwner = modifierListOwner;
  }

  public AddAnnotationFix(final String FQN) {
    myFQN = FQN;
    myModifierListOwner = null;
  }

  @NotNull
  public String getText() {
    final String shortName = myFQN.substring(myFQN.lastIndexOf('.') + 1);
    if (myModifierListOwner instanceof PsiNamedElement) {
      final String name = ((PsiNamedElement)myModifierListOwner).getName();
      if (name != null) {
        FindUsagesProvider provider = myModifierListOwner.getLanguage().getFindUsagesProvider();
        return CodeInsightBundle.message("inspection.i18n.quickfix.annotate.element.as", provider.getType(myModifierListOwner), name, shortName);
      }
    }
    return CodeInsightBundle.message("inspection.i18n.quickfix.annotate.as", shortName);
  }

  @NotNull
  public String getName() {
    return getText();
  }

  @NotNull
  public String getFamilyName() {
    return CodeInsightBundle.message("intention.add.annotation.family");
  }

  public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    try {
      invoke(project, null, descriptor.getPsiElement().getContainingFile());
    }
    catch (IncorrectOperationException e) {
      LOG.error(e);
    }
  }

  public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
    if (myModifierListOwner != null) {
      return myModifierListOwner.isValid()
             && PsiManager.getInstance(project).isInProject(myModifierListOwner)
             && myModifierListOwner.getModifierList() != null;
    }
    if (!file.getManager().isInProject(file)) {
      final PsiElement element = file.findElementAt(editor.getCaretModel().getOffset());
      if (element != null) {
        final PsiElement parent = element.getParent();
        if (parent instanceof PsiMethod) {
          return true;
        }
        if (parent instanceof PsiField) {
          return true;
        }
        if (parent instanceof PsiParameter) {
          return true;
        }
      }
    }
    return false;
  }

  public void invoke(@NotNull final Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    final ExternalAnnotationsManager annotationsManager = ExternalAnnotationsManager.getInstance();
    if (myModifierListOwner != null) {
      final PsiModifierList modifierList = myModifierListOwner.getModifierList();
      LOG.assertTrue(modifierList != null);
      if (modifierList.findAnnotation(myFQN) != null) return;<commit_msg>do not allow annotating the same node with the same annotation for many times ( 	 IDEADEV-18591)<commit_after>package com.intellij.codeInsight.intention.impl;

import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.codeInsight.CodeInsightUtil;
import com.intellij.codeInsight.ExternalAnnotationsManager;
import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.lang.findUsages.FindUsagesProvider;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;

/**
 * @author ven
 */
public class AddAnnotationFix implements IntentionAction, LocalQuickFix {
  private final String myFQN;
  private final PsiModifierListOwner myModifierListOwner;
  private static final Logger LOG = Logger.getInstance("#" + AddAnnotationFix.class.getName());


  public AddAnnotationFix(String fqn, PsiModifierListOwner modifierListOwner) {
    myFQN = fqn;
    myModifierListOwner = modifierListOwner;
  }

  public AddAnnotationFix(final String FQN) {
    myFQN = FQN;
    myModifierListOwner = null;
  }

  @NotNull
  public String getText() {
    final String shortName = myFQN.substring(myFQN.lastIndexOf('.') + 1);
    if (myModifierListOwner instanceof PsiNamedElement) {
      final String name = ((PsiNamedElement)myModifierListOwner).getName();
      if (name != null) {
        FindUsagesProvider provider = myModifierListOwner.getLanguage().getFindUsagesProvider();
        return CodeInsightBundle.message("inspection.i18n.quickfix.annotate.element.as", provider.getType(myModifierListOwner), name, shortName);
      }
    }
    return CodeInsightBundle.message("inspection.i18n.quickfix.annotate.as", shortName);
  }

  @NotNull
  public String getName() {
    return getText();
  }

  @NotNull
  public String getFamilyName() {
    return CodeInsightBundle.message("intention.add.annotation.family");
  }

  public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    try {
      invoke(project, null, descriptor.getPsiElement().getContainingFile());
    }
    catch (IncorrectOperationException e) {
      LOG.error(e);
    }
  }

  public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
    if (myModifierListOwner != null) {
      return myModifierListOwner.isValid()
             && PsiManager.getInstance(project).isInProject(myModifierListOwner)
             && myModifierListOwner.getModifierList() != null;
    }
    if (!file.getManager().isInProject(file)) {
      final PsiElement element = file.findElementAt(editor.getCaretModel().getOffset());
      if (element != null) {
        final PsiElement parent = element.getParent();
        PsiModifierListOwner owner = null;
        if (parent instanceof PsiMethod || parent instanceof PsiField || parent instanceof PsiParameter) {
          owner = (PsiModifierListOwner)parent;
        }
        return owner != null && !AnnotationUtil.isAnnotated(owner, myFQN, false);
      }
    }
    return false;
  }

  public void invoke(@NotNull final Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    final ExternalAnnotationsManager annotationsManager = ExternalAnnotationsManager.getInstance();
    if (myModifierListOwner != null) {
      final PsiModifierList modifierList = myModifierListOwner.getModifierList();
      LOG.assertTrue(modifierList != null);
      if (modifierList.findAnnotation(myFQN) != null) return;
{'fraction_non_alphanumeric': 0.09676560628244377, 'fraction_numerical': 0.001996539331824837, 'mean_word_length': 4.306497175141243, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7153521', 'n_tokens_mistral': 2185, 'n_tokens_neox': 2180, 'n_words': 438}