| 1 |
package org.iwakura.larcjb.actions |
| 2 |
|
| 3 |
import com.intellij.openapi.actionSystem.ActionUpdateThread |
| 4 |
import com.intellij.openapi.actionSystem.AnAction |
| 5 |
import com.intellij.openapi.actionSystem.AnActionEvent |
| 6 |
import com.intellij.openapi.actionSystem.CommonDataKeys |
| 7 |
import com.intellij.openapi.project.DumbAware |
| 8 |
import com.intellij.openapi.vcs.changes.VcsDirtyScopeManager |
| 9 |
import com.intellij.openapi.vfs.VirtualFile |
| 10 |
import org.iwakura.larcjb.LarcExecutable |
| 11 |
import org.iwakura.larcjb.LarcVcs |
| 12 |
|
| 13 |
/** |
| 14 |
* Action to add files to Larc staging area. |
| 15 |
*/ |
| 16 |
class LarcAddAction : AnAction(), DumbAware { |
| 17 |
|
| 18 |
override fun actionPerformed(e: AnActionEvent) { |
| 19 |
val project = e.project ?: return |
| 20 |
val files = e.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY) ?: return |
| 21 |
|
| 22 |
val executable = LarcExecutable(project) |
| 23 |
|
| 24 |
val filesByRoot = files.groupBy { findLarcRoot(it) } |
| 25 |
|
| 26 |
for ((root, rootFiles) in filesByRoot) { |
| 27 |
if (root == null) continue |
| 28 |
|
| 29 |
val relativePaths = rootFiles.map { |
| 30 |
it.path.removePrefix(root.path + "/") |
| 31 |
} |
| 32 |
|
| 33 |
val result = executable.add(root, *relativePaths.toTypedArray()) |
| 34 |
|
| 35 |
if (result.isSuccess) { |
| 36 |
/* Refresh VCS status */ |
| 37 |
VcsDirtyScopeManager.getInstance(project).dirDirtyRecursively(root) |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
override fun update(e: AnActionEvent) { |
| 43 |
val project = e.project |
| 44 |
val files = e.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY) |
| 45 |
|
| 46 |
e.presentation.isEnabledAndVisible = project != null && |
| 47 |
files != null && |
| 48 |
files.isNotEmpty() && |
| 49 |
files.any { findLarcRoot(it) != null } |
| 50 |
} |
| 51 |
|
| 52 |
override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.BGT |
| 53 |
|
| 54 |
private fun findLarcRoot(file: VirtualFile): VirtualFile? { |
| 55 |
var current: VirtualFile? = file.parent |
| 56 |
while (current != null) { |
| 57 |
if (current.findChild(LarcVcs.LARC_DIR) != null) { |
| 58 |
return current |
| 59 |
} |
| 60 |
current = current.parent |
| 61 |
} |
| 62 |
return null |
| 63 |
} |
| 64 |
} |
| 65 |
|