From 790a1c558c87f13122db73c4e37da90f1687c269 Mon Sep 17 00:00:00 2001 From: Julia Beliaeva Date: Thu, 27 Sep 2018 21:59:45 +0300 Subject: [PATCH] [vcs-log] extract compare action to use in log --- .../vcs-log/impl/src/META-INF/vcs-log.xml | 8 ++- .../actions/CompareRevisionsFromLogAction.kt | 47 +++++++++++++++ ...evisionsFromFileHistoryActionProvider.java | 2 +- ...visionsFromFolderHistoryActionProvider.kt} | 60 +++++++++---------- 4 files changed, 85 insertions(+), 32 deletions(-) create mode 100644 platform/vcs-log/impl/src/com/intellij/vcs/log/ui/actions/CompareRevisionsFromLogAction.kt rename platform/vcs-log/impl/src/com/intellij/vcs/log/ui/actions/history/{CompareRevisionsFromHistoryActionProvider.kt => CompareRevisionsFromFolderHistoryActionProvider.kt} (68%) diff --git a/platform/vcs-log/impl/src/META-INF/vcs-log.xml b/platform/vcs-log/impl/src/META-INF/vcs-log.xml index b81ef1ef13f7..405f98da17c1 100644 --- a/platform/vcs-log/impl/src/META-INF/vcs-log.xml +++ b/platform/vcs-log/impl/src/META-INF/vcs-log.xml @@ -42,7 +42,8 @@ - + @@ -97,6 +98,9 @@ + @@ -124,6 +128,7 @@ + @@ -166,6 +171,7 @@ + diff --git a/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/actions/CompareRevisionsFromLogAction.kt b/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/actions/CompareRevisionsFromLogAction.kt new file mode 100644 index 000000000000..8c4baa031326 --- /dev/null +++ b/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/actions/CompareRevisionsFromLogAction.kt @@ -0,0 +1,47 @@ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.intellij.vcs.log.ui.actions + +import com.intellij.openapi.actionSystem.AnActionEvent +import com.intellij.openapi.project.DumbAwareAction +import com.intellij.openapi.vcs.FilePath +import com.intellij.vcs.log.VcsLogDataKeys +import com.intellij.vcs.log.statistics.VcsLogUsageTriggerCollector +import com.intellij.vcs.log.ui.VcsLogInternalDataKeys +import com.intellij.vcsUtil.VcsUtil + +open class CompareRevisionsFromLogAction : DumbAwareAction() { + protected open fun getFilePath(e: AnActionEvent): FilePath? { + val log = e.getData(VcsLogDataKeys.VCS_LOG) ?: return null + val selectedCommits = log.selectedCommits + if (selectedCommits.isEmpty() || selectedCommits.size > 2) return null + if (selectedCommits.first().root != selectedCommits.last().root) return null + return VcsUtil.getFilePath(selectedCommits.first().root) + } + + override fun update(e: AnActionEvent) { + val log = e.getData(VcsLogDataKeys.VCS_LOG) + val handler = e.getData(VcsLogInternalDataKeys.LOG_DIFF_HANDLER) + val filePath = getFilePath(e) + if (log == null || filePath == null || handler == null) { + e.presentation.isEnabledAndVisible = false + return + } + + e.presentation.isVisible = true + e.presentation.isEnabled = log.selectedCommits.size == 2 + } + + override fun actionPerformed(e: AnActionEvent) { + val log = e.getRequiredData(VcsLogDataKeys.VCS_LOG) + val handler = e.getRequiredData(VcsLogInternalDataKeys.LOG_DIFF_HANDLER) + val filePath = getFilePath(e)!! + + VcsLogUsageTriggerCollector.triggerUsage(e) + + val commits = log.selectedCommits + if (commits.size == 2) { + handler.showDiff(commits[1].root, filePath, commits[1].hash, filePath, commits[0].hash) + } + } + +} \ No newline at end of file diff --git a/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/actions/history/CompareRevisionsFromFileHistoryActionProvider.java b/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/actions/history/CompareRevisionsFromFileHistoryActionProvider.java index c203b7f898e3..204d174c44ba 100644 --- a/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/actions/history/CompareRevisionsFromFileHistoryActionProvider.java +++ b/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/actions/history/CompareRevisionsFromFileHistoryActionProvider.java @@ -35,7 +35,7 @@ public class CompareRevisionsFromFileHistoryActionProvider implements AnActionEx return; } - CompareRevisionsFromHistoryActionProvider.updateActionText(e, log); + CompareRevisionsFromFolderHistoryActionProvider.updateActionText(e, log); e.getPresentation().setVisible(true); if (e.getInputEvent() instanceof KeyEvent) { diff --git a/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/actions/history/CompareRevisionsFromHistoryActionProvider.kt b/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/actions/history/CompareRevisionsFromFolderHistoryActionProvider.kt similarity index 68% rename from platform/vcs-log/impl/src/com/intellij/vcs/log/ui/actions/history/CompareRevisionsFromHistoryActionProvider.kt rename to platform/vcs-log/impl/src/com/intellij/vcs/log/ui/actions/history/CompareRevisionsFromFolderHistoryActionProvider.kt index 55c4ce0926f8..954e8398b728 100644 --- a/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/actions/history/CompareRevisionsFromHistoryActionProvider.kt +++ b/platform/vcs-log/impl/src/com/intellij/vcs/log/ui/actions/history/CompareRevisionsFromFolderHistoryActionProvider.kt @@ -20,80 +20,80 @@ import com.intellij.openapi.actionSystem.AnActionExtensionProvider import com.intellij.openapi.actionSystem.CommonDataKeys import com.intellij.openapi.ui.MessageType import com.intellij.openapi.util.Condition +import com.intellij.openapi.vcs.FilePath 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.VcsLogDataKeys import com.intellij.vcs.log.history.FileHistoryUtil -import com.intellij.vcs.log.ui.VcsLogInternalDataKeys import com.intellij.vcs.log.statistics.VcsLogUsageTriggerCollector +import com.intellij.vcs.log.ui.VcsLogInternalDataKeys +import com.intellij.vcs.log.ui.actions.CompareRevisionsFromLogAction import java.awt.event.KeyEvent -class CompareRevisionsFromHistoryActionProvider : AnActionExtensionProvider { +class CompareRevisionsFromFolderHistoryActionProvider : CompareRevisionsFromLogAction(), AnActionExtensionProvider { + + override fun getFilePath(e: AnActionEvent): FilePath? { + return e.getData(VcsDataKeys.FILE_PATH) + } 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 + if (e.getData(VcsLogInternalDataKeys.FILE_HISTORY_UI) == null) return false + val filePath = getFilePath(e) + return 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) { + val logData = e.getData(VcsLogInternalDataKeys.LOG_DATA) + val log = e.getData(VcsLogDataKeys.VCS_LOG) + val filePath = getFilePath(e) + if (log == null || project == null || logData == null || filePath == null) { e.presentation.isEnabledAndVisible = false return } - e.presentation.isVisible = true - val log = ui.vcsLog updateActionText(e, log) + e.presentation.isVisible = true + 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 + if (log.selectedCommits.size >= 2) { + super.update(e) } else { - e.presentation.isEnabled = commits.size == 1 + e.presentation.isEnabled = log.selectedCommits.isNotEmpty() } } 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) + val logData = e.getRequiredData(VcsLogInternalDataKeys.LOG_DATA) + val log = e.getRequiredData(VcsLogDataKeys.VCS_LOG) + val filePath = getFilePath(e)!! - 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, filePath, olderId.hash, filePath, newestId.hash) + val commits = log.selectedCommits + if (commits.size >= 2) { + super.actionPerformed(e) 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 commitIds = ContainerUtil.map(commits) { c -> logData.getCommitIndex(c.hash, c.root) } + logData.commitDetailsGetter.loadCommitsData(commitIds, { details -> val detail = notNull(ContainerUtil.getFirstItem(details)) val changes = FileHistoryUtil.collectRelevantChanges(detail, Condition { change -> FileHistoryUtil.affectsDirectory(change, filePath) }) 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) + null) } companion object {