Skip to content

Commit

Permalink
Merge branch 'AlexPl292-IdeaVim-EasyMotion'
Browse files Browse the repository at this point in the history
  • Loading branch information
breandan committed Oct 18, 2019
2 parents 01e7ce0 + 10fdada commit c4d1dde
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
24 changes: 24 additions & 0 deletions src/main/kotlin/org/acejump/control/Handler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.intellij.openapi.editor.actionSystem.*
import com.intellij.openapi.editor.colors.EditorColors.CARET_COLOR
import com.intellij.openapi.editor.colors.EditorColors.TEXT_SEARCH_RESULT_ATTRIBUTES
import com.intellij.openapi.editor.colors.EditorColorsManager
import com.intellij.util.containers.ContainerUtil
import org.acejump.control.Scroller.restoreScroll
import org.acejump.control.Scroller.saveScroll
import org.acejump.label.Pattern
Expand All @@ -27,6 +28,7 @@ import java.awt.Color
*/

object Handler : TypedActionHandler, Resettable {
private val listeners: MutableList<AceJumpListener> = ContainerUtil.createLockFreeCopyOnWriteList()
private val logger = Logger.getInstance(Handler::class.java)
private var enabled = false
private val typingAction = EditorActionManager.getInstance().typedAction
Expand All @@ -50,9 +52,14 @@ object Handler : TypedActionHandler, Resettable {
override fun doExecute(e: Editor, c: Caret?, dc: DataContext?) = handle()
}

@ExternalUsage
fun regexSearch(regex: Pattern, bounds: Boundary = FULL_FILE_BOUNDARY) =
Canvas.reset().also { Finder.search(regex, bounds) }

@ExternalUsage
fun customRegexSearch(regex: String, bounds: Boundary = FULL_FILE_BOUNDARY) =
Canvas.reset().also { Finder.search(regex, bounds) }

fun activate() = if (!enabled) configureEditor() else { }

private fun clear() {
Expand Down Expand Up @@ -111,10 +118,23 @@ object Handler : TypedActionHandler, Resettable {
override fun reset() {
if (enabled) Listener.disable()

// In order to get Finder.query value, listeners should
// be placed before cleanup
listeners.forEach(AceJumpListener::finished)
clear()
editor.restoreSettings()
}

@ExternalUsage
fun addAceJumpListener(listener: AceJumpListener) {
listeners += listener
}

@ExternalUsage
fun removeAceJumpListener(listener: AceJumpListener) {
listeners -= listener
}

private fun Editor.restoreSettings() = runNow {
enabled = false
swapActionHandler()
Expand Down Expand Up @@ -163,4 +183,8 @@ object Handler : TypedActionHandler, Resettable {
.apply { backgroundColor = Model.naturalHighlight })
}
}

interface AceJumpListener {
fun finished() {}
}
}
11 changes: 10 additions & 1 deletion src/main/kotlin/org/acejump/search/AceUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,13 @@ fun Editor.normalizeLine(line: Int) = max(0, min(line, getLineCount() - 1))

fun Editor.normalizeOffset(line: Int, offset: Int, allowEnd: Boolean) =
if (getFileSize(allowEnd) == 0) 0 else
max(min(offset, getLineEndOffset(line, allowEnd)), getLineStartOffset(line))
max(min(offset, getLineEndOffset(line, allowEnd)), getLineStartOffset(line))

/**
* This annotation is a marker which means that the annotated function is
* used in external plugins.
*/

@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.FUNCTION)
annotation class ExternalUsage
22 changes: 21 additions & 1 deletion src/main/kotlin/org/acejump/search/Finder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,38 @@ object Finder : Resettable {

fun search(pattern: Pattern, bounds: Boundary = FULL_FILE_BOUNDARY) {
logger.info("Searching for regular expression: ${pattern.name} in $bounds")
search(pattern.string, bounds)
}

fun search(pattern: String, bounds: Boundary = FULL_FILE_BOUNDARY) {
boundaries = bounds
// TODO: Fix this broken reset
reset()
Tagger.reset()
search(AceFindModel(pattern.string, true))
search(AceFindModel(pattern, true))
}

fun search(model: AceFindModel = AceFindModel(query)) {
measureTimeMillis {
results = Scanner.findMatchingSites(editorText, model, results)
}.let { logger.info("Found ${results.size} matching sites in $it ms") }

markResults(results, model)
}

/**
* This method should not be inlined because it's used in IdeaVim integration plugin
*
* The default model is like that because if this function was called from
* the outer scope, it means that [results] are already collected and
* AceFindModel should be empty. Additionally, if
* AceFindModes.isRegex == true, only one symbol is highlighted in document.
*/
@ExternalUsage
fun markResults(
results: SortedSet<Int>,
model: AceFindModel = AceFindModel("", true)
) {
if (!skim) tag(model, results)
markup(Tagger.markers, model)
}
Expand Down
21 changes: 21 additions & 0 deletions src/main/kotlin/org/acejump/view/Boundary.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,26 @@ enum class Boundary: ClosedRange<Int> {
get() = max(editor.caretModel.offset, viewBounds.first)
override val endInclusive: Int
get() = viewBounds.last
},
// Search on the current line
CURRENT_LINE_BOUNDARY {
override val start: Int
get() = editor.document.getLineStartOffset(editor.caretModel.logicalPosition.line)
override val endInclusive: Int
get() = editor.document.getLineEndOffset(editor.caretModel.logicalPosition.line)
},
// Search after caret within line
CURRENT_LINE_AFTER_CARET_BOUNDARY {
override val start: Int
get() = max(editor.caretModel.offset, viewBounds.first)
override val endInclusive: Int
get() = editor.document.getLineEndOffset(editor.caretModel.logicalPosition.line)
},
// Search before caret within line
CURRENT_LINE_BEFORE_CARET_BOUNDARY {
override val start: Int
get() = editor.document.getLineStartOffset(editor.caretModel.logicalPosition.line)
override val endInclusive: Int
get() = min(editor.caretModel.offset, viewBounds.last)
}
}

0 comments on commit c4d1dde

Please sign in to comment.