-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAttributeFolderAction.kt
More file actions
49 lines (42 loc) · 1.89 KB
/
AttributeFolderAction.kt
File metadata and controls
49 lines (42 loc) · 1.89 KB
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
package dev.zbinski.htmlattributefolder
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.editor.FoldRegion
class AttributeFolderAction : AnAction() {
private val settings = AttributeFolderState.instance
private fun getFoldRegionsForRelevantAttributes(foldRegions: Array<FoldRegion>): List<FoldRegion> {
val relevantAttributes = settings.attributes
val result = mutableListOf<FoldRegion>()
// Identifying a fold based on its "groupName"/"debugName" is possible because all folds relevant
// to us have a self-defined `FoldingGroup` including the "groupname"/"debugName".
// (see ->AttributeFolder.buildFoldRegions:37)
for (region in foldRegions) {
val group = region.group
// The `group.toString()` returns the "groupName"/"debugName"
if (group != null && relevantAttributes.contains(group.toString())) {
result.add(region)
}
}
return result
}
private fun performFoldOperation(fold: FoldRegion) {
fold.isExpanded = !settings.collapse
}
override fun actionPerformed(e: AnActionEvent) {
val editor = e.getData(CommonDataKeys.EDITOR)
val project = e.getData(CommonDataKeys.PROJECT)
if (editor == null || project == null) {
return
}
settings.collapse = !settings.collapse
val foldingModel = editor.foldingModel
val allFoldRegions = foldingModel.allFoldRegions
ApplicationManager.getApplication().runWriteAction {
foldingModel.runBatchFoldingOperation {
getFoldRegionsForRelevantAttributes(allFoldRegions).forEach(::performFoldOperation)
}
}
}
}