summaryrefslogtreecommitdiff
path: root/src/main/kotlin/me/kyren223/trident/SelectAction.kt
blob: 2e09bb983ec465efb8925dff81fe184aa8b5f3a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package me.kyren223.trident

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 SelectAction : AnAction() {
    override fun actionPerformed(e: AnActionEvent) {
        val project = e.project ?: return
        var index = when (val actionId = e.actionManager.getId(this)) {
            NEXT -> {
                val current = TridentState.getIndexOfFile(project, e.getData(PlatformDataKeys.VIRTUAL_FILE))
                if (current == -1) return
                current + 1
            }
            PREV -> {
                val current = TridentState.getIndexOfFile(project, e.getData(PlatformDataKeys.VIRTUAL_FILE))
                if (current == -1) return
                current - 1
            }
            else -> {
                getIndex(actionId)
            }
        }

        val count = TridentState.getFileCount(project)
        index = ((index % count) + count) % count
        val file = TridentState.selectFile(project, index) ?: return
        FileEditorManager.getInstance(project).openFile(file, true)
    }

    private fun getIndex(actionId: String): Int {
        return actionId.substring(SELECT.length).toInt() - 1
    }

    companion object {
        const val NEXT = "TridentSelectNext"
        const val PREV = "TridentSelectPrev"
        const val SELECT = "TridentSelect"
    }
}