Skip to content

Commit

Permalink
Multiple file selection & commit window's context menu (#113)
Browse files Browse the repository at this point in the history
* - Multiple file selection
- Plugin now included in local changes context menu in Commit window

* Filter out null

* Adjust temp file creation

* Rollback name change
  • Loading branch information
dgomolka authored Jan 30, 2024
1 parent d0d4b85 commit cd15bd6
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import com.intellij.openapi.progress.impl.BackgroundableProcessIndicator
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VfsUtil
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiFile
import com.intellij.psi.xml.XmlFile
import com.intellij.vcsUtil.VcsFileUtil
import java.io.*
import java.net.URL
import javax.swing.SwingUtilities
Expand All @@ -20,9 +19,9 @@ import kotlin.io.path.createTempFile
class AvocadoSizeItRightClickAction : AnAction() {

Check notice on line 19 in src/main/kotlin/com/github/drjacky/avocado/actions/AvocadoSizeItRightClickAction.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Package name does not match containing directory

Package directive does not match the file location

override fun actionPerformed(e: AnActionEvent) {
val projectObject: Project? = e.project
if (projectObject != null) {
val os = System.getProperty("os.name").toLowerCase()
val project: Project? = e.project
if (project != null) {
val os = System.getProperty("os.name").lowercase()
val executableName = when {
os.contains("mac") -> "avocado-macos"
os.contains("win") -> "avocado-win.exe"
Expand All @@ -35,23 +34,16 @@ class AvocadoSizeItRightClickAction : AnAction() {
val avocadoScriptPath = this::class.java.classLoader.getResource(executableName)

if (avocadoScriptPath != null) {
val psiFile = e.getData(CommonDataKeys.PSI_FILE)

if (psiFile != null) {
if (isXmlFileInDrawableFolder(psiFile)) {
val task: Task.Backgroundable =
object : Task.Backgroundable(e.project, "Avocado Size It", true) {
override fun run(indicator: ProgressIndicator) {
avocadoSizeIt(projectObject, avocadoScriptPath, executableName, psiFile.virtualFile)
}
}
ProgressManager.getInstance()
.runProcessWithProgressAsynchronously(task, BackgroundableProcessIndicator(task))
val files = e.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY)

if (!files.isNullOrEmpty()) {
if (isXmlFileInDrawableFolder(files)) {
processFilesWithAvocado(project, avocadoScriptPath, executableName, files)
} else {
println("Right-clicked on XML file, but not in the expected folder")
println("Right-clicked on file, but not in the expected folder or not xml")
}
} else {
println("Right-clicked, but PSI file is null")
println("Right-clicked, but files are null/empty")
}
} else {
println("avocadoScriptPath is blank!")
Expand All @@ -61,56 +53,69 @@ class AvocadoSizeItRightClickAction : AnAction() {
}
}

override fun update(e: AnActionEvent) {
super.update(e)
SwingUtilities.invokeLater {
val psiFile = e.getData(CommonDataKeys.PSI_FILE)
e.presentation.isEnabledAndVisible = isXmlFileInDrawableFolder(psiFile)
private fun processFilesWithAvocado(
project: Project,
avocadoScriptPath: URL,
executableName: String,
files: Array<VirtualFile>
) {
val task = object : Task.Backgroundable(project, "Avocado-Size It", true) {

Check warning on line 62 in src/main/kotlin/com/github/drjacky/avocado/actions/AvocadoSizeItRightClickAction.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Incorrect string capitalization

String 'Avocado-Size It' is not properly capitalized. It should have sentence capitalization
override fun run(indicator: ProgressIndicator) {
val executableFile = createTempExecutableFile(avocadoScriptPath, executableName)
if (executableFile.exists()) {
val processedFiles = files.mapNotNull { file ->
file.takeIf {
avocadoSizeIt(executableFile.absolutePath, file)
}
}
refreshFiles(processedFiles, project)
} else {
println("Avocado executable not found: ${executableFile.absolutePath}")
}
}
}
ProgressManager.getInstance()
.runProcessWithProgressAsynchronously(task, BackgroundableProcessIndicator(task))
}

override fun getActionUpdateThread(): ActionUpdateThread {
return ActionUpdateThread.EDT
}

private fun isXmlFileInDrawableFolder(psiFile: PsiFile?): Boolean {
val parentFolder = psiFile?.virtualFile?.parent
return psiFile is XmlFile &&
psiFile.virtualFile?.extension == "xml" &&
(parentFolder?.name == "drawable" || parentFolder?.name?.startsWith("drawable-") == true) &&
parentFolder.parent?.name == "res"
}
private fun createTempExecutableFile(avocadoScriptPath: URL, executableName: String): File {
val executableFile: File = if (avocadoScriptPath.protocol == "jar") {
val tempFile = createTempFile(executableName).toFile()

private fun avocadoSizeIt(project: Project, avocadoScriptPath: URL, executableName: String, file: VirtualFile) {
val fullPath = file.path
try {
val executableFile: File = if (avocadoScriptPath.protocol == "jar") {
val tempFile = createTempFile(executableName).toFile()
tempFile.deleteOnExit()

tempFile.deleteOnExit()

// Copy the executable from the JAR to the temporary file
avocadoScriptPath.openStream().use { input ->
FileOutputStream(tempFile).use { output ->
input.copyTo(output)
}
// Copy the executable from the JAR to the temporary file
avocadoScriptPath.openStream().use { input ->
FileOutputStream(tempFile).use { output ->
input.copyTo(output)
}
}

// Set execute permission on the temporary file
tempFile.setExecutable(true)
// Set execute permission on the temporary file
tempFile.setExecutable(true)

tempFile
} else {
File(avocadoScriptPath.toURI())
}
tempFile
} else {
File(avocadoScriptPath.toURI())
}

if (!executableFile.exists()) {
println("Executable not found: ${executableFile.absolutePath}")
return
}
return executableFile
}

private fun isXmlFileInDrawableFolder(virtualFiles: Array<VirtualFile>?): Boolean {
return virtualFiles?.all { virtualFile ->
val parentFolder = virtualFile.parent
virtualFile.extension == "xml" &&
(parentFolder?.name == "drawable" || parentFolder?.name?.startsWith("drawable-") == true) &&
parentFolder.parent?.name == "res"
} ?: false
}

private fun avocadoSizeIt(executableFilePath: String, file: VirtualFile): Boolean {
val fullPath = file.path
try {
val additionalParams = listOf("-i", fullPath)
val command = mutableListOf(executableFile.absolutePath)
val command = mutableListOf(executableFilePath)
command.addAll(additionalParams)

val processBuilder = ProcessBuilder(command)
Expand All @@ -132,22 +137,35 @@ class AvocadoSizeItRightClickAction : AnAction() {

reader.close()

refreshFile(file, project)
return true
} catch (e: IOException) {
e.printStackTrace()
} catch (e: InterruptedException) {
e.printStackTrace()
}

return false
}

private fun refreshFile(file: VirtualFile, project: Project) {
ProgressManager.getInstance().run(object : Task.Backgroundable(project, "", false) {
private fun refreshFiles(files: List<VirtualFile>, project: Project) {
val task = object : Task.Backgroundable(project, "", false) {
override fun run(indicator: ProgressIndicator) {
ApplicationManager.getApplication().invokeAndWait {
VfsUtil.markDirtyAndRefresh(true, false, true, file)
VfsUtil.markDirtyAndRefresh(true, false, true, *files.toTypedArray())
VcsFileUtil.markFilesDirty(project, files)
}
}
})
}
ProgressManager.getInstance().run(task)
}

override fun getActionUpdateThread() = ActionUpdateThread.EDT

override fun update(e: AnActionEvent) {
super.update(e)
SwingUtilities.invokeLater {
val files = e.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY)
e.presentation.isEnabledAndVisible = isXmlFileInDrawableFolder(files)
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<add-to-group group-id="EditorPopupMenu" anchor="first"/>
<add-to-group group-id="PopupMenuActions" anchor="first"/>
<add-to-group group-id="ProjectViewPopupMenu" anchor="first"/>
<add-to-group group-id="ChangesViewPopupMenu" anchor="last" />
</action>
</actions>

Expand Down

0 comments on commit cd15bd6

Please sign in to comment.