-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ability to Delete snapshots for entire method or entire class
- Loading branch information
1 parent
23af8ec
commit c09c9d2
Showing
3 changed files
with
47 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 35 additions & 7 deletions
42
src/main/kotlin/com/getyourguide/paparazzi/actions/DeleteFileAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,51 @@ | ||
package com.getyourguide.paparazzi.actions | ||
|
||
import com.getyourguide.paparazzi.service.service | ||
import com.getyourguide.paparazzi.service.toFileInfo | ||
import com.intellij.icons.AllIcons | ||
import com.intellij.ide.util.DeleteHandler | ||
import com.intellij.openapi.actionSystem.AnAction | ||
import com.intellij.openapi.actionSystem.AnActionEvent | ||
import com.intellij.openapi.application.WriteAction | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.vfs.VirtualFile | ||
|
||
private const val ACTION_NAME = "Delete snapshot" | ||
import com.intellij.psi.PsiClass | ||
import com.intellij.psi.PsiMethod | ||
import org.jetbrains.kotlin.idea.core.util.toPsiFile | ||
|
||
/** | ||
* Action to delete the snapshot file | ||
*/ | ||
class DeleteFileAction(val file: VirtualFile) : AnAction(ACTION_NAME, null, AllIcons.Actions.GC) { | ||
class DeleteFileAction( | ||
private val files: List<VirtualFile> = emptyList(), | ||
private val psiClass: PsiClass? = null, | ||
private val psiMethod: PsiMethod? = null | ||
) : AnAction(getActionName(files), null, AllIcons.Actions.GC) { | ||
|
||
override fun actionPerformed(e: AnActionEvent) { | ||
WriteAction.run<Throwable> { | ||
file.delete(this) | ||
e.project?.service?.loadFromSelectedEditor(true) | ||
val project = e.project ?: return | ||
if (psiClass != null) { | ||
val file = psiClass.containingFile?.virtualFile ?: return | ||
val fileInfo = file.toFileInfo(project, false) | ||
val methodName = psiMethod?.name | ||
val snapshots = if (methodName != null) { | ||
fileInfo.snapshotsForMethod(methodName) | ||
} else { | ||
fileInfo.allSnapshots() | ||
} | ||
val files = snapshots.map { it.file } | ||
deleteSnapshots(project, files) | ||
} else { | ||
deleteSnapshots(project, files) | ||
} | ||
} | ||
|
||
private fun deleteSnapshots(project: Project, files: List<VirtualFile>) { | ||
val psiFiles = files.mapNotNull { it.toPsiFile(project) } | ||
DeleteHandler.deletePsiElement(psiFiles.toTypedArray(), project, true) | ||
project.service.loadFromSelectedEditor(true) | ||
} | ||
} | ||
|
||
private fun getActionName(files: List<VirtualFile>): String { | ||
return if (files.size == 1) "Delete Snapshot" else "Delete All Snapshots" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters