<commit_before>package com.intellij.openapi.vcs.changes;

import com.intellij.diff.impl.DiffRequestProcessor;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonShortcuts;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.vcs.VcsConfiguration;
import com.intellij.openapi.vcs.changes.actions.diff.lst.LocalChangeListDiffTool;
import com.intellij.openapi.vcs.changes.ui.ChangesTree;
import com.intellij.openapi.wm.ToolWindowId;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.util.ui.update.MergingUpdateQueue;
import com.intellij.util.ui.update.Update;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.Objects;
import java.util.function.Supplier;

import static com.intellij.openapi.util.text.StringUtil.notNullize;

public abstract class EditorTabPreview implements ChangesViewPreview {
  private final Project myProject;
  @NotNull private final PreviewDiffVirtualFile myPreviewDiffVirtualFile;
  private final VcsConfiguration myVcsConfiguration;
  private final DiffRequestProcessor myChangeProcessor;

  private MergingUpdateQueue mySelectInEditor;

  public EditorTabPreview(@NotNull DiffRequestProcessor changeProcessor,
                   @NotNull JComponent contentPanel, @NotNull ChangesTree changesTree) {
    myProject = changeProcessor.getProject();

    mySelectInEditor = new MergingUpdateQueue("selectInEditorQueue", 100, true, MergingUpdateQueue.ANY_COMPONENT, changeProcessor.getProject(), null, true);
    mySelectInEditor.setRestartTimerOnAdd(true);

    myChangeProcessor = changeProcessor;
    MyDiffPreviewProvider previewProvider = new MyDiffPreviewProvider(changeProcessor, this::getCurrentName);
    myPreviewDiffVirtualFile = new PreviewDiffVirtualFile(previewProvider);
    myVcsConfiguration = VcsConfiguration.getInstance(myProject);

    //do not open file aggressively on start up, do it later
    DumbService.getInstance(myProject).smartInvokeLater(() -> {
      if (myProject.isDisposed()) return;

      changesTree.addSelectionListener(() -> {
        mySelectInEditor.queue(Update.create(this, () -> {
          if (skipPreviewUpdate()) return;

          setDiffPreviewVisible(true);
        }));
      });
    });

    new DumbAwareAction() {
      {
        copyShortcutFrom(ActionManager.getInstance().getAction("NextDiff"));
      }

      @Override
      public void actionPerformed(@NotNull AnActionEvent e) {
        openEditorPreview(true);
      }
    }.registerCustomShortcutSet(contentPanel, null);
  }

  @Nullable
  protected abstract String getCurrentName();

  protected abstract void doRefresh();

  protected boolean skipPreviewUpdate() {
    ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(myProject);

    //todo use Commit tw name when it will be in platform
    return toolWindowManager.isEditorComponentActive()
      || !Objects.equals(toolWindowManager.getActiveToolWindowId(), ToolWindowId.VCS);
  }<commit_msg>vcs: cleanup - use named constant, fix yellow code<commit_after>package com.intellij.openapi.vcs.changes;

import com.intellij.diff.impl.DiffRequestProcessor;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonShortcuts;
import com.intellij.openapi.actionSystem.IdeActions;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.vcs.VcsConfiguration;
import com.intellij.openapi.vcs.changes.actions.diff.lst.LocalChangeListDiffTool;
import com.intellij.openapi.vcs.changes.ui.ChangesTree;
import com.intellij.openapi.wm.ToolWindowId;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.util.ObjectUtils;
import com.intellij.util.ui.update.MergingUpdateQueue;
import com.intellij.util.ui.update.Update;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.Objects;
import java.util.function.Supplier;

import static com.intellij.openapi.util.text.StringUtil.notNullize;

public abstract class EditorTabPreview implements ChangesViewPreview {
  private final Project myProject;
  @NotNull private final PreviewDiffVirtualFile myPreviewDiffVirtualFile;
  private final VcsConfiguration myVcsConfiguration;
  private final DiffRequestProcessor myChangeProcessor;

  private final MergingUpdateQueue mySelectInEditor;

  public EditorTabPreview(@NotNull DiffRequestProcessor changeProcessor,
                          @NotNull JComponent contentPanel, @NotNull ChangesTree changesTree) {
    myProject = ObjectUtils.assertNotNull(changeProcessor.getProject());

    mySelectInEditor = new MergingUpdateQueue("selectInEditorQueue", 100, true, MergingUpdateQueue.ANY_COMPONENT, myProject, null, true);
    mySelectInEditor.setRestartTimerOnAdd(true);

    myChangeProcessor = changeProcessor;
    MyDiffPreviewProvider previewProvider = new MyDiffPreviewProvider(changeProcessor, this::getCurrentName);
    myPreviewDiffVirtualFile = new PreviewDiffVirtualFile(previewProvider);
    myVcsConfiguration = VcsConfiguration.getInstance(myProject);

    //do not open file aggressively on start up, do it later
    DumbService.getInstance(myProject).smartInvokeLater(() -> {
      if (myProject.isDisposed()) return;

      changesTree.addSelectionListener(() -> {
        mySelectInEditor.queue(Update.create(this, () -> {
          if (skipPreviewUpdate()) return;

          setDiffPreviewVisible(true);
        }));
      });
    });

    new DumbAwareAction() {
      {
        copyShortcutFrom(ActionManager.getInstance().getAction(IdeActions.ACTION_NEXT_DIFF));
      }

      @Override
      public void actionPerformed(@NotNull AnActionEvent e) {
        openEditorPreview(true);
      }
    }.registerCustomShortcutSet(contentPanel, null);
  }

  @Nullable
  protected abstract String getCurrentName();

  protected abstract void doRefresh();

  protected boolean skipPreviewUpdate() {
    ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(myProject);

    //todo use Commit tw name when it will be in platform
    return toolWindowManager.isEditorComponentActive()
      || !Objects.equals(toolWindowManager.getActiveToolWindowId(), ToolWindowId.VCS);
  }
{'fraction_non_alphanumeric': 0.08945086705202313, 'fraction_numerical': 0.0008670520231213873, 'mean_word_length': 5.124778761061947, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2146296', 'n_tokens_mistral': 1959, 'n_tokens_neox': 1922, 'n_words': 356}