diff options
Diffstat (limited to 'src/main/kotlin/me/kyren223/trident/data')
3 files changed, 338 insertions, 0 deletions
diff --git a/src/main/kotlin/me/kyren223/trident/data/SettingsData.kt b/src/main/kotlin/me/kyren223/trident/data/SettingsData.kt new file mode 100644 index 0000000..2c5c467 --- /dev/null +++ b/src/main/kotlin/me/kyren223/trident/data/SettingsData.kt @@ -0,0 +1,124 @@ +package me.kyren223.trident.data + +import com.intellij.ui.JBColor +import com.intellij.ui.JBIntSpinner +import com.intellij.ui.components.JBCheckBox +import com.intellij.ui.components.JBLabel +import com.intellij.util.ui.FormBuilder +import com.intellij.util.ui.JBFont +import javax.swing.JPanel + +class SettingsData { + + private val minValue = 1 + private val maxValue = 5000 + private val step = 10 + + val panel: JPanel + + private val width = JBIntSpinner(SettingsState.instance.width, minValue, maxValue, step) + private val height = JBIntSpinner(SettingsState.instance.height, minValue, maxValue, step) + private val fontSize = JBIntSpinner(SettingsState.instance.fontSize, minValue, maxValue, step) + + private val enterToSelect = JBCheckBox(null, SettingsState.instance.enterToSelect) + private val rememberLine = JBCheckBox(null, SettingsState.instance.rememberLine) + private val automaticMapping = JBCheckBox(null, SettingsState.instance.automaticMapping) + private val recursiveMapping = JBCheckBox(null, SettingsState.instance.recursiveMapping) + + init { + panel = FormBuilder.createFormBuilder() + .addComponent(JBLabel("Popup settings").withFont(JBFont.h4())) + .addLabeledComponent("Popup width", width) + .addLabeledComponent("Popup height", height) + .addLabeledComponent("Popup font size", fontSize) + .addSeparator() + + .addComponent(JBLabel("Trident settings").withFont(JBFont.h4())) + .addLabeledComponent("Enter to select item", enterToSelect) + .addComponent(desc("If enabled while the TridentList is open, " + + "pressing enter will select the item in the current line and open the file (if it exists).")) + .addVerticalGap(step) + + .addLabeledComponent("Automatic mapping (when possible)", automaticMapping) + .addComponent(desc("If enabled, when using the Append action, " + + "the full file path will be added to the Trident Mappings.")) + .addComponent(desc("Mapping Entry: $<file>=<path>/<file>.<extension>")) + .addVerticalGap(step) + + .addLabeledComponent("Recursive mapping", recursiveMapping) + .addComponent(desc("If enabled, when expanding a mapping for a path, " + + "each entry will be expanded recursively.")) + .addComponent(desc("This allows mapping entries to be nested (include other mappings).")) + .addVerticalGap(step) + + .addLabeledComponent("Remember last line", rememberLine) + .addComponent(desc("If enabled, the current line of the TridentList will be remembered " + + "and will be selected when the TridentList is opened.")) + .addComponent(desc("Note, this does not persist across restarts, it's run-time only.")) + + .addComponentFillVertically(JPanel(), 0) + .panel + } + + private fun desc(text: String): JBLabel { + val label = JBLabel(text).withFont(JBFont.small()) + label.foreground = JBColor.GRAY + return label + } + + fun getWidth(): Int { + return width.number + } + + fun getHeight(): Int { + return height.number + } + + fun getFontSize(): Int { + return fontSize.number + } + + fun getEnterToSelect(): Boolean { + return enterToSelect.isSelected + } + + fun setWidth(width: Int) { + this.width.number = width + } + + fun setHeight(height: Int) { + this.height.number = height + } + + fun setFontSize(fontSize: Int) { + this.fontSize.number = fontSize + } + + fun setEnterToSelect(enter: Boolean) { + this.enterToSelect.isSelected = enter + } + + fun getAutomaticMapping(): Boolean { + return automaticMapping.isSelected + } + + fun setAutomaticMapping(automaticMapping: Boolean) { + this.automaticMapping.isSelected = automaticMapping + } + + fun getRememberLine(): Boolean { + return rememberLine.isSelected + } + + fun setRememberLine(rememberLine: Boolean) { + this.rememberLine.isSelected = rememberLine + } + + fun getRecursiveMapping(): Boolean { + return recursiveMapping.isSelected + } + + fun setRecursiveMapping(recursiveMapping: Boolean) { + this.recursiveMapping.isSelected = recursiveMapping + } +} diff --git a/src/main/kotlin/me/kyren223/trident/data/SettingsState.kt b/src/main/kotlin/me/kyren223/trident/data/SettingsState.kt new file mode 100644 index 0000000..16ec372 --- /dev/null +++ b/src/main/kotlin/me/kyren223/trident/data/SettingsState.kt @@ -0,0 +1,33 @@ +package me.kyren223.trident.data + +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.components.PersistentStateComponent +import com.intellij.openapi.components.State +import com.intellij.openapi.components.Storage +import com.intellij.util.xmlb.XmlSerializerUtil + +// Copyright 2000-2022 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +@State(name = "me.kyren223.trident.data.SettingsState", storages = [Storage("SdkSettingsPlugin.xml")]) +class SettingsState : PersistentStateComponent<SettingsState?> { + + var width = 800 + var height = 400 + var fontSize = 20 + var enterToSelect = true + var automaticMapping = false + var recursiveMapping = false + var rememberLine = false + + override fun getState(): SettingsState { + return this + } + + override fun loadState(state: SettingsState) { + XmlSerializerUtil.copyBean(state, this) + } + + companion object { + val instance: SettingsState + get() = ApplicationManager.getApplication().getService(SettingsState::class.java) + } +} diff --git a/src/main/kotlin/me/kyren223/trident/data/TridentState.kt b/src/main/kotlin/me/kyren223/trident/data/TridentState.kt new file mode 100644 index 0000000..54529e9 --- /dev/null +++ b/src/main/kotlin/me/kyren223/trident/data/TridentState.kt @@ -0,0 +1,181 @@ +package me.kyren223.trident.data + +//object TridentState { +// private const val TRIDENT_LIST_KEY = "TridentList" +// private const val TRIDENT_MAPPINGS_KEY = "TridentMappings" +// +// private var list: MutableMap<String, MutableList<VirtualFile>> = mutableMapOf() +// private var mappings: MutableMap<String, MutableMap<String, String>> = mutableMapOf() +// +// private var quickMenu: QuickMenu? = null +// var quickMenuSelectedIndex: Int? = null +// var quickMenuContent: String = "" +// private var pathMappingsMenu: PathMappingsMenu? = null +// var pathMappingsContent: String = "" +// +//// fun appendFile(project: Project, file: VirtualFile) { +//// if (!file.isValid) return +//// loadProject(project) +//// val files = list[project.name] +//// files!!.add(file) +//// saveProject(project) +//// } +// +//// fun selectFile(project: Project, index: Int): VirtualFile? { +//// if (index < 0) return null +//// loadProject(project) +//// val files = list[project.name] +//// if (files!!.size <= index) return null +//// return files[index] +//// } +// +//// private fun loadProject(project: Project) { +//// if (project.name in list) return +//// val properties = PropertiesComponent.getInstance(project) +//// +//// val pathMappingsList = properties.getList(TRIDENT_MAPPINGS_KEY) +//// var map = mutableMapOf<String, String>() +//// if (pathMappingsList == null) { +//// map["..."] = project.basePath!! +//// } else { +//// for (entry in pathMappingsList) { +//// val split = entry.split("=") +//// if (split.size != 2) continue +//// map[split[0]] = split[1] +//// } +//// } +//// mappings[project.name] = map +//// +//// val list = properties.getList(TRIDENT_LIST_KEY) +//// if (list == null) { +//// this.list[project.name] = mutableListOf() +//// } else { +//// val fs = LocalFileSystem.getInstance() +//// val files = list.stream() +//// .map { fs.findFileByPath(it) } +//// .toList() +//// .filterNotNull() +//// .filter { it.isValid } +//// .toMutableList() +//// this.list[project.name] = files +//// } +//// } +// +//// private fun saveProject(project: Project) { +//// if (project.name !in list) return +//// val files = list[project.name] +//// val properties = PropertiesComponent.getInstance(project) +//// +//// val stringList = files!!.stream() +//// .filter { it.isValid } +//// .map { it.path } +//// .toList() +//// properties.setList(TRIDENT_LIST_KEY, stringList) +//// +//// val pathMappingsString = mappings[project.name]!!.entries +//// .joinToString("\n") { "${it.key}=${it.value}" } +//// .split("\n") +//// properties.setList(TRIDENT_MAPPINGS_KEY, pathMappingsString) +//// } +// +//// fun getIndexOfFile(project: Project, data: VirtualFile?): Int { +//// loadProject(project) +//// val files = list[project.name] +//// if (files == null || data == null) return -1 +//// return files.indexOfFirst { it.path == data.path } +//// } +// +//// fun getFileCount(project: Project): Int { +//// loadProject(project) +//// return list[project.name]!!.size +//// } +// +// fun toggleQuickMenu(project: Project) { +// if (quickMenu != null && quickMenu!!.isShowing) { +// quickMenu!!.doCancelAction() +// return +// } +// +//// loadProject(project) +// val content = TridentList.get(project).joinToString("\n") +// quickMenu = QuickMenu(content) +// val result = quickMenu!!.showAndGet() +// +// if (content != quickMenuContent) { +// retrieveProjectFiles(project, quickMenuContent) +//// saveProject(project) +// } +// +// if (!result) return +// val index = quickMenuSelectedIndex ?: return +// quickMenuSelectedIndex = null +// val file = TridentList.select(project, index) ?: return +// FileEditorManager.getInstance(project).openFile(file, true) +// } +// +// fun quickMenuSelect() { +// if (quickMenu == null || !quickMenu!!.isShowing) return +// quickMenu!!.select() +// } +// +//// private fun getProjectContent(project: Project): String { +//// loadProject(project) +//// val map = mappings[project.name] ?: throw IllegalStateException("Path mappings not found") +//// println("map: $map") +//// var content = list[project.name]!! +//// .joinToString("\n") { it.path } +//// .trim() +//// println("content: $content") +//// for ((key, value) in map) { +//// println("key: $key | value: $value") +//// content = content.replace(value, key) +//// } +//// println("content: $content") +//// return content +//// } +// +//// private fun retrieveProjectFiles(project: Project, content: String) { +//// loadProject(project) +//// val files = content +//// .split("\n") +//// .asSequence() +//// .map { it.trim() } +//// .map { line -> +//// mappings[project.name]!!.entries.fold(line) { acc, (key, value) -> +//// acc.replace(key, value) +//// } +//// } +////// .map { +////// pathMappings[project.name]!!.entries.forEach { (key, value) -> it.replace(key, value) } +////// it +////// } +//// .filter { it.isNotBlank() } +//// .map { LocalFileSystem.getInstance().findFileByPath(it) } +//// .filterNotNull() +//// .filter { it.isValid } +//// .toMutableList() +//// list[project.name] = files +//// } +// +// fun togglePathMappingsMenu(project: Project) { +// if (pathMappingsMenu != null && pathMappingsMenu!!.isShowing) { +// pathMappingsMenu!!.doCancelAction() +// return +// } +// +//// loadProject(project) +// val content = mappings[project.name]!!.entries +// .joinToString("\n") { "${it.key}=${it.value}" } +// pathMappingsMenu = PathMappingsMenu(content) +// pathMappingsMenu!!.showAndGet() +// +// if (content != pathMappingsContent) { +// pathMappingsContent.split("\n").forEach { +// val split = it.split("=") +// if (split.size != 2) return@forEach +// mappings[project.name]!![split[0]] = split[1] +// } +//// saveProject(project) +// } +// } +//}
\ No newline at end of file |
