Skip to content

Commit

Permalink
Fix task cancellation (#69)
Browse files Browse the repository at this point in the history
* Working implementation with coroutines

* Fix task cancellation and update minor version

* Lower the default debounce period

* Halve the default debounce time
  • Loading branch information
MituuZ authored Jun 29, 2024
1 parent b7d14c2 commit aade76e
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 35 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

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

repositories {
mavenCentral()
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.1
- Fix task cancellation, making lower debounce time feel much better
- Lower default debounce time

## Version 0.25.0
- Clear up settings grouping
- Add option to highlight filename matches in the file list
Expand Down
46 changes: 29 additions & 17 deletions src/main/kotlin/com/mituuz/fuzzier/Fuzzier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ import com.intellij.openapi.vfs.VirtualFileManager
import com.intellij.openapi.wm.WindowManager
import com.mituuz.fuzzier.components.FuzzyFinderComponent
import com.mituuz.fuzzier.entities.FuzzyMatchContainer
import com.mituuz.fuzzier.settings.FuzzierSettingsService
import com.mituuz.fuzzier.settings.FuzzierSettingsService.RecentFilesMode.NONE
import org.apache.commons.lang3.StringUtils
import java.awt.event.*
import javax.swing.*
import kotlin.coroutines.cancellation.CancellationException

open class Fuzzier : FuzzyAction() {
private var defaultDoc: Document? = null
Expand Down Expand Up @@ -159,27 +159,39 @@ open class Fuzzier : FuzzyAction() {

currentTask?.takeIf { !it.isDone }?.cancel(true)
currentTask = ApplicationManager.getApplication().executeOnPooledThread {
component.fileList.setPaintBusy(true)
var listModel = DefaultListModel<FuzzyMatchContainer>()
try {
// Create a reference to the current task to check if it has been cancelled
val task = currentTask
component.fileList.setPaintBusy(true)
var listModel = DefaultListModel<FuzzyMatchContainer>()

val stringEvaluator = StringEvaluator(
fuzzierSettingsService.state.exclusionSet,
fuzzierSettingsService.state.modules,
changeListManager
)
val stringEvaluator = StringEvaluator(
fuzzierSettingsService.state.exclusionSet,
fuzzierSettingsService.state.modules,
changeListManager
)

val moduleManager = ModuleManager.getInstance(project)
processModules(moduleManager, stringEvaluator, searchString, listModel)
if (task?.isCancelled == true) throw CancellationException()

listModel = fuzzierUtil.sortAndLimit(listModel)
val moduleManager = ModuleManager.getInstance(project)
processModules(moduleManager, stringEvaluator, searchString, listModel)

ApplicationManager.getApplication().invokeLater {
component.fileList.model = listModel
component.fileList.cellRenderer = getCellRenderer()
component.fileList.setPaintBusy(false)
if (!component.fileList.isEmpty) {
component.fileList.setSelectedValue(listModel[0], true)
if (task?.isCancelled == true) throw CancellationException()

listModel = fuzzierUtil.sortAndLimit(listModel)

if (task?.isCancelled == true) throw CancellationException()

ApplicationManager.getApplication().invokeLater {
component.fileList.model = listModel
component.fileList.cellRenderer = getCellRenderer()
component.fileList.setPaintBusy(false)
if (!component.fileList.isEmpty) {
component.fileList.setSelectedValue(listModel[0], true)
}
}
} catch (e: CancellationException) {
// Do nothing
}
}
}
Expand Down
44 changes: 28 additions & 16 deletions src/main/kotlin/com/mituuz/fuzzier/FuzzyMover.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import org.apache.commons.lang3.StringUtils
import java.awt.event.*
import java.util.concurrent.CompletableFuture
import javax.swing.*
import kotlin.coroutines.cancellation.CancellationException

class FuzzyMover : FuzzyAction() {
private val dimensionKey: String = "FuzzyMoverPopup"
Expand Down Expand Up @@ -194,28 +195,39 @@ class FuzzyMover : FuzzyAction() {
}

currentTask?.takeIf { !it.isDone }?.cancel(true)

currentTask = ApplicationManager.getApplication().executeOnPooledThread {
component.fileList.setPaintBusy(true)
var listModel = DefaultListModel<FuzzyMatchContainer>()
try {
// Create a reference to the current task to check if it has been cancelled
val task = currentTask
component.fileList.setPaintBusy(true)
var listModel = DefaultListModel<FuzzyMatchContainer>()

val stringEvaluator = StringEvaluator(
fuzzierSettingsService.state.exclusionSet,
fuzzierSettingsService.state.modules
)
val stringEvaluator = StringEvaluator(
fuzzierSettingsService.state.exclusionSet,
fuzzierSettingsService.state.modules
)

val moduleManager = ModuleManager.getInstance(project)
processModules(moduleManager, stringEvaluator, searchString, listModel)
if (task?.isCancelled == true) throw CancellationException()

listModel = fuzzierUtil.sortAndLimit(listModel, true)
val moduleManager = ModuleManager.getInstance(project)
processModules(moduleManager, stringEvaluator, searchString, listModel)

ApplicationManager.getApplication().invokeLater {
component.fileList.model = listModel
component.fileList.cellRenderer = getCellRenderer()
component.fileList.setPaintBusy(false)
if (!component.fileList.isEmpty) {
component.fileList.setSelectedValue(listModel[0], true)
if (task?.isCancelled == true) throw CancellationException()

listModel = fuzzierUtil.sortAndLimit(listModel, true)

if (task?.isCancelled == true) throw CancellationException()

ApplicationManager.getApplication().invokeLater {
component.fileList.model = listModel
component.fileList.cellRenderer = getCellRenderer()
component.fileList.setPaintBusy(false)
if (!component.fileList.isEmpty) {
component.fileList.setSelectedValue(listModel[0], true)
}
}
} catch (e: CancellationException) {
// Do nothing
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class FuzzierSettingsService : PersistentStateComponent<FuzzierSettingsService.S
var exclusionSet: Set<String> = setOf("/.idea/*", "/.git/*", "/target/*", "/build/*", "/.gradle/*", "/.run/*")
var newTab: Boolean = false
var prioritizeShorterDirPaths = true
var debouncePeriod: Int = 150
var debouncePeriod: Int = 80
var resetWindow = false
var fileListLimit: Int = 50

Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
</extensions>

<change-notes><![CDATA[
<h2>Version 0.25.1</h2>
- Fix task cancellation, making lower debounce time feel much better<br>
- Lower default debounce time<br>
<h2>Version 0.25.0</h2>
- Clear up settings grouping<br>
- Add option to highlight filename matches in the file list<br>
Expand Down

0 comments on commit aade76e

Please sign in to comment.