Skip to content

Commit

Permalink
Add settings to control highlight (only for styled) and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MituuZ committed Jun 16, 2024
1 parent 73a364a commit 0f39545
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 18 deletions.
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,16 @@ class FuzzierSettingsComponent {
""".trimIndent(),
false)

val highlightFilename = SettingsComponent(JBCheckBox(), "Highlight filename in file list*",
"""
This is still experimental and may cause some performance issues.
<br><br>
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 +204,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 +267,9 @@ class FuzzierSettingsComponent {
for (filenameType in FilenameType.entries) {
filenameTypeSelector.getFilenameTypeComboBox().addItem(filenameType)
}
filenameTypeSelector.getFilenameTypeComboBox().addItemListener {
highlightFilename.getCheckBox().isEnabled = it.item == FilenameType.FILENAME_WITH_PATH_STYLED
}

startTestBench.getButton().addActionListener {
startTestBench.getButton().isEnabled = false
Expand Down
28 changes: 18 additions & 10 deletions src/main/kotlin/com/mituuz/fuzzier/entities/FuzzyMatchContainer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ 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 sm: String = "<font style='background-color: ${colorAsHex(color)};'>"
private val em: String = "</font>"
private val startStyleTag: String = "<font style='background-color: ${colorAsHex(color)};'>"
private val endStyleTag: String = "</font>"
private var initialPath: String? = null

companion object {
fun createOrderedContainer(order: Int, filePath: String, initialPath:String, filename: String): FuzzyMatchContainer {
val fuzzyScore = FuzzyScore()
Expand All @@ -47,13 +48,20 @@ 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 {
Expand All @@ -62,17 +70,17 @@ class FuzzyMatchContainer(val score: FuzzyScore, var filePath: String, var filen
val hlIndexes = score.highlightCharacters.sorted()
for (i in hlIndexes) {
if (i < source.length) {
stringBuilder.insert(i + offset, sm)
offset += sm.length
stringBuilder.insert(i + offset + 1, em)
offset += em.length
stringBuilder.insert(i + offset, startStyleTag)
offset += startStyleTag.length
stringBuilder.insert(i + offset + 1, endStyleTag)
offset += endStyleTag.length
}
}
return stringBuilder.toString()
}

private fun getFilenameWithPathStyled(): String {
return "<html><strong>${highlight(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
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
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
Expand Up @@ -43,8 +43,10 @@ class FuzzierSettingsConfigurableTest {
state.recentFilesMode = NONE
state.prioritizeShorterDirPaths = false
state.debouncePeriod = 140
state.filenameType = FILENAME_WITH_PATH_STYLED
state.fileListLimit = 200

state.filenameType = FILENAME_WITH_PATH_STYLED
state.highlightFilename = false
state.fileListFontSize = 15
state.previewFontSize = 0
state.fileListSpacing = 2
Expand All @@ -67,8 +69,10 @@ class FuzzierSettingsConfigurableTest {
state.newTab = true
state.recentFilesMode = RECENT_PROJECT_FILES
state.debouncePeriod = 140
state.filenameType = FILENAME_WITH_PATH_STYLED
state.fileListLimit = 200

state.filenameType = FILENAME_WITH_PATH_STYLED
state.highlightFilename = false
state.fileListFontSize = 15
state.previewFontSize = 0
state.fileListSpacing = 2
Expand Down

0 comments on commit 0f39545

Please sign in to comment.