From 7451f83d6685073d10df9bae5c957fdb55dcdc53 Mon Sep 17 00:00:00 2001 From: Kyren223 Date: Fri, 23 Aug 2024 00:31:20 +0300 Subject: fix: issues with IdeaVim version 2.16.0, everything should work now --- .../trident/actions/TridentAppendAction.kt | 32 +- .../kyren223/trident/actions/TridentListAction.kt | 26 +- .../trident/actions/TridentListSelectAction.kt | 22 +- .../trident/actions/TridentMappingsAction.kt | 24 +- .../trident/actions/TridentSelectAction.kt | 88 ++--- .../kotlin/me/kyren223/trident/data/Settings.kt | 66 ++-- .../me/kyren223/trident/data/SettingsData.kt | 350 +++++++++--------- .../trident/tests/MappingsExpansionTest.kt | 82 ++--- .../kotlin/me/kyren223/trident/tests/SplitTest.kt | 120 +++---- .../me/kyren223/trident/ui/SettingsConfigurable.kt | 134 +++---- .../me/kyren223/trident/ui/TridentListMenu.kt | 204 +++++------ .../me/kyren223/trident/ui/TridentMappingsMenu.kt | 171 +++++---- .../me/kyren223/trident/utils/TridentUtils.kt | 390 ++++++++++----------- src/main/kotlin/me/kyren223/trident/utils/Utils.kt | 42 +-- src/main/resources/META-INF/Trident-IdeaVim.xml | 4 +- src/main/resources/META-INF/plugin.xml | 264 +++++++------- src/main/resources/META-INF/pluginIcon.svg | 52 +-- 17 files changed, 1030 insertions(+), 1041 deletions(-) (limited to 'src') diff --git a/src/main/kotlin/me/kyren223/trident/actions/TridentAppendAction.kt b/src/main/kotlin/me/kyren223/trident/actions/TridentAppendAction.kt index 1b0d33a..571eb0f 100644 --- a/src/main/kotlin/me/kyren223/trident/actions/TridentAppendAction.kt +++ b/src/main/kotlin/me/kyren223/trident/actions/TridentAppendAction.kt @@ -1,16 +1,16 @@ -package me.kyren223.trident.actions - -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 -import me.kyren223.trident.utils.TridentList - -class TridentAppendAction : AnAction() { - override fun actionPerformed(e: AnActionEvent) { - val project: Project = e.project ?: return - val file: VirtualFile = e.getData(PlatformDataKeys.VIRTUAL_FILE) ?: return - TridentList.append(project, file) - } -} +package me.kyren223.trident.actions + +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 +import me.kyren223.trident.utils.TridentList + +class TridentAppendAction : AnAction() { + override fun actionPerformed(e: AnActionEvent) { + val project: Project = e.project ?: return + val file: VirtualFile = e.getData(PlatformDataKeys.VIRTUAL_FILE) ?: return + TridentList.append(project, file) + } +} diff --git a/src/main/kotlin/me/kyren223/trident/actions/TridentListAction.kt b/src/main/kotlin/me/kyren223/trident/actions/TridentListAction.kt index 0d733f3..f37f0aa 100644 --- a/src/main/kotlin/me/kyren223/trident/actions/TridentListAction.kt +++ b/src/main/kotlin/me/kyren223/trident/actions/TridentListAction.kt @@ -1,13 +1,13 @@ -package me.kyren223.trident.actions - -import com.intellij.openapi.actionSystem.AnAction -import com.intellij.openapi.actionSystem.AnActionEvent -import me.kyren223.trident.ui.TridentListMenu - -class TridentListAction : AnAction() { - override fun actionPerformed(e: AnActionEvent) { - val project = e.project ?: return - TridentListMenu.open(project) - } - -} +package me.kyren223.trident.actions + +import com.intellij.openapi.actionSystem.AnAction +import com.intellij.openapi.actionSystem.AnActionEvent +import me.kyren223.trident.ui.TridentListMenu + +class TridentListAction : AnAction() { + override fun actionPerformed(e: AnActionEvent) { + val project = e.project ?: return + TridentListMenu.open(project) + } + +} diff --git a/src/main/kotlin/me/kyren223/trident/actions/TridentListSelectAction.kt b/src/main/kotlin/me/kyren223/trident/actions/TridentListSelectAction.kt index 1b68a59..4161927 100644 --- a/src/main/kotlin/me/kyren223/trident/actions/TridentListSelectAction.kt +++ b/src/main/kotlin/me/kyren223/trident/actions/TridentListSelectAction.kt @@ -1,11 +1,11 @@ -package me.kyren223.trident.actions - -import com.intellij.openapi.actionSystem.AnAction -import com.intellij.openapi.actionSystem.AnActionEvent -import me.kyren223.trident.ui.TridentListMenu - -class TridentListSelectAction : AnAction() { - override fun actionPerformed(e: AnActionEvent) { - TridentListMenu.instance?.select() - } -} +package me.kyren223.trident.actions + +import com.intellij.openapi.actionSystem.AnAction +import com.intellij.openapi.actionSystem.AnActionEvent +import me.kyren223.trident.ui.TridentListMenu + +class TridentListSelectAction : AnAction() { + override fun actionPerformed(e: AnActionEvent) { + TridentListMenu.instance?.select() + } +} diff --git a/src/main/kotlin/me/kyren223/trident/actions/TridentMappingsAction.kt b/src/main/kotlin/me/kyren223/trident/actions/TridentMappingsAction.kt index b303c9e..77e3a11 100644 --- a/src/main/kotlin/me/kyren223/trident/actions/TridentMappingsAction.kt +++ b/src/main/kotlin/me/kyren223/trident/actions/TridentMappingsAction.kt @@ -1,12 +1,12 @@ -package me.kyren223.trident.actions - -import com.intellij.openapi.actionSystem.AnAction -import com.intellij.openapi.actionSystem.AnActionEvent -import me.kyren223.trident.ui.TridentMappingsMenu - -class TridentMappingsAction : AnAction() { - override fun actionPerformed(e: AnActionEvent) { - val project = e.project ?: return - TridentMappingsMenu.open(project) - } -} +package me.kyren223.trident.actions + +import com.intellij.openapi.actionSystem.AnAction +import com.intellij.openapi.actionSystem.AnActionEvent +import me.kyren223.trident.ui.TridentMappingsMenu + +class TridentMappingsAction : AnAction() { + override fun actionPerformed(e: AnActionEvent) { + val project = e.project ?: return + TridentMappingsMenu.open(project) + } +} diff --git a/src/main/kotlin/me/kyren223/trident/actions/TridentSelectAction.kt b/src/main/kotlin/me/kyren223/trident/actions/TridentSelectAction.kt index 60973f2..2eeada3 100644 --- a/src/main/kotlin/me/kyren223/trident/actions/TridentSelectAction.kt +++ b/src/main/kotlin/me/kyren223/trident/actions/TridentSelectAction.kt @@ -1,44 +1,44 @@ -package me.kyren223.trident.actions - -import com.intellij.openapi.actionSystem.AnAction -import com.intellij.openapi.actionSystem.AnActionEvent -import com.intellij.openapi.actionSystem.PlatformDataKeys -import com.intellij.openapi.fileEditor.FileEditorManager -import me.kyren223.trident.utils.TridentList - -class TridentSelectAction : AnAction() { - override fun actionPerformed(e: AnActionEvent) { - val project = e.project ?: return - val index = when (val actionId = e.actionManager.getId(this)) { - NEXT -> { - val file = e.getData(PlatformDataKeys.VIRTUAL_FILE) ?: return - val current = TridentList.getIndexOfFile(project, file) ?: return - current + 1 - } - - PREV -> { - val file = e.getData(PlatformDataKeys.VIRTUAL_FILE) ?: return - val current = TridentList.getIndexOfFile(project, file) ?: return - current - 1 - } - - else -> { - getIndex(actionId!!) - } - } - - val file = TridentList.select(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" - } -} - +package me.kyren223.trident.actions + +import com.intellij.openapi.actionSystem.AnAction +import com.intellij.openapi.actionSystem.AnActionEvent +import com.intellij.openapi.actionSystem.PlatformDataKeys +import com.intellij.openapi.fileEditor.FileEditorManager +import me.kyren223.trident.utils.TridentList + +class TridentSelectAction : AnAction() { + override fun actionPerformed(e: AnActionEvent) { + val project = e.project ?: return + val index = when (val actionId = e.actionManager.getId(this)) { + NEXT -> { + val file = e.getData(PlatformDataKeys.VIRTUAL_FILE) ?: return + val current = TridentList.getIndexOfFile(project, file) ?: return + current + 1 + } + + PREV -> { + val file = e.getData(PlatformDataKeys.VIRTUAL_FILE) ?: return + val current = TridentList.getIndexOfFile(project, file) ?: return + current - 1 + } + + else -> { + getIndex(actionId!!) + } + } + + val file = TridentList.select(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/data/Settings.kt b/src/main/kotlin/me/kyren223/trident/data/Settings.kt index e97737c..4c9204a 100644 --- a/src/main/kotlin/me/kyren223/trident/data/Settings.kt +++ b/src/main/kotlin/me/kyren223/trident/data/Settings.kt @@ -1,33 +1,33 @@ -package me.kyren223.trident.data - -import com.intellij.openapi.application.ApplicationManager -import com.intellij.openapi.components.* -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. -@Service -@State(name = "me.kyren223.trident.data.SettingsState", storages = [Storage("SdkSettingsPlugin.xml")]) -class SettingsState : PersistentStateComponent { - var width = 800 - var height = 400 - var fontSize = 20 - var enterToSelect = true - var automaticMapping = AutomaticMapping.Disabled - var automaticReplacing = AutomaticReplacing.Smart - var recursiveMapping = false - var rememberLine = false - var indexCycling = 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) - } -} +package me.kyren223.trident.data + +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.components.* +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. +@Service +@State(name = "me.kyren223.trident.data.SettingsState", storages = [Storage("SdkSettingsPlugin.xml")]) +class SettingsState : PersistentStateComponent { + var width = 800 + var height = 400 + var fontSize = 20 + var enterToSelect = true + var automaticMapping = AutomaticMapping.Disabled + var automaticReplacing = AutomaticReplacing.Smart + var recursiveMapping = false + var rememberLine = false + var indexCycling = 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/SettingsData.kt b/src/main/kotlin/me/kyren223/trident/data/SettingsData.kt index 26cb1d1..49ca322 100644 --- a/src/main/kotlin/me/kyren223/trident/data/SettingsData.kt +++ b/src/main/kotlin/me/kyren223/trident/data/SettingsData.kt @@ -1,175 +1,175 @@ -package me.kyren223.trident.data - -import com.intellij.openapi.ui.ComboBox -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 = ComboBox(AutomaticMapping.entries.toTypedArray()) - private val automaticReplacing = ComboBox(AutomaticReplacing.entries.toTypedArray()) - private val recursiveMapping = JBCheckBox(null, SettingsState.instance.recursiveMapping) - private val indexCycling = JBCheckBox(null, SettingsState.instance.indexCycling) - - init { - automaticMapping.selectedItem = SettingsState.instance.automaticMapping - automaticReplacing.selectedItem = SettingsState.instance.automaticReplacing - - 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("Disabled - adds the full file path to the Trident Mappings.")) - .addComponent(desc("Filename - Adds the filename (with extension).")) - .addComponent(desc("Filename (no extension) - Adds the filename (without extension).")) - .addVerticalGap(step) - - .addLabeledComponent("Automatic replacing (when possible)", automaticReplacing) - .addComponent(desc("Disabled - Keeps the full file path.")) - .addComponent(desc("Exact - Replaces the full path if an exact match is found.")) - .addComponent(desc("Smart - Like exact but if no exact match found, " + - "it will try to make the path shorter.")) - .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.")) - - .addLabeledComponent("Index cycling", indexCycling) - .addComponent(desc("If enabled, indexes will be cyclic, " + - "For example if you have 3 files and hotkey to 4, it'll cycle to 1.")) - - .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(): AutomaticMapping { - return automaticMapping.selectedItem as AutomaticMapping - } - - fun setAutomaticMapping(automaticMapping: AutomaticMapping) { - this.automaticMapping.selectedItem = automaticMapping - } - - fun getAutomaticReplacing(): AutomaticReplacing { - return automaticReplacing.selectedItem as AutomaticReplacing - } - - fun setAutomaticReplacing(automaticReplacing: AutomaticReplacing) { - this.automaticReplacing.selectedItem = automaticReplacing - } - - 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 - } - - fun getIndexCycling(): Boolean { - return indexCycling.isSelected - } - - fun setIndexCycling(indexCycling: Boolean) { - this.indexCycling.isSelected = indexCycling - } -} - -enum class AutomaticMapping { - Disabled, - Filename, - FilenameNoExtension, - ; - - override fun toString(): String { - if (this == FilenameNoExtension) return "Filename (without extension)" - return super.toString() - } -} - -enum class AutomaticReplacing { - Disabled, - Exact, - Smart, -} +package me.kyren223.trident.data + +import com.intellij.openapi.ui.ComboBox +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 = ComboBox(AutomaticMapping.entries.toTypedArray()) + private val automaticReplacing = ComboBox(AutomaticReplacing.entries.toTypedArray()) + private val recursiveMapping = JBCheckBox(null, SettingsState.instance.recursiveMapping) + private val indexCycling = JBCheckBox(null, SettingsState.instance.indexCycling) + + init { + automaticMapping.selectedItem = SettingsState.instance.automaticMapping + automaticReplacing.selectedItem = SettingsState.instance.automaticReplacing + + 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("Disabled - adds the full file path to the Trident Mappings.")) + .addComponent(desc("Filename - Adds the filename (with extension).")) + .addComponent(desc("Filename (no extension) - Adds the filename (without extension).")) + .addVerticalGap(step) + + .addLabeledComponent("Automatic replacing (when possible)", automaticReplacing) + .addComponent(desc("Disabled - Keeps the full file path.")) + .addComponent(desc("Exact - Replaces the full path if an exact match is found.")) + .addComponent(desc("Smart - Like exact but if no exact match found, " + + "it will try to make the path shorter.")) + .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.")) + + .addLabeledComponent("Index cycling", indexCycling) + .addComponent(desc("If enabled, indexes will be cyclic, " + + "For example if you have 3 files and hotkey to 4, it'll cycle to 1.")) + + .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(): AutomaticMapping { + return automaticMapping.selectedItem as AutomaticMapping + } + + fun setAutomaticMapping(automaticMapping: AutomaticMapping) { + this.automaticMapping.selectedItem = automaticMapping + } + + fun getAutomaticReplacing(): AutomaticReplacing { + return automaticReplacing.selectedItem as AutomaticReplacing + } + + fun setAutomaticReplacing(automaticReplacing: AutomaticReplacing) { + this.automaticReplacing.selectedItem = automaticReplacing + } + + 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 + } + + fun getIndexCycling(): Boolean { + return indexCycling.isSelected + } + + fun setIndexCycling(indexCycling: Boolean) { + this.indexCycling.isSelected = indexCycling + } +} + +enum class AutomaticMapping { + Disabled, + Filename, + FilenameNoExtension, + ; + + override fun toString(): String { + if (this == FilenameNoExtension) return "Filename (without extension)" + return super.toString() + } +} + +enum class AutomaticReplacing { + Disabled, + Exact, + Smart, +} diff --git a/src/main/kotlin/me/kyren223/trident/tests/MappingsExpansionTest.kt b/src/main/kotlin/me/kyren223/trident/tests/MappingsExpansionTest.kt index 0b66c4b..7fe33ad 100644 --- a/src/main/kotlin/me/kyren223/trident/tests/MappingsExpansionTest.kt +++ b/src/main/kotlin/me/kyren223/trident/tests/MappingsExpansionTest.kt @@ -1,42 +1,42 @@ -package me.kyren223.trident.tests - -import org.junit.Test - -class MappingsExpansionTest { - @Test - fun testExpand() { - val mappings = mapOf( - "\$A" to "A", - "\$BB" to "B", - "\$CCC" to "C", - "\$DDD" to "D", - "\$EEE" to "E", - "\$FFFFF" to "F", - "\$GG" to "G", - "\$UUU" to "U", - "\$VVVV" to "V", - "\$LLLLLL" to "L", - "\$MM" to "M", - "\$NN" to "N", - "\$OOO" to "O", - "\$PPP" to "P", - "\$QQQQ" to "Q", - "\$R" to "R", - "\$WWW" to "W", - "\$XX" to "X", - "\$HI" to "H", - "\$IJ" to "I", - "\$JetBrains" to "J", - "\$Krypton" to "K", - "\$Soft" to "S", - "\$Tnt" to "T", - "\$Ycombinator" to "Y", - "\$Zig" to "Z", - ).toList().sortedByDescending { it.first.length } - - mappings.forEach { (key, value) -> - println("$key -> $value") - } - - } +package me.kyren223.trident.tests + +import org.junit.Test + +class MappingsExpansionTest { + @Test + fun testExpand() { + val mappings = mapOf( + "\$A" to "A", + "\$BB" to "B", + "\$CCC" to "C", + "\$DDD" to "D", + "\$EEE" to "E", + "\$FFFFF" to "F", + "\$GG" to "G", + "\$UUU" to "U", + "\$VVVV" to "V", + "\$LLLLLL" to "L", + "\$MM" to "M", + "\$NN" to "N", + "\$OOO" to "O", + "\$PPP" to "P", + "\$QQQQ" to "Q", + "\$R" to "R", + "\$WWW" to "W", + "\$XX" to "X", + "\$HI" to "H", + "\$IJ" to "I", + "\$JetBrains" to "J", + "\$Krypton" to "K", + "\$Soft" to "S", + "\$Tnt" to "T", + "\$Ycombinator" to "Y", + "\$Zig" to "Z", + ).toList().sortedByDescending { it.first.length } + + mappings.forEach { (key, value) -> + println("$key -> $value") + } + + } } \ No newline at end of file diff --git a/src/main/kotlin/me/kyren223/trident/tests/SplitTest.kt b/src/main/kotlin/me/kyren223/trident/tests/SplitTest.kt index e4ea477..586fef1 100644 --- a/src/main/kotlin/me/kyren223/trident/tests/SplitTest.kt +++ b/src/main/kotlin/me/kyren223/trident/tests/SplitTest.kt @@ -1,61 +1,61 @@ -package me.kyren223.trident.tests - -import org.junit.Test - -class SplitTest { - - @Test - fun testSplit() { - val filenameAndExtension = "SplitTest.kt" - val filename = "SplitTest" - val dotfile = ".ideavimrc" - - val split = filenameAndExtension.split(".") - assert(split.size == 2) - assert(split[0] == filename) - assert(split[1] == "kt") - - val split2 = filename.split(".") - assert(split2.size == 1) - assert(split2[0] == filename) - - val split3 = dotfile.split(".") - assert(split3.size == 2) - assert(split3[0] == "") - assert(split3[1] == "ideavimrc") - - - } - - @Test - fun testSplit2() { - val path = "C:/Users/Owner/Projects/Trident/src/main/kotlin/me/kyren223/trident/tests/SplitTest.kt" - testPath(path, "SplitTest", "kt") - testPath("SplitTest.kt", "SplitTest", "kt") - testPath("SplitTest", "SplitTest", null) - testPath(".ideavimrc", null, "ideavimrc") - testPath("build.gradle.kts", "build.gradle", "kts") - - } - - private fun testPath(path: String, filename: String?, extension: String?) { - - val fullFilename = path.split("/").last() - println("fullFilename: $fullFilename") - val lastDot = fullFilename.lastIndexOf(".") - val noExtension = lastDot == -1 - - val filename2 = if (noExtension) fullFilename else { - fullFilename.substring(0, lastDot).ifEmpty { null } - } - val extension2 = if (noExtension) null else fullFilename.substring(lastDot + 1) - - println("filename: $filename") - println("extension: $extension") - println("filename2: $filename2") - println("extension2: $extension2") - - assert(filename2 == filename) - assert(extension2 == extension) - } +package me.kyren223.trident.tests + +import org.junit.Test + +class SplitTest { + + @Test + fun testSplit() { + val filenameAndExtension = "SplitTest.kt" + val filename = "SplitTest" + val dotfile = ".ideavimrc" + + val split = filenameAndExtension.split(".") + assert(split.size == 2) + assert(split[0] == filename) + assert(split[1] == "kt") + + val split2 = filename.split(".") + assert(split2.size == 1) + assert(split2[0] == filename) + + val split3 = dotfile.split(".") + assert(split3.size == 2) + assert(split3[0] == "") + assert(split3[1] == "ideavimrc") + + + } + + @Test + fun testSplit2() { + val path = "C:/Users/Owner/Projects/Trident/src/main/kotlin/me/kyren223/trident/tests/SplitTest.kt" + testPath(path, "SplitTest", "kt") + testPath("SplitTest.kt", "SplitTest", "kt") + testPath("SplitTest", "SplitTest", null) + testPath(".ideavimrc", null, "ideavimrc") + testPath("build.gradle.kts", "build.gradle", "kts") + + } + + private fun testPath(path: String, filename: String?, extension: String?) { + + val fullFilename = path.split("/").last() + println("fullFilename: $fullFilename") + val lastDot = fullFilename.lastIndexOf(".") + val noExtension = lastDot == -1 + + val filename2 = if (noExtension) fullFilename else { + fullFilename.substring(0, lastDot).ifEmpty { null } + } + val extension2 = if (noExtension) null else fullFilename.substring(lastDot + 1) + + println("filename: $filename") + println("extension: $extension") + println("filename2: $filename2") + println("extension2: $extension2") + + assert(filename2 == filename) + assert(extension2 == extension) + } } \ No newline at end of file diff --git a/src/main/kotlin/me/kyren223/trident/ui/SettingsConfigurable.kt b/src/main/kotlin/me/kyren223/trident/ui/SettingsConfigurable.kt index f6979b1..085c6f2 100644 --- a/src/main/kotlin/me/kyren223/trident/ui/SettingsConfigurable.kt +++ b/src/main/kotlin/me/kyren223/trident/ui/SettingsConfigurable.kt @@ -1,67 +1,67 @@ -// 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.automaticReplacing != this.settings!!.getAutomaticReplacing()) return true - if (settings.recursiveMapping != this.settings!!.getRecursiveMapping()) return true - if (settings.rememberLine != this.settings!!.getRememberLine()) return true - if (settings.indexCycling != this.settings!!.getIndexCycling()) 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.automaticReplacing = this.settings!!.getAutomaticReplacing() - settings.recursiveMapping = this.settings!!.getRecursiveMapping() - settings.rememberLine = this.settings!!.getRememberLine() - settings.indexCycling = this.settings!!.getIndexCycling() - } - - 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!!.setAutomaticReplacing(settings.automaticReplacing) - this.settings!!.setRecursiveMapping(settings.recursiveMapping) - this.settings!!.setRememberLine(settings.rememberLine) - this.settings!!.setIndexCycling(settings.indexCycling) - } - - override fun disposeUIResources() { - settings = null - } -} +// 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.automaticReplacing != this.settings!!.getAutomaticReplacing()) return true + if (settings.recursiveMapping != this.settings!!.getRecursiveMapping()) return true + if (settings.rememberLine != this.settings!!.getRememberLine()) return true + if (settings.indexCycling != this.settings!!.getIndexCycling()) 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.automaticReplacing = this.settings!!.getAutomaticReplacing() + settings.recursiveMapping = this.settings!!.getRecursiveMapping() + settings.rememberLine = this.settings!!.getRememberLine() + settings.indexCycling = this.settings!!.getIndexCycling() + } + + 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!!.setAutomaticReplacing(settings.automaticReplacing) + this.settings!!.setRecursiveMapping(settings.recursiveMapping) + this.settings!!.setRememberLine(settings.rememberLine) + this.settings!!.setIndexCycling(settings.indexCycling) + } + + 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 index 75e059a..45eaf76 100644 --- a/src/main/kotlin/me/kyren223/trident/ui/TridentListMenu.kt +++ b/src/main/kotlin/me/kyren223/trident/ui/TridentListMenu.kt @@ -1,102 +1,102 @@ -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.injectEnterToSelectMapping -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() { - doOKAction() - } - - override fun doOKAction() { - save() - super.doOKAction() - } - - override fun doCancelAction() { - save() - super.doCancelAction() - } - - private fun save() { - instance = null - files = editor.text.split("\n") - line = editor.editor?.caretModel?.logicalPosition?.line ?: 0 - } - - override fun createSouthPanel(): JComponent? { - return null - } - - companion object { - var instance: TridentListMenu? = null - var files: List? = null - var line = 0 - fun open(project: Project) { - val content = TridentList.get(project).joinToString("\n") - instance = TridentListMenu(content) - injectEnterToSelectMapping() - val selected = instance!!.showAndGet() - - files?.let { TridentList.set(project, it) } - files = null - - if (!selected) return - val file = TridentList.select(project, line) ?: return - FileEditorManager.getInstance(project).openFile(file, true) - } - } -} +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.injectEnterToSelectMapping +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 + editor.mode = Mode.NORMAL() +// setEditorMode(editor, Mode.NORMAL()) + } + + override fun focusLost(e: FocusEvent?) { /* Do nothing */ + } + }) + + return editor + } + + + override fun getPreferredFocusedComponent(): JComponent { + return this.editor + } + + fun select() { + doOKAction() + } + + override fun doOKAction() { + save() + super.doOKAction() + } + + override fun doCancelAction() { + save() + super.doCancelAction() + } + + private fun save() { + instance = null + files = editor.text.split("\n") + line = editor.editor?.caretModel?.logicalPosition?.line ?: 0 + } + + override fun createSouthPanel(): JComponent? { + return null + } + + companion object { + var instance: TridentListMenu? = null + var files: List? = null + var line = 0 + fun open(project: Project) { + val content = TridentList.get(project).joinToString("\n") + instance = TridentListMenu(content) + injectEnterToSelectMapping() + val selected = instance!!.showAndGet() + + files?.let { TridentList.set(project, it) } + files = null + + if (!selected) return + val file = TridentList.select(project, line) ?: return + FileEditorManager.getInstance(project).openFile(file, true) + } + } +} diff --git a/src/main/kotlin/me/kyren223/trident/ui/TridentMappingsMenu.kt b/src/main/kotlin/me/kyren223/trident/ui/TridentMappingsMenu.kt index 79f1df8..062c618 100644 --- a/src/main/kotlin/me/kyren223/trident/ui/TridentMappingsMenu.kt +++ b/src/main/kotlin/me/kyren223/trident/ui/TridentMappingsMenu.kt @@ -1,86 +1,85 @@ -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.state - 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() { - map = editor.text.split("\n") - .map { entry -> entry.split("=") } - .filter { entry -> entry.size == 2 } - .associate { it[0] to it[1] } - } - - override fun createSouthPanel(): JComponent? { - return null - } - - companion object { - var map: Map? = null - fun open(project: Project) { - val content = TridentMappings.get(project).entries - .joinToString("\n") { "${it.key}=${it.value}" } - val menu = TridentMappingsMenu(content) - menu.show() - map?.let { TridentMappings.set(project, it) } - map = null - } - } -} +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 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.state + 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 + editor.mode = 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() { + map = editor.text.split("\n") + .map { entry -> entry.split("=") } + .filter { entry -> entry.size == 2 } + .associate { it[0] to it[1] } + } + + override fun createSouthPanel(): JComponent? { + return null + } + + companion object { + var map: Map? = null + fun open(project: Project) { + val content = TridentMappings.get(project).entries + .joinToString("\n") { "${it.key}=${it.value}" } + val menu = TridentMappingsMenu(content) + menu.show() + map?.let { TridentMappings.set(project, it) } + map = null + } + } +} diff --git a/src/main/kotlin/me/kyren223/trident/utils/TridentUtils.kt b/src/main/kotlin/me/kyren223/trident/utils/TridentUtils.kt index 96ab454..5f96bd3 100644 --- a/src/main/kotlin/me/kyren223/trident/utils/TridentUtils.kt +++ b/src/main/kotlin/me/kyren223/trident/utils/TridentUtils.kt @@ -1,196 +1,196 @@ -package me.kyren223.trident.utils - -import com.intellij.ide.util.PropertiesComponent -import com.intellij.openapi.project.Project -import com.intellij.openapi.project.guessProjectDir -import com.intellij.openapi.vfs.LocalFileSystem -import com.intellij.openapi.vfs.VirtualFile -import me.kyren223.trident.data.AutomaticMapping -import me.kyren223.trident.data.AutomaticReplacing -import me.kyren223.trident.data.SettingsState - -object TridentList { - private const val PROPERTIES_KEY = "TridentList" - - private fun isValidFile(project: Project, path: String): Boolean { - // Requirements: - // - File must exist on disk after expansion - val expandedPath = TridentMappings.expand(project, path) - return expandedPath.isNotBlank() && LocalFileSystem.getInstance().findFileByPath(expandedPath) != null - } - - private fun getModifiedPath(project: Project, path: String): String { - val automaticMapping = SettingsState.instance.automaticMapping - val automaticReplacing = SettingsState.instance.automaticReplacing - val mappings = TridentMappings.get(project) - - val hasReplacement = automaticReplacing != AutomaticReplacing.Disabled - && mappings.containsValue(path) - - if (hasReplacement) { - return mappings.filterValues { it == path }.keys.firstOrNull()!! - } - - if (automaticMapping == AutomaticMapping.Disabled) { - return if (automaticReplacing == AutomaticReplacing.Smart) { - return smartReplacement(path, mappings) - } else path - } - - return path.split("/") - .lastOrNull() - ?.let { - if (automaticMapping == AutomaticMapping.Filename) { - return@let it - } - val dotIndex = it.lastIndexOf(".") - val filename = if (dotIndex == -1) it else it.substring(0, dotIndex).ifBlank { null } - val extension = if (dotIndex == -1) null else it.substring(dotIndex + 1) - return@let (filename ?: extension)!! - }?.let { - val key = "\$$it" - if (TridentMappings.exists(project, key)) { - return@let null - } - TridentMappings.append(project, key, path) - return@let key - } ?: (if (automaticReplacing == AutomaticReplacing.Smart) smartReplacement(path, mappings) else path) - } - - private fun smartReplacement(path: String, mappings: Map): String { - // This is technically redundant, - // The smart path is sorted based on length, so the longest path will be the first - // And the longest path will be the first path that exactly matches - val exactPath = mappings.filter { it.value == path }.keys.firstOrNull() - exactPath?.let { return it } - - var smartPath = path - for ((key, value) in mappings - .toList() - .sortedByDescending { it.second.length } - .associate { it.first to it.second }) { - smartPath = smartPath.replace(value, key) - } - return smartPath - } - - fun append(project: Project, file: VirtualFile) { - println("TridentList.append - file: ${file.path}") - val files = get(project).toMutableList() - val path = getModifiedPath(project, file.path) - println("TridentList.append - path: $path") - files.add(path) - println("TridentList.append - add: " + files.joinToString(", ")) - set(project, files) - println("TridentList.append - set") - - println("TridentList.append - TridentList: " + get(project).joinToString(", ")) - println("TridentList.append - TridentMappings: " + TridentMappings.get(project) - .map { "${it.key}=${it.value}" } - .joinToString(", ")) - } - - fun set(project: Project, files: List) { - val properties = PropertiesComponent.getInstance(project) - val list = files.filter { isValidFile(project, it) } - properties.setList(PROPERTIES_KEY, list) - } - - fun get(project: Project): List { - val properties = PropertiesComponent.getInstance(project) - return properties.getList(PROPERTIES_KEY) ?: emptyList() - } - - private fun getFiles(project: Project): List { - return get(project) - .map { TridentMappings.expand(project, it) } - .mapNotNull { LocalFileSystem.getInstance().findFileByPath(it) } - } - - fun getIndexOfFile(project: Project, file: VirtualFile): Int? { - val files = getFiles(project) - return files.indexOfFirst { it.path == file.path }.takeUnless { it == -1 } - } - - fun select(project: Project, index: Int): VirtualFile? { - val cycle = SettingsState.instance.indexCycling - val count = getFiles(project).size - val i = if (cycle) ((index % count) + count) % count else index - val files = getFiles(project) - return files.getOrNull(i) - } -} - -object TridentMappings { - private const val PROPERTIES_KEY = "TridentMappings" - - private fun isValidEntry(key: String, value: String): Boolean { - // Requirements: - // - Key and value must not be blank - // - key must start with $ - // - key must only contain alphanumeric characters, underscores, hyphens, and dots - // - value must not contain = - return key.isNotBlank() && value.isNotBlank() && - key.matches(Regex("""^\$[a-zA-Z0-9_.\-]+$""")) && - value.matches(Regex("""^[^=]+$""")) - } - - fun append(project: Project, key: String, value: String, overwrite: Boolean = false) { - println("TridentMappings.append - key: $key, value: $value") - val map = get(project).toMutableMap() - if (map.containsKey(key) && !overwrite) return - println("TridentMappings.append - map: $map") - map[key] = value - println("TridentMappings.append - map after: $map") - set(project, map) - println("TridentMappings.append - set: ${get(project).map { "${it.key}=${it.value}" }.joinToString(", ")}") - } - - fun set(project: Project, mappings: Map) { - val properties = PropertiesComponent.getInstance(project) - - val list = mappings - .filter { isValidEntry(it.key, it.value) } - .map { "${it.key}=${it.value}" } - .toMutableList() - - properties.setList(PROPERTIES_KEY, list) - } - - fun get(project: Project): Map { - val properties = PropertiesComponent.getInstance(project) - val list = properties.getList(PROPERTIES_KEY) ?: return emptyMap() - val map = list - .map { it.split("=") } - .associate { it[0] to it[1] } - .toMutableMap() - project.guessProjectDir()?.let { - map["..."] = it.path - map["\$project"] = it.path - } - return map - } - - fun expand(project: Project, path: String): String { - val recursive = SettingsState.instance.recursiveMapping - val mappings = get(project).toList().sortedByDescending { it.first.length } - var expandedPath = path - while (true) { - var expanded = false - mappings.forEach { (key, value) -> - val expansion = expandedPath.replace(key, value) - if (expansion != expandedPath) { - expandedPath = expansion - expanded = true - } - } - if (!recursive || !expanded) break - } - - return expandedPath - } - - fun exists(project: Project, key: String): Boolean { - return get(project).containsKey(key) - } +package me.kyren223.trident.utils + +import com.intellij.ide.util.PropertiesComponent +import com.intellij.openapi.project.Project +import com.intellij.openapi.project.guessProjectDir +import com.intellij.openapi.vfs.LocalFileSystem +import com.intellij.openapi.vfs.VirtualFile +import me.kyren223.trident.data.AutomaticMapping +import me.kyren223.trident.data.AutomaticReplacing +import me.kyren223.trident.data.SettingsState + +object TridentList { + private const val PROPERTIES_KEY = "TridentList" + + private fun isValidFile(project: Project, path: String): Boolean { + // Requirements: + // - File must exist on disk after expansion + val expandedPath = TridentMappings.expand(project, path) + return expandedPath.isNotBlank() && LocalFileSystem.getInstance().findFileByPath(expandedPath) != null + } + + private fun getModifiedPath(project: Project, path: String): String { + val automaticMapping = SettingsState.instance.automaticMapping + val automaticReplacing = SettingsState.instance.automaticReplacing + val mappings = TridentMappings.get(project) + + val hasReplacement = automaticReplacing != AutomaticReplacing.Disabled + && mappings.containsValue(path) + + if (hasReplacement) { + return mappings.filterValues { it == path }.keys.firstOrNull()!! + } + + if (automaticMapping == AutomaticMapping.Disabled) { + return if (automaticReplacing == AutomaticReplacing.Smart) { + return smartReplacement(path, mappings) + } else path + } + + return path.split("/") + .lastOrNull() + ?.let { + if (automaticMapping == AutomaticMapping.Filename) { + return@let it + } + val dotIndex = it.lastIndexOf(".") + val filename = if (dotIndex == -1) it else it.substring(0, dotIndex).ifBlank { null } + val extension = if (dotIndex == -1) null else it.substring(dotIndex + 1) + return@let (filename ?: extension)!! + }?.let { + val key = "\$$it" + if (TridentMappings.exists(project, key)) { + return@let null + } + TridentMappings.append(project, key, path) + return@let key + } ?: (if (automaticReplacing == AutomaticReplacing.Smart) smartReplacement(path, mappings) else path) + } + + private fun smartReplacement(path: String, mappings: Map): String { + // This is technically redundant, + // The smart path is sorted based on length, so the longest path will be the first + // And the longest path will be the first path that exactly matches + val exactPath = mappings.filter { it.value == path }.keys.firstOrNull() + exactPath?.let { return it } + + var smartPath = path + for ((key, value) in mappings + .toList() + .sortedByDescending { it.second.length } + .associate { it.first to it.second }) { + smartPath = smartPath.replace(value, key) + } + return smartPath + } + + fun append(project: Project, file: VirtualFile) { + println("TridentList.append - file: ${file.path}") + val files = get(project).toMutableList() + val path = getModifiedPath(project, file.path) + println("TridentList.append - path: $path") + files.add(path) + println("TridentList.append - add: " + files.joinToString(", ")) + set(project, files) + println("TridentList.append - set") + + println("TridentList.append - TridentList: " + get(project).joinToString(", ")) + println("TridentList.append - TridentMappings: " + TridentMappings.get(project) + .map { "${it.key}=${it.value}" } + .joinToString(", ")) + } + + fun set(project: Project, files: List) { + val properties = PropertiesComponent.getInstance(project) + val list = files.filter { isValidFile(project, it) } + properties.setList(PROPERTIES_KEY, list) + } + + fun get(project: Project): List { + val properties = PropertiesComponent.getInstance(project) + return properties.getList(PROPERTIES_KEY) ?: emptyList() + } + + private fun getFiles(project: Project): List { + return get(project) + .map { TridentMappings.expand(project, it) } + .mapNotNull { LocalFileSystem.getInstance().findFileByPath(it) } + } + + fun getIndexOfFile(project: Project, file: VirtualFile): Int? { + val files = getFiles(project) + return files.indexOfFirst { it.path == file.path }.takeUnless { it == -1 } + } + + fun select(project: Project, index: Int): VirtualFile? { + val cycle = SettingsState.instance.indexCycling + val count = getFiles(project).size + val i = if (cycle) ((index % count) + count) % count else index + val files = getFiles(project) + return files.getOrNull(i) + } +} + +object TridentMappings { + private const val PROPERTIES_KEY = "TridentMappings" + + private fun isValidEntry(key: String, value: String): Boolean { + // Requirements: + // - Key and value must not be blank + // - key must start with $ + // - key must only contain alphanumeric characters, underscores, hyphens, and dots + // - value must not contain = + return key.isNotBlank() && value.isNotBlank() && + key.matches(Regex("""^\$[a-zA-Z0-9_.\-]+$""")) && + value.matches(Regex("""^[^=]+$""")) + } + + fun append(project: Project, key: String, value: String, overwrite: Boolean = false) { + println("TridentMappings.append - key: $key, value: $value") + val map = get(project).toMutableMap() + if (map.containsKey(key) && !overwrite) return + println("TridentMappings.append - map: $map") + map[key] = value + println("TridentMappings.append - map after: $map") + set(project, map) + println("TridentMappings.append - set: ${get(project).map { "${it.key}=${it.value}" }.joinToString(", ")}") + } + + fun set(project: Project, mappings: Map) { + val properties = PropertiesComponent.getInstance(project) + + val list = mappings + .filter { isValidEntry(it.key, it.value) } + .map { "${it.key}=${it.value}" } + .toMutableList() + + properties.setList(PROPERTIES_KEY, list) + } + + fun get(project: Project): Map { + val properties = PropertiesComponent.getInstance(project) + val list = properties.getList(PROPERTIES_KEY) ?: return emptyMap() + val map = list + .map { it.split("=") } + .associate { it[0] to it[1] } + .toMutableMap() + project.guessProjectDir()?.let { + map["..."] = it.path + map["\$project"] = it.path + } + return map + } + + fun expand(project: Project, path: String): String { + val recursive = SettingsState.instance.recursiveMapping + val mappings = get(project).toList().sortedByDescending { it.first.length } + var expandedPath = path + while (true) { + var expanded = false + mappings.forEach { (key, value) -> + val expansion = expandedPath.replace(key, value) + if (expansion != expandedPath) { + expandedPath = expansion + expanded = true + } + } + if (!recursive || !expanded) break + } + + return expandedPath + } + + fun exists(project: Project, key: String): Boolean { + return get(project).containsKey(key) + } } \ No newline at end of file diff --git a/src/main/kotlin/me/kyren223/trident/utils/Utils.kt b/src/main/kotlin/me/kyren223/trident/utils/Utils.kt index e688dd8..d9b4f34 100644 --- a/src/main/kotlin/me/kyren223/trident/utils/Utils.kt +++ b/src/main/kotlin/me/kyren223/trident/utils/Utils.kt @@ -1,26 +1,16 @@ -package me.kyren223.trident.utils - -import com.maddyhome.idea.vim.api.VimEditor -import com.maddyhome.idea.vim.api.injector -import com.maddyhome.idea.vim.command.MappingMode -import com.maddyhome.idea.vim.command.OperatorArguments -import com.maddyhome.idea.vim.key.MappingOwner -import com.maddyhome.idea.vim.state.mode.Mode - -fun setEditorMode(editor: VimEditor, mode: Mode) { - val context = injector.executionContextManager.onEditor(editor, null) - val operatorArguments = OperatorArguments(false, 0, mode) - editor.exitInsertMode(context, operatorArguments) - editor.vimChangeActionSwitchMode = Mode.NORMAL() -} - -fun injectEnterToSelectMapping() { - injector.keyGroup.putKeyMapping( - MappingMode.NV, - injector.parser.parseKeys(""), - MappingOwner.Plugin.get("Trident"), - injector.parser.parseKeys(":action TridentListSelect"), - false - ) -} - +package me.kyren223.trident.utils + +import com.maddyhome.idea.vim.api.injector +import com.maddyhome.idea.vim.command.MappingMode +import com.maddyhome.idea.vim.key.MappingOwner + +fun injectEnterToSelectMapping() { + injector.keyGroup.putKeyMapping( + MappingMode.NV, + injector.parser.parseKeys(""), + MappingOwner.Plugin.get("Trident"), + injector.parser.parseKeys(":action TridentListSelect"), + false + ) +} + diff --git a/src/main/resources/META-INF/Trident-IdeaVim.xml b/src/main/resources/META-INF/Trident-IdeaVim.xml index 164f46c..b72ce85 100644 --- a/src/main/resources/META-INF/Trident-IdeaVim.xml +++ b/src/main/resources/META-INF/Trident-IdeaVim.xml @@ -1,2 +1,2 @@ - - + + diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index e52e798..60095a7 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -1,133 +1,133 @@ - - - - me.kyren223.trident - - - Trident - - - Kyren223 - - - Trident - Trident is a plugin for JetBrains IDEs that provides a fast and intuitive
- way to navigate between files effortlessly.
-
- The plugin is inspired by the "Harpoon" plugin made
- by ThePrimeagen for Neovim.
-
- Trident aims to bring the same experience to JetBrains IDEs.
- but with additional features and proper integration with the IDE.
-
- Want to help contribute to the project?
- Source code: GitHub Repo
- ]]>
- - - com.intellij.modules.platform - IdeaVIM - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + me.kyren223.trident + + + Trident + + + Kyren223 + + + Trident + Trident is a plugin for JetBrains IDEs that provides a fast and intuitive
+ way to navigate between files effortlessly.
+
+ The plugin is inspired by the "Harpoon" plugin made
+ by ThePrimeagen for Neovim.
+
+ Trident aims to bring the same experience to JetBrains IDEs.
+ but with additional features and proper integration with the IDE.
+
+ Want to help contribute to the project?
+ Source code: GitHub Repo
+ ]]>
+ + + com.intellij.modules.platform + IdeaVIM + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
\ No newline at end of file diff --git a/src/main/resources/META-INF/pluginIcon.svg b/src/main/resources/META-INF/pluginIcon.svg index e79cf98..1bcfd45 100644 --- a/src/main/resources/META-INF/pluginIcon.svg +++ b/src/main/resources/META-INF/pluginIcon.svg @@ -1,26 +1,26 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + -- cgit v1.3.1