<commit_before>package io.flutter.analytics;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;

import com.jetbrains.lang.dart.ide.completion.DartCompletionTimerExtension;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.time.Instant;

/**
 * Implementation of the {@link DartCompletionTimerExtension} which allows the Flutter plugin to
 * measure the total time to present a code completion to the user.
 */
public class DartCompletionTimerListener extends DartCompletionTimerExtension {
  @Nullable FlutterAnalysisServerListener dasListener;
  @Nullable Long startTimeMS;

  @Override
  public void dartCompletionStart() {
    if (dasListener == null) {
      Project project = ProjectManager.getInstance().getDefaultProject();
      dasListener = FlutterAnalysisServerListener.getInstance(project);
    }
    startTimeMS = Instant.now().toEpochMilli();
  }

  @Override
  public void dartCompletionEnd() {
    if (dasListener != null && startTimeMS != null) {
      long durationTimeMS = Instant.now().toEpochMilli() - startTimeMS;
      dasListener.logE2ECompletionSuccessMS(durationTimeMS); // test: logE2ECompletionSuccessMS()
      startTimeMS = null;
    }
  }

  /**<commit_msg>Catch exception if default project was disposed (#6401)<commit_after>package io.flutter.analytics;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;

import com.intellij.util.IncorrectOperationException;
import com.jetbrains.lang.dart.ide.completion.DartCompletionTimerExtension;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.time.Instant;

/**
 * Implementation of the {@link DartCompletionTimerExtension} which allows the Flutter plugin to
 * measure the total time to present a code completion to the user.
 */
public class DartCompletionTimerListener extends DartCompletionTimerExtension {
  @Nullable FlutterAnalysisServerListener dasListener;
  @Nullable Long startTimeMS;

  @Override
  public void dartCompletionStart() {
    if (dasListener == null) {
      try {
        Project project = ProjectManager.getInstance().getDefaultProject();
        dasListener = FlutterAnalysisServerListener.getInstance(project);
      } catch (IncorrectOperationException ex) {
        return;
      }
    }
    startTimeMS = Instant.now().toEpochMilli();
  }

  @Override
  public void dartCompletionEnd() {
    if (dasListener != null && startTimeMS != null) {
      long durationTimeMS = Instant.now().toEpochMilli() - startTimeMS;
      dasListener.logE2ECompletionSuccessMS(durationTimeMS); // test: logE2ECompletionSuccessMS()
      startTimeMS = null;
    }
  }

  /**
{'fraction_non_alphanumeric': 0.09226298583363603, 'fraction_numerical': 0.002905920813657828, 'mean_word_length': 4.574898785425101, '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': '5137124', 'n_tokens_mistral': 776, 'n_tokens_neox': 742, 'n_words': 174}