diff: add option to enable align changes in side by side diff
[idea/community.git] / platform / diff-impl / src / com / intellij / diff / actions / impl / ToggleDiffAligningModeAction.kt
1 // Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2 package com.intellij.diff.actions.impl
3
4 import com.intellij.diff.tools.simple.SimpleDiffViewer
5 import com.intellij.diff.tools.util.DiffDataKeys.DIFF_VIEWER
6 import com.intellij.diff.tools.util.base.TextDiffViewerUtil
7 import com.intellij.diff.util.DiffUserDataKeys.ALIGNED_TWO_SIDED_DIFF
8 import com.intellij.diff.util.DiffUtil
9 import com.intellij.openapi.actionSystem.AnActionEvent
10 import com.intellij.openapi.project.DumbAwareToggleAction
11
12 class ToggleDiffAligningModeAction : DumbAwareToggleAction() {
13
14   override fun update(e: AnActionEvent) {
15     val viewer = e.getData(DIFF_VIEWER)
16     val available = e.project != null
17                     && viewer is SimpleDiffViewer
18                     && !DiffUtil.isUserDataFlagSet(ALIGNED_TWO_SIDED_DIFF, viewer.request)
19     if (!available) {
20       e.presentation.isEnabledAndVisible = available
21       return
22     }
23
24     super.update(e)
25   }
26
27   override fun isSelected(e: AnActionEvent): Boolean {
28     val viewer = e.getData(DIFF_VIEWER) as SimpleDiffViewer
29     return TextDiffViewerUtil.getTextSettings(viewer.context).isEnableAligningChangesMode
30   }
31
32   override fun setSelected(e: AnActionEvent, state: Boolean) {
33     val viewer = e.getData(DIFF_VIEWER) as SimpleDiffViewer
34
35     TextDiffViewerUtil.getTextSettings(viewer.context).isEnableAligningChangesMode = state
36     viewer.rediff()
37   }
38 }