Skip to content

Commit

Permalink
refs acejump#41 word actions and different boundry modes for Finder
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Speckmaier committed Feb 2, 2018
1 parent c0c7a17 commit 0743147
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.intellij.openapi.options.Configurable
import com.johnlindquist.acejump.view.Model.Settings
import javax.swing.JComponent

@State(name = "AceConfig", storages = arrayOf(Storage("AceJump.xml")))
@State(uname = "AceConfig", storages = arrayOf(Storage("AceJump.xml")))
class AceConfig : Configurable, PersistentStateComponent<Settings> {
companion object {
var settings = Settings()
Expand Down
25 changes: 24 additions & 1 deletion src/main/kotlin/com/johnlindquist/acejump/control/AceAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.intellij.openapi.actionSystem.CommonDataKeys.EDITOR
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.DumbAwareAction
import com.johnlindquist.acejump.label.Pattern.LINE_MARK
import com.johnlindquist.acejump.label.Pattern.ALL_WORDS
import com.johnlindquist.acejump.search.Finder
import com.johnlindquist.acejump.search.getNameOfFileInEditor
import com.johnlindquist.acejump.view.Model.editor
Expand All @@ -21,6 +22,7 @@ open class AceAction : DumbAwareAction() {
}

override fun actionPerformed(e: AnActionEvent) {
Finder.fullFileMode()
editor = e.getData(EDITOR) ?: editor
logger.info("Invoked on ${editor.getNameOfFileInEditor()}")
Handler.activate()
Expand All @@ -43,4 +45,25 @@ object AceKeyAction : AceAction() {
logger.info("Registered key: ${KeyEvent.getKeyText(inputEvent.keyCode)}")
Handler.processCommand(inputEvent.keyCode)
}
}
}

class AceWordAction : AceAction() {
override fun actionPerformed(e: AnActionEvent) =
super.actionPerformed(e).also { Finder.search(ALL_WORDS) }
}

object AceWordForwardAction : AceAction() {
override fun actionPerformed(e: AnActionEvent) =
super.actionPerformed(e).also {
Finder.forwardMode()
Finder.search(ALL_WORDS)
}
}

object AceWordBackwardsAction : AceAction() {
override fun actionPerformed(e: AnActionEvent) =
super.actionPerformed(e).also {
Finder.backwardsMode()
Finder.search(ALL_WORDS)
}
}
56 changes: 53 additions & 3 deletions src/main/kotlin/com/johnlindquist/acejump/search/Finder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.editor.markup.HighlighterLayer
import com.intellij.openapi.editor.markup.HighlighterTargetArea.EXACT_RANGE
import com.intellij.openapi.editor.markup.RangeHighlighter
import com.intellij.openapi.editor.Editor
import com.johnlindquist.acejump.control.Handler
import com.johnlindquist.acejump.control.Trigger
import com.johnlindquist.acejump.label.Pattern
Expand All @@ -22,6 +23,41 @@ import java.util.*
import kotlin.system.measureTimeMillis
import kotlin.text.RegexOption.MULTILINE

interface FinderBoundry {
fun getStart() : Int;
fun getEnd() : Int;
}

class FullFileBoundry : FinderBoundry {
override fun getStart() : Int = max(0, viewBounds.first - 20000)
override fun getEnd() : Int = min(viewBounds.last + 20000, editorText.length)
}

class ScreenBoundry : FinderBoundry {
override fun getStart() : Int = max(0, viewBounds.first)
override fun getEnd() : Int = min(viewBounds.last, editorText.length)
}

class BeforeCurserBoundry : FinderBoundry {
override fun getStart() : Int = max(0, viewBounds.first)
override fun getEnd() : Int {

var offset = editor.getCaretModel().getOffset()

return min( offset - 1, min(viewBounds.last, editorText.length) )
}
}

class AfterCurserBoundry : FinderBoundry {
override fun getStart() : Int {
var offset = editor.getCaretModel().getOffset()

return max(offset + 1, max(0, viewBounds.first))
}
override fun getEnd() : Int = min(viewBounds.last, editorText.length)
}


/**
* Singleton that searches for text in editor and highlights matching results.
*
Expand All @@ -37,6 +73,8 @@ object Finder : Resettable {
private var HIGHLIGHT_LAYER = HighlighterLayer.LAST + 1
private val logger = Logger.getInstance(Finder::class.java)
var isShiftSelectEnabled = false

private var boundries : FinderBoundry = FullFileBoundry()

var skim = false
private set
Expand Down Expand Up @@ -96,6 +134,18 @@ object Finder : Resettable {
})
}

fun backwardsMode() {
boundries = BeforeCurserBoundry()
}

fun forwardMode() {
boundries = AfterCurserBoundry()
}

fun fullFileMode() {
boundries = FullFileBoundry()
}

fun search(model: FindModel = FindModel().apply { stringToFind = query }) {
measureTimeMillis {
results = editorText.findMatchingSites(model).toSortedSet()
Expand Down Expand Up @@ -167,8 +217,8 @@ object Finder : Resettable {
generateSequence({ Regex(key, MULTILINE).find(this, start) },
Finder::filterNextResult).map { it.range.first }

private fun getStartBound() = max(0, viewBounds.first - 20000)
private fun getEndBound() = min(viewBounds.last + 20000, editorText.length)
private fun getStartBound() = boundries.getStart()
private fun getEndBound() = boundries.getEnd()

private tailrec fun filterNextResult(result: MatchResult): MatchResult? {
val next = result.next()
Expand All @@ -190,4 +240,4 @@ object Finder : Resettable {
textHighlights = listOf()
viewHighlights = listOf()
}
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/com/johnlindquist/acejump/view/Model.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,4 @@ object Model {
naturalColor = colorsScheme.getColor(CARET_COLOR) ?: BLACK
colorsScheme.setColor(CARET_COLOR, AceConfig.settings.jumpModeColor)
}
}
}
17 changes: 16 additions & 1 deletion src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,20 @@
<keyboard-shortcut keymap="Mac OS X 10.5+" first-keystroke="ctrl alt SEMICOLON"/>
<keyboard-shortcut keymap="$default" first-keystroke="ctrl alt SEMICOLON"/>
</action>
<action id="AceWordAction"
class="com.johnlindquist.acejump.control.AceWordAction"
text="Start in Word Mode"
description="Searches for all words on the screen in AceJump">
</action>
<action id="AceWordForwardAction"
class="com.johnlindquist.acejump.control.AceWordForwardAction"
text="Start in Word Mode only searching after the cursor"
description="Searches for all words after the cursor in AceJump">
</action>
<action id="AceWordBackwardsAction"
class="com.johnlindquist.acejump.control.AceWordBackwardsAction"
text="Start in Word Mode only searching before the cursor"
description="Searches for all words before the cursor in AceJump">
</action>
</actions>
</idea-plugin>
</idea-plugin>

0 comments on commit 0743147

Please sign in to comment.