diff options
| author | Kyren223 <ulmliad223@gmail.com> | 2024-02-13 18:07:25 +0200 |
|---|---|---|
| committer | Kyren223 <ulmliad223@gmail.com> | 2024-02-13 18:07:25 +0200 |
| commit | 4f6266ddefa3a621d1208cc3ff538bf30319aecc (patch) | |
| tree | a685da403841d36c2e31e47069adb31a07a9162e | |
| parent | f5462d7b4b50fe05b98cee3274c26a9b9f52dd93 (diff) | |
Deleted un-needed file, implemented select action, includes prev/next, for prev/next implemented wrap around
3 files changed, 36 insertions, 32 deletions
diff --git a/src/main/kotlin/me/kyren223/harpoonforjb/HarpoonSelectAction.kt b/src/main/kotlin/me/kyren223/harpoonforjb/HarpoonSelectAction.kt index d63b6f7..3c196b8 100644 --- a/src/main/kotlin/me/kyren223/harpoonforjb/HarpoonSelectAction.kt +++ b/src/main/kotlin/me/kyren223/harpoonforjb/HarpoonSelectAction.kt @@ -2,22 +2,37 @@ package me.kyren223.harpoonforjb import com.intellij.openapi.actionSystem.AnAction import com.intellij.openapi.actionSystem.AnActionEvent +import com.intellij.openapi.actionSystem.PlatformDataKeys +import com.intellij.openapi.fileEditor.FileEditorManager class HarpoonSelectAction : AnAction() { override fun actionPerformed(e: AnActionEvent) { + val project = e.project ?: return val actionId = e.actionManager.getId(this) - if (actionId == NEXT) { - println("Next") - } else if (actionId.equals(PREV)) { - println("Previous") - } else { - val index = getIndex(actionId) - println("Goto $index") + var index = when (actionId) { + NEXT -> { + val current = HarpoonState.getIndexOfFile(project, e.getData(PlatformDataKeys.VIRTUAL_FILE)) + if (current == -1) return + current + 1 + } + PREV -> { + val current = HarpoonState.getIndexOfFile(project, e.getData(PlatformDataKeys.VIRTUAL_FILE)) + if (current == -1) return + current - 1 + } + else -> { + getIndex(actionId) + } } + + val count = HarpoonState.getFileCount(project) + index = ((index % count) + count) % count + val file = HarpoonState.selectFile(project, index) ?: return + FileEditorManager.getInstance(project).openFile(file, true) } private fun getIndex(actionId: String): Int { - return actionId.substring("HarpoonGoto".length).toInt() + return actionId.substring(SELECT.length).toInt() - 1 } companion object { diff --git a/src/main/kotlin/me/kyren223/harpoonforjb/HarpoonSetAction.kt b/src/main/kotlin/me/kyren223/harpoonforjb/HarpoonSetAction.kt deleted file mode 100644 index 80d31bc..0000000 --- a/src/main/kotlin/me/kyren223/harpoonforjb/HarpoonSetAction.kt +++ /dev/null @@ -1,19 +0,0 @@ -package me.kyren223.harpoonforjb - -import com.intellij.openapi.actionSystem.AnAction -import com.intellij.openapi.actionSystem.AnActionEvent - -class HarpoonSetAction : AnAction() { - override fun actionPerformed(e: AnActionEvent) { - val actionId = e.actionManager.getId(this) - if (!actionId.startsWith("HarpoonSet")) { - return - } - val index = getIndex(actionId) - println("Set $index") - } - - private fun getIndex(actionId: String): Int { - return actionId.substring("HarpoonSet".length).toInt() - } -}
\ No newline at end of file diff --git a/src/main/kotlin/me/kyren223/harpoonforjb/HarpoonState.kt b/src/main/kotlin/me/kyren223/harpoonforjb/HarpoonState.kt index 75db721..fcb20a5 100644 --- a/src/main/kotlin/me/kyren223/harpoonforjb/HarpoonState.kt +++ b/src/main/kotlin/me/kyren223/harpoonforjb/HarpoonState.kt @@ -6,12 +6,8 @@ import com.intellij.openapi.vfs.LocalFileSystem import com.intellij.openapi.vfs.VirtualFile object HarpoonState { - private var previousIndex: Int? = null - private var currentIndex: Int? = null - private var nextIndex: Int? = null - private var projectFiles: MutableMap<String, MutableList<VirtualFile>> = mutableMapOf() - private val HARPOON_LIST_KEY = "HarpoonList" + private const val HARPOON_LIST_KEY = "HarpoonList" fun setFile(project: Project, index: Int, file: VirtualFile) { if (index < 0) return @@ -66,4 +62,16 @@ object HarpoonState { .toList() properties.setList(HARPOON_LIST_KEY, stringList) } + + fun getIndexOfFile(project: Project, data: VirtualFile?): Int { + loadProject(project) + val files = projectFiles[project.name] + if (files == null || data == null) return -1 + return files.indexOfFirst { it.path == data.path } + } + + fun getFileCount(project: Project): Int { + loadProject(project) + return projectFiles[project.name]!!.size + } }
\ No newline at end of file |
