<commit_before>/*
 * Copyright 2000-2017 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.vcs.log.ui.actions.history;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.AnActionExtensionProvider;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.vcs.FilePath;
import com.intellij.openapi.vcs.VcsDataKeys;
import com.intellij.openapi.vcs.changes.Change;
import com.intellij.openapi.vcs.history.VcsDiffUtil;
import com.intellij.openapi.vcs.ui.VcsBalloonProblemNotifier;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.vcs.log.CommitId;
import com.intellij.vcs.log.VcsFullCommitDetails;
import com.intellij.vcs.log.VcsLog;
import com.intellij.vcs.log.VcsLogDiffHandler;
import com.intellij.vcs.log.history.FileHistoryUi;
import com.intellij.vcs.log.statistics.VcsLogUsageTriggerCollector;
import com.intellij.vcs.log.ui.VcsLogInternalDataKeys;
import org.jetbrains.annotations.NotNull;

import java.awt.event.KeyEvent;
import java.util.List;

import static com.intellij.util.ObjectUtils.notNull;

public class CompareRevisionsFromHistoryActionProvider implements AnActionExtensionProvider {
  private static final String COMPARE_TEXT = "Compare";
  private static final String COMPARE_DESCRIPTION = "Compare selected versions";
  private static final String DIFF_TEXT = "Show Diff";
  private static final String DIFF_DESCRIPTION = "Show diff with previous version";

  @Override
  public boolean isActive(@NotNull AnActionEvent e) {
    FilePath filePath = e.getData(VcsDataKeys.FILE_PATH);
    return e.getData(VcsLogInternalDataKeys.FILE_HISTORY_UI) != null && filePath != null && filePath.isDirectory();
  }

  @Override
  public void update(@NotNull AnActionEvent e) {
    Project project = e.getProject();
    FileHistoryUi ui = e.getData(VcsLogInternalDataKeys.FILE_HISTORY_UI);
    FilePath filePath = e.getData(VcsDataKeys.FILE_PATH);
    if (project == null || ui == null || filePath == null) {
      e.getPresentation().setEnabledAndVisible(false);
      return;
    }
    e.getPresentation().setVisible(true);

    VcsLog log = ui.getVcsLog();
    updateActionText(e, log);

    if (e.getInputEvent() instanceof KeyEvent) {
      e.getPresentation().setEnabled(true);
      return;
    }

    List<CommitId> commits = log.getSelectedCommits();
    if (commits.size() == 2) {
      e.getPresentation().setEnabled(e.getData(VcsLogInternalDataKeys.LOG_DIFF_HANDLER) != null);
    }
    else {
      e.getPresentation().setEnabled(commits.size() == 1);
    }
  }

  @Override
  public void actionPerformed(@NotNull AnActionEvent e) {
    Project project = e.getRequiredData(CommonDataKeys.PROJECT);
    FileHistoryUi ui = e.getRequiredData(VcsLogInternalDataKeys.FILE_HISTORY_UI);
    FilePath filePath = e.getRequiredData(VcsDataKeys.FILE_PATH);

    VcsLogUsageTriggerCollector.triggerUsage(e);

    List<CommitId> commits = ui.getVcsLog().getSelectedCommits();
    if (commits.size() == 2) {
      VcsLogDiffHandler handler = e.getData(VcsLogInternalDataKeys.LOG_DIFF_HANDLER);
      // this check is needed here since we may come on key event without performing proper checks
      if (handler == null) return;

      CommitId newestId = commits.get(0);
      CommitId olderId = commits.get(1);
      notNull(handler).showDiff(olderId.getRoot(), ui.getPathInCommit(olderId.getHash()), olderId.getHash(),
                                ui.getPathInCommit(newestId.getHash()), newestId.getHash());
      return;
    }

    if (commits.size() != 1) return;

    List<Integer> commitIds = ContainerUtil.map(commits, c -> ui.getLogData().getCommitIndex(c.getHash(), c.getRoot()));
    ui.getLogData().getCommitDetailsGetter().loadCommitsData(commitIds, details -> {
      VcsFullCommitDetails detail = notNull(ContainerUtil.getFirstItem(details));
      List<Change> changes = ui.collectRelevantChanges(detail);
      VcsDiffUtil.showChangesDialog(project, "Changes in " + detail.getId().toShortString() + " for " + filePath.getName(),
                                    ContainerUtil.newArrayList(changes));
    }, t -> VcsBalloonProblemNotifier.showOverChangesView(project, "Could not load selected commits: " + t.getMessage(),
                                                          MessageType.ERROR), null);
  }

  public static void updateActionText(@NotNull AnActionEvent e, @NotNull VcsLog log) {
    if (log.getSelectedCommits().size() >= 2) {
      e.getPresentation().setText(COMPARE_TEXT);
      e.getPresentation().setDescription(COMPARE_DESCRIPTION);
    }
    else {
      e.getPresentation().setText(DIFF_TEXT);
      e.getPresentation().setDescription(DIFF_DESCRIPTION);
    }
  }
}<commit_msg>[file-history] convert CompareRevisionsFromHistoryActionProvider to kotlin<commit_after>/*
 * Copyright 2000-2017 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.vcs.log.ui.actions.history

import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.AnActionExtensionProvider
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.ui.MessageType
import com.intellij.openapi.vcs.VcsDataKeys
import com.intellij.openapi.vcs.history.VcsDiffUtil
import com.intellij.openapi.vcs.ui.VcsBalloonProblemNotifier
import com.intellij.util.ObjectUtils.notNull
import com.intellij.util.containers.ContainerUtil
import com.intellij.vcs.log.VcsLog
import com.intellij.vcs.log.ui.VcsLogInternalDataKeys
import com.intellij.vcs.log.statistics.VcsLogUsageTriggerCollector
import java.awt.event.KeyEvent

class CompareRevisionsFromHistoryActionProvider : AnActionExtensionProvider {

  override fun isActive(e: AnActionEvent): Boolean {
    val filePath = e.getData(VcsDataKeys.FILE_PATH)
    return e.getData(VcsLogInternalDataKeys.FILE_HISTORY_UI) != null && filePath != null && filePath.isDirectory
  }

  override fun update(e: AnActionEvent) {
    val project = e.project
    val ui = e.getData(VcsLogInternalDataKeys.FILE_HISTORY_UI)
    val filePath = e.getData(VcsDataKeys.FILE_PATH)
    if (project == null || ui == null || filePath == null) {
      e.presentation.isEnabledAndVisible = false
      return
    }
    e.presentation.isVisible = true

    val log = ui.vcsLog
    updateActionText(e, log)

    if (e.inputEvent is KeyEvent) {
      e.presentation.isEnabled = true
      return
    }

    val commits = log.selectedCommits
    if (commits.size == 2) {
      e.presentation.isEnabled = e.getData(VcsLogInternalDataKeys.LOG_DIFF_HANDLER) != null
    }
    else {
      e.presentation.isEnabled = commits.size == 1
    }
  }

  override fun actionPerformed(e: AnActionEvent) {
    val project = e.getRequiredData(CommonDataKeys.PROJECT)
    val ui = e.getRequiredData(VcsLogInternalDataKeys.FILE_HISTORY_UI)
    val filePath = e.getRequiredData(VcsDataKeys.FILE_PATH)

    VcsLogUsageTriggerCollector.triggerUsage(e)

    val commits = ui.vcsLog.selectedCommits
    if (commits.size == 2) {
      val handler = e.getData(VcsLogInternalDataKeys.LOG_DIFF_HANDLER) ?: return
      // this check is needed here since we may come on key event without performing proper checks

      val newestId = commits[0]
      val olderId = commits[1]
      notNull(handler).showDiff(olderId.root, ui.getPathInCommit(olderId.hash), olderId.hash,
                                ui.getPathInCommit(newestId.hash), newestId.hash)
      return
    }

    if (commits.size != 1) return

    val commitIds = ContainerUtil.map(commits) { c -> ui.logData.getCommitIndex(c.hash, c.root) }
    ui.logData.commitDetailsGetter.loadCommitsData(commitIds, { details ->
      val detail = notNull(ContainerUtil.getFirstItem(details))
      val changes = ui.collectRelevantChanges(detail)
      VcsDiffUtil.showChangesDialog(project, "Changes in " + detail.id.toShortString() + " for " + filePath.name,
                                    ContainerUtil.newArrayList(changes))
    }, { t -> VcsBalloonProblemNotifier.showOverChangesView(project, "Could not load selected commits: " + t.message, MessageType.ERROR) },
                                                   null)
  }

  companion object {
    private const val COMPARE_TEXT = "Compare"
    private const val COMPARE_DESCRIPTION = "Compare selected versions"
    private const val DIFF_TEXT = "Show Diff"
    private const val DIFF_DESCRIPTION = "Show diff with previous version"

    @JvmStatic
    fun updateActionText(e: AnActionEvent, log: VcsLog) {
      if (log.selectedCommits.size >= 2) {
        e.presentation.text = COMPARE_TEXT
        e.presentation.description = COMPARE_DESCRIPTION
      }
      else {
        e.presentation.text = DIFF_TEXT
        e.presentation.description = DIFF_DESCRIPTION
      }
    }
  }
}
{'fraction_non_alphanumeric': 0.09959718026183283, 'fraction_numerical': 0.003826787512588117, 'mean_word_length': 3.7607861936720997, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '836649', 'n_tokens_mistral': 3061, 'n_tokens_neox': 2997, 'n_words': 653}