<commit_before>package com.intellij.tasks.navigation;

import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiComment;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.PsiReferenceProvider;
import com.intellij.tasks.TaskManager;
import com.intellij.tasks.TaskRepository;
import com.intellij.util.ProcessingContext;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Vladislav.Rassokhin
 */
class TaskInPsiCommentReferenceProvider extends PsiReferenceProvider {
  @NotNull
  @Override
  public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
    if (!(element instanceof PsiComment)) {
      return PsiReference.EMPTY_ARRAY;
    }
    final PsiComment comment = (PsiComment) element;
    final TaskNavigationConfig config = ServiceManager.getService(comment.getProject(), TaskNavigationConfig.class);
    if (!config.searchInComments) {
      return PsiReference.EMPTY_ARRAY;
    }
    final String text = comment.getText();
    if (text== null) {
      return PsiReference.EMPTY_ARRAY;
    }
    final List<TextRange> ranges = new ArrayList<TextRange>();
    final TaskRepository[] repositories = TaskManager.getManager(comment.getProject()).getAllRepositories();
    for (TaskRepository repository : repositories) {
      int prev = 0;
      String toCheck = text;
      while (true) {
        final String id = repository.extractId(toCheck);
        if (id == null) {
          break;
        }
        int i = toCheck.indexOf(id);
        if (i < 0) {
          break;
        }
        ranges.add(new TextRange(prev + i, prev + i + id.length()));
        prev += i + id.length();
        toCheck = text.substring(prev);
      }
    }

    // TODO: Check intersecting ranges (between many TaskRepositories)

    final List<PsiCommentToTaskReference> references = new ArrayList<PsiCommentToTaskReference>();<commit_msg>Fix possible NPE<commit_after>package com.intellij.tasks.navigation;

import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiComment;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.PsiReferenceProvider;
import com.intellij.tasks.TaskManager;
import com.intellij.tasks.TaskRepository;
import com.intellij.util.ProcessingContext;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Vladislav.Rassokhin
 */
class TaskInPsiCommentReferenceProvider extends PsiReferenceProvider {
  @NotNull
  @Override
  public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
    if (!(element instanceof PsiComment)) {
      return PsiReference.EMPTY_ARRAY;
    }
    final PsiComment comment = (PsiComment) element;
    final TaskNavigationConfig config = ServiceManager.getService(comment.getProject(), TaskNavigationConfig.class);
    if (!config.searchInComments) {
      return PsiReference.EMPTY_ARRAY;
    }
    final String text = comment.getText();
    if (text== null) {
      return PsiReference.EMPTY_ARRAY;
    }
    final List<TextRange> ranges = new ArrayList<TextRange>();
    final TaskManager manager = TaskManager.getManager(comment.getProject());
    if (manager == null) {
      return PsiReference.EMPTY_ARRAY;
    }
    final TaskRepository[] repositories = manager.getAllRepositories();
    for (TaskRepository repository : repositories) {
      int prev = 0;
      String toCheck = text;
      while (true) {
        final String id = repository.extractId(toCheck);
        if (id == null) {
          break;
        }
        int i = toCheck.indexOf(id);
        if (i < 0) {
          break;
        }
        ranges.add(new TextRange(prev + i, prev + i + id.length()));
        prev += i + id.length();
        toCheck = text.substring(prev);
      }
    }

    // TODO: Check intersecting ranges (between many TaskRepositories)

    final List<PsiCommentToTaskReference> references = new ArrayList<PsiCommentToTaskReference>();
{'fraction_non_alphanumeric': 0.10307328605200945, 'fraction_numerical': 0.0009456264775413711, 'mean_word_length': 3.6648291069459757, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, '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': '5541657', 'n_tokens_mistral': 1218, 'n_tokens_neox': 1200, 'n_words': 270}