Skip to content

Commit

Permalink
Use lazy loading to compute the start tag string
Browse files Browse the repository at this point in the history
  • Loading branch information
MituuZ committed Jun 16, 2024
1 parent 0f39545 commit 3673db1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ import com.mituuz.fuzzier.settings.FuzzierSettingsService
import java.awt.Color

class FuzzyMatchContainer(val score: FuzzyScore, var filePath: String, var filename: String, private var module: String = "") {
private val color = JBColor.YELLOW
private val startStyleTag: String = "<font style='background-color: ${colorAsHex(color)};'>"
private val endStyleTag: String = "</font>"
private var initialPath: String? = null

companion object {
const val END_STYLE_TAG: String = "</font>"

val startStyleTag: String by lazy {
val color = JBColor.YELLOW
"<font style='background-color: ${colorAsHex(color)};'>"
}

fun createOrderedContainer(order: Int, filePath: String, initialPath:String, filename: String): FuzzyMatchContainer {
val fuzzyScore = FuzzyScore()
fuzzyScore.filenameScore = order
Expand Down Expand Up @@ -72,8 +76,8 @@ class FuzzyMatchContainer(val score: FuzzyScore, var filePath: String, var filen
if (i < source.length) {
stringBuilder.insert(i + offset, startStyleTag)
offset += startStyleTag.length
stringBuilder.insert(i + offset + 1, endStyleTag)
offset += endStyleTag.length
stringBuilder.insert(i + offset + 1, END_STYLE_TAG)
offset += END_STYLE_TAG.length
}
}
return stringBuilder.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,15 @@ SOFTWARE.
package com.mituuz.fuzzier.entities

import com.intellij.testFramework.TestApplicationManager
import com.intellij.ui.JBColor
import com.mituuz.fuzzier.entities.FuzzyMatchContainer.Companion.colorAsHex
import com.mituuz.fuzzier.entities.FuzzyMatchContainer.Companion.END_STYLE_TAG
import com.mituuz.fuzzier.entities.FuzzyMatchContainer.Companion.startStyleTag
import com.mituuz.fuzzier.entities.FuzzyMatchContainer.FuzzyScore
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

class FuzzyMatchContainerTest {
@Suppress("unused")
private val testManager = TestApplicationManager.getInstance()
private lateinit var yellow: String
private lateinit var startTag: String
private var endTag = "</font>"

@BeforeEach
fun setUp() {
yellow = colorAsHex(JBColor.YELLOW)
startTag = "<font style='background-color: $yellow;'>"
}

@Test
fun `Test highlight indexing simple case`() {
Expand All @@ -51,7 +41,7 @@ class FuzzyMatchContainerTest {
score.highlightCharacters.add(4)
val container = FuzzyMatchContainer(score, "", "Hello")
val res = container.highlight(container.filename)
assertEquals("${startTag}H${endTag}ell${startTag}o$endTag", res)
assertEquals("${startStyleTag}H${END_STYLE_TAG}ell${startStyleTag}o$END_STYLE_TAG", res)
}

@Test
Expand All @@ -66,19 +56,18 @@ class FuzzyMatchContainerTest {
score.highlightCharacters.add(18) // r

val container = FuzzyMatchContainer(score, "", "FuzzyMatchContainerTest.kt")
yellow = colorAsHex(JBColor.YELLOW)
val res = container.highlight(container.filename)
val sb = StringBuilder()

sb.append(startTag, "F", endTag)
sb.append(startTag, "u", endTag)
sb.append(startTag, "z", endTag)
sb.append(startTag, "z", endTag)
sb.append(startStyleTag, "F", END_STYLE_TAG)
sb.append(startStyleTag, "u", END_STYLE_TAG)
sb.append(startStyleTag, "z", END_STYLE_TAG)
sb.append(startStyleTag, "z", END_STYLE_TAG)
sb.append("yMatchConta")
sb.append(startTag, "i", endTag)
sb.append(startStyleTag, "i", END_STYLE_TAG)
sb.append("n")
sb.append(startTag, "e", endTag)
sb.append(startTag, "r", endTag)
sb.append(startStyleTag, "e", END_STYLE_TAG)
sb.append(startStyleTag, "r", END_STYLE_TAG)
sb.append("Test.kt")
var i = 0
while (i < res.length) {
Expand Down

0 comments on commit 3673db1

Please sign in to comment.