c4e963250420bca340dcfd61cc21d40891e23037
[idea/community.git] / plugins / git4idea / src / git4idea / index / actions / GitAcceptConflictSideAction.kt
1 // Copyright 2000-2020 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.
2 package git4idea.index.actions
3
4 import com.intellij.openapi.actionSystem.AnActionEvent
5 import com.intellij.openapi.actionSystem.Presentation
6 import com.intellij.openapi.project.Project
7 import git4idea.conflicts.acceptConflictSide
8 import git4idea.conflicts.getConflictOperationLock
9 import git4idea.i18n.GitBundle
10 import git4idea.index.ui.*
11 import org.jetbrains.annotations.Nls
12 import java.util.function.Supplier
13
14 class GitAcceptTheirsAction: GitAcceptConflictSideAction(true)
15 class GitAcceptYoursAction: GitAcceptConflictSideAction(false)
16
17 abstract class GitAcceptConflictSideAction(private val takeTheirs: Boolean) :
18   GitFileStatusNodeAction(getActionText(takeTheirs), Presentation.NULL_STRING, null) {
19
20   override fun update(e: AnActionEvent) {
21     val project = e.project
22     val statusInfoStream = e.getData(GIT_FILE_STATUS_NODES_STREAM)
23     if (project == null || statusInfoStream == null || !statusInfoStream.anyMatch(this::matches)) {
24       e.presentation.isEnabledAndVisible = false
25       return
26     }
27
28     e.presentation.isVisible = true
29     e.presentation.isEnabled = e.getRequiredData(GIT_FILE_STATUS_NODES_STREAM).anyMatch { node ->
30       node.createConflict()?.let { conflict -> getConflictOperationLock(project, conflict).isLocked } == false
31     }
32   }
33
34   override fun matches(statusNode: GitFileStatusNode): Boolean = statusNode.kind == NodeKind.CONFLICTED
35
36   override fun perform(project: Project, nodes: List<GitFileStatusNode>) {
37     val conflicts = nodes.mapNotNull { it.createConflict() }
38     acceptConflictSide(project, createMergeHandler(project), conflicts, takeTheirs, project::isReversedRoot)
39   }
40 }
41
42 private fun getActionText(takeTheirs: Boolean): Supplier<@Nls String> {
43   return if (takeTheirs) GitBundle.messagePointer("conflicts.accept.theirs.action.text")
44   else GitBundle.messagePointer("conflicts.accept.yours.action.text")
45 }