mirror of
https://github.com/chylex/IntelliJ-AceJump.git
synced 2025-04-09 17:15:43 +02:00
refactoring for readability
This commit is contained in:
parent
bf4e83a82b
commit
3b1d3fb816
src/main/kotlin/com/johnlindquist/acejump
@ -40,7 +40,7 @@ object Handler {
|
||||
VK_ESCAPE to { reset() },
|
||||
VK_BACK_SPACE to { processBackspaceCommand() },
|
||||
VK_ENTER to { Tagger.maybeJumpIfJustOneTagRemains() },
|
||||
VK_TAB to { Skipper.doesQueryExistIfSoSkipToIt(!isShiftDown) }
|
||||
VK_TAB to { Skipper.ifQueryExistsSkipAhead(!isShiftDown) }
|
||||
)
|
||||
|
||||
fun activate() = runNow { if (!enabled) start() else toggleTargetMode() }
|
||||
|
@ -59,7 +59,7 @@ fun Editor.isFirstCharacterOfLine(index: Int) =
|
||||
index == getLineStartOffset(offsetToLogicalPosition(index).line)
|
||||
|
||||
fun Editor.getView(): IntRange {
|
||||
val firstVisibleLine = getVisualLineAtTopOfScreen()
|
||||
val firstVisibleLine = max(0, getVisualLineAtTopOfScreen() - 1)
|
||||
val firstLine = visualLineToLogicalLine(firstVisibleLine)
|
||||
val startOffset = getLineStartOffset(firstLine)
|
||||
|
||||
@ -170,9 +170,6 @@ fun Editor.getLineStartOffset(line: Int) =
|
||||
else -> document.getLineStartOffset(line)
|
||||
}
|
||||
|
||||
fun Editor.getLineStartOffsetForOffset(offset: Int) =
|
||||
getLineStartOffset(offsetToLogicalPosition(offset).line)
|
||||
|
||||
/**
|
||||
* Returns the offset of the end of the requested line.
|
||||
*
|
||||
|
@ -36,9 +36,12 @@ object Finder {
|
||||
var query: String = ""
|
||||
set(value) {
|
||||
field = value.toLowerCase()
|
||||
|
||||
if (value.isEmpty()) return
|
||||
if (value.length == 1) skim() else searchForQueryOrDropLastCharacter()
|
||||
when {
|
||||
value.isEmpty() -> return
|
||||
value.length == 1 -> skim()
|
||||
value.isValidQuery() -> search()
|
||||
else -> field = field.dropLast(1)
|
||||
}
|
||||
}
|
||||
|
||||
private fun skim() {
|
||||
@ -132,9 +135,6 @@ object Finder {
|
||||
else it.range.first
|
||||
}
|
||||
|
||||
private fun searchForQueryOrDropLastCharacter() =
|
||||
if (query.isValidQuery()) search() else query = query.dropLast(1)
|
||||
|
||||
private fun String.isValidQuery() =
|
||||
results.any { editorText.regionMatches(it, this, 0, length) } ||
|
||||
Tagger.hasTagSuffix(query)
|
||||
|
@ -20,7 +20,7 @@ object Skipper {
|
||||
private var scrollX = 0
|
||||
private var scrollY = 0
|
||||
|
||||
fun doesQueryExistIfSoSkipToIt(isNext: Boolean = true): Boolean {
|
||||
fun ifQueryExistsSkipAhead(isNext: Boolean = true): Boolean {
|
||||
val position = if (isNext) findNextPosition() ?: return false
|
||||
else findPreviousPosition() ?: return false
|
||||
editor.scrollingModel.disableAnimation()
|
||||
|
@ -57,21 +57,17 @@ object Tagger {
|
||||
tagMap.entries.firstOrNull()?.run { Jumper.jump(value) }
|
||||
|
||||
fun markTags() {
|
||||
|
||||
computeMarkers()
|
||||
|
||||
if (markers.size > 1 || query.length < 2) return
|
||||
|
||||
if (markers.isEmpty()) Skipper.doesQueryExistIfSoSkipToIt()
|
||||
if (markers.isEmpty() && query.length > 1) Skipper.ifQueryExistsSkipAhead()
|
||||
}
|
||||
|
||||
private fun giveJumpOpportunity() {
|
||||
private fun giveJumpOpportunity() =
|
||||
tagMap.forEach {
|
||||
if (query.endsWith(it.key)) {
|
||||
return Jumper.jump(it.value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun allBigrams() = settings.allowedChars.run { flatMap { e -> map { c -> "$e$c" } } }
|
||||
|
||||
@ -89,7 +85,6 @@ object Tagger {
|
||||
.map { (tag, index) -> Marker(query, tag, index) }
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Shortens assigned tags. Effectively, this will only shorten two-character
|
||||
* tags to one-character tags. This will happen if and only if:
|
||||
@ -171,7 +166,7 @@ object Tagger {
|
||||
* Iterates through the remaining available tags, until we find one that
|
||||
* matches our criteria, i.e. does not collide with an existing tag or
|
||||
* plaintext string. To have the desired behavior, this has a surprising
|
||||
* number of edge cases and irregularities that must explicitly prevented.
|
||||
* number of edge cases that must explicitly prevented.
|
||||
*
|
||||
* @param idx the index which a tag is to be assigned
|
||||
*/
|
||||
@ -186,12 +181,7 @@ object Tagger {
|
||||
if (hasNearbyTag(idx)) return
|
||||
|
||||
val (matching, nonMatching) = availableTags.partition { tag ->
|
||||
// Prevents a situation where some sites couldn't be assigned last time
|
||||
!newTags.containsKey("${tag[0]}") &&
|
||||
((idx + 1)..min(right, editorText.length)).map {
|
||||
// Never use a tag which can be partly completed by typing plaintext
|
||||
editorText.substring(idx, it) + tag[0]
|
||||
}.none { it in editorText }
|
||||
!newTags.containsKey("${tag[0]}") && !tag.collidesWithText(idx, right)
|
||||
}
|
||||
|
||||
val tag = matching.firstOrNull()
|
||||
@ -209,14 +199,6 @@ object Tagger {
|
||||
}
|
||||
}
|
||||
|
||||
query.run {
|
||||
if (isNotEmpty())
|
||||
substring(max(0, length - 2)).let {
|
||||
if (it in tagMap && it.length < length)
|
||||
return HashBiMap.create(mapOf(it to tagMap[it]))
|
||||
}
|
||||
}
|
||||
|
||||
newTags.run { if (regex && isNotEmpty() && values.allInView) return this }
|
||||
|
||||
sortValidJumpTargets(digraphs).forEach {
|
||||
@ -297,4 +279,20 @@ object Tagger {
|
||||
fun hasTagSuffix(query: String) = tagMap.any { query overlaps it.key }
|
||||
infix fun String.overlaps(xx: String) = endsWith(xx.first()) || endsWith(xx)
|
||||
infix fun canDiscard(i: Int) = !(Finder.skim || tagMap.containsValue(i))
|
||||
|
||||
/**
|
||||
* Returns true IFF the receiver, inserted between the left and right indices,
|
||||
* matches an existing substring elsewhere in the editor text. We should never
|
||||
* use a tag which can be partly completed by typing plaintext, where the tag
|
||||
* is the receiver, the tag index is the leftIndex, and rightIndex is the last
|
||||
* character we care about (this is usually the last letter of the same word).
|
||||
*
|
||||
* @param leftIndex index where a tag is to be used
|
||||
* @param rightIndex index of last character (ie. end of the word)
|
||||
*/
|
||||
|
||||
private fun String.collidesWithText(leftIndex: Int, rightIndex: Int) =
|
||||
((leftIndex + 1)..min(rightIndex, editorText.length)).map {
|
||||
editorText.substring(leftIndex, it) + this[0]
|
||||
}.any { it in editorText }
|
||||
}
|
Loading…
Reference in New Issue
Block a user