From dc5a11ba17a435c40b331d04f4b67d430aaa2286 Mon Sep 17 00:00:00 2001 From: a690700752 Date: Wed, 9 May 2018 23:36:53 +0800 Subject: [PATCH] Add word1 jump. --- README.md | 12 +++--- src/main/kotlin/com/werfad/JumpHandler.kt | 9 ++-- src/main/kotlin/com/werfad/KJumpAction.kt | 12 ++++++ .../kotlin/com/werfad/finder/Word1Finder.kt | 42 +++++++++++++++++++ src/main/resources/META-INF/plugin.xml | 8 ++-- 5 files changed, 70 insertions(+), 13 deletions(-) create mode 100644 src/main/kotlin/com/werfad/finder/Word1Finder.kt diff --git a/README.md b/README.md index 390ee68..5a2701a 100644 --- a/README.md +++ b/README.md @@ -8,10 +8,12 @@ There are no default activated shortcuts. You can assign KJump activation shortc nmap s :action KJumpAction nmap w :action KJumpAction.Word0 nmap l :action KJumpAction.Line +// more action see table below ``` -| Name | Action | Desc | -|--------------|-------------------|--------------------------------------------------| -| KJump | KJumpAction | Input 1 character and jump to any same character | -| KJump Word 0 | KJumpAction.Word0 | Jump to any word. | -| KJump Line | KJumpAction Line | Jump to any line. | +| Name | Action | Desc | +|--------------|-------------------|-------------------------------------------------------------------| +| KJump | KJumpAction | Input 1 character and jump to any same character. | +| KJump Word 0 | KJumpAction.Word0 | Jump to any word. | +| KJump Word 1 | KJumpAction.Word1 | Input 1 character and jump to any word start with this character. | +| KJump Line | KJumpAction Line | Jump to any line. | diff --git a/src/main/kotlin/com/werfad/JumpHandler.kt b/src/main/kotlin/com/werfad/JumpHandler.kt index 4a362d4..a774048 100644 --- a/src/main/kotlin/com/werfad/JumpHandler.kt +++ b/src/main/kotlin/com/werfad/JumpHandler.kt @@ -7,16 +7,14 @@ import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.actionSystem.EditorActionHandler import com.intellij.openapi.editor.actionSystem.EditorActionManager import com.intellij.openapi.editor.actionSystem.TypedActionHandler -import com.werfad.finder.Char1Finder -import com.werfad.finder.Finder -import com.werfad.finder.LineFinder -import com.werfad.finder.Word0Finder +import com.werfad.finder.* import com.werfad.utils.getVisibleRangeOffset object JumpHandler : TypedActionHandler { val MODE_CHAR1 = 0 val MODE_WORD0 = 1 - val MODE_LINE = 2 + val MODE_WORD1 = 2 + val MODE_LINE = 3 private lateinit var mOldTypedHandler: TypedActionHandler private var mOldEscActionHandler: EditorActionHandler? = null @@ -81,6 +79,7 @@ object JumpHandler : TypedActionHandler { finder = when (mode) { MODE_CHAR1 -> Char1Finder() MODE_WORD0 -> Word0Finder() + MODE_WORD1 -> Word1Finder() MODE_LINE -> LineFinder() else -> throw Exception("Invalid start mode: $mode .") } diff --git a/src/main/kotlin/com/werfad/KJumpAction.kt b/src/main/kotlin/com/werfad/KJumpAction.kt index 5cbd87d..610475d 100644 --- a/src/main/kotlin/com/werfad/KJumpAction.kt +++ b/src/main/kotlin/com/werfad/KJumpAction.kt @@ -6,6 +6,7 @@ import com.intellij.openapi.project.DumbAwareAction import com.werfad.JumpHandler.MODE_CHAR1 import com.werfad.JumpHandler.MODE_LINE import com.werfad.JumpHandler.MODE_WORD0 +import com.werfad.JumpHandler.MODE_WORD1 class KJumpAction : DumbAwareAction() { override fun update(e: AnActionEvent) { @@ -29,6 +30,17 @@ class Word0Action : DumbAwareAction() { } } +class Word1Action : DumbAwareAction() { + override fun update(e: AnActionEvent) { + val editor = e.getData(CommonDataKeys.EDITOR) + e.presentation.isEnabled = editor != null + } + + override fun actionPerformed(event: AnActionEvent) { + JumpHandler.start(event.getData(CommonDataKeys.EDITOR)!!, MODE_WORD1) + } +} + class LineAction: DumbAwareAction() { override fun update(e: AnActionEvent) { val editor = e.getData(CommonDataKeys.EDITOR) diff --git a/src/main/kotlin/com/werfad/finder/Word1Finder.kt b/src/main/kotlin/com/werfad/finder/Word1Finder.kt new file mode 100644 index 0000000..f5ec0cf --- /dev/null +++ b/src/main/kotlin/com/werfad/finder/Word1Finder.kt @@ -0,0 +1,42 @@ +package com.werfad.finder + +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.util.TextRange +import com.werfad.DEFAULT_TAGS_KEYMAP +import com.werfad.KeyTagsGenerator +import com.werfad.Mark +import com.werfad.utils.findAllRegex + +class Word1Finder : Finder { + private val STATE_WAIT_SEARCH_CHAR = 0 + private val STATE_WAIT_KEY = 1 + private var state = STATE_WAIT_SEARCH_CHAR + + private lateinit var s: String + private lateinit var visibleRange: TextRange + + override fun start(e: Editor, s: String, visibleRange: TextRange): List? { + this.s = s + this.visibleRange = visibleRange + state = STATE_WAIT_SEARCH_CHAR + return null + } + + override fun input(e: Editor, c: Char, lastMarks: List): List { + return when (state) { + STATE_WAIT_SEARCH_CHAR -> { + val offsets = s.findAllRegex("\\b\\w") + .filter { s[it] == c } + .map { it + visibleRange.startOffset } + .sortedBy { Math.abs(it - e.caretModel.offset) } + val tags = KeyTagsGenerator.createTagsTree(offsets.size, DEFAULT_TAGS_KEYMAP) + state = STATE_WAIT_KEY + offsets.mapIndexed { index, offset -> + Mark(tags[index], offset) + } + } + STATE_WAIT_KEY -> matchInputAndCreateMarks(c, lastMarks) + else -> throw RuntimeException("Impossible.") + } + } +} \ 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 ab24445..a38b8cb 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -8,14 +8,14 @@ There are no default activated shortcut. You can assign KJump activation shortcuts in Settings->Keymap->KJump menu, such as <c-,> <c-;> etc, or integrate with IdeaVim by add below section in ~/.ideavimrc:
- nmap <leader><leader>s :action KJumpAction - Jump Github to see more.
+ nmap <leader><leader>s :action KJumpAction
+ Goto Github to see more.

]]> + * v0.0.3 add features: word0 jump, word1 jump, and line jump
* v0.0.2 compat with IntelliJ Platform Products
* v0.0.1 initial release
]]> @@ -42,6 +42,8 @@ description="Input 1 character and jump to any same character."/> +