<commit_before>package com.intellij.execution.impl;

import com.intellij.codeInsight.navigation.IncrementalSearchHandler;
import com.intellij.execution.ExecutionBundle;
import com.intellij.execution.filters.*;
import com.intellij.execution.process.ProcessHandler;
import com.intellij.execution.ui.ConsoleView;
import com.intellij.execution.ui.ConsoleViewContentType;
import com.intellij.ide.CommonActionsManager;
import com.intellij.ide.DataAccessor;
import com.intellij.ide.GeneralSettings;
import com.intellij.ide.OccurenceNavigator;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.diff.actions.DiffActions;
import com.intellij.openapi.editor.*;
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
import com.intellij.openapi.editor.actionSystem.EditorActionManager;
import com.intellij.openapi.editor.actionSystem.TypedAction;
import com.intellij.openapi.editor.actionSystem.TypedActionHandler;
import com.intellij.openapi.editor.colors.CodeInsightColors;
import com.intellij.openapi.editor.colors.EditorColors;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.event.DocumentAdapter;
import com.intellij.openapi.editor.event.DocumentEvent;
import com.intellij.openapi.editor.event.EditorMouseAdapter;
import com.intellij.openapi.editor.event.EditorMouseEvent;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.editor.ex.MarkupModelEx;
import com.intellij.openapi.editor.highlighter.EditorHighlighter;
import com.intellij.openapi.editor.highlighter.HighlighterClient;
import com.intellij.openapi.editor.highlighter.HighlighterIterator;
import com.intellij.openapi.editor.markup.HighlighterLayer;
import com.intellij.openapi.editor.markup.HighlighterTargetArea;
import com.intellij.openapi.editor.markup.RangeHighlighter;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.ide.CopyPasteManager;
import com.intellij.openapi.keymap.Keymap;
import com.intellij.openapi.keymap.KeymapManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.pom.Navigatable;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.Alarm;
import com.intellij.util.EditorPopupHandler;
import com.intellij.util.containers.HashMap;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.IOException;
import java.util.*;
import java.util.List;

public final class ConsoleViewImpl extends JPanel implements ConsoleView, DataProvider, OccurenceNavigator {
  private static final Logger LOG = Logger.getInstance("#com.intellij.execution.impl.ConsoleViewImpl");

  private static final int FLUSH_DELAY = 200; //TODO : make it an option

  private static final Key<ConsoleViewImpl> CONSOLE_VIEW_IN_EDITOR_VIEW = Key.create("CONSOLE_VIEW_IN_EDITOR_VIEW");
  static {
    final EditorActionManager actionManager = EditorActionManager.getInstance();
    final TypedAction typedAction = actionManager.getTypedAction();
    typedAction.setupHandler(new MyTypedHandler(typedAction.getHandler()));
  }

  private static final Color BACKGROUND_COLOR = Color.white;
  private static final TextAttributes HYPERLINK_ATTRIBUTES = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(CodeInsightColors.HYPERLINK_ATTRIBUTES);
  private static final TextAttributes FOLLOWED_HYPERLINK_ATTRIBUTES = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(CodeInsightColors.FOLLOWED_HYPERLINK_ATTRIBUTES);

  private final DisposedPsiManagerCheck myPsiDisposedCheck;
  private ConsoleState myState = ConsoleState.NOT_STARTED;
  private final int CYCLIC_BUFFER_SIZE = GeneralSettings.getInstance().getCyclicBufferSize();
  private final boolean USE_CYCLIC_BUFFER = GeneralSettings.getInstance().isUseCyclicBuffer();
  private static final int HYPERLINK_LAYER = HighlighterLayer.SELECTION - 123;
  private Alarm mySpareTimeAlarm = new Alarm();

  private static class TokenInfo{
    private final ConsoleViewContentType contentType;
    private int startOffset;
    private int endOffset;
    private final TextAttributes attributes;

    public TokenInfo(final ConsoleViewContentType contentType, final int startOffset, final int endOffset) {
      this.contentType = contentType;
      this.startOffset = startOffset;
      this.endOffset = endOffset;
      attributes = contentType.getAttributes();
    }
  }

  private final Project myProject;

  private boolean myOutputPaused;

  private Editor myEditor;

  private final Object LOCK = new Object();

  private int myContentSize;
  private StringBuffer myDeferredOutput = new StringBuffer();
  private StringBuffer myDeferredUserInput = new StringBuffer();

  private ArrayList<TokenInfo> myTokens = new ArrayList<TokenInfo>();
  private final Hyperlinks myHyperlinks = new Hyperlinks();

  private String myHelpId;

  private Alarm myFlushAlarm = new Alarm();

  private final Runnable myFlushDeferredRunnable = new Runnable() {
    public void run() {
      flushDeferredText();
    }
  };

  private CompositeFilter myMessageFilter = new CompositeFilter();

  public ConsoleViewImpl(final Project project) {
    super(new BorderLayout());
    myPsiDisposedCheck = new DisposedPsiManagerCheck(project);
    myProject = project;

    final ConsoleFilterProvider[] filterProviders = Extensions.getExtensions(ConsoleFilterProvider.FILTER_PROVIDERS);
    for (ConsoleFilterProvider filterProvider : filterProviders) {
      final Filter[] defaultFilters = filterProvider.getDefaultFilters(project);
      for (Filter filter : defaultFilters) {
        addMessageFilter(filter);
      }
    }
  }

  public void attachToProcess(final ProcessHandler processHandler){
    myState = myState.attachTo(this, processHandler);
  }

  public void clear() {
    assertIsDispatchThread();

    synchronized(LOCK){
      myContentSize = 0;
      if (USE_CYCLIC_BUFFER) {
        myDeferredOutput = new StringBuffer(Math.min(myDeferredOutput.length(), CYCLIC_BUFFER_SIZE));
      }
      else {
        myDeferredOutput = new StringBuffer();
      }
      myDeferredUserInput = new StringBuffer();
      if (myEditor != null){
        ApplicationManager.getApplication().runWriteAction(new Runnable() {
          public void run() {
            myHyperlinks.clear();
            myEditor.getMarkupModel().removeAllHighlighters();
            myTokens.clear();
            CommandProcessor.getInstance().executeCommand(myProject, new Runnable() {
              public void run() {
                myEditor.getDocument().deleteString(0, myEditor.getDocument().getTextLength());
              }
            }, null, myEditor.getDocument());
          }
        });
      }
    }
  }

  public void scrollTo(final int offset) {<commit_msg>deadlock fix<commit_after>package com.intellij.execution.impl;

import com.intellij.codeInsight.navigation.IncrementalSearchHandler;
import com.intellij.execution.ExecutionBundle;
import com.intellij.execution.filters.*;
import com.intellij.execution.process.ProcessHandler;
import com.intellij.execution.ui.ConsoleView;
import com.intellij.execution.ui.ConsoleViewContentType;
import com.intellij.ide.CommonActionsManager;
import com.intellij.ide.DataAccessor;
import com.intellij.ide.GeneralSettings;
import com.intellij.ide.OccurenceNavigator;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.diff.actions.DiffActions;
import com.intellij.openapi.editor.*;
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
import com.intellij.openapi.editor.actionSystem.EditorActionManager;
import com.intellij.openapi.editor.actionSystem.TypedAction;
import com.intellij.openapi.editor.actionSystem.TypedActionHandler;
import com.intellij.openapi.editor.colors.CodeInsightColors;
import com.intellij.openapi.editor.colors.EditorColors;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.event.DocumentAdapter;
import com.intellij.openapi.editor.event.DocumentEvent;
import com.intellij.openapi.editor.event.EditorMouseAdapter;
import com.intellij.openapi.editor.event.EditorMouseEvent;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.editor.ex.MarkupModelEx;
import com.intellij.openapi.editor.highlighter.EditorHighlighter;
import com.intellij.openapi.editor.highlighter.HighlighterClient;
import com.intellij.openapi.editor.highlighter.HighlighterIterator;
import com.intellij.openapi.editor.markup.HighlighterLayer;
import com.intellij.openapi.editor.markup.HighlighterTargetArea;
import com.intellij.openapi.editor.markup.RangeHighlighter;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.ide.CopyPasteManager;
import com.intellij.openapi.keymap.Keymap;
import com.intellij.openapi.keymap.KeymapManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.pom.Navigatable;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.Alarm;
import com.intellij.util.EditorPopupHandler;
import com.intellij.util.containers.HashMap;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.IOException;
import java.util.*;
import java.util.List;

public final class ConsoleViewImpl extends JPanel implements ConsoleView, DataProvider, OccurenceNavigator {
  private static final Logger LOG = Logger.getInstance("#com.intellij.execution.impl.ConsoleViewImpl");

  private static final int FLUSH_DELAY = 200; //TODO : make it an option

  private static final Key<ConsoleViewImpl> CONSOLE_VIEW_IN_EDITOR_VIEW = Key.create("CONSOLE_VIEW_IN_EDITOR_VIEW");
  static {
    final EditorActionManager actionManager = EditorActionManager.getInstance();
    final TypedAction typedAction = actionManager.getTypedAction();
    typedAction.setupHandler(new MyTypedHandler(typedAction.getHandler()));
  }

  private static final Color BACKGROUND_COLOR = Color.white;
  private static final TextAttributes HYPERLINK_ATTRIBUTES = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(CodeInsightColors.HYPERLINK_ATTRIBUTES);
  private static final TextAttributes FOLLOWED_HYPERLINK_ATTRIBUTES = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(CodeInsightColors.FOLLOWED_HYPERLINK_ATTRIBUTES);

  private final DisposedPsiManagerCheck myPsiDisposedCheck;
  private ConsoleState myState = ConsoleState.NOT_STARTED;
  private final int CYCLIC_BUFFER_SIZE = GeneralSettings.getInstance().getCyclicBufferSize();
  private final boolean USE_CYCLIC_BUFFER = GeneralSettings.getInstance().isUseCyclicBuffer();
  private static final int HYPERLINK_LAYER = HighlighterLayer.SELECTION - 123;
  private Alarm mySpareTimeAlarm = new Alarm();

  private static class TokenInfo{
    private final ConsoleViewContentType contentType;
    private int startOffset;
    private int endOffset;
    private final TextAttributes attributes;

    public TokenInfo(final ConsoleViewContentType contentType, final int startOffset, final int endOffset) {
      this.contentType = contentType;
      this.startOffset = startOffset;
      this.endOffset = endOffset;
      attributes = contentType.getAttributes();
    }
  }

  private final Project myProject;

  private boolean myOutputPaused;

  private Editor myEditor;

  private final Object LOCK = new Object();

  private int myContentSize;
  private StringBuffer myDeferredOutput = new StringBuffer();
  private StringBuffer myDeferredUserInput = new StringBuffer();

  private ArrayList<TokenInfo> myTokens = new ArrayList<TokenInfo>();
  private final Hyperlinks myHyperlinks = new Hyperlinks();

  private String myHelpId;

  private Alarm myFlushAlarm = new Alarm();

  private final Runnable myFlushDeferredRunnable = new Runnable() {
    public void run() {
      flushDeferredText();
    }
  };

  private CompositeFilter myMessageFilter = new CompositeFilter();

  public ConsoleViewImpl(final Project project) {
    super(new BorderLayout());
    myPsiDisposedCheck = new DisposedPsiManagerCheck(project);
    myProject = project;

    final ConsoleFilterProvider[] filterProviders = Extensions.getExtensions(ConsoleFilterProvider.FILTER_PROVIDERS);
    for (ConsoleFilterProvider filterProvider : filterProviders) {
      final Filter[] defaultFilters = filterProvider.getDefaultFilters(project);
      for (Filter filter : defaultFilters) {
        addMessageFilter(filter);
      }
    }
  }

  public void attachToProcess(final ProcessHandler processHandler){
    myState = myState.attachTo(this, processHandler);
  }

  public void clear() {
    assertIsDispatchThread();

    final Document document;
    synchronized(LOCK){
      myContentSize = 0;
      if (USE_CYCLIC_BUFFER) {
        myDeferredOutput = new StringBuffer(Math.min(myDeferredOutput.length(), CYCLIC_BUFFER_SIZE));
      }
      else {
        myDeferredOutput = new StringBuffer();
      }
      myDeferredUserInput = new StringBuffer();
      myHyperlinks.clear();
      myEditor.getMarkupModel().removeAllHighlighters();
      myTokens.clear();
      document = myEditor == null? null : myEditor.getDocument();
    }
    if (document != null){
      ApplicationManager.getApplication().runWriteAction(new Runnable() {
        public void run() {
          CommandProcessor.getInstance().executeCommand(myProject, new Runnable() {
            public void run() {
              document.deleteString(0, document.getTextLength());
            }
          }, null, document);
        }
      });
    }
  }

  public void scrollTo(final int offset) {
{'fraction_non_alphanumeric': 0.08990327282363854, 'fraction_numerical': 0.001060023850536637, 'mean_word_length': 5.19917864476386, 'pattern_counts': {'":': 0, '<': 9, '<?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': '5011363', 'n_tokens_mistral': 4438, 'n_tokens_neox': 4362, 'n_words': 815}