1
0
mirror of https://github.com/chylex/IntelliJ-AceJump.git synced 2025-04-09 17:15:43 +02:00
This commit is contained in:
breandan.considine 2017-07-24 02:37:44 -04:00
parent fb6f4acce5
commit 81e19c4c7b
6 changed files with 87 additions and 13 deletions
src/main/kotlin/com/johnlindquist/acejump

View File

@ -1,6 +1,5 @@
package com.johnlindquist.acejump.control
import com.intellij.find.EditorSearchSession
import com.intellij.find.FindModel
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.CustomShortcutSet
@ -16,7 +15,6 @@ import com.johnlindquist.acejump.search.Skipper.storeScroll
import com.johnlindquist.acejump.view.Canvas
import com.johnlindquist.acejump.view.Model
import com.johnlindquist.acejump.view.Model.editor
import com.johnlindquist.acejump.view.Model.project
import com.johnlindquist.acejump.view.Model.setupCursor
import com.johnlindquist.acejump.view.Model.viewBounds
import java.awt.event.KeyEvent.*
@ -44,9 +42,9 @@ object Handler {
VK_TAB to { Skipper.doesQueryExistIfSoSkipToIt(!isShiftDown) }
)
private fun findOrDropLast(key: String) =
private fun findOrDropLast() =
if (!Finder.isQueryDeadEnd(text)) {
find(key)
find(text)
} else {
text = text.dropLast(1)
}
@ -74,6 +72,7 @@ object Handler {
private fun processBackspaceCommand() {
text = ""
Finder.reset()
Searcher.discard()
updateUIState()
}
@ -83,7 +82,7 @@ object Handler {
if (text.length < 2) {
find(text, skim = true)
Trigger.restart(400L) { find(text, skim = false) }
} else findOrDropLast(text)
} else findOrDropLast()
}
private fun configureEditor() =
@ -128,7 +127,6 @@ object Handler {
Jumper.hasJumped = false
reset()
} else {
EditorSearchSession.start(editor, Finder.findModel, project)
Canvas.jumpLocations = Finder.markers
Canvas.repaint()
}
@ -153,6 +151,7 @@ object Handler {
enabled = false
text = ""
Finder.reset()
Searcher.discard()
editor.restoreSettings()
}

View File

@ -45,7 +45,7 @@ internal object Listener : CaretListener, FocusListener, AncestorListener,
Trigger.restart(delay = (750L - elapsed).coerceAtLeast(0L)) { redoFind() }
}
private fun canTagsSurviveViewResize(): Boolean =
private fun canTagsSurviveViewResize() =
editor.getView().run {
if (first in viewBounds && last in viewBounds) return true
else if (Finder.isRegex) return false

View File

@ -41,8 +41,8 @@ object Finder {
var skim = true
fun findOrJump(findModel: FindModel, skim: Boolean = false) {
this.findModel = findModel
if (!isRegex) isRegex = findModel.isRegularExpressions
this.findModel = findModel
this.skim = skim
origQ = findModel.stringToFind
@ -144,8 +144,7 @@ object Finder {
// Provides a way to short-circuit the full text search if a match is found
private operator fun String.contains(key: String) =
if (textMatches.isEmpty()) findMatchingSites(key).any()
else textMatches.any { regionMatches(it, key, 0, key.length) }
textMatches.any { regionMatches(it, key, 0, key.length) }
/**
* Builds a map of all existing bigrams, starting from the index of the last
@ -330,4 +329,7 @@ object Finder {
computeMarkers()
return textMatches.isEmpty() && markers.isEmpty()
}
fun hasTagsStartingWithChar(c: Char) = tagMap.any { it.key.startsWith(c.toLowerCase()) }
fun hasTagsAtIndex(i: Int) = tagMap.containsValue(i)
}

View File

@ -0,0 +1,68 @@
package com.johnlindquist.acejump.search
import com.intellij.find.FindModel
import com.intellij.find.FindResult
import com.intellij.find.impl.livePreview.LivePreviewController
import com.intellij.find.impl.livePreview.SearchResults
import com.intellij.openapi.Disposable
import com.intellij.openapi.editor.colors.EditorColors.TEXT_SEARCH_RESULT_ATTRIBUTES
import com.johnlindquist.acejump.view.Model.editor
import com.johnlindquist.acejump.view.Model.naturalHighlight
import com.johnlindquist.acejump.view.Model.project
import java.awt.Color.GREEN
object Searcher : Disposable {
var searchResults: SearchResults? = null
private var occurencesInView: List<FindResult>? = null
override fun dispose() {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
private lateinit var livePreviewController: LivePreviewController
fun search(findModel: FindModel) {
if (searchResults == null) init()
searchResults?.run {
if (occurencesInView == null)
occurencesInView = occurrences.filter { it.startOffset in editor.getView() }
else {
val partitioned = occurencesInView!!.partition { Finder.hasTagsAtIndex(it.startOffset) }
partitioned.second.forEach { exclude(it) }
occurencesInView = partitioned.first
}
}
if (Finder.hasTagsStartingWithChar(findModel.stringToFind.last())) return
livePreviewController.on()
}
private fun init() {
searchResults = SearchResults(editor, project)
editor.colorsScheme.run {
setAttributes(TEXT_SEARCH_RESULT_ATTRIBUTES,
getAttributes(TEXT_SEARCH_RESULT_ATTRIBUTES)
.apply { backgroundColor = GREEN })
}
livePreviewController = LivePreviewController(searchResults, null, this)
livePreviewController.userActivityDelay = 0
livePreviewController.on()
}
fun discard() {
searchResults?.clear()
editor.colorsScheme.run {
setAttributes(TEXT_SEARCH_RESULT_ATTRIBUTES,
getAttributes(TEXT_SEARCH_RESULT_ATTRIBUTES)
.apply { backgroundColor = naturalHighlight })
}
livePreviewController.off()
searchResults = null
occurencesInView = null
}
}

View File

@ -54,7 +54,8 @@ class Marker(val query: String, val tag: String?, val index: Int) {
?.apply { Canvas.registerTag(this, tag) }
?.let { highlightTag(it); drawTagForeground(it) }
// highlightText()
if (Finder.isRegex)
highlightText()
}
private fun Graphics2D.highlightText() {
@ -119,7 +120,7 @@ class Marker(val query: String, val tag: String?, val index: Int) {
composite = getInstance(SRC_OVER, 0.40.toFloat())
color = settings.textHighlightColor
if (tag != null && lastQueryChar == tag.first() && lastQueryChar != textChar) {
if (tag != null && lastQueryChar == tag.first() && lastQueryChar != textChar) {
fillRoundRect(tagX!!, yPosition, fontWidth, rectHeight, arcD, arcD)
tagX += fontWidth
tagWidth -= fontWidth

View File

@ -2,6 +2,7 @@ package com.johnlindquist.acejump.view
import com.intellij.openapi.editor.Editor
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.getInstance
import com.intellij.openapi.editor.colors.EditorColorsScheme
import com.intellij.openapi.editor.ex.EditorSettingsExternalizable
@ -44,9 +45,12 @@ object Model {
get() = editor.project
var editorText = editor.document.text.toLowerCase()
var globalScheme = getInstance().globalScheme
var naturalBlock = EditorSettingsExternalizable.getInstance().isBlockCursor
var naturalBlink = EditorSettingsExternalizable.getInstance().isBlinkCaret
var naturalColor = getInstance().globalScheme.getColor(CARET_COLOR) ?: BLACK
var naturalColor = globalScheme.getColor(CARET_COLOR) ?: BLACK
var naturalHighlight = globalScheme.getAttributes(TEXT_SEARCH_RESULT_ATTRIBUTES).backgroundColor
val targetModeStyle = TextAttributes(null, null, RED, BOXED, Font.PLAIN)
val highlightStyle = TextAttributes(null, GREEN, GREEN, BOXED, Font.PLAIN)