<commit_before>package com.intellij.testFramework.fixtures;

import com.intellij.codeInsight.completion.CompletionPhase;
import com.intellij.codeInsight.completion.impl.CompletionServiceImpl;
import com.intellij.codeInsight.editorActions.CompletionAutoPopupHandler;
import com.intellij.codeInsight.lookup.impl.LookupImpl;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.ex.DocumentEx;
import com.intellij.openapi.util.EmptyRunnable;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.testFramework.EdtTestUtil;
import com.intellij.testFramework.TestModeFlags;
import com.intellij.testFramework.UsefulTestCase;
import com.intellij.util.ThrowableRunnable;
import com.intellij.util.TimeoutUtil;
import com.intellij.util.ui.UIUtil;
import junit.framework.TestCase;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Predicate;

/**
 * @author peter
 */
public class CompletionAutoPopupTester {
  private final CodeInsightTestFixture myFixture;

  public CompletionAutoPopupTester(CodeInsightTestFixture fixture) {
    myFixture = fixture;
  }

  public void runWithAutoPopupEnabled(@NotNull ThrowableRunnable<Throwable> r) throws Throwable {
    assert !ApplicationManager.getApplication().isDispatchThread();
    TestModeFlags.set(CompletionAutoPopupHandler.ourTestingAutopopup, true);
    try {
      r.run();
    }
    finally {
      TestModeFlags.reset(CompletionAutoPopupHandler.ourTestingAutopopup);
      ((DocumentEx)myFixture.getEditor().getDocument()).setModificationStamp(0);// to force possible autopopup handler's invokeLater cancel itself
    }
  }

  public void joinCompletion() {
    waitPhase(phase -> !(phase instanceof CompletionPhase.CommittingDocuments ||
                         phase instanceof CompletionPhase.Synchronous ||
                         phase instanceof CompletionPhase.BgCalculation));
  }

  @SuppressWarnings("UseOfSystemOutOrSystemErr")
  private static void waitPhase(Predicate<? super CompletionPhase> condition) {<commit_msg>[OC-20280] SourceKit completion: fix autopopup tests NPE<commit_after>package com.intellij.testFramework.fixtures;

import com.intellij.codeInsight.completion.CompletionPhase;
import com.intellij.codeInsight.completion.impl.CompletionServiceImpl;
import com.intellij.codeInsight.editorActions.CompletionAutoPopupHandler;
import com.intellij.codeInsight.lookup.impl.LookupImpl;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.ex.DocumentEx;
import com.intellij.openapi.util.EmptyRunnable;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.testFramework.EdtTestUtil;
import com.intellij.testFramework.TestModeFlags;
import com.intellij.testFramework.UsefulTestCase;
import com.intellij.util.ThrowableRunnable;
import com.intellij.util.TimeoutUtil;
import com.intellij.util.ui.UIUtil;
import junit.framework.TestCase;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Predicate;

/**
 * @author peter
 */
public class CompletionAutoPopupTester {
  private final CodeInsightTestFixture myFixture;

  public CompletionAutoPopupTester(CodeInsightTestFixture fixture) {
    myFixture = fixture;
  }

  public void runWithAutoPopupEnabled(@NotNull ThrowableRunnable<Throwable> r) throws Throwable {
    assert !ApplicationManager.getApplication().isDispatchThread();
    TestModeFlags.set(CompletionAutoPopupHandler.ourTestingAutopopup, true);
    try {
      r.run();
    }
    finally {
      TestModeFlags.reset(CompletionAutoPopupHandler.ourTestingAutopopup);
      Editor editor = myFixture.getEditor();
      if (editor != null) {
        ((DocumentEx)editor.getDocument()).setModificationStamp(0);// to force possible autopopup handler's invokeLater cancel itself
      }
    }
  }

  public void joinCompletion() {
    waitPhase(phase -> !(phase instanceof CompletionPhase.CommittingDocuments ||
                         phase instanceof CompletionPhase.Synchronous ||
                         phase instanceof CompletionPhase.BgCalculation));
  }

  @SuppressWarnings("UseOfSystemOutOrSystemErr")
  private static void waitPhase(Predicate<? super CompletionPhase> condition) {
{'fraction_non_alphanumeric': 0.08950759318913944, 'fraction_numerical': 0.0016106764841233318, 'mean_word_length': 5.21, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '4446446', 'n_tokens_mistral': 1271, 'n_tokens_neox': 1252, 'n_words': 208}