Skip to content
Open
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
25 changes: 15 additions & 10 deletions src/main/kotlin/com/mituuz/fuzzier/actions/FuzzyAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,15 @@ import kotlinx.coroutines.*
import java.awt.Component
import java.awt.Font
import java.awt.event.ActionEvent
import java.util.*
import java.util.Timer
import java.util.concurrent.ConcurrentHashMap
import javax.swing.*
import kotlin.concurrent.schedule

abstract class FuzzyAction : AnAction() {
lateinit var component: FuzzyComponent
lateinit var popup: JBPopup
private lateinit var originalDownHandler: EditorActionHandler
private lateinit var originalUpHandler: EditorActionHandler
private var debounceTimer: TimerTask? = null
private var originalDownHandler: EditorActionHandler? = null
private var originalUpHandler: EditorActionHandler? = null
private var debounceJob: Job? = null
protected lateinit var projectState: FuzzierSettingsService.State
protected val globalState = service<FuzzierGlobalSettingsService>().state
protected var defaultDoc: Document? = null
Expand Down Expand Up @@ -138,9 +135,10 @@ abstract class FuzzyAction : AnAction() {
val document = component.searchField.document
val listener: DocumentListener = object : DocumentListener {
override fun documentChanged(event: DocumentEvent) {
debounceTimer?.cancel()
debounceJob?.cancel()
val debouncePeriod = globalState.debouncePeriod
debounceTimer = Timer().schedule(debouncePeriod.toLong()) {
debounceJob = actionScope?.launch {
delay(debouncePeriod.toLong())
updateListContents(project, component.searchField.text)
}
}
Expand All @@ -161,6 +159,9 @@ abstract class FuzzyAction : AnAction() {
fun cleanupPopup() {
resetOriginalHandlers()

debounceJob?.cancel()
debounceJob = null

currentUpdateListContentJob?.cancel()
currentUpdateListContentJob = null

Expand All @@ -180,8 +181,12 @@ abstract class FuzzyAction : AnAction() {

fun resetOriginalHandlers() {
val actionManager = EditorActionManager.getInstance()
actionManager.setActionHandler(IdeActions.ACTION_EDITOR_MOVE_CARET_DOWN, originalDownHandler)
actionManager.setActionHandler(IdeActions.ACTION_EDITOR_MOVE_CARET_UP, originalUpHandler)
originalDownHandler?.let {
actionManager.setActionHandler(IdeActions.ACTION_EDITOR_MOVE_CARET_DOWN, it)
}
originalUpHandler?.let {
actionManager.setActionHandler(IdeActions.ACTION_EDITOR_MOVE_CARET_UP, it)
}
}

class FuzzyListActionHandler(private val fuzzyAction: FuzzyAction, private val isUp: Boolean) :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,13 @@ import java.util.concurrent.Future
import javax.swing.DefaultListModel
import javax.swing.JPanel
import javax.swing.table.DefaultTableModel
import kotlin.concurrent.schedule

class TestBenchComponent : JPanel(), Disposable {
private val columnNames =
arrayOf("Filename", "Filepath", "Streak", "MultiMatch", "PartialPath", "Filename", "Total")
private val table = JBTable()
private var searchField = EditorTextField()
private var debounceTimer: TimerTask? = null
private var debounceJob: Job? = null

@Volatile
var currentTask: Future<*>? = null
Expand Down Expand Up @@ -122,9 +121,10 @@ class TestBenchComponent : JPanel(), Disposable {
val document = searchField.document
val listener: DocumentListener = object : DocumentListener {
override fun documentChanged(event: DocumentEvent) {
debounceTimer?.cancel()
debounceJob?.cancel()
val debouncePeriod = liveSettingsComponent.debounceTimerValue.getIntSpinner().value as Int
debounceTimer = Timer().schedule(debouncePeriod.toLong()) {
debounceJob = actionScope.launch {
delay(debouncePeriod.toLong())
updateListContents(project, searchField.text)
}
}
Expand Down Expand Up @@ -199,14 +199,16 @@ class TestBenchComponent : JPanel(), Disposable {
}

override fun dispose() {
debounceTimer?.cancel()
debounceTimer = null
debounceJob?.cancel()
debounceJob = null

currentTask?.let { task ->
if (!task.isDone) task.cancel(true)
}
currentTask = null

actionScope.cancel()

ApplicationManager.getApplication().invokeLater {
try {
table.setPaintBusy(false)
Expand Down
54 changes: 30 additions & 24 deletions src/main/kotlin/com/mituuz/fuzzier/entities/FuzzyContainer.kt
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
/*
MIT License

Copyright (c) 2025 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.
*/
* MIT License
*
* Copyright (c) 2025 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.openapi.vfs.VirtualFile
import com.mituuz.fuzzier.settings.FuzzierGlobalSettingsService

abstract class FuzzyContainer(val filePath: String, val basePath: String, val filename: String) {
abstract class FuzzyContainer(
val filePath: String,
val basePath: String,
val filename: String,
val virtualFile: VirtualFile? = null,
) {
/**
* Get display string for the popup
*/
Expand All @@ -34,7 +40,7 @@ abstract class FuzzyContainer(val filePath: String, val basePath: String, val fi
/**
* Get the complete URI for the file
*/
fun getFileUri() : String {
fun getFileUri(): String {
return "$basePath$filePath"
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/com/mituuz/fuzzier/entities/GrepConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@

package com.mituuz.fuzzier.entities

import com.intellij.openapi.vfs.VirtualFile

enum class CaseMode {
SENSITIVE,
INSENSITIVE,
}

class GrepConfig(
val targets: List<String>,
val targets: Set<VirtualFile>?,
val caseMode: CaseMode,
val title: String = "",
val supportsSecondaryField: Boolean = true,
Expand Down
6 changes: 4 additions & 2 deletions src/main/kotlin/com/mituuz/fuzzier/entities/RowContainer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package com.mituuz.fuzzier.entities

import com.intellij.openapi.vfs.VirtualFile
import com.mituuz.fuzzier.settings.FuzzierGlobalSettingsService
import java.io.File

Expand All @@ -32,8 +33,9 @@ class RowContainer(
filename: String,
val rowNumber: Int,
val trimmedRow: String,
val columnNumber: Int = 0
) : FuzzyContainer(filePath, basePath, filename) {
val columnNumber: Int = 0,
virtualFile: VirtualFile? = null
) : FuzzyContainer(filePath, basePath, filename, virtualFile) {
companion object {
private val FILE_SEPARATOR: String = File.separator
private val RG_PATTERN: Regex = Regex("""^.+:\d+:\d+:\s*.+$""")
Expand Down
43 changes: 29 additions & 14 deletions src/main/kotlin/com/mituuz/fuzzier/grep/FuzzyGrep.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import com.intellij.openapi.editor.LogicalPosition
import com.intellij.openapi.editor.ScrollType
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.vcs.changes.ChangeListManager
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileManager
import com.intellij.util.SingleAlarm
import com.mituuz.fuzzier.actions.FuzzyAction
Expand Down Expand Up @@ -72,7 +74,7 @@ open class FuzzyGrep : FuzzyAction() {

open fun getGrepConfig(project: Project): GrepConfig {
return GrepConfig(
targets = listOf("."),
targets = null,
caseMode = CaseMode.SENSITIVE,
title = "Fuzzy Grep",
)
Expand Down Expand Up @@ -102,8 +104,7 @@ open class FuzzyGrep : FuzzyAction() {
defaultDoc = EditorFactory.getInstance().createDocument("")
val showSecondaryField = backend!!.supportsSecondaryField() && grepConfig.supportsSecondaryField
component = FuzzyFinderComponent(
project = project,
showSecondaryField = showSecondaryField
project = project, showSecondaryField = showSecondaryField
)
previewAlarmProvider = CoroutinePreviewAlarmProvider(actionScope)
previewAlarm = previewAlarmProvider?.getPreviewAlarm(component, defaultDoc)
Expand Down Expand Up @@ -155,8 +156,7 @@ open class FuzzyGrep : FuzzyAction() {
try {
val results = withContext(Dispatchers.IO) {
findInFiles(
searchString,
project
searchString, project
)
}
coroutineContext.ensureActive()
Expand All @@ -172,17 +172,35 @@ open class FuzzyGrep : FuzzyAction() {
project: Project,
): ListModel<FuzzyContainer> {
val listModel = DefaultListModel<FuzzyContainer>()
val projectBasePath = project.basePath.toString()
val projectBasePath = project.basePath

if (backend != null) {
if (backend != null && projectBasePath != null) {
val secondaryFieldText = (component as FuzzyFinderComponent).getSecondaryText()
val commands = backend!!.buildCommand(grepConfig, searchString, secondaryFieldText)
commandRunner.runCommandPopulateListModel(commands, listModel, projectBasePath, backend!!)
backend!!.handleSearch(
grepConfig, searchString, secondaryFieldText, commandRunner, listModel, projectBasePath, project
) { vf -> validVf(vf, secondaryFieldText, ChangeListManager.getInstance(project)) }
}

return listModel
}

private fun validVf(
virtualFile: VirtualFile, secondaryFieldText: String? = null, clm: ChangeListManager
): Boolean {
if (virtualFile.isDirectory) return false
if (virtualFile.fileType.isBinary) return false

if (clm.isIgnoredFile(virtualFile)) return false

if (secondaryFieldText.isNullOrBlank()) {
return true
} else if (virtualFile.extension.equals(secondaryFieldText, ignoreCase = true)) {
return true
}

return false
}

private fun createListeners(project: Project) {
// Add a listener that updates the contents of the preview pane
component.fileList.addListSelectionListener { event ->
Expand All @@ -199,15 +217,12 @@ open class FuzzyGrep : FuzzyAction() {

private fun handleInput(project: Project) {
val selectedValue = component.fileList.selectedValue
val virtualFile =
VirtualFileManager.getInstance().findFileByUrl("file://${selectedValue?.getFileUri()}")
val virtualFile = VirtualFileManager.getInstance().findFileByUrl("file://${selectedValue?.getFileUri()}")
virtualFile?.let {
val fileEditorManager = FileEditorManager.getInstance(project)

FileOpeningUtil.openFile(
fileEditorManager,
virtualFile,
globalState.newTab
fileEditorManager, virtualFile, globalState.newTab
) {
popup.cancel()
ApplicationManager.getApplication().invokeLater {
Expand Down
10 changes: 5 additions & 5 deletions src/main/kotlin/com/mituuz/fuzzier/grep/FuzzyGrepVariants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class FuzzyGrepOpenTabsCI : FuzzyGrep() {
override fun getGrepConfig(project: Project): GrepConfig {
val fileEditorManager = FileEditorManager.getInstance(project)
val openFiles: Array<VirtualFile> = fileEditorManager.openFiles
val targets = openFiles.map { it.path }
val targets = openFiles.toSet()

return GrepConfig(
targets = targets,
Expand All @@ -54,7 +54,7 @@ class FuzzyGrepOpenTabs : FuzzyGrep() {
override fun getGrepConfig(project: Project): GrepConfig {
val fileEditorManager = FileEditorManager.getInstance(project)
val openFiles: Array<VirtualFile> = fileEditorManager.openFiles
val targets = openFiles.map { it.path }
val targets = openFiles.toSet()

return GrepConfig(
targets = targets,
Expand All @@ -69,7 +69,7 @@ class FuzzyGrepCurrentBufferCI : FuzzyGrep() {
val editor = FileEditorManager.getInstance(project).selectedTextEditor
val virtualFile: VirtualFile? =
editor?.let { FileEditorManager.getInstance(project).selectedFiles.firstOrNull() }
val targets = virtualFile?.path?.let { listOf(it) } ?: emptyList()
val targets = virtualFile?.let { setOf(it) } ?: emptySet()

return GrepConfig(
targets = targets,
Expand All @@ -85,7 +85,7 @@ class FuzzyGrepCurrentBuffer : FuzzyGrep() {
val editor = FileEditorManager.getInstance(project).selectedTextEditor
val virtualFile: VirtualFile? =
editor?.let { FileEditorManager.getInstance(project).selectedFiles.firstOrNull() }
val targets = virtualFile?.path?.let { listOf(it) } ?: emptyList()
val targets = virtualFile?.let { setOf(it) } ?: emptySet()

return GrepConfig(
targets = targets,
Expand All @@ -99,7 +99,7 @@ class FuzzyGrepCurrentBuffer : FuzzyGrep() {
class FuzzyGrepCI : FuzzyGrep() {
override fun getGrepConfig(project: Project): GrepConfig {
return GrepConfig(
targets = listOf("."),
targets = null,
caseMode = CaseMode.INSENSITIVE,
title = FuzzyGrepTitles.DEFAULT,
)
Expand Down
21 changes: 11 additions & 10 deletions src/main/kotlin/com/mituuz/fuzzier/grep/backend/BackendResolver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@ import com.mituuz.fuzzier.runner.CommandRunner
class BackendResolver(val isWindows: Boolean) {
suspend fun resolveBackend(commandRunner: CommandRunner, projectBasePath: String): Result<BackendStrategy> {
return when {
isInstalled(commandRunner, "rg", projectBasePath) -> Result.success(BackendStrategy.Ripgrep)
isWindows && isInstalled(
commandRunner,
"findstr",
projectBasePath
) -> Result.success(BackendStrategy.Findstr)

!isWindows && isInstalled(commandRunner, "grep", projectBasePath) -> Result.success(
BackendStrategy.Grep
)
true -> Result.success(FuzzierGrep)
// isInstalled(commandRunner, "rg", projectBasePath) -> Result.success(BackendStrategy.Ripgrep)
// isWindows && isInstalled(
// commandRunner,
// "findstr",
// projectBasePath
// ) -> Result.success(BackendStrategy.Findstr)
//
// !isWindows && isInstalled(commandRunner, "grep", projectBasePath) -> Result.success(
// BackendStrategy.Grep
// )

else -> Result.failure(Exception("No suitable grep command found"))
}
Expand Down
Loading
Loading