Skip to content

Commit

Permalink
pass back necessary info instead of reconstructing
Browse files Browse the repository at this point in the history
Merge branch 'listener_update' of https://github.com/AlexPl292/AceJump into AlexPl292-listener_update

# Conflicts:
#	src/test/kotlin/ExternalUsageTest.kt
  • Loading branch information
breandan committed Apr 17, 2021
1 parent b3df72e commit 4622fbb
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 21 deletions.
2 changes: 0 additions & 2 deletions src/main/kotlin/org/acejump/action/AceEditorAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.editor.Caret
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.actionSystem.EditorActionHandler
import org.acejump.boundaries.StandardBoundaries
import org.acejump.boundaries.StandardBoundaries.*
import org.acejump.search.Pattern
import org.acejump.search.Pattern.*
import org.acejump.session.Session
import org.acejump.session.SessionManager
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/org/acejump/search/Tagger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ internal class Tagger(private val editor: Editor) {

if (!isRegex) {
for (entry in tagMap.entries)
if (entry solves queryText) return TaggingResult.Jump(entry.value)
if (entry solves queryText)
return TaggingResult.Jump(query = queryText.substringBefore(entry.key), tag = entry.key, offset = entry.value)

if (queryText.length == 1) removeResultsWithOverlappingTags(results)
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/org/acejump/search/TaggingResult.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.acejump.search

import org.acejump.view.Tag

internal sealed class TaggingResult {
class Jump(val offset: Int): TaggingResult()
sealed class TaggingResult {
class Jump(val query: String, val tag: String, val offset: Int): TaggingResult()
class Mark(val tags: List<Tag>): TaggingResult()
}
2 changes: 1 addition & 1 deletion src/main/kotlin/org/acejump/session/AceJumpListener.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.acejump.session

interface AceJumpListener {
fun finished()
fun finished(mark: String?, query: String?)
}
16 changes: 10 additions & 6 deletions src/main/kotlin/org/acejump/session/Session.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import org.acejump.action.TagJumper
import org.acejump.action.TagVisitor
import org.acejump.boundaries.Boundaries
import org.acejump.boundaries.EditorOffsetCache
import org.acejump.boundaries.StandardBoundaries
import org.acejump.boundaries.StandardBoundaries.*
import org.acejump.config.AceConfig
import org.acejump.input.EditorKeyListener
Expand Down Expand Up @@ -126,7 +125,7 @@ class Session(private val editor: Editor) {
is TaggingResult.Jump -> {
tagJumper.jump(result.offset, shiftMode)
tagCanvas.removeMarkers()
end()
end(result)
}

is TaggingResult.Mark -> {
Expand Down Expand Up @@ -217,12 +216,14 @@ class Session(private val editor: Editor) {
/**
* See [TagVisitor.visitNext]. If there are no tags, nothing happens.
*/
fun visitNextTag() = if (tagVisitor?.visitNext() == true) end() else Unit
fun visitNextTag() =
if (tagVisitor?.visitNext() == true) end() else Unit

/**
* Ends this session.
*/
fun end() = SessionManager.end(editor)
fun end(taggingResult: TaggingResult? = null) =
SessionManager.end(editor, taggingResult)

/**
* Clears any currently active search, tags, and highlights.
Expand All @@ -239,13 +240,16 @@ class Session(private val editor: Editor) {
* Should only be used from [SessionManager] to dispose a
* successfully ended session.
*/
internal fun dispose() {
internal fun dispose(taggingResult: TaggingResult?) {
tagger = Tagger(editor)
EditorKeyListener.detach(editor)
tagCanvas.unbind()
textHighlighter.reset()
EditorCache.invalidate()
listeners.forEach(AceJumpListener::finished)

val (tag, query) = (taggingResult as TaggingResult.Jump?)
.let { it?.tag to it?.query }
listeners.forEach { it.finished(tag, query) }

if (!editor.isDisposed) {
originalSettings.restore(editor)
Expand Down
6 changes: 4 additions & 2 deletions src/main/kotlin/org/acejump/session/SessionManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.acejump.session

import com.intellij.openapi.editor.Editor
import org.acejump.ExternalUsage
import org.acejump.search.TaggingResult

/**
* Manages active [Session]s in [Editor]s. There may only be
Expand Down Expand Up @@ -35,8 +36,9 @@ object SessionManager {
* Ends the active [Session] in the specified [Editor],
* or does nothing if the [Editor] has no active session.
*/
fun end(editor: Editor) = sessions.remove(editor)?.dispose() ?: Unit
fun end(editor: Editor, taggingResult: TaggingResult?) =
sessions.remove(editor)?.dispose(taggingResult) ?: Unit

private fun cleanup() = sessions.keys.filter { it.isDisposed }
.forEach { disposedEditor -> sessions.remove(disposedEditor)?.dispose() }
.forEach { disposedEditor -> sessions.remove(disposedEditor)?.dispose(null) }
}
2 changes: 1 addition & 1 deletion src/main/kotlin/org/acejump/view/Tag.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import kotlin.math.max
/**
* Describes a 1 or 2 character shortcut that points to a specific character in the editor.
*/
internal class Tag(
class Tag(
private val tag: String,
val offsetL: Int,
val offsetR: Int,
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/org/acejump/view/TagFont.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import java.awt.FontMetrics
/**
* Stores font metrics for aligning and rendering [Tag]s.
*/
internal class TagFont(editor: Editor) {
class TagFont(editor: Editor) {
val tagFont: Font = editor.colorsScheme.getFont(BOLD)
val tagCharWidth = editor.component.getFontMetrics(tagFont).charWidth('W')

Expand Down
69 changes: 64 additions & 5 deletions src/test/kotlin/ExternalUsageTest.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import com.intellij.openapi.editor.Editor
import com.intellij.util.ui.UIUtil
import junit.framework.TestCase
import org.acejump.boundaries.Boundaries
import org.acejump.boundaries.EditorOffsetCache
import org.acejump.action.AceAction
import org.acejump.boundaries.*
import org.acejump.boundaries.StandardBoundaries.WHOLE_FILE
import org.acejump.input.JumpMode
import org.acejump.search.Pattern.ALL_WORDS
import org.acejump.session.AceJumpListener
import org.acejump.session.SessionManager
import org.acejump.session.*
import org.acejump.test.util.BaseTest

/**
Expand All @@ -24,7 +24,7 @@ class ExternalUsageTest: BaseTest() {

var shouldBeTrueAfterFinished = false
session.addAceJumpListener(object: AceJumpListener {
override fun finished() {
override fun finished(mark: String?, query: String?) {
shouldBeTrueAfterFinished = true
}
})
Expand Down Expand Up @@ -71,4 +71,63 @@ class ExternalUsageTest: BaseTest() {
TestCase.assertEquals(1, session.tags.size)
TestCase.assertEquals(14, session.tags.single().value)
}

fun `test listener query and mark`() {
"<caret>testing 1234".search("g")

var detectedMark: String? = null
var detectedQuery: String? = null
session.addAceJumpListener(object: AceJumpListener {
override fun finished(mark: String?, query: String?) {
detectedMark = mark
detectedQuery = query
}
})

val mark = session.tags[0].key
typeAndWaitForResults(mark)

TestCase.assertEquals(mark, detectedMark)
TestCase.assertEquals("g", detectedQuery)
}

fun `test listener after escape`() {
"<caret>testing 1234".search("g")

var detectedMark: String? = null
var detectedQuery: String? = null
session.addAceJumpListener(object: AceJumpListener {
override fun finished(mark: String?, query: String?) {
detectedMark = mark
detectedQuery = query
}
})

myFixture.performEditorAction("EditorEscape")
UIUtil.dispatchAllInvocationEvents()

TestCase.assertEquals(null, detectedMark)
TestCase.assertEquals(null, detectedQuery)
}

fun `test listener for word motion`() {
makeEditor("test word action")

takeAction(AceAction.StartAllWordsMode())

var detectedMark: String? = null
var detectedQuery: String? = null
session.addAceJumpListener(object: AceJumpListener {
override fun finished(mark: String?, query: String?) {
detectedMark = mark
detectedQuery = query
}
})

val mark = session.tags[1].key
typeAndWaitForResults(mark)

TestCase.assertEquals(mark, detectedMark)
TestCase.assertEquals("", detectedQuery)
}
}

0 comments on commit 4622fbb

Please sign in to comment.