From 8c9df19aea8bdff299c54727a48aa851994fb835 Mon Sep 17 00:00:00 2001 From: metepg Date: Mon, 9 Dec 2024 18:33:05 +0200 Subject: [PATCH 1/3] Refactor to use JumpMode instead of global variables - Created class JumpMode since enums are not available without Kotlin standard library. This allows both jump handlers to use the same values. - Added variable 'handler' as a parameter for BaseAction so its more descriptive which handler the action uses. --- src/main/kotlin/com/werfad/Actions.kt | 47 ++++++++++--------- .../kotlin/com/werfad/GlobalJumpHandler.kt | 6 +-- src/main/kotlin/com/werfad/JumpHandler.kt | 22 +++------ src/main/kotlin/com/werfad/JumpMode.kt | 12 +++++ 4 files changed, 47 insertions(+), 40 deletions(-) create mode 100644 src/main/kotlin/com/werfad/JumpMode.kt diff --git a/src/main/kotlin/com/werfad/Actions.kt b/src/main/kotlin/com/werfad/Actions.kt index 9831344..58626ab 100644 --- a/src/main/kotlin/com/werfad/Actions.kt +++ b/src/main/kotlin/com/werfad/Actions.kt @@ -3,53 +3,58 @@ package com.werfad import com.intellij.openapi.actionSystem.ActionUpdateThread import com.intellij.openapi.actionSystem.AnActionEvent import com.intellij.openapi.actionSystem.CommonDataKeys +import com.intellij.openapi.editor.actionSystem.TypedActionHandler import com.intellij.openapi.project.DumbAwareAction -abstract class BaseAction : DumbAwareAction() { +abstract class BaseAction(private val handler: TypedActionHandler) : DumbAwareAction() { override fun update(e: AnActionEvent) { val editor = e.getData(CommonDataKeys.EDITOR) e.presentation.isEnabled = editor != null } + /** + * JumpHandler is used for actions on the currently focused editor. + * GlobalJumpHandler is used for actions across all editors. + */ override fun actionPerformed(e: AnActionEvent) { - JumpHandler.start(getMode(), e) + when (handler) { + is JumpHandler -> handler.start(getMode(), e) + is GlobalJumpHandler -> handler.start(getMode(), e) + else -> error("Unsupported handler: ${handler::class}") + } } override fun getActionUpdateThread(): ActionUpdateThread { return ActionUpdateThread.EDT } - abstract fun getMode(): Int + abstract fun getMode(): JumpMode } -class KJumpAction : BaseAction() { - override fun getMode() = JumpHandler.MODE_CHAR1 +class KJumpAction : BaseAction(JumpHandler) { + override fun getMode() = JumpMode.Char1 } -class Char2Action : BaseAction() { - override fun getMode() = JumpHandler.MODE_CHAR2 +class Char2Action : BaseAction(JumpHandler) { + override fun getMode() = JumpMode.Char2 } -class Word0Action : BaseAction() { - override fun getMode() = JumpHandler.MODE_WORD0 +class Word0Action : BaseAction(JumpHandler) { + override fun getMode() = JumpMode.Word0 } -class Word1Action : BaseAction() { - override fun getMode() = JumpHandler.MODE_WORD1 +class Word1Action : BaseAction(JumpHandler) { + override fun getMode() = JumpMode.Word1 } -class LineAction : BaseAction() { - override fun getMode() = JumpHandler.MODE_LINE +class LineAction : BaseAction(JumpHandler) { + override fun getMode() = JumpMode.Line } -class GotoDeclarationWord1Action : BaseAction() { - override fun getMode() = JumpHandler.MODE_WORD1_DECLARATION +class GotoDeclarationWord1Action : BaseAction(JumpHandler) { + override fun getMode() = JumpMode.Word1Declaration } -class GlobalWord0Action : BaseAction() { - override fun getMode() = GlobalJumpHandler.MODE_WORD0 - - override fun actionPerformed(e: AnActionEvent) { - GlobalJumpHandler.start(getMode(), e) - } +class GlobalWord0Action : BaseAction(GlobalJumpHandler) { + override fun getMode() = JumpMode.Word0 } \ No newline at end of file diff --git a/src/main/kotlin/com/werfad/GlobalJumpHandler.kt b/src/main/kotlin/com/werfad/GlobalJumpHandler.kt index 1c1ed57..c485093 100644 --- a/src/main/kotlin/com/werfad/GlobalJumpHandler.kt +++ b/src/main/kotlin/com/werfad/GlobalJumpHandler.kt @@ -11,8 +11,6 @@ import com.werfad.finder.* import com.werfad.utils.getVisibleRangeOffset object GlobalJumpHandler : TypedActionHandler { - const val MODE_WORD0 = 2 - private var mOldTypedHandler: TypedActionHandler? = null private var mOldEscActionHandler: EditorActionHandler? = null private var isStart = false @@ -106,7 +104,7 @@ object GlobalJumpHandler : TypedActionHandler { } } - fun start(mode: Int, anActionEvent: AnActionEvent) { + fun start(mode: JumpMode, anActionEvent: AnActionEvent) { if (isStart) return isStart = true val editor = anActionEvent.getData(CommonDataKeys.EDITOR) ?: return @@ -119,7 +117,7 @@ object GlobalJumpHandler : TypedActionHandler { val visibleBorderOffset = editor.getVisibleRangeOffset() val visibleString = editor.document.getText(visibleBorderOffset) when (mode) { - MODE_WORD0 -> finder = GlobalWord0Finder() + JumpMode.Word0 -> finder = GlobalWord0Finder() else -> throw RuntimeException("Invalid start mode: $mode") } val marks = finder.start(editor, visibleString, visibleBorderOffset) diff --git a/src/main/kotlin/com/werfad/JumpHandler.kt b/src/main/kotlin/com/werfad/JumpHandler.kt index cd80542..b3acefc 100644 --- a/src/main/kotlin/com/werfad/JumpHandler.kt +++ b/src/main/kotlin/com/werfad/JumpHandler.kt @@ -15,13 +15,6 @@ import com.werfad.finder.* import com.werfad.utils.getVisibleRangeOffset object JumpHandler : TypedActionHandler { - const val MODE_CHAR1 = 0 - const val MODE_CHAR2 = 1 - const val MODE_WORD0 = 2 - const val MODE_WORD1 = 3 - const val MODE_LINE = 4 - const val MODE_WORD1_DECLARATION = 5 - private var mOldTypedHandler: TypedActionHandler? = null private var mOldEscActionHandler: EditorActionHandler? = null private val mMarksCanvas: MarksCanvas by lazy { MarksCanvas() } @@ -98,7 +91,7 @@ object JumpHandler : TypedActionHandler { * * @param mode mode enum, see [.MODE_CHAR1] [.MODE_CHAR2] etc */ - fun start(mode: Int, anActionEvent: AnActionEvent) { + fun start(mode: JumpMode, anActionEvent: AnActionEvent) { if (isStart) return isStart = true val editor = anActionEvent.getData(CommonDataKeys.EDITOR) ?: return @@ -110,12 +103,12 @@ object JumpHandler : TypedActionHandler { manager.setActionHandler(IdeActions.ACTION_EDITOR_ESCAPE, escActionHandler) onJump = null when (mode) { - MODE_CHAR1 -> finder = Char1Finder() - MODE_CHAR2 -> finder = Char2Finder() - MODE_WORD0 -> finder = Word0Finder() - MODE_WORD1 -> finder = Word1Finder() - MODE_LINE -> finder = LineFinder() - MODE_WORD1_DECLARATION -> { + JumpMode.Char1 -> finder = Char1Finder() + JumpMode.Char2 -> finder = Char2Finder() + JumpMode.Word0 -> finder = Word0Finder() + JumpMode.Word1 -> finder = Word1Finder() + JumpMode.Line -> finder = LineFinder() + JumpMode.Word1Declaration -> { finder = Word1Finder() onJump = { ActionManager @@ -124,7 +117,6 @@ object JumpHandler : TypedActionHandler { .actionPerformed(anActionEvent) } } - else -> throw RuntimeException("Invalid start mode: $mode") } val visibleBorderOffset = editor.getVisibleRangeOffset() val visibleString = editor.document.getText(visibleBorderOffset) diff --git a/src/main/kotlin/com/werfad/JumpMode.kt b/src/main/kotlin/com/werfad/JumpMode.kt new file mode 100644 index 0000000..0c78d20 --- /dev/null +++ b/src/main/kotlin/com/werfad/JumpMode.kt @@ -0,0 +1,12 @@ +package com.werfad + +sealed class JumpMode(private val value: Int) { + object Char1: JumpMode(0) + object Char2: JumpMode(1) + object Word0: JumpMode(2) + object Word1: JumpMode(3) + object Line: JumpMode(4) + object Word1Declaration: JumpMode(5) + + override fun toString() = value.toString() +} \ No newline at end of file From a15d3f622688861e6bd54cc0b0a47e7ede71f790 Mon Sep 17 00:00:00 2001 From: metepg Date: Mon, 9 Dec 2024 18:47:39 +0200 Subject: [PATCH 2/3] feature: add new action 'GlobalLineFinder' - This action is copy of the 'LineFinder' action but with the added possibility of jumping through all the visible editors. --- src/main/kotlin/com/werfad/Actions.kt | 4 ++++ src/main/kotlin/com/werfad/GlobalJumpHandler.kt | 1 + .../com/werfad/finder/GlobalLineFinder.kt | 17 +++++++++++++++++ src/main/resources/META-INF/plugin.xml | 2 ++ 4 files changed, 24 insertions(+) create mode 100644 src/main/kotlin/com/werfad/finder/GlobalLineFinder.kt diff --git a/src/main/kotlin/com/werfad/Actions.kt b/src/main/kotlin/com/werfad/Actions.kt index 58626ab..80d44b3 100644 --- a/src/main/kotlin/com/werfad/Actions.kt +++ b/src/main/kotlin/com/werfad/Actions.kt @@ -57,4 +57,8 @@ class GotoDeclarationWord1Action : BaseAction(JumpHandler) { class GlobalWord0Action : BaseAction(GlobalJumpHandler) { override fun getMode() = JumpMode.Word0 +} + +class GlobalLineAction : BaseAction(GlobalJumpHandler) { + override fun getMode() = JumpMode.Line } \ No newline at end of file diff --git a/src/main/kotlin/com/werfad/GlobalJumpHandler.kt b/src/main/kotlin/com/werfad/GlobalJumpHandler.kt index c485093..2f992c2 100644 --- a/src/main/kotlin/com/werfad/GlobalJumpHandler.kt +++ b/src/main/kotlin/com/werfad/GlobalJumpHandler.kt @@ -118,6 +118,7 @@ object GlobalJumpHandler : TypedActionHandler { val visibleString = editor.document.getText(visibleBorderOffset) when (mode) { JumpMode.Word0 -> finder = GlobalWord0Finder() + JumpMode.Line -> finder = GlobalLineFinder() else -> throw RuntimeException("Invalid start mode: $mode") } val marks = finder.start(editor, visibleString, visibleBorderOffset) diff --git a/src/main/kotlin/com/werfad/finder/GlobalLineFinder.kt b/src/main/kotlin/com/werfad/finder/GlobalLineFinder.kt new file mode 100644 index 0000000..5f22483 --- /dev/null +++ b/src/main/kotlin/com/werfad/finder/GlobalLineFinder.kt @@ -0,0 +1,17 @@ +package com.werfad.finder +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.util.TextRange +import com.werfad.MarksCanvas +import com.werfad.utils.getMarksFromAllEditors + +private val pattern = Regex("(?m)^") + +class GlobalLineFinder: Finder { + override fun start(e: Editor, s: String, visibleRange: TextRange): List { + return e.project.getMarksFromAllEditors(pattern) + } + + override fun input(e: Editor, c: Char, lastMarks: List): List { + return advanceGlobalMarks(c, lastMarks) + } +} \ No newline at end of file diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 54bf60c..8160680 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -27,6 +27,8 @@ description="Input 1 character and jump to declaration of any word start with this character."/> + From 6837ddf13913bcbf3c203f5c30aecd835e7ba959 Mon Sep 17 00:00:00 2001 From: metepg Date: Mon, 9 Dec 2024 18:49:33 +0200 Subject: [PATCH 3/3] Update Readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index f528735..0e4738b 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ nmap f :action KJumpAction.Word1 | KJump Word 0 | KJumpAction.Word0 | Jump to any word | | KJump Word 1 | KJumpAction.Word1 | Input 1 character and jump to any word starting with this character | | KJump Line | KJumpAction.Line | Jump to any line | +| KJump Global Line | KJumpAction.GlobalLine | Jump to any line across all visible editors | | KJump Global Word 0 | KJumpAction.GlobalWord0 | Jump to any word across all visible editors |