Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize rendering by bulk-adding text highlights and reducing unnecessary work #341

Merged
merged 2 commits into from
Nov 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions src/main/kotlin/org/acejump/search/Finder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.acejump.view.Boundary.FULL_FILE_BOUNDARY
import org.acejump.view.Marker
import org.acejump.view.Model.LONG_DOCUMENT
import org.acejump.view.Model.boundaries
import org.acejump.view.Model.editor
import org.acejump.view.Model.editorText
import org.acejump.view.Model.markup
import org.acejump.view.Model.viewBounds
Expand Down Expand Up @@ -125,17 +126,25 @@ object Finder : Resettable {
* [com.intellij.openapi.editor.markup.MarkupModel].
*/

fun markup(markers: Set<Int> = results, isRegexQuery: Boolean = false) =
fun markup(markers: Set<Int> = results, isRegexQuery: Boolean = false) {
if (markers.isEmpty()) {
return
}

runLater {
markers.ifEmpty { return@runLater }
val highlightLen = if (isRegexQuery) 1 else query.length

editor.document.isInBulkUpdate = true
textHighlights.forEach { markup.removeHighlighter(it) }

textHighlights = markers.map {
val start = it - if (it == editorText.length) 1 else 0
val end = start + if (isRegexQuery) 1 else query.length
val end = start + highlightLen
createTextHighlight(max(start, 0), min(end, editorText.length - 1))
}
editor.document.isInBulkUpdate = false
}
}

private fun createTextHighlight(start: Int, end: Int) =
markup.addRangeHighlighter(start, end, HIGHLIGHT_LAYER, null, EXACT_RANGE)
Expand Down Expand Up @@ -163,13 +172,18 @@ object Finder : Resettable {
if (numDiscarded != 0) logger.info("Discarded $numDiscarded highlights")
}

fun List<RangeHighlighter>.eraseIf(cond: RangeHighlighter.() -> Boolean) =
filter {
if (cond(it)) {
runLater { markup.removeHighlighter(it) }
false
} else true
fun List<RangeHighlighter>.eraseIf(cond: RangeHighlighter.() -> Boolean): List<RangeHighlighter> {
val (erased, kept) = partition(cond)

if (erased.isNotEmpty()) {
runLater {
editor.document.isInBulkUpdate = true
erased.forEach { markup.removeHighlighter(it) }
editor.document.isInBulkUpdate = false
}
}
return kept
}

fun visibleResults() = results.filter { it in viewBounds }

Expand All @@ -186,7 +200,11 @@ object Finder : Resettable {
}

override fun reset() {
runLater { markup.removeAllHighlighters() }
runLater {
editor.document.isInBulkUpdate = true
markup.removeAllHighlighters()
editor.document.isInBulkUpdate = false
}
query = ""
skim = false
results = sortedSetOf()
Expand Down
46 changes: 28 additions & 18 deletions src/main/kotlin/org/acejump/view/Marker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Marker {
val index: Int
private val query: String
val tag: String?
private val tagUpperCase: String?
private var srcPoint: Point
private var queryLength: Int
private var trueOffset: Int
Expand All @@ -52,6 +53,7 @@ class Marker {
constructor(query: String, tag: String?, index: Int) {
this.query = query
this.tag = tag
this.tagUpperCase = tag?.toUpperCase()
this.index = index
val lastQueryChar = query.last()
endsWith = tag != null && lastQueryChar == tag[0]
Expand All @@ -62,7 +64,7 @@ class Marker {

var i = 1
while (i < query.length && index + i < text.length &&
query[i].toLowerCase() == text[index + i].toLowerCase()) i++
query[i].equals(text[index + i], ignoreCase = true)) i++
trueOffset = i - 1
queryLength = i

Expand Down Expand Up @@ -109,45 +111,53 @@ class Marker {
setRenderingHint(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON)
color = AceConfig.textHighlightColor
val start = editor.getPoint(highlighter.startOffset)
val queryLength = highlighter.endOffset - highlighter.startOffset
val searchWidth = if (regex) 0 else queryLength * fontWidth
val startY = start.y + rectVOffset

fun highlightRegex() {
composite = getInstance(SRC_OVER, 0.40.toFloat())
fillRoundRect(start.x, startY, fontWidth, rectHeight, arcD, arcD)
if (regex) {
highlightRegex(start.x, startY)
}

if (regex) highlightRegex()
else {
val queryLength = highlighter.endOffset - highlighter.startOffset
val searchWidth = queryLength * fontWidth
fillRoundRect(start.x, startY, searchWidth, rectHeight, arcD, arcD)
if (JumpMode.equals(TARGET)) surroundTargetWord(highlighter.startOffset, startY)
if (JumpMode.equals(TARGET)) {
surroundTargetWord(highlighter.startOffset, startY)
}
}

if (highlighter.startOffset == Selector.nearestVisibleMatches().firstOrNull()) {
color = naturalCaretColor
drawLine(start.x, startY, start.x, startY + rectHeight)
}
// TODO this is a lag-fest, results should be cached before this gets turned back on
//if (highlighter.startOffset == Selector.nearestVisibleMatches().firstOrNull()) {
// color = naturalCaretColor
// drawLine(start.x, startY, start.x, startY + rectHeight)
//}
}

private fun Graphics2D.highlightRegex(x: Int, y: Int) {
composite = getInstance(SRC_OVER, 0.40.toFloat())
fillRoundRect(x, y, fontWidth, rectHeight, arcD, arcD)
}

private fun Graphics2D.surroundTargetWord(index: Int, startY: Int) {
if (!text[index].isLetterOrDigit()) {
return
}

fun Graphics2D.surroundTargetWord(index: Int, startY: Int) {
color = AceConfig.targetModeColor
val (wordStart, wordEnd) = text.wordBounds(index)

val xPos = editor.getPoint(wordStart).x
val wordWidth = (wordEnd - wordStart) * fontWidth

if (text[index].isLetterOrDigit())
drawRoundRect(xPos, startY, wordWidth, rectHeight, arcD, arcD)
drawRoundRect(xPos, startY, wordWidth, rectHeight, arcD, arcD)
}
}

private fun Graphics2D.drawTagForeground(tagPosition: Point?) {
private fun Graphics2D.drawTagForeground(tagPosition: Point) {
font = Model.font
color = AceConfig.tagForegroundColor
composite = getInstance(SRC_OVER, 1.toFloat())

drawString(tag!!.toUpperCase(), tagPosition!!.x, tagPosition.y + fontHeight)
drawString(tagUpperCase!!, tagPosition.x, tagPosition.y + fontHeight)
}

// TODO: Fix tag alignment and visibility issues
Expand Down