Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add new action 'GlobalLine' #88

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ nmap <leader><leader>f :action KJumpAction.Word1<cr>
| 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 |

<!-- Plugin description end -->
Expand Down
49 changes: 29 additions & 20 deletions src/main/kotlin/com/werfad/Actions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,62 @@ 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
class GlobalWord0Action : BaseAction(GlobalJumpHandler) {
override fun getMode() = JumpMode.Word0
}

override fun actionPerformed(e: AnActionEvent) {
GlobalJumpHandler.start(getMode(), e)
}
class GlobalLineAction : BaseAction(GlobalJumpHandler) {
override fun getMode() = JumpMode.Line
}
7 changes: 3 additions & 4 deletions src/main/kotlin/com/werfad/GlobalJumpHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
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
Expand Down Expand Up @@ -106,7 +104,7 @@
}
}

fun start(mode: Int, anActionEvent: AnActionEvent) {
fun start(mode: JumpMode, anActionEvent: AnActionEvent) {
if (isStart) return
isStart = true
val editor = anActionEvent.getData(CommonDataKeys.EDITOR) ?: return
Expand All @@ -118,8 +116,9 @@
manager.setActionHandler(IdeActions.ACTION_EDITOR_ESCAPE, escActionHandler)
val visibleBorderOffset = editor.getVisibleRangeOffset()
val visibleString = editor.document.getText(visibleBorderOffset)
when (mode) {

Check notice on line 119 in src/main/kotlin/com/werfad/GlobalJumpHandler.kt

View workflow job for this annotation

GitHub Actions / Inspect code

Return or assignment can be lifted out

'Assignment' can be lifted out of 'when'
MODE_WORD0 -> finder = GlobalWord0Finder()
JumpMode.Word0 -> finder = GlobalWord0Finder()
JumpMode.Line -> finder = GlobalLineFinder()
else -> throw RuntimeException("Invalid start mode: $mode")
}
val marks = finder.start(editor, visibleString, visibleBorderOffset)
Expand Down
22 changes: 7 additions & 15 deletions src/main/kotlin/com/werfad/JumpHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions src/main/kotlin/com/werfad/JumpMode.kt
Original file line number Diff line number Diff line change
@@ -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()
}
17 changes: 17 additions & 0 deletions src/main/kotlin/com/werfad/finder/GlobalLineFinder.kt
Original file line number Diff line number Diff line change
@@ -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<MarksCanvas.Mark> {
return e.project.getMarksFromAllEditors(pattern)
}

override fun input(e: Editor, c: Char, lastMarks: List<MarksCanvas.Mark>): List<MarksCanvas.Mark> {
return advanceGlobalMarks(c, lastMarks)
}
}
2 changes: 2 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
description="Input 1 character and jump to declaration of any word start with this character."/>
<action id="KJumpAction.GlobalWord0" class="com.werfad.GlobalWord0Action" text="KJump Global Word 0"
description="Jump to any word across all visible editors."/>
<action id="KJumpAction.GlobalLine" class="com.werfad.GlobalLineAction" text="KJump Global Line"
description="Jump to any line across editors."/>
</actions>

</idea-plugin>
Loading