summaryrefslogtreecommitdiff
path: root/src/main/kotlin/me/kyren223/trident/ui
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/me/kyren223/trident/ui')
-rw-r--r--src/main/kotlin/me/kyren223/trident/ui/SettingsConfigurable.kt63
-rw-r--r--src/main/kotlin/me/kyren223/trident/ui/TridentListMenu.kt94
-rw-r--r--src/main/kotlin/me/kyren223/trident/ui/TridentMappingsMenu.kt84
3 files changed, 241 insertions, 0 deletions
diff --git a/src/main/kotlin/me/kyren223/trident/ui/SettingsConfigurable.kt b/src/main/kotlin/me/kyren223/trident/ui/SettingsConfigurable.kt
new file mode 100644
index 0000000..6283a29
--- /dev/null
+++ b/src/main/kotlin/me/kyren223/trident/ui/SettingsConfigurable.kt
@@ -0,0 +1,63 @@
+// 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.
+package me.kyren223.trident.ui
+
+import com.intellij.openapi.options.Configurable
+import com.intellij.openapi.util.NlsContexts.ConfigurableName
+import me.kyren223.trident.data.SettingsData
+import me.kyren223.trident.data.SettingsState
+import javax.swing.JComponent
+
+class SettingsConfigurable : Configurable {
+
+ private var settings: SettingsData? = null
+
+ override fun getDisplayName(): @ConfigurableName String {
+ return "Trident Settings"
+ }
+
+ override fun createComponent(): JComponent {
+ settings = SettingsData()
+ return settings!!.panel
+ }
+
+ override fun isModified(): Boolean {
+ val settings = SettingsState.instance
+ if (settings.width != this.settings!!.getWidth()) return true
+ if (settings.height != this.settings!!.getHeight()) return true
+ if (settings.fontSize != this.settings!!.getFontSize()) return true
+ if (settings.enterToSelect != this.settings!!.getEnterToSelect()) return true
+ if (settings.automaticMapping != this.settings!!.getAutomaticMapping()) return true
+ if (settings.recursiveMapping != this.settings!!.getRecursiveMapping()) return true
+ if (settings.rememberLine != this.settings!!.getRememberLine()) return true
+ return false
+ }
+
+ override fun apply() {
+ val settings = SettingsState.instance
+ settings.width = this.settings!!.getWidth()
+ settings.height = this.settings!!.getHeight()
+ settings.fontSize = this.settings!!.getFontSize()
+ settings.enterToSelect = this.settings!!.getEnterToSelect()
+ settings.automaticMapping = this.settings!!.getAutomaticMapping()
+ settings.recursiveMapping = this.settings!!.getRecursiveMapping()
+ settings.rememberLine = this.settings!!.getRememberLine()
+ }
+
+ override fun reset() {
+ val settings = SettingsState.instance
+ if (this.settings == null) {
+ this.settings = SettingsData()
+ }
+ this.settings!!.setWidth(settings.width)
+ this.settings!!.setHeight(settings.height)
+ this.settings!!.setFontSize(settings.fontSize)
+ this.settings!!.setEnterToSelect(settings.enterToSelect)
+ this.settings!!.setAutomaticMapping(settings.automaticMapping)
+ this.settings!!.setRecursiveMapping(settings.recursiveMapping)
+ this.settings!!.setRememberLine(settings.rememberLine)
+ }
+
+ override fun disposeUIResources() {
+ settings = null
+ }
+}
diff --git a/src/main/kotlin/me/kyren223/trident/ui/TridentListMenu.kt b/src/main/kotlin/me/kyren223/trident/ui/TridentListMenu.kt
new file mode 100644
index 0000000..4062837
--- /dev/null
+++ b/src/main/kotlin/me/kyren223/trident/ui/TridentListMenu.kt
@@ -0,0 +1,94 @@
+@file:Suppress("SameParameterValue")
+
+package me.kyren223.trident.ui
+
+import com.intellij.openapi.editor.EditorSettings
+import com.intellij.openapi.editor.LogicalPosition
+import com.intellij.openapi.fileEditor.FileEditorManager
+import com.intellij.openapi.project.Project
+import com.intellij.openapi.ui.DialogWrapper
+import com.intellij.ui.EditorTextField
+import com.maddyhome.idea.vim.newapi.vim
+import com.maddyhome.idea.vim.state.mode.Mode
+import me.kyren223.trident.data.SettingsState
+import me.kyren223.trident.utils.TridentList
+import me.kyren223.trident.utils.setEditorMode
+import java.awt.event.FocusEvent
+import java.awt.event.FocusListener
+import javax.swing.JComponent
+
+class TridentListMenu(private val content: String) : DialogWrapper(true) {
+
+ private lateinit var editor: EditorTextField
+
+ init {
+ this.title = "Trident List"
+ val settings = SettingsState.instance
+ setSize(settings.width, settings.height)
+ super.init()
+ }
+
+ override fun createCenterPanel(): JComponent {
+ editor = EditorTextField(this.content)
+ editor.setOneLineMode(false)
+ editor.addSettingsProvider {
+ it.setFontSize(SettingsState.instance.fontSize)
+ it.isInsertMode = false
+ val lineNumber = if (SettingsState.instance.rememberLine) line else 0
+ it.caretModel.moveToLogicalPosition(LogicalPosition(lineNumber, 0))
+ it.settings.isLineNumbersShown = true
+ it.settings.lineNumerationType = EditorSettings.LineNumerationType.ABSOLUTE
+ }
+
+ editor.addFocusListener(object : FocusListener {
+ override fun focusGained(e: FocusEvent?) {
+ val editor = editor.editor!!.vim
+ setEditorMode(editor, Mode.NORMAL())
+ }
+
+ override fun focusLost(e: FocusEvent?) { /* Do nothing */ }
+ })
+
+ return editor
+ }
+
+
+ override fun getPreferredFocusedComponent(): JComponent {
+ return this.editor
+ }
+
+ fun select() {
+ val project = editor.project
+ doOKAction()
+ val file = TridentList.select(project, line) ?: return
+ FileEditorManager.getInstance(project).openFile(file, true)
+ }
+
+ override fun doOKAction() {
+ save()
+ super.doOKAction()
+ }
+
+ override fun doCancelAction() {
+ save()
+ super.doCancelAction()
+ }
+
+ private fun save() {
+ TridentList.set(editor.project, editor.text.split("\n"))
+ line = editor.editor!!.caretModel.logicalPosition.line
+ }
+
+ override fun createSouthPanel(): JComponent? {
+ return null
+ }
+
+ companion object {
+ var line = 0
+ fun open(project: Project) {
+ val content = TridentList.get(project).joinToString("\n")
+ val menu = TridentListMenu(content)
+ menu.show()
+ }
+ }
+}
diff --git a/src/main/kotlin/me/kyren223/trident/ui/TridentMappingsMenu.kt b/src/main/kotlin/me/kyren223/trident/ui/TridentMappingsMenu.kt
new file mode 100644
index 0000000..a84b561
--- /dev/null
+++ b/src/main/kotlin/me/kyren223/trident/ui/TridentMappingsMenu.kt
@@ -0,0 +1,84 @@
+package me.kyren223.trident.ui
+
+import com.intellij.openapi.editor.EditorSettings
+import com.intellij.openapi.editor.LogicalPosition
+import com.intellij.openapi.project.Project
+import com.intellij.openapi.ui.DialogWrapper
+import com.intellij.ui.EditorTextField
+import com.maddyhome.idea.vim.newapi.vim
+import com.maddyhome.idea.vim.state.mode.Mode
+import me.kyren223.trident.data.SettingsState
+import me.kyren223.trident.utils.TridentMappings
+import me.kyren223.trident.utils.setEditorMode
+import java.awt.event.FocusEvent
+import java.awt.event.FocusListener
+import javax.swing.JComponent
+
+class TridentMappingsMenu(private val content: String) : DialogWrapper(true) {
+ private lateinit var editor: EditorTextField
+
+ init {
+ val settings = SettingsState.instance
+ this.title = "Trident Mappings"
+ setSize(settings.width, settings.height)
+ super.init()
+ }
+
+ override fun createCenterPanel(): JComponent {
+ editor = EditorTextField(this.content)
+ editor.setOneLineMode(false)
+ editor.addSettingsProvider {
+ it.setFontSize(SettingsState.instance.fontSize)
+ it.isInsertMode = false
+ it.caretModel.moveToLogicalPosition(LogicalPosition(0, 0))
+ it.settings.isLineNumbersShown = true
+ it.settings.lineNumerationType = EditorSettings.LineNumerationType.ABSOLUTE
+ }
+
+ editor.addFocusListener(object : FocusListener {
+ override fun focusGained(e: FocusEvent?) {
+ val editor = editor.editor!!.vim
+ setEditorMode(editor, Mode.NORMAL())
+ }
+
+ override fun focusLost(e: FocusEvent?) { /* Do nothing */ }
+ })
+
+ return editor
+ }
+
+ override fun getPreferredFocusedComponent(): JComponent {
+ return this.editor
+ }
+
+ override fun doOKAction() {
+ save()
+ super.doOKAction()
+ }
+
+ override fun doCancelAction() {
+ save()
+ super.doCancelAction()
+ }
+
+ private fun save() {
+ val map = editor.text.split("\n")
+ .map { entry -> entry.split("=") }
+ .filter { entry -> entry.size == 2 }
+ .associate { it[0] to it[1] }
+ TridentMappings.set(editor.project, map)
+ }
+
+ override fun createSouthPanel(): JComponent? {
+ return null
+ }
+
+ companion object {
+ fun open(project: Project) {
+ val content = TridentMappings.get(project).entries
+ .joinToString("\n") { "${it.key}=${it.value}" }
+ val menu = TridentMappingsMenu(content)
+ menu.show()
+ }
+ }
+}