Skip to content

Commit

Permalink
Match highlighting (#66)
Browse files Browse the repository at this point in the history
* Initial working version

* Use the actual length

* Get color from global scheme

* Use JB yellow

* Make the test dynamic

* Some cleanup

* Sort the indexes before applying the highlight

* Add license

* Line up

* Rename hex function

* Use indexes instead of a for loop

* Just handle each letter one by one

* Add settings to control highlight (only for styled) and fix tests

* Use lazy loading to compute the start tag string

* Make colorAsHex private

* Create a config class to store the style tags instead of a lazy var

* Remove extra print

* Add change notes and increment version

* Ensure that highlight option is properly set at start
  • Loading branch information
MituuZ authored Jun 29, 2024
1 parent 04480ff commit b7d14c2
Show file tree
Hide file tree
Showing 13 changed files with 217 additions and 18 deletions.
6 changes: 4 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
id("org.jetbrains.kotlin.jvm") version "2.0.0"
id("org.jetbrains.intellij") version "1.17.4"
}

group = "com.mituuz"
version = "0.24.0"
version = "0.25.0"

repositories {
mavenCentral()
Expand All @@ -30,7 +32,7 @@ intellij {

tasks {
withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions.jvmTarget = "17"
compilerOptions.jvmTarget.set(JvmTarget.JVM_17)
}

patchPluginXml {
Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Changelog
## Version 0.25.0
- Clear up settings grouping
- Add option to highlight filename matches in the file list

## Version 0.24.0
- Add an option to change the font size for the preview window
- Some dependency updates
Expand Down
6 changes: 5 additions & 1 deletion src/main/kotlin/com/mituuz/fuzzier/FuzzyAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ abstract class FuzzyAction : AnAction() {
} else {
fuzzierSettingsService.state.filenameType
}
renderer.text = container.toString(filenameType)
renderer.text = container.toString(filenameType, fuzzierSettingsService.state.highlightFilename)
fuzzierSettingsService.state.fileListSpacing.let {
renderer.border = BorderFactory.createEmptyBorder(it, 0, it, 0)
}
Expand All @@ -199,4 +199,8 @@ abstract class FuzzyAction : AnAction() {
fun setFiletype(filenameType: FilenameType) {
fuzzierSettingsService.state.filenameType = filenameType
}

fun setHighlight(highlight: Boolean) {
fuzzierSettingsService.state.highlightFilename = highlight
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ class FuzzierSettingsComponent {
""".trimIndent(),
false)

val highlightFilename = SettingsComponent(JBCheckBox(), "Highlight filename in file list*",
"""
Toggles highlighting of the filename on the file list.
<br>
Only works with styled file list, which supports html styling.
""".trimIndent(),
false)

val fileListLimit = SettingsComponent(JBIntSpinner(50, 1, 5000), "File list limit",
"""
Controls how many files are shown and listed on the popup.
Expand Down Expand Up @@ -194,11 +202,12 @@ class FuzzierSettingsComponent {
.addComponent(recentFileModeSelector)
.addComponent(prioritizeShortDirs)
.addComponent(debounceTimerValue)
.addComponent(filenameTypeSelector)
.addComponent(fileListLimit)

.addSeparator()
.addComponent(JBLabel("<html><strong>Popup styling</strong></html>"))
.addComponent(filenameTypeSelector)
.addComponent(highlightFilename)
.addComponent(fileListFontSize)
.addComponent(previewFontSize)
.addComponent(fileListSpacing)
Expand Down Expand Up @@ -256,6 +265,10 @@ class FuzzierSettingsComponent {
for (filenameType in FilenameType.entries) {
filenameTypeSelector.getFilenameTypeComboBox().addItem(filenameType)
}
filenameTypeSelector.getFilenameTypeComboBox().addItemListener {
highlightFilename.getCheckBox().isEnabled = it.item == FilenameType.FILENAME_WITH_PATH_STYLED
}
highlightFilename.getCheckBox().isEnabled = filenameTypeSelector.getFilenameTypeComboBox().item == FilenameType.FILENAME_WITH_PATH_STYLED

startTestBench.getButton().addActionListener {
startTestBench.getButton().isEnabled = false
Expand Down
34 changes: 30 additions & 4 deletions src/main/kotlin/com/mituuz/fuzzier/entities/FuzzyMatchContainer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ SOFTWARE.
package com.mituuz.fuzzier.entities

import com.intellij.openapi.components.service
import com.mituuz.fuzzier.settings.FuzzierConfiguration.END_STYLE_TAG
import com.mituuz.fuzzier.settings.FuzzierConfiguration.startStyleTag
import com.mituuz.fuzzier.settings.FuzzierSettingsService

class FuzzyMatchContainer(val score: FuzzyScore, var filePath: String, var filename: String, private var module: String = "") {
private var initialPath: String? = null

companion object {
fun createOrderedContainer(order: Int, filePath: String, initialPath:String, filename: String): FuzzyMatchContainer {
val fuzzyScore = FuzzyScore()
Expand All @@ -38,17 +41,39 @@ class FuzzyMatchContainer(val score: FuzzyScore, var filePath: String, var filen
}
}

fun toString(filenameType: FilenameType): String {
fun toString(filenameType: FilenameType, highlight: Boolean): String {
return when (filenameType) {
FilenameType.FILENAME_ONLY -> filename
FilenameType.FILE_PATH_ONLY -> filePath
FilenameType.FILENAME_WITH_PATH -> "$filename ($filePath)"
FilenameType.FILENAME_WITH_PATH_STYLED -> getFilenameWithPathStyled()
FilenameType.FILENAME_WITH_PATH_STYLED -> getFilenameWithPathStyled(highlight)
}
}

private fun getStyledFilename(highlight: Boolean): String {
if (highlight) {
return highlight(filename)
}
return filename
}

fun highlight(source: String): String {
val stringBuilder: StringBuilder = StringBuilder(source)
var offset = 0
val hlIndexes = score.highlightCharacters.sorted()
for (i in hlIndexes) {
if (i < source.length) {
stringBuilder.insert(i + offset, startStyleTag)
offset += startStyleTag.length
stringBuilder.insert(i + offset + 1, END_STYLE_TAG)
offset += END_STYLE_TAG.length
}
}
return stringBuilder.toString()
}

private fun getFilenameWithPathStyled(): String {
return "<html><strong>$filename</strong> <i>($filePath)</i></html>"
private fun getFilenameWithPathStyled(highlight: Boolean): String {
return "<html><strong>${getStyledFilename(highlight)}</strong> <i>($filePath)</i></html>"
}

fun getFileUri(): String {
Expand Down Expand Up @@ -85,6 +110,7 @@ class FuzzyMatchContainer(val score: FuzzyScore, var filePath: String, var filen
var multiMatchScore = 0
var partialPathScore = 0
var filenameScore = 0
val highlightCharacters: MutableSet<Int> = HashSet()

fun getTotalScore(): Int {
return streakScore + multiMatchScore + partialPathScore + filenameScore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ class ScoreCalculator(searchString: String) {
private fun processFilenameChar(searchStringPartChar: Char) {
val filePathPartChar = currentFilePath[filePathIndex]
if (searchStringPartChar == filePathPartChar) {
fuzzyScore.highlightCharacters.add(filePathIndex - filenameIndex)
searchStringIndex++
currentFilenameStreak++
if (currentFilenameStreak > longestFilenameStreak) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
MIT License
Copyright (c) 2024 Mitja Leino
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package com.mituuz.fuzzier.settings

import com.intellij.ui.JBColor
import java.awt.Color

object FuzzierConfiguration {
var startStyleTag = createStartStyleTag()
const val END_STYLE_TAG: String = "</font>"

private fun createStartStyleTag(): String {
val color = JBColor.YELLOW
return "<font style='background-color: ${colorAsHex(color)};'>"
}

@Suppress("unused") // TODO: Update the base color for highlights
private fun updateStartStyleTag(colorAsHex: String) {
this.startStyleTag = "<font style='background-color: $colorAsHex;'>"
}

private fun colorAsHex(color: Color): String {
return String.format("#%02x%02x%02x", color.red, color.green, color.blue)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ class FuzzierSettingsConfigurable : Configurable {
component.recentFileModeSelector.getRecentFilesTypeComboBox().selectedIndex = state.recentFilesMode.ordinal
component.prioritizeShortDirs.getCheckBox().isSelected = state.prioritizeShorterDirPaths
component.debounceTimerValue.getIntSpinner().value = state.debouncePeriod
component.filenameTypeSelector.getFilenameTypeComboBox().selectedIndex = state.filenameType.ordinal
component.fileListLimit.getIntSpinner().value = state.fileListLimit

component.filenameTypeSelector.getFilenameTypeComboBox().selectedIndex = state.filenameType.ordinal
component.highlightFilename.getCheckBox().isSelected = state.highlightFilename
component.fileListFontSize.getIntSpinner().value = state.fileListFontSize
component.previewFontSize.getIntSpinner().value = state.previewFontSize
component.fileListSpacing.getIntSpinner().value = state.fileListSpacing
Expand All @@ -73,8 +75,10 @@ class FuzzierSettingsConfigurable : Configurable {
|| state.recentFilesMode != component.recentFileModeSelector.getRecentFilesTypeComboBox().selectedItem
|| state.prioritizeShorterDirPaths != component.prioritizeShortDirs.getCheckBox().isSelected
|| state.debouncePeriod != component.debounceTimerValue.getIntSpinner().value
|| state.filenameType != component.filenameTypeSelector.getFilenameTypeComboBox().selectedItem
|| state.fileListLimit != component.fileListLimit.getIntSpinner().value

|| state.filenameType != component.filenameTypeSelector.getFilenameTypeComboBox().selectedItem
|| state.highlightFilename != component.highlightFilename.getCheckBox().isSelected
|| state.fileListFontSize != component.fileListFontSize.getIntSpinner().value
|| state.previewFontSize != component.previewFontSize.getIntSpinner().value
|| state.fileListSpacing != component.fileListSpacing.getIntSpinner().value
Expand All @@ -97,8 +101,10 @@ class FuzzierSettingsConfigurable : Configurable {
state.recentFilesMode = RecentFilesMode.entries.toTypedArray()[component.recentFileModeSelector.getRecentFilesTypeComboBox().selectedIndex]
state.prioritizeShorterDirPaths = component.prioritizeShortDirs.getCheckBox().isSelected
state.debouncePeriod = component.debounceTimerValue.getIntSpinner().value as Int
state.filenameType = FilenameType.entries.toTypedArray()[component.filenameTypeSelector.getFilenameTypeComboBox().selectedIndex]
state.fileListLimit = component.fileListLimit.getIntSpinner().value as Int

state.filenameType = FilenameType.entries.toTypedArray()[component.filenameTypeSelector.getFilenameTypeComboBox().selectedIndex]
state.highlightFilename = component.highlightFilename.getCheckBox().isSelected
state.fileListFontSize = component.fileListFontSize.getIntSpinner().value as Int
state.previewFontSize = component.previewFontSize.getIntSpinner().value as Int
state.fileListSpacing = component.fileListSpacing.getIntSpinner().value as Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class FuzzierSettingsService : PersistentStateComponent<FuzzierSettingsService.S
var fileListLimit: Int = 50

var filenameType: FilenameType = FILE_PATH_ONLY
var highlightFilename = false
var fileListFontSize = 14
var previewFontSize = 0
var fileListSpacing = 0
Expand Down
7 changes: 3 additions & 4 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@
</extensions>

<change-notes><![CDATA[
<h2>Version 0.24.0</h2>
- Add an option to change the font size for the preview window<br>
- Some dependency updates<br>
- Separate settings sections<br>
<h2>Version 0.25.0</h2>
- Clear up settings grouping<br>
- Add option to highlight filename matches in the file list<br>
]]>
</change-notes>

Expand Down
17 changes: 16 additions & 1 deletion src/test/kotlin/com/mituuz/fuzzier/FuzzyActionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,24 @@ class FuzzyActionTest {
}

@Test
fun `Check renderer with styled path`() {
fun `Check renderer with styled path, no highlight`() {
val action = getAction()
action.setFiletype(FILENAME_WITH_PATH_STYLED)
action.setHighlight(false)
action.component = SimpleFinderComponent()
val renderer = action.getCellRenderer()
val container = FuzzyMatchContainer(FuzzyScore(), "/src/asd", "asd")
val dummyList = JList<FuzzyMatchContainer>()
val component = renderer.getListCellRendererComponent(dummyList, container, 0, false, false) as JLabel
assertNotNull(component)
assertEquals("<html><strong>asd</strong> <i>(/src/asd)</i></html>", component.text)
}

@Test
fun `Check renderer with styled path, with highlight but no values`() {
val action = getAction()
action.setFiletype(FILENAME_WITH_PATH_STYLED)
action.setHighlight(true)
action.component = SimpleFinderComponent()
val renderer = action.getCellRenderer()
val container = FuzzyMatchContainer(FuzzyScore(), "/src/asd", "asd")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
MIT License
Copyright (c) 2024 Mitja Leino
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package com.mituuz.fuzzier.entities

import com.intellij.testFramework.TestApplicationManager
import com.mituuz.fuzzier.entities.FuzzyMatchContainer.FuzzyScore
import com.mituuz.fuzzier.settings.FuzzierConfiguration.END_STYLE_TAG
import com.mituuz.fuzzier.settings.FuzzierConfiguration.startStyleTag
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test

class FuzzyMatchContainerTest {
@Suppress("unused")
private val testManager = TestApplicationManager.getInstance()

@Test
fun `Test highlight indexing simple case`() {
val score = FuzzyScore()
score.highlightCharacters.add(0)
score.highlightCharacters.add(4)
val container = FuzzyMatchContainer(score, "", "Hello")
val res = container.highlight(container.filename)
assertEquals("${startStyleTag}H${END_STYLE_TAG}ell${startStyleTag}o$END_STYLE_TAG", res)
}

@Test
fun `Test highlight indexing complex case`() {
val score = FuzzyScore()
score.highlightCharacters.add(0) // f
score.highlightCharacters.add(1) // u
score.highlightCharacters.add(2) // z
score.highlightCharacters.add(3) // z
score.highlightCharacters.add(15) // i
score.highlightCharacters.add(17) // e
score.highlightCharacters.add(18) // r

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

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(startStyleTag, "i", END_STYLE_TAG)
sb.append("n")
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) {
assertEquals(sb.toString()[i], res[i])
i++
}
}
}
Loading

0 comments on commit b7d14c2

Please sign in to comment.