Skip to content

Commit

Permalink
Add word1 jump.
Browse files Browse the repository at this point in the history
  • Loading branch information
a690700752 committed May 9, 2018
1 parent 3cc566f commit dc5a11b
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 13 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ There are no default activated shortcuts. You can assign KJump activation shortc
nmap <leader><leader>s :action KJumpAction
nmap <leader><leader>w :action KJumpAction.Word0
nmap <leader><leader>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. |
9 changes: 4 additions & 5 deletions src/main/kotlin/com/werfad/JumpHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 .")
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/kotlin/com/werfad/KJumpAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
42 changes: 42 additions & 0 deletions src/main/kotlin/com/werfad/finder/Word1Finder.kt
Original file line number Diff line number Diff line change
@@ -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<Mark>? {
this.s = s
this.visibleRange = visibleRange
state = STATE_WAIT_SEARCH_CHAR
return null
}

override fun input(e: Editor, c: Char, lastMarks: List<Mark>): List<Mark> {
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.")
}
}
}
8 changes: 5 additions & 3 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
There are no default activated shortcut.
You can assign KJump activation shortcuts in Settings->Keymap->KJump menu, such as &lt;c-,&gt; &lt;c-;&gt; etc,
or integrate with IdeaVim by add below section in ~/.ideavimrc:<br>
nmap &lt;leader&gt;&lt;leader&gt;s :action KJumpAction<cr>
<a href="https://github.com/a690700752/KJump">Jump Github to see more.</a><br>
nmap &lt;leader&gt;&lt;leader&gt;s :action KJumpAction<cr><br>
<a href="https://github.com/a690700752/KJump">Goto Github to see more.</a><br>
<br>
]]></description>


<change-notes><![CDATA[
* v0.0.3 add features: word0 jump and line jump<br>
* v0.0.3 add features: word0 jump, word1 jump, and line jump<br>
* v0.0.2 compat with IntelliJ Platform Products<br>
* v0.0.1 initial release<br>
]]>
Expand All @@ -42,6 +42,8 @@
description="Input 1 character and jump to any same character."/>
<action id="KJumpAction.Word0" class="com.werfad.Word0Action" text="KJump Word 0"
description="Jump to any word."/>
<action id="KJumpAction.Word1" class="com.werfad.Word1Action" text="KJump Word 1"
description="Input 1 character and jump to any word start with this character."/>
<action id="KJumpAction.Line" class="com.werfad.LineAction" text="KJump Line"
description="Jump to any line."/>
</actions>
Expand Down

0 comments on commit dc5a11b

Please sign in to comment.