diff --git a/platform/vcs-impl/src/com/intellij/vcs/commit/AbstractCommitMessagePolicy.kt b/platform/vcs-impl/src/com/intellij/vcs/commit/AbstractCommitMessagePolicy.kt new file mode 100644 index 000000000000..8f85d72d3fe4 --- /dev/null +++ b/platform/vcs-impl/src/com/intellij/vcs/commit/AbstractCommitMessagePolicy.kt @@ -0,0 +1,44 @@ +// Copyright 2000-2019 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.commit + +import com.intellij.openapi.project.Project +import com.intellij.openapi.vcs.AbstractVcs +import com.intellij.openapi.vcs.VcsConfiguration +import com.intellij.openapi.vcs.changes.Change +import com.intellij.openapi.vcs.changes.ChangeListManager +import com.intellij.openapi.vcs.changes.ChangesUtil +import com.intellij.openapi.vcs.changes.ChangesUtil.processChangesByVcs +import com.intellij.openapi.vcs.changes.LocalChangeList +import com.intellij.openapi.vcs.changes.ui.CommitMessageProvider + +internal abstract class AbstractCommitMessagePolicy(protected val project: Project) { + protected val vcsConfiguration: VcsConfiguration get() = VcsConfiguration.getInstance(project) + protected val changeListManager: ChangeListManager get() = ChangeListManager.getInstance(project) + + protected fun save(changeListName: String, commitMessage: String) { + changeListManager.editComment(changeListName, commitMessage) + } + + protected fun getCommitMessageFor(changeList: LocalChangeList): String? { + CommitMessageProvider.EXTENSION_POINT_NAME.extensionList.forEach { provider -> + val providerMessage = provider.getCommitMessage(changeList, project) + if (providerMessage != null) return providerMessage + } + + val changeListDescription = changeList.comment + if (!changeListDescription.isNullOrBlank()) return changeListDescription + + return if (!changeList.hasDefaultName()) changeList.name else null + } + + protected fun getCommitMessageFromVcs(changes: List): String? { + var result: String? = null + processChangesByVcs(project, changes) { vcs, vcsChanges -> + if (result == null) result = getCommitMessageFromVcs(vcs, vcsChanges) + } + return result + } + + private fun getCommitMessageFromVcs(vcs: AbstractVcs, changes: List): String? = + vcs.checkinEnvironment?.getDefaultMessageFor(ChangesUtil.getPaths(changes).toTypedArray()) +} \ No newline at end of file diff --git a/platform/vcs-impl/src/com/intellij/vcs/commit/ChangesViewCommitMessagePolicy.kt b/platform/vcs-impl/src/com/intellij/vcs/commit/ChangesViewCommitMessagePolicy.kt new file mode 100644 index 000000000000..d518cf011f21 --- /dev/null +++ b/platform/vcs-impl/src/com/intellij/vcs/commit/ChangesViewCommitMessagePolicy.kt @@ -0,0 +1,19 @@ +// Copyright 2000-2019 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.commit + +import com.intellij.openapi.project.Project +import com.intellij.openapi.vcs.changes.Change +import com.intellij.openapi.vcs.changes.LocalChangeList + +internal class ChangesViewCommitMessagePolicy(project: Project) : AbstractCommitMessagePolicy(project) { + fun getCommitMessage(changeList: LocalChangeList, changesSupplier: () -> List): String? = + if (vcsConfiguration.CLEAR_INITIAL_COMMIT_MESSAGE) null + else getCommitMessageFor(changeList)?.takeIf { it.isNotBlank() } + ?: getCommitMessageFromVcs(changesSupplier()) + ?: vcsConfiguration.LAST_COMMIT_MESSAGE + + fun save(changeList: LocalChangeList?, commitMessage: String, saveToHistory: Boolean) { + if (saveToHistory) vcsConfiguration.saveCommitMessage(commitMessage) + changeList?.let { save(it.name, commitMessage) } + } +} \ No newline at end of file diff --git a/platform/vcs-impl/src/com/intellij/vcs/commit/ChangesViewCommitWorkflowHandler.kt b/platform/vcs-impl/src/com/intellij/vcs/commit/ChangesViewCommitWorkflowHandler.kt index 855bc22c661d..f4d61cd35e64 100644 --- a/platform/vcs-impl/src/com/intellij/vcs/commit/ChangesViewCommitWorkflowHandler.kt +++ b/platform/vcs-impl/src/com/intellij/vcs/commit/ChangesViewCommitWorkflowHandler.kt @@ -6,7 +6,6 @@ import com.intellij.openapi.actionSystem.* import com.intellij.openapi.util.Disposer import com.intellij.openapi.vcs.CheckinProjectPanel import com.intellij.openapi.vcs.FilePath -import com.intellij.openapi.vcs.VcsConfiguration import com.intellij.openapi.vcs.VcsDataKeys.COMMIT_WORKFLOW_HANDLER import com.intellij.openapi.vcs.VcsException import com.intellij.openapi.vcs.changes.* @@ -16,6 +15,7 @@ import com.intellij.util.EventDispatcher import com.intellij.vcs.commit.AbstractCommitWorkflow.Companion.getCommitExecutors import gnu.trove.THashSet import java.util.* +import kotlin.properties.Delegates.observable private fun Collection.toPartialAwareSet() = THashSet(this, ChangeListChange.HASHING_STRATEGY) @@ -37,6 +37,10 @@ class ChangesViewCommitWorkflowHandler( private val inclusionModel = PartialCommitInclusionModel(project) private var areCommitOptionsCreated = false + private val commitMessagePolicy = ChangesViewCommitMessagePolicy(project) + private var currentChangeList by observable(null) { _, oldValue, newValue -> + if (oldValue != newValue) changeListChanged(oldValue, newValue) + } init { Disposer.register(this, Disposable { workflow.disposeCommitOptions() }) @@ -125,6 +129,8 @@ class ChangesViewCommitWorkflowHandler( val inclusion = inclusionModel.getInclusion() val isChangeListFullyIncluded = changeList.changes.run { isNotEmpty() && all { it in inclusion } } if (isChangeListFullyIncluded) ui.select(changeList) else ui.selectFirst(inclusion) + + currentChangeList = workflow.getAffectedChangeList(inclusion.filterIsInstance()) } private fun setInclusion(items: Collection, force: Boolean) { @@ -164,6 +170,13 @@ class ChangesViewCommitWorkflowHandler( fun showCommitOptions(isFromToolbar: Boolean, dataContext: DataContext) = ui.showCommitOptions(ensureCommitOptions(), getCommitActionName(), isFromToolbar, dataContext) + private fun changeListChanged(oldChangeList: LocalChangeList?, newChangeList: LocalChangeList?) { + oldChangeList?.let { commitMessagePolicy.save(it, getCommitMessage(), false) } + + val newCommitMessage = newChangeList?.let { commitMessagePolicy.getCommitMessage(it) { getIncludedChanges() } } + setCommitMessage(newCommitMessage) + } + override fun inclusionChanged() { val inclusion = inclusionModel.getInclusion() val activeChanges = changeListManager.defaultChangeList.changes @@ -192,7 +205,7 @@ class ChangesViewCommitWorkflowHandler( return super.saveCommitOptions() } - override fun saveCommitMessage(success: Boolean) = VcsConfiguration.getInstance(project).saveCommitMessage(getCommitMessage()) + override fun saveCommitMessage(success: Boolean) = commitMessagePolicy.save(currentChangeList, getCommitMessage(), success) interface ActivityListener : EventListener { fun activityStateChanged() diff --git a/platform/vcs-impl/src/com/intellij/vcs/commit/SingleChangeListCommitMessagePolicy.kt b/platform/vcs-impl/src/com/intellij/vcs/commit/SingleChangeListCommitMessagePolicy.kt index 1c0ca4476673..e4b336c7a332 100644 --- a/platform/vcs-impl/src/com/intellij/vcs/commit/SingleChangeListCommitMessagePolicy.kt +++ b/platform/vcs-impl/src/com/intellij/vcs/commit/SingleChangeListCommitMessagePolicy.kt @@ -2,18 +2,11 @@ package com.intellij.vcs.commit import com.intellij.openapi.project.Project -import com.intellij.openapi.vcs.AbstractVcs -import com.intellij.openapi.vcs.VcsConfiguration import com.intellij.openapi.vcs.changes.Change -import com.intellij.openapi.vcs.changes.ChangeListManager -import com.intellij.openapi.vcs.changes.ChangesUtil -import com.intellij.openapi.vcs.changes.ChangesUtil.processChangesByVcs import com.intellij.openapi.vcs.changes.LocalChangeList -import com.intellij.openapi.vcs.changes.ui.CommitMessageProvider -class SingleChangeListCommitMessagePolicy(private val project: Project, private val initialCommitMessage: String?) { - private val vcsConfiguration = VcsConfiguration.getInstance(project) - private val changeListManager = ChangeListManager.getInstance(project) +internal class SingleChangeListCommitMessagePolicy(project: Project, private val initialCommitMessage: String?) : + AbstractCommitMessagePolicy(project) { var defaultNameChangeListMessage: String? = null private var lastChangeListName: String? = null @@ -66,33 +59,9 @@ class SingleChangeListCommitMessagePolicy(private val project: Project, private saveMessages() } - private fun getCommitMessageFor(changeList: LocalChangeList): String? { - CommitMessageProvider.EXTENSION_POINT_NAME.extensionList.forEach { provider -> - val providerMessage = provider.getCommitMessage(changeList, project) - if (providerMessage != null) return providerMessage - } - - val changeListDescription = changeList.comment - if (!changeListDescription.isNullOrBlank()) return changeListDescription - - return if (!changeList.hasDefaultName()) changeList.name else null - } - - private fun getCommitMessageFromVcs(changes: List): String? { - var result: String? = null - processChangesByVcs(project, changes) { vcs, vcsChanges -> - if (result == null) result = getCommitMessageFromVcs(vcs, vcsChanges) - } - return result - } - - private fun getCommitMessageFromVcs(vcs: AbstractVcs, changes: List): String? = - vcs.checkinEnvironment?.getDefaultMessageFor(ChangesUtil.getPaths(changes).toTypedArray()) - private fun rememberMessage(message: String) = lastChangeListName?.let { messagesToSave[it] = message } private fun forgetMessage() = lastChangeListName?.let { messagesToSave -= it } - private fun saveMessages() = - messagesToSave.forEach { changeListName, commitMessage -> changeListManager.editComment(changeListName, commitMessage) } + private fun saveMessages() = messagesToSave.forEach { (changeListName, commitMessage) -> save(changeListName, commitMessage) } } \ No newline at end of file diff --git a/platform/vcs-impl/src/com/intellij/vcs/commit/SingleChangeListCommitWorkflow.kt b/platform/vcs-impl/src/com/intellij/vcs/commit/SingleChangeListCommitWorkflow.kt index e3ff25068c35..0a320c202202 100644 --- a/platform/vcs-impl/src/com/intellij/vcs/commit/SingleChangeListCommitWorkflow.kt +++ b/platform/vcs-impl/src/com/intellij/vcs/commit/SingleChangeListCommitWorkflow.kt @@ -48,7 +48,7 @@ open class SingleChangeListCommitWorkflow( val isPartialCommitEnabled: Boolean = vcses.any { it.arePartialChangelistsSupported() } && (isDefaultCommitEnabled || commitExecutors.any { it.supportsPartialCommit() }) - val commitMessagePolicy: SingleChangeListCommitMessagePolicy = SingleChangeListCommitMessagePolicy(project, initialCommitMessage) + internal val commitMessagePolicy: SingleChangeListCommitMessagePolicy = SingleChangeListCommitMessagePolicy(project, initialCommitMessage) internal lateinit var commitState: ChangeListCommitState