Skip to content

Commit

Permalink
feat(diff-ui): implement apply patch from clipboard dialog and enhanc…
Browse files Browse the repository at this point in the history
…e SingleFileDiffLangSketch #153

Add a new dialog class `MyApplyPatchFromClipboardDialog` to handle the application of patches from the clipboard. This includes a custom checkbox for on-the-fly analysis option.

Update `SingleFileDiffLangSketch` by:
- Refactoring code for better readability.
- Implementing progress indicator for rollback action.
- Enhancing the rollback functionality to handle exceptions.
- Changing the mouse event behavior for a better UI experience.
  • Loading branch information
phodal committed Dec 9, 2024
1 parent a62b117 commit a1e89d2
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.phodal.shirecore.ui.viewer

import com.intellij.openapi.project.Project
import com.intellij.openapi.vcs.VcsApplicationSettings
import com.intellij.openapi.vcs.VcsBundle
import com.intellij.openapi.vcs.changes.patch.ApplyPatchDefaultExecutor
import com.intellij.openapi.vcs.changes.patch.ApplyPatchDifferentiatedDialog
import com.intellij.openapi.vcs.changes.patch.ApplyPatchMode
import com.intellij.testFramework.LightVirtualFile
import java.awt.event.ActionEvent
import java.awt.event.KeyEvent
import javax.swing.JCheckBox
import javax.swing.JComponent

class MyApplyPatchFromClipboardDialog(project: Project, clipboardText: String) :
ApplyPatchDifferentiatedDialog(
project, ApplyPatchDefaultExecutor(project), emptyList(), ApplyPatchMode.APPLY_PATCH_IN_MEMORY,
LightVirtualFile("clipboardPatchFile", clipboardText), null, null, //NON-NLS
null, null, null, false
) {
override fun createDoNotAskCheckbox(): JComponent? {
return createAnalyzeOnTheFlyOptionPanel()
}

companion object {
private fun createAnalyzeOnTheFlyOptionPanel(): JCheckBox {
val removeOptionCheckBox =
JCheckBox(VcsBundle.message("patch.apply.analyze.from.clipboard.on.the.fly.checkbox"))
removeOptionCheckBox.mnemonic = KeyEvent.VK_L
removeOptionCheckBox.isSelected = VcsApplicationSettings.getInstance().DETECT_PATCH_ON_THE_FLY
removeOptionCheckBox.addActionListener { e: ActionEvent? ->
VcsApplicationSettings.getInstance().DETECT_PATCH_ON_THE_FLY = removeOptionCheckBox.isSelected
}
return removeOptionCheckBox
}
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
package com.phodal.shirecore.ui.viewer

import com.intellij.diff.DiffContentFactory
import com.intellij.diff.DiffManager
import com.intellij.diff.contents.DocumentContent
import com.intellij.diff.requests.SimpleDiffRequest
import com.intellij.diff.util.DiffUserDataKeys
import com.intellij.execution.ui.ConsoleViewContentType
import com.intellij.icons.AllIcons
import com.intellij.lang.Language
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.runInEdt
import com.intellij.openapi.application.runWriteAction
import com.intellij.openapi.diff.impl.patch.PatchReader
import com.intellij.openapi.diff.impl.patch.TextFilePatch
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.DialogWrapper
import com.intellij.openapi.vcs.VcsApplicationSettings
import com.intellij.openapi.vcs.VcsBundle
import com.intellij.openapi.vcs.VcsException
import com.intellij.openapi.vcs.changes.patch.*
import com.intellij.openapi.vcs.changes.ui.RollbackProgressModifier
import com.intellij.openapi.vcs.impl.projectlevelman.AllVcses
import com.intellij.openapi.vcs.rollback.RollbackEnvironment
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.testFramework.LightVirtualFile
import com.intellij.ui.IdeBorderFactory
import com.intellij.ui.JBColor
import com.intellij.ui.components.JBLabel
import com.intellij.ui.components.panels.VerticalLayout
Expand All @@ -33,8 +28,6 @@ import com.phodal.shirecore.ShireCoreBundle
import com.phodal.shirecore.ShirelangNotifications
import com.phodal.shirecore.findFile
import java.awt.BorderLayout
import java.awt.event.ActionEvent
import java.awt.event.KeyEvent
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import javax.swing.*
Expand Down Expand Up @@ -84,16 +77,16 @@ class DiffLangSketch(private val myProject: Project, private var patchContent: S
}

val rejectButton = JButton(ShireCoreBundle.message("sketch.patch.action.reject")).apply {
icon = AllIcons.Actions.Rollback
toolTipText = ShireCoreBundle.message("sketch.patch.action.reject.tooltip")
this.icon = AllIcons.Actions.Rollback
this.toolTipText = ShireCoreBundle.message("sketch.patch.action.reject.tooltip")
addActionListener {
handleRejectAction()
}
}

val viewDiffButton = JButton(ShireCoreBundle.message("sketch.patch.action.viewDiff")).apply {
toolTipText = ShireCoreBundle.message("sketch.patch.action.viewDiff.tooltip")
icon = AllIcons.Actions.ListChanges
this.toolTipText = ShireCoreBundle.message("sketch.patch.action.viewDiff.tooltip")
this.icon = AllIcons.Actions.ListChanges
addActionListener {
handleViewDiffAction()
}
Expand All @@ -120,18 +113,37 @@ class DiffLangSketch(private val myProject: Project, private var patchContent: S
patchGroups.putValue(patchInProgress.base, patchInProgress)
}

if(filePatches.isEmpty() ) {
if (filePatches.isEmpty()) {
ShirelangNotifications.error(myProject, "PatchProcessor: no patches found")
return@invokeAndWait
}

val additionalInfo = myReader.getAdditionalInfo(ApplyPatchDefaultExecutor.pathsFromGroups(patchGroups))
val pathsFromGroups = ApplyPatchDefaultExecutor.pathsFromGroups(patchGroups)
val additionalInfo = myReader.getAdditionalInfo(pathsFromGroups)
shelfExecutor.apply(filePatches, patchGroups, null, "LlmGen.diff", additionalInfo)
}
}

private fun handleRejectAction() {
//
val instance = AllVcses.getInstance(myProject)
val rollbackEnvironment = instance.all.firstOrNull {
it is RollbackEnvironment
} as? RollbackEnvironment

val exceptions: MutableList<VcsException> = ArrayList()
ApplicationManager.getApplication().invokeAndWait {
val indicator =
BackgroundableProcessIndicator(
myProject, "", null, null, true
)
filePatches.forEach { patch ->
val originFile = myProject.findFile(patch.beforeFileName!!) ?: return@forEach
rollbackEnvironment?.rollbackModifiedWithoutCheckout(
listOf(originFile), exceptions, RollbackProgressModifier(1.0, indicator)
)
}
}
}

private fun handleViewDiffAction() {
Expand Down Expand Up @@ -166,7 +178,7 @@ class SingleFileDiffLangSketch(private val myProject: Project, private val filep

addMouseListener(object : MouseAdapter() {
override fun mouseEntered(e: MouseEvent) {
foreground = JBColor(0x0000FF, 0x0000FF)
background = JBColor(0x0000FF, 0x0000FF)
}

override fun mouseClicked(e: MouseEvent?) {
Expand Down Expand Up @@ -219,27 +231,3 @@ class SingleFileDiffLangSketch(private val myProject: Project, private val filep
return mainPanel
}
}

class MyApplyPatchFromClipboardDialog(project: Project, clipboardText: String) :
ApplyPatchDifferentiatedDialog(
project, ApplyPatchDefaultExecutor(project), emptyList(), ApplyPatchMode.APPLY_PATCH_IN_MEMORY,
LightVirtualFile("clipboardPatchFile", clipboardText), null, null, //NON-NLS
null, null, null, false
) {
override fun createDoNotAskCheckbox(): JComponent? {
return createAnalyzeOnTheFlyOptionPanel()
}

companion object {
private fun createAnalyzeOnTheFlyOptionPanel(): JCheckBox {
val removeOptionCheckBox =
JCheckBox(VcsBundle.message("patch.apply.analyze.from.clipboard.on.the.fly.checkbox"))
removeOptionCheckBox.mnemonic = KeyEvent.VK_L
removeOptionCheckBox.isSelected = VcsApplicationSettings.getInstance().DETECT_PATCH_ON_THE_FLY
removeOptionCheckBox.addActionListener { e: ActionEvent? ->
VcsApplicationSettings.getInstance().DETECT_PATCH_ON_THE_FLY = removeOptionCheckBox.isSelected
}
return removeOptionCheckBox
}
}
}

0 comments on commit a1e89d2

Please sign in to comment.