blob: 36f3b0c33adfacb889d4227b47528468b4eb63be (
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
package me.kyren223.harpoonforjb
import com.intellij.ide.util.PropertiesComponent
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.openapi.vfs.VirtualFile
object HarpoonState {
private var projectFiles: MutableMap<String, MutableList<VirtualFile>> = mutableMapOf()
private const val HARPOON_LIST_KEY = "HarpoonList"
private var quickMenu: HarpoonQuickMenu? = null
var quickMenuSelectedIndex: Int? = null
var quickMenuContent: String? = ""
fun setFile(project: Project, index: Int, file: VirtualFile) {
if (index < 0) return
loadProject(project)
val files = projectFiles[project.name]
if (files!!.size <= index) return
files[index] = file
saveProject(project)
}
fun appendFile(project: Project, file: VirtualFile) {
if (!file.isValid) return
loadProject(project)
val files = projectFiles[project.name]
files!!.add(file)
saveProject(project)
}
fun selectFile(project: Project, index: Int): VirtualFile? {
if (index < 0) return null
loadProject(project)
val files = projectFiles[project.name]
if (files!!.size <= index) return null
return files[index]
}
private fun loadProject(project: Project) {
if (project.name in projectFiles) return
val properties = PropertiesComponent.getInstance(project)
val list = properties.getList(HARPOON_LIST_KEY)
if (list == null) {
projectFiles[project.name] = mutableListOf()
return
}
val fs = LocalFileSystem.getInstance()
val files = list.stream()
.map { fs.findFileByPath(it) }
.toList()
.filterNotNull()
.filter { it.isValid }
.toMutableList()
projectFiles[project.name] = files
}
private fun saveProject(project: Project) {
if (project.name !in projectFiles) return
val files = projectFiles[project.name]
val properties = PropertiesComponent.getInstance(project)
val stringList = files!!.stream()
.filter { it.isValid }
.map { it.path }
.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
}
fun toggleQuickMenu(project: Project) {
loadProject(project)
val content = projectFiles[project.name]!!
.joinToString("\n") { it.path }
.replace(project.basePath!!, "...")
.trim()
quickMenu = HarpoonQuickMenu(content)
val result = quickMenu!!.showAndGet()
if (content != quickMenuContent) {
// Update the content
// TODO: Update the content
}
if (!result || quickMenuSelectedIndex == null) return
println("Selected index: $quickMenuSelectedIndex")
val file = selectFile(project, quickMenuSelectedIndex!!) ?: return
FileEditorManager.getInstance(project).openFile(file, true)
}
fun quickMenuSelect() {
if (quickMenu == null || !quickMenu!!.isShowing) return
quickMenu!!.select()
}
}
|