<commit_before>package org.coursera.courier;

import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.lang.annotation.Annotator;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.psi.PsiElement;
import org.coursera.courier.psi.CourierEnumSymbol;
import org.coursera.courier.psi.CourierFieldName;
import org.coursera.courier.psi.CourierPropNameDeclaration;
import org.coursera.courier.psi.CourierTypeNameDeclaration;
import org.coursera.courier.psi.CourierTypeReference;
import org.coursera.courier.psi.CourierVisitor;
import org.jetbrains.annotations.NotNull;

public class CourierAnnotator implements Annotator {
  @Override
  public void annotate(@NotNull final PsiElement element, @NotNull final AnnotationHolder holder) {
    element.accept(new CourierVisitor() {
      @Override
      public void visitFieldName(@NotNull CourierFieldName o) {
        super.visitFieldName(o);
        setHighlighting(o, holder, CourierSyntaxHighlighter.FIELD);
      }
    });
    element.accept(new CourierVisitor() {
      @Override
      public void visitTypeNameDeclaration(@NotNull CourierTypeNameDeclaration o) {
        super.visitTypeNameDeclaration(o);
        setHighlighting(o, holder, CourierSyntaxHighlighter.TYPE_NAME);
      }
    });
    element.accept(new CourierVisitor() {
      @Override
      public void visitTypeReference(@NotNull CourierTypeReference o) {
        super.visitTypeReference(o);
        setHighlighting(o, holder, CourierSyntaxHighlighter.TYPE_REFERENCE);
        if (!o.getFullname().isPrimitive()) {
          if (o.getReference() == null) {
            holder.createErrorAnnotation(o, "Cannot resolve symbol '" + o.getText() + "'");
          }
        }
      }
    });
    element.accept(new CourierVisitor() {
      @Override
      public void visitPropNameDeclaration(@NotNull CourierPropNameDeclaration o) {
        super.visitPropNameDeclaration(o);
        setHighlighting(o, holder, CourierSyntaxHighlighter.PROPERTY);
      }
    });
    element.accept(new CourierVisitor() {
      @Override
      public void visitEnumSymbol(@NotNull CourierEnumSymbol o) {
        super.visitEnumSymbol(o);
        setHighlighting(o, holder, CourierSyntaxHighlighter.FIELD);
      }
    });
  }

  private static void setHighlighting(@NotNull PsiElement element, @NotNull AnnotationHolder holder,
                                      @NotNull TextAttributesKey key) {
    holder.createInfoAnnotation(element, null).setEnforcedTextAttributes(TextAttributes.ERASE_MARKER);
    holder.createInfoAnnotation(element, null).setEnforcedTextAttributes(
        EditorColorsManager.getInstance().getGlobalScheme().getAttributes(key));
  }
}
<commit_msg>Add unused import detection to Courier IntelliJ plugin.<commit_after>package org.coursera.courier;

import com.intellij.codeInsight.daemon.impl.HighlightInfoType;
import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.lang.annotation.Annotator;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.psi.PsiElement;
import org.coursera.courier.psi.CourierEnumSymbol;
import org.coursera.courier.psi.CourierFieldName;
import org.coursera.courier.psi.CourierImportDeclaration;
import org.coursera.courier.psi.CourierPropNameDeclaration;
import org.coursera.courier.psi.CourierTypeNameDeclaration;
import org.coursera.courier.psi.CourierTypeReference;
import org.coursera.courier.psi.CourierVisitor;
import org.jetbrains.annotations.NotNull;

public class CourierAnnotator implements Annotator {
  @Override
  public void annotate(@NotNull final PsiElement element, @NotNull final AnnotationHolder holder) {
    element.accept(new CourierVisitor() {
      @Override
      public void visitFieldName(@NotNull CourierFieldName o) {
        super.visitFieldName(o);
        setHighlighting(o, holder, CourierSyntaxHighlighter.FIELD);
      }
    });
    element.accept(new CourierVisitor() {
      @Override
      public void visitTypeNameDeclaration(@NotNull CourierTypeNameDeclaration o) {
        super.visitTypeNameDeclaration(o);
        setHighlighting(o, holder, CourierSyntaxHighlighter.TYPE_NAME);
      }
    });
    element.accept(new CourierVisitor() {
      @Override
      public void visitTypeReference(@NotNull CourierTypeReference o) {
        super.visitTypeReference(o);
        setHighlighting(o, holder, CourierSyntaxHighlighter.TYPE_REFERENCE);
        if (!o.getFullname().isPrimitive()) {
          if (o.getReference() == null) {
            holder.createErrorAnnotation(o, "Cannot resolve symbol '" + o.getText() + "'");
          }
        }
      }
    });
    element.accept(new CourierVisitor() {
      @Override
      public void visitPropNameDeclaration(@NotNull CourierPropNameDeclaration o) {
        super.visitPropNameDeclaration(o);
        setHighlighting(o, holder, CourierSyntaxHighlighter.PROPERTY);
      }
    });
    element.accept(new CourierVisitor() {
      @Override
      public void visitEnumSymbol(@NotNull CourierEnumSymbol o) {
        super.visitEnumSymbol(o);
        setHighlighting(o, holder, CourierSyntaxHighlighter.FIELD);
      }
    });
    element.accept(new CourierVisitor() {
      @Override
      public void visitImportDeclaration(@NotNull CourierImportDeclaration o) {
        super.visitImportDeclaration(o);
        boolean found = false;
        for (CourierTypeReference ref: o.getCourierFile().getTypeReferences()) {
          if (ref.getFullname().equals(o.getFullname())) {
            found = true;
            break;
          }
        }
        if (!found) {
          setHighlighting(o, holder, HighlightInfoType.UNUSED_SYMBOL.getAttributesKey());
        }
      }
    });
  }

  private static void setHighlighting(@NotNull PsiElement element, @NotNull AnnotationHolder holder,
                                      @NotNull TextAttributesKey key) {
    holder.createInfoAnnotation(element, null).setEnforcedTextAttributes(TextAttributes.ERASE_MARKER);
    holder.createInfoAnnotation(element, null).setEnforcedTextAttributes(
        EditorColorsManager.getInstance().getGlobalScheme().getAttributes(key));
  }
}

{'fraction_non_alphanumeric': 0.09680950891460745, 'fraction_numerical': 0.0, 'mean_word_length': 3.7546468401486988, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4624701', 'n_tokens_mistral': 1822, 'n_tokens_neox': 1779, 'n_words': 301}