package org.iwakura.larcjb.actions import com.intellij.openapi.actionSystem.ActionUpdateThread import com.intellij.openapi.actionSystem.AnAction import com.intellij.openapi.actionSystem.AnActionEvent import com.intellij.openapi.actionSystem.CommonDataKeys import com.intellij.openapi.project.DumbAware import com.intellij.openapi.vcs.changes.VcsDirtyScopeManager import com.intellij.openapi.vfs.VirtualFile import org.iwakura.larcjb.LarcExecutable import org.iwakura.larcjb.LarcVcs /** * Action to add files to Larc staging area. */ class LarcAddAction : AnAction(), DumbAware { override fun actionPerformed(e: AnActionEvent) { val project = e.project ?: return val files = e.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY) ?: return val executable = LarcExecutable(project) val filesByRoot = files.groupBy { findLarcRoot(it) } for ((root, rootFiles) in filesByRoot) { if (root == null) continue val relativePaths = rootFiles.map { it.path.removePrefix(root.path + "/") } val result = executable.add(root, *relativePaths.toTypedArray()) if (result.isSuccess) { /* Refresh VCS status */ VcsDirtyScopeManager.getInstance(project).dirDirtyRecursively(root) } } } override fun update(e: AnActionEvent) { val project = e.project val files = e.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY) e.presentation.isEnabledAndVisible = project != null && files != null && files.isNotEmpty() && files.any { findLarcRoot(it) != null } } override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.BGT private fun findLarcRoot(file: VirtualFile): VirtualFile? { var current: VirtualFile? = file.parent while (current != null) { if (current.findChild(LarcVcs.LARC_DIR) != null) { return current } current = current.parent } return null } }