vcs: add "Rollback current line" action to Alt+Enter: IDEA-217323
authorKirill Likhodedov <Kirill.Likhodedov@jetbrains.com>
Mon, 21 Dec 2020 13:22:50 +0000 (16:22 +0300)
committerintellij-monorepo-bot <intellij-monorepo-bot-no-reply@jetbrains.com>
Thu, 24 Dec 2020 14:01:08 +0000 (14:01 +0000)
GitOrigin-RevId: 6e5e81efd186eb03779ee14018ad0e25bb7f063a

platform/platform-resources-en/src/intentionDescriptions/RollbackCurrentLineIntention/after.txt.template [new file with mode: 0644]
platform/platform-resources-en/src/intentionDescriptions/RollbackCurrentLineIntention/before.txt.template [new file with mode: 0644]
platform/platform-resources-en/src/intentionDescriptions/RollbackCurrentLineIntention/description.html [new file with mode: 0644]
platform/vcs-api/vcs-api-core/resources/messages/VcsBundle.properties
platform/vcs-impl/resources/META-INF/VcsExtensions.xml
platform/vcs-impl/src/com/intellij/vcs/RollbackCurrentLineIntention.kt [new file with mode: 0644]

diff --git a/platform/platform-resources-en/src/intentionDescriptions/RollbackCurrentLineIntention/after.txt.template b/platform/platform-resources-en/src/intentionDescriptions/RollbackCurrentLineIntention/after.txt.template
new file mode 100644 (file)
index 0000000..6fe7de1
--- /dev/null
@@ -0,0 +1 @@
+foo(<spot>original</spot>)
\ No newline at end of file
diff --git a/platform/platform-resources-en/src/intentionDescriptions/RollbackCurrentLineIntention/before.txt.template b/platform/platform-resources-en/src/intentionDescriptions/RollbackCurrentLineIntention/before.txt.template
new file mode 100644 (file)
index 0000000..db4b028
--- /dev/null
@@ -0,0 +1 @@
+foo(<spot>modification</spot>)
\ No newline at end of file
diff --git a/platform/platform-resources-en/src/intentionDescriptions/RollbackCurrentLineIntention/description.html b/platform/platform-resources-en/src/intentionDescriptions/RollbackCurrentLineIntention/description.html
new file mode 100644 (file)
index 0000000..3af3c16
--- /dev/null
@@ -0,0 +1,7 @@
+<html>
+<body>
+Rolls back local uncommitted changes made in the current line,
+in the same way as the "Rollback Lines" action does from the editor gutter.
+If there is a selection in the editor, the action rolls back all changes inside the selected fragment.
+</body>
+</html>
\ No newline at end of file
index ce93004a66db16a3fa65a482d95c3b5d32d012c5..723c61b213358b61099a928fa9354f6b83cbe57a 100644 (file)
@@ -1144,3 +1144,4 @@ get.from.vcs.extension.list.accessible.name=Repository Location
 vcs.dnd.image.text.n.files={0} {0, choice, 1#file|2#files}
 checking.recent.changes=Checking recent changes...
 show.diff.in.editor.tab.got.it.tooltip=To show diff in an editor tab again, use this menu.
+intention.name.rollback.changes.in.current.line=Rollback changes in current line
index 3ccfca6d3bb6a479022ed8cffa1558bba38a1785..ff1a0cf88b7f65c4fbc55b28d3deec8cc3e69058 100644 (file)
     <diff.actions.ShowDiffAction.ExtensionProvider implementation="com.intellij.openapi.vcs.changes.actions.diff.ShowDiffFromLocalChangesActionProvider"/>
     <diff.actions.ShowDiffAction.ExtensionProvider implementation="com.intellij.openapi.vcs.changes.actions.diff.ShowDiffAction" order="last"/>
 
+    <intentionAction>
+      <className>com.intellij.vcs.RollbackCurrentLineIntention</className>
+      <category>Other</category>
+    </intentionAction>
+
     <openapi.vcs.history.actions.ShowDiffAfterWithLocalAction.ExtensionProvider implementation="com.intellij.openapi.vcs.changes.shelf.DiffShelvedChangesWithLocalActionProvider"/>
     <openapi.vcs.changes.actions.CreatePatchFromChangesAction.Dialog.ExtensionProvider implementation="com.intellij.openapi.vcs.history.actions.CreatePatchFromDirectoryAction"/>
 
diff --git a/platform/vcs-impl/src/com/intellij/vcs/RollbackCurrentLineIntention.kt b/platform/vcs-impl/src/com/intellij/vcs/RollbackCurrentLineIntention.kt
new file mode 100644 (file)
index 0000000..b4854e1
--- /dev/null
@@ -0,0 +1,55 @@
+// 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.
+package com.intellij.vcs
+
+import com.intellij.codeInsight.intention.IntentionAction
+import com.intellij.codeInsight.intention.LowPriorityAction
+import com.intellij.openapi.editor.Editor
+import com.intellij.openapi.project.DumbAware
+import com.intellij.openapi.project.Project
+import com.intellij.openapi.vcs.VcsBundle
+import com.intellij.openapi.vcs.ex.LineStatusTracker
+import com.intellij.openapi.vcs.ex.RollbackLineStatusAction
+import com.intellij.openapi.vcs.impl.LineStatusTrackerManager
+import com.intellij.psi.PsiFile
+import java.util.*
+
+class RollbackCurrentLineIntention : IntentionAction, LowPriorityAction, DumbAware {
+  override fun isAvailable(project: Project, editor: Editor?, file: PsiFile?): Boolean {
+    if (editor == null) return false
+    val tracker = getValidTrackerOrNull(project, editor)
+    if (tracker == null) return false
+
+    val caret = editor.caretModel.currentCaret
+    val lines = BitSet()
+    if (caret.hasSelection()) {
+      lines.set(caret.selectionStart, caret.selectionEnd)
+    }
+    else {
+      val currentLine = editor.document.getLineNumber(caret.offset)
+      lines.set(currentLine)
+    }
+    return tracker.getRangesForLines(lines)?.isNotEmpty() ?: false
+  }
+
+  override fun invoke(project: Project, editor: Editor?, file: PsiFile?) {
+    if (editor == null) return
+    val tracker = getValidTrackerOrNull(project, editor)
+    if (tracker == null) return
+
+    RollbackLineStatusAction.rollback(tracker, editor)
+  }
+
+  private fun getValidTrackerOrNull(project: Project, editor: Editor) : LineStatusTracker<*>? {
+    val tracker = LineStatusTrackerManager.getInstance(project).getLineStatusTracker(editor.document)
+    if (tracker == null || !tracker.isValid() || !tracker.isAvailableAt(editor)) {
+      return null
+    }
+    return tracker
+  }
+
+  override fun getText(): String = VcsBundle.message("intention.name.rollback.changes.in.current.line")
+
+  override fun getFamilyName(): String = text
+
+  override fun startInWriteAction(): Boolean = true
+}
\ No newline at end of file