From 5da72c7ec98edfab7fbebc335e1a9fba116988a5 Mon Sep 17 00:00:00 2001 From: Kyren223 Date: Fri, 23 Feb 2024 23:11:09 +0200 Subject: Added a new menu for path mappings to allow shortcuts like the built-in `...` Note: need to rethink how normal paths are saved, they should potentially be saved raw (not absolute paths) and then the virtual file list needs to change to string and then dynamically on the fly replace with mappings of key -> value to make an absolute path that then will be used --- .../me/kyren223/trident/AppSettingsConfigurable.kt | 50 --------- .../kotlin/me/kyren223/trident/AppSettingsState.kt | 30 ------ .../kotlin/me/kyren223/trident/AppendAction.kt | 15 +++ .../kotlin/me/kyren223/trident/PathMappingsMenu.kt | 63 ++++++++++++ .../me/kyren223/trident/PathMappingsMenuAction.kt | 11 ++ src/main/kotlin/me/kyren223/trident/QuickMenu.kt | 75 ++++++++++++++ .../me/kyren223/trident/QuickMenuSelectAction.kt | 11 ++ .../kotlin/me/kyren223/trident/SelectAction.kt | 43 ++++++++ .../me/kyren223/trident/SettingsConfigurable.kt | 50 +++++++++ .../kotlin/me/kyren223/trident/SettingsState.kt | 30 ++++++ .../me/kyren223/trident/ToggleQuickMenuAction.kt | 31 ++++++ .../me/kyren223/trident/TridentAppendAction.kt | 15 --- .../kotlin/me/kyren223/trident/TridentQuickMenu.kt | 78 -------------- .../trident/TridentQuickMenuSelectAction.kt | 11 -- .../me/kyren223/trident/TridentSelectAction.kt | 43 -------- .../kotlin/me/kyren223/trident/TridentState.kt | 113 ++++++++++++++++----- .../trident/TridentToggleQuickMenuAction.kt | 31 ------ src/main/kotlin/me/kyren223/trident/Utils.kt | 12 +++ 18 files changed, 428 insertions(+), 284 deletions(-) delete mode 100644 src/main/kotlin/me/kyren223/trident/AppSettingsConfigurable.kt delete mode 100644 src/main/kotlin/me/kyren223/trident/AppSettingsState.kt create mode 100644 src/main/kotlin/me/kyren223/trident/AppendAction.kt create mode 100644 src/main/kotlin/me/kyren223/trident/PathMappingsMenu.kt create mode 100644 src/main/kotlin/me/kyren223/trident/PathMappingsMenuAction.kt create mode 100644 src/main/kotlin/me/kyren223/trident/QuickMenu.kt create mode 100644 src/main/kotlin/me/kyren223/trident/QuickMenuSelectAction.kt create mode 100644 src/main/kotlin/me/kyren223/trident/SelectAction.kt create mode 100644 src/main/kotlin/me/kyren223/trident/SettingsConfigurable.kt create mode 100644 src/main/kotlin/me/kyren223/trident/SettingsState.kt create mode 100644 src/main/kotlin/me/kyren223/trident/ToggleQuickMenuAction.kt delete mode 100644 src/main/kotlin/me/kyren223/trident/TridentAppendAction.kt delete mode 100644 src/main/kotlin/me/kyren223/trident/TridentQuickMenu.kt delete mode 100644 src/main/kotlin/me/kyren223/trident/TridentQuickMenuSelectAction.kt delete mode 100644 src/main/kotlin/me/kyren223/trident/TridentSelectAction.kt delete mode 100644 src/main/kotlin/me/kyren223/trident/TridentToggleQuickMenuAction.kt create mode 100644 src/main/kotlin/me/kyren223/trident/Utils.kt (limited to 'src/main/kotlin/me') diff --git a/src/main/kotlin/me/kyren223/trident/AppSettingsConfigurable.kt b/src/main/kotlin/me/kyren223/trident/AppSettingsConfigurable.kt deleted file mode 100644 index e04b3b8..0000000 --- a/src/main/kotlin/me/kyren223/trident/AppSettingsConfigurable.kt +++ /dev/null @@ -1,50 +0,0 @@ -// 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 - -import com.intellij.openapi.options.Configurable -import com.intellij.openapi.util.NlsContexts.ConfigurableName -import javax.swing.JComponent - -class AppSettingsConfigurable : 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 = AppSettingsState.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 - return false - } - - override fun apply() { - val settings = AppSettingsState.instance - settings.width = this.settings!!.getWidth() - settings.height = this.settings!!.getHeight() - settings.fontSize = this.settings!!.getFontSize() - settings.enterToSelect = this.settings!!.getEnterToSelect() - } - - override fun reset() { - val settings = AppSettingsState.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) - } - - override fun disposeUIResources() { - settings = null - } -} diff --git a/src/main/kotlin/me/kyren223/trident/AppSettingsState.kt b/src/main/kotlin/me/kyren223/trident/AppSettingsState.kt deleted file mode 100644 index 6d61ea0..0000000 --- a/src/main/kotlin/me/kyren223/trident/AppSettingsState.kt +++ /dev/null @@ -1,30 +0,0 @@ -package me.kyren223.trident - -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.AppSettingsState", storages = [Storage("SdkSettingsPlugin.xml")]) -class AppSettingsState : PersistentStateComponent { - - var enterToSelect = true - var width = 800 - var height = 400 - var fontSize = 20 - - override fun getState(): AppSettingsState { - return this - } - - override fun loadState(state: AppSettingsState) { - XmlSerializerUtil.copyBean(state, this) - } - - companion object { - val instance: AppSettingsState - get() = ApplicationManager.getApplication().getService(AppSettingsState::class.java) - } -} diff --git a/src/main/kotlin/me/kyren223/trident/AppendAction.kt b/src/main/kotlin/me/kyren223/trident/AppendAction.kt new file mode 100644 index 0000000..f058f86 --- /dev/null +++ b/src/main/kotlin/me/kyren223/trident/AppendAction.kt @@ -0,0 +1,15 @@ +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.project.Project +import com.intellij.openapi.vfs.VirtualFile + +class AppendAction : AnAction() { + override fun actionPerformed(e: AnActionEvent) { + val project: Project = e.project ?: return + val file: VirtualFile = e.getData(PlatformDataKeys.VIRTUAL_FILE) ?: return + TridentState.appendFile(project, file) + } +} diff --git a/src/main/kotlin/me/kyren223/trident/PathMappingsMenu.kt b/src/main/kotlin/me/kyren223/trident/PathMappingsMenu.kt new file mode 100644 index 0000000..d098323 --- /dev/null +++ b/src/main/kotlin/me/kyren223/trident/PathMappingsMenu.kt @@ -0,0 +1,63 @@ +package me.kyren223.trident + +import com.intellij.openapi.editor.LogicalPosition +import com.intellij.openapi.ui.DialogWrapper +import com.intellij.ui.EditorTextField +import com.maddyhome.idea.vim.api.injector +import com.maddyhome.idea.vim.newapi.vim +import java.awt.event.FocusEvent +import java.awt.event.FocusListener +import javax.swing.JComponent + +class PathMappingsMenu(private val content: String) : DialogWrapper(true) { + private lateinit var editorTextField: EditorTextField + + init { + val settings = SettingsState.instance + this.title = "Trident Path Mappings Menu" + setSize(settings.width, settings.height) + super.init() + } + + override fun createCenterPanel(): JComponent { + val settings = SettingsState.instance + editorTextField = EditorTextField(this.content) + editorTextField.setOneLineMode(false) + editorTextField.addSettingsProvider { + it.setFontSize(settings.fontSize) + it.isInsertMode = true + it.caretModel.moveToLogicalPosition(LogicalPosition(0, 0)) + it.settings.isLineNumbersShown = true + } + + editorTextField.addFocusListener(object : FocusListener { + override fun focusGained(e: FocusEvent?) { + val editor = editorTextField.editor!!.vim + val context = injector.executionContextManager.onEditor(editor, null) + simulateKeyPress("", editor, context) + } + + override fun focusLost(e: FocusEvent?) { /* Do nothing */ } + }) + + return editorTextField + } + + override fun getPreferredFocusedComponent(): JComponent { + return this.editorTextField + } + + override fun doOKAction() { + TridentState.pathMappingsContent = editorTextField.text + super.doOKAction() + } + + override fun doCancelAction() { + TridentState.pathMappingsContent = editorTextField.text + super.doCancelAction() + } + + override fun createSouthPanel(): JComponent? { + return null + } +} diff --git a/src/main/kotlin/me/kyren223/trident/PathMappingsMenuAction.kt b/src/main/kotlin/me/kyren223/trident/PathMappingsMenuAction.kt new file mode 100644 index 0000000..6b46f6a --- /dev/null +++ b/src/main/kotlin/me/kyren223/trident/PathMappingsMenuAction.kt @@ -0,0 +1,11 @@ +package me.kyren223.trident + +import com.intellij.openapi.actionSystem.AnAction +import com.intellij.openapi.actionSystem.AnActionEvent + +class PathMappingsMenuAction : AnAction() { + override fun actionPerformed(e: AnActionEvent) { + val project = e.project ?: return + TridentState.togglePathMappingsMenu(project) + } +} diff --git a/src/main/kotlin/me/kyren223/trident/QuickMenu.kt b/src/main/kotlin/me/kyren223/trident/QuickMenu.kt new file mode 100644 index 0000000..3ef3871 --- /dev/null +++ b/src/main/kotlin/me/kyren223/trident/QuickMenu.kt @@ -0,0 +1,75 @@ +@file:Suppress("SameParameterValue") + +package me.kyren223.trident + +import com.intellij.openapi.editor.LogicalPosition +import com.intellij.openapi.ui.DialogWrapper +import com.intellij.ui.EditorTextField +import com.maddyhome.idea.vim.KeyHandler +import com.maddyhome.idea.vim.api.ExecutionContext +import com.maddyhome.idea.vim.api.VimEditor +import com.maddyhome.idea.vim.api.injector +import com.maddyhome.idea.vim.newapi.vim +import java.awt.event.FocusEvent +import java.awt.event.FocusListener +import javax.swing.JComponent + +class QuickMenu(private val content: String) : DialogWrapper(true) { + private lateinit var editorTextField: EditorTextField + + init { + val settings = SettingsState.instance + this.title = "Trident Quick Menu" + setSize(settings.width, settings.height) + super.init() + } + + override fun createCenterPanel(): JComponent { + val settings = SettingsState.instance + editorTextField = EditorTextField(this.content) + editorTextField.setOneLineMode(false) + editorTextField.addSettingsProvider { + it.setFontSize(settings.fontSize) + it.isInsertMode = true + it.caretModel.moveToLogicalPosition(LogicalPosition(0, 0)) + it.settings.isLineNumbersShown = true + } + + editorTextField.addFocusListener(object : FocusListener { + override fun focusGained(e: FocusEvent?) { + val editor = editorTextField.editor!!.vim + val context = injector.executionContextManager.onEditor(editor, null) + simulateKeyPress("", editor, context) + } + + override fun focusLost(e: FocusEvent?) { /* Do nothing */ } + }) + + return editorTextField + } + + + override fun getPreferredFocusedComponent(): JComponent { + return this.editorTextField + } + + fun select() { + if (this.editorTextField.editor == null) return + TridentState.quickMenuSelectedIndex = this.editorTextField.editor!!.caretModel.logicalPosition.line + doOKAction() + } + + override fun doOKAction() { + TridentState.quickMenuContent = editorTextField.text + super.doOKAction() + } + + override fun doCancelAction() { + TridentState.quickMenuContent = editorTextField.text + super.doCancelAction() + } + + override fun createSouthPanel(): JComponent? { + return null + } +} diff --git a/src/main/kotlin/me/kyren223/trident/QuickMenuSelectAction.kt b/src/main/kotlin/me/kyren223/trident/QuickMenuSelectAction.kt new file mode 100644 index 0000000..0ae320a --- /dev/null +++ b/src/main/kotlin/me/kyren223/trident/QuickMenuSelectAction.kt @@ -0,0 +1,11 @@ +package me.kyren223.trident + +import com.intellij.openapi.actionSystem.AnAction +import com.intellij.openapi.actionSystem.AnActionEvent + +class QuickMenuSelectAction : AnAction() { + override fun actionPerformed(e: AnActionEvent) { + TridentState.quickMenuSelect() + } + +} diff --git a/src/main/kotlin/me/kyren223/trident/SelectAction.kt b/src/main/kotlin/me/kyren223/trident/SelectAction.kt new file mode 100644 index 0000000..2e09bb9 --- /dev/null +++ b/src/main/kotlin/me/kyren223/trident/SelectAction.kt @@ -0,0 +1,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" + } +} + diff --git a/src/main/kotlin/me/kyren223/trident/SettingsConfigurable.kt b/src/main/kotlin/me/kyren223/trident/SettingsConfigurable.kt new file mode 100644 index 0000000..3caaf4b --- /dev/null +++ b/src/main/kotlin/me/kyren223/trident/SettingsConfigurable.kt @@ -0,0 +1,50 @@ +// 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 + +import com.intellij.openapi.options.Configurable +import com.intellij.openapi.util.NlsContexts.ConfigurableName +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 + 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() + } + + 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) + } + + override fun disposeUIResources() { + settings = null + } +} diff --git a/src/main/kotlin/me/kyren223/trident/SettingsState.kt b/src/main/kotlin/me/kyren223/trident/SettingsState.kt new file mode 100644 index 0000000..b0544f4 --- /dev/null +++ b/src/main/kotlin/me/kyren223/trident/SettingsState.kt @@ -0,0 +1,30 @@ +package me.kyren223.trident + +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.SettingsState", storages = [Storage("SdkSettingsPlugin.xml")]) +class SettingsState : PersistentStateComponent { + + var enterToSelect = true + var width = 800 + var height = 400 + var fontSize = 20 + + 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/ToggleQuickMenuAction.kt b/src/main/kotlin/me/kyren223/trident/ToggleQuickMenuAction.kt new file mode 100644 index 0000000..a8a78ab --- /dev/null +++ b/src/main/kotlin/me/kyren223/trident/ToggleQuickMenuAction.kt @@ -0,0 +1,31 @@ +package me.kyren223.trident + +import com.intellij.openapi.actionSystem.AnAction +import com.intellij.openapi.actionSystem.AnActionEvent +import com.maddyhome.idea.vim.api.injector +import com.maddyhome.idea.vim.command.MappingMode +import com.maddyhome.idea.vim.key.MappingOwner + +class ToggleQuickMenuAction : AnAction() { + override fun actionPerformed(e: AnActionEvent) { + if (!remapped) remap() + if (e.project != null) { + TridentState.toggleQuickMenu(e.project!!) + } + } + + private fun remap() { + if (!SettingsState.instance.enterToSelect) return + val keys = injector.parser.parseKeys(":action TridentQuickMenuSelect") + val keyGroup = injector.keyGroup + keyGroup.putKeyMapping(MappingMode.NVO, + injector.parser.parseKeys(""), + MappingOwner.Plugin.get("Trident"), keys, false + ) + remapped = true + } + + companion object { + var remapped = false + } +} diff --git a/src/main/kotlin/me/kyren223/trident/TridentAppendAction.kt b/src/main/kotlin/me/kyren223/trident/TridentAppendAction.kt deleted file mode 100644 index 621b9cd..0000000 --- a/src/main/kotlin/me/kyren223/trident/TridentAppendAction.kt +++ /dev/null @@ -1,15 +0,0 @@ -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.project.Project -import com.intellij.openapi.vfs.VirtualFile - -class TridentAppendAction : AnAction() { - override fun actionPerformed(e: AnActionEvent) { - val project: Project = e.project ?: return - val file: VirtualFile = e.getData(PlatformDataKeys.VIRTUAL_FILE) ?: return - TridentState.appendFile(project, file) - } -} diff --git a/src/main/kotlin/me/kyren223/trident/TridentQuickMenu.kt b/src/main/kotlin/me/kyren223/trident/TridentQuickMenu.kt deleted file mode 100644 index e7903e0..0000000 --- a/src/main/kotlin/me/kyren223/trident/TridentQuickMenu.kt +++ /dev/null @@ -1,78 +0,0 @@ -@file:Suppress("SameParameterValue") - -package me.kyren223.trident - -import com.intellij.openapi.editor.LogicalPosition -import com.intellij.openapi.ui.DialogWrapper -import com.intellij.ui.EditorTextField -import com.maddyhome.idea.vim.KeyHandler -import com.maddyhome.idea.vim.api.ExecutionContext -import com.maddyhome.idea.vim.api.VimEditor -import com.maddyhome.idea.vim.api.injector -import com.maddyhome.idea.vim.newapi.vim -import java.awt.event.FocusEvent -import java.awt.event.FocusListener -import javax.swing.JComponent - -class TridentQuickMenu(private val content: String) : DialogWrapper(true) { - private lateinit var editorTextField: EditorTextField - - init { - val settings = AppSettingsState.instance - this.title = "Trident Quick Menu" - setSize(settings.width, settings.height) - super.init() - } - - override fun createCenterPanel(): JComponent { - val settings = AppSettingsState.instance - editorTextField = EditorTextField(this.content) - editorTextField.setOneLineMode(false) - editorTextField.addSettingsProvider { - it.setFontSize(settings.fontSize) - it.isInsertMode = true - it.caretModel.moveToLogicalPosition(LogicalPosition(0, 0)) - it.settings.isLineNumbersShown = true - } - - editorTextField.addFocusListener(object : FocusListener { - override fun focusGained(e: FocusEvent?) { - val editor = editorTextField.editor!!.vim - val context = injector.executionContextManager.onEditor(editor, null) - simulateKeyPress("", editor, context) - } - - override fun focusLost(e: FocusEvent?) { /* Do nothing */ } - }) - - return editorTextField - } - - private fun simulateKeyPress(key: String, editor: VimEditor, context: ExecutionContext.Editor) { - KeyHandler.getInstance().handleKey(editor, injector.parser.parseKeys(key)[0], context) - } - - override fun getPreferredFocusedComponent(): JComponent { - return this.editorTextField - } - - fun select() { - if (this.editorTextField.editor == null) return - TridentState.quickMenuSelectedIndex = this.editorTextField.editor!!.caretModel.logicalPosition.line - doOKAction() - } - - override fun doOKAction() { - TridentState.quickMenuContent = editorTextField.text - super.doOKAction() - } - - override fun doCancelAction() { - TridentState.quickMenuContent = editorTextField.text - super.doCancelAction() - } - - override fun createSouthPanel(): JComponent? { - return null - } -} diff --git a/src/main/kotlin/me/kyren223/trident/TridentQuickMenuSelectAction.kt b/src/main/kotlin/me/kyren223/trident/TridentQuickMenuSelectAction.kt deleted file mode 100644 index f9db35a..0000000 --- a/src/main/kotlin/me/kyren223/trident/TridentQuickMenuSelectAction.kt +++ /dev/null @@ -1,11 +0,0 @@ -package me.kyren223.trident - -import com.intellij.openapi.actionSystem.AnAction -import com.intellij.openapi.actionSystem.AnActionEvent - -class TridentQuickMenuSelectAction : AnAction() { - override fun actionPerformed(e: AnActionEvent) { - TridentState.quickMenuSelect() - } - -} diff --git a/src/main/kotlin/me/kyren223/trident/TridentSelectAction.kt b/src/main/kotlin/me/kyren223/trident/TridentSelectAction.kt deleted file mode 100644 index 3122650..0000000 --- a/src/main/kotlin/me/kyren223/trident/TridentSelectAction.kt +++ /dev/null @@ -1,43 +0,0 @@ -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 TridentSelectAction : 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" - } -} - diff --git a/src/main/kotlin/me/kyren223/trident/TridentState.kt b/src/main/kotlin/me/kyren223/trident/TridentState.kt index 57a47b8..1163789 100644 --- a/src/main/kotlin/me/kyren223/trident/TridentState.kt +++ b/src/main/kotlin/me/kyren223/trident/TridentState.kt @@ -8,10 +8,14 @@ import com.intellij.openapi.vfs.VirtualFile object TridentState { private var projectFiles: MutableMap> = mutableMapOf() + private var pathMappings: MutableMap> = mutableMapOf() private const val TRIDENT_LIST_KEY = "TridentList" - private var quickMenu: TridentQuickMenu? = null + private const val PATH_MAPPINGS_LIST_KEY = "PathMappings" + private var quickMenu: QuickMenu? = null var quickMenuSelectedIndex: Int? = null - var quickMenuContent: String? = "" + var quickMenuContent: String = "" + private var pathMappingsMenu: PathMappingsMenu? = null + var pathMappingsContent: String = "" fun appendFile(project: Project, file: VirtualFile) { if (!file.isValid) return @@ -33,30 +37,49 @@ object TridentState { if (project.name in projectFiles) return val properties = PropertiesComponent.getInstance(project) + val pathMappingsList = properties.getList(PATH_MAPPINGS_LIST_KEY) + var map = mutableMapOf() + 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] + } + } + this.pathMappings[project.name] = map + val list = properties.getList(TRIDENT_LIST_KEY) if (list == null) { projectFiles[project.name] = mutableListOf() - return - } - val fs = LocalFileSystem.getInstance() - val files = list.stream() + } else { + val fs = LocalFileSystem.getInstance() + val files = list.stream() .map { fs.findFileByPath(it) } .toList() .filterNotNull() .filter { it.isValid } .toMutableList() - projectFiles[project.name] = files + 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() + .filter { it.isValid } + .map { it.path } + .toList() properties.setList(TRIDENT_LIST_KEY, stringList) + + val pathMappingsString = pathMappings[project.name]!!.entries + .joinToString("\n") { "${it.key}=${it.value}" } + .split("\n") + properties.setList(PATH_MAPPINGS_LIST_KEY, pathMappingsString) } fun getIndexOfFile(project: Project, data: VirtualFile?): Int { @@ -79,11 +102,11 @@ object TridentState { loadProject(project) val content = getProjectContent(project) - quickMenu = TridentQuickMenu(content) + quickMenu = QuickMenu(content) val result = quickMenu!!.showAndGet() - if (quickMenuContent != null && content != quickMenuContent) { - retrieveProjectFiles(project, quickMenuContent!!) + if (content != quickMenuContent) { + retrieveProjectFiles(project, quickMenuContent) saveProject(project) } @@ -100,25 +123,63 @@ object TridentState { } private fun getProjectContent(project: Project): String { - val content = projectFiles[project.name]!! - .joinToString("\n") { it.path } - .replace(project.basePath!!, "...") - .trim() + loadProject(project) + val map = pathMappings[project.name] ?: throw IllegalStateException("Path mappings not found") + println("map: $map") + var content = projectFiles[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 { it.replace("...", project.basePath!!) } - .filter { it.isNotBlank() } - .map { LocalFileSystem.getInstance().findFileByPath(it) } - .filterNotNull() - .filter { it.isValid } - .toMutableList() + .split("\n") + .asSequence() + .map { it.trim() } + .map { line -> + pathMappings[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() projectFiles[project.name] = files } + + fun togglePathMappingsMenu(project: Project) { + if (pathMappingsMenu != null && pathMappingsMenu!!.isShowing) { + pathMappingsMenu!!.doCancelAction() + return + } + + loadProject(project) + val content = pathMappings[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 + this.pathMappings[project.name]!![split[0]] = split[1] + } + saveProject(project) + } + } } \ No newline at end of file diff --git a/src/main/kotlin/me/kyren223/trident/TridentToggleQuickMenuAction.kt b/src/main/kotlin/me/kyren223/trident/TridentToggleQuickMenuAction.kt deleted file mode 100644 index a3f8cc7..0000000 --- a/src/main/kotlin/me/kyren223/trident/TridentToggleQuickMenuAction.kt +++ /dev/null @@ -1,31 +0,0 @@ -package me.kyren223.trident - -import com.intellij.openapi.actionSystem.AnAction -import com.intellij.openapi.actionSystem.AnActionEvent -import com.maddyhome.idea.vim.api.injector -import com.maddyhome.idea.vim.command.MappingMode -import com.maddyhome.idea.vim.key.MappingOwner - -class TridentToggleQuickMenuAction : AnAction() { - override fun actionPerformed(e: AnActionEvent) { - if (!remapped) remap() - if (e.project != null) { - TridentState.toggleQuickMenu(e.project!!) - } - } - - private fun remap() { - if (!AppSettingsState.instance.enterToSelect) return - val keys = injector.parser.parseKeys(":action TridentQuickMenuSelect") - val keyGroup = injector.keyGroup - keyGroup.putKeyMapping(MappingMode.NVO, - injector.parser.parseKeys(""), - MappingOwner.Plugin.get("Trident"), keys, false - ) - remapped = true - } - - companion object { - var remapped = false - } -} diff --git a/src/main/kotlin/me/kyren223/trident/Utils.kt b/src/main/kotlin/me/kyren223/trident/Utils.kt new file mode 100644 index 0000000..7ff2d58 --- /dev/null +++ b/src/main/kotlin/me/kyren223/trident/Utils.kt @@ -0,0 +1,12 @@ +package me.kyren223.trident + +import com.maddyhome.idea.vim.KeyHandler +import com.maddyhome.idea.vim.api.ExecutionContext +import com.maddyhome.idea.vim.api.VimEditor +import com.maddyhome.idea.vim.api.injector + +fun simulateKeyPress(key: String, editor: VimEditor, context: ExecutionContext.Editor) { + KeyHandler.getInstance().handleKey(editor, injector.parser.parseKeys(key)[0], context) +} + + -- cgit v1.3.1