Skip to content

Commit

Permalink
Mitigate InterruptedException | Improved task cancelling (#79)
Browse files Browse the repository at this point in the history
* Improve task cancelling

* Increment version and add change notes

* Update testing version
  • Loading branch information
MituuZ authored Sep 5, 2024
1 parent 3a00263 commit 7b0e0a9
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 62 deletions.
7 changes: 3 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

// Use same version and group for the jar and the plugin
val currentVersion = "1.0.0"
val currentVersion = "1.1.0"
val myGroup = "com.mituuz"
version = currentVersion
group = myGroup
Expand All @@ -23,7 +23,7 @@ repositories {

dependencies {
intellijPlatform {
intellijIdeaCommunity("2024.2.0.1")
intellijIdeaCommunity("2024.2.1")

pluginVerifier()
zipSigner()
Expand Down Expand Up @@ -61,8 +61,7 @@ intellijPlatform {

changeNotes = """
<h2>Version $currentVersion</h2>
- Re-implement project file handling as a backup if no modules are present<br>
- Migrate IntelliJ Platform Gradle Plugin to 2.x
- Improve task cancelling to avoid InterruptedException and reduce cancelling delay<br>
""".trimIndent()

ideaVersion {
Expand Down
70 changes: 43 additions & 27 deletions src/main/kotlin/com/mituuz/fuzzier/Fuzzier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import com.mituuz.fuzzier.entities.FuzzyMatchContainer
import com.mituuz.fuzzier.settings.FuzzierSettingsService.RecentFilesMode.NONE
import org.apache.commons.lang3.StringUtils
import java.awt.event.*
import java.util.concurrent.Future
import javax.swing.*
import kotlin.coroutines.cancellation.CancellationException

Expand Down Expand Up @@ -151,14 +152,7 @@ open class Fuzzier : FuzzyAction() {

override fun updateListContents(project: Project, searchString: String) {
if (StringUtils.isBlank(searchString)) {
if (fuzzierSettingsService.state.recentFilesMode != NONE) {
createInitialView(project)
} else {
ApplicationManager.getApplication().invokeLater {
component.fileList.model = DefaultListModel()
defaultDoc?.let { (component as FuzzyFinderComponent).previewPane.updateFile(it) }
}
}
handleEmptySearchString(project)
return
}

Expand All @@ -170,26 +164,17 @@ open class Fuzzier : FuzzyAction() {
component.fileList.setPaintBusy(true)
var listModel = DefaultListModel<FuzzyMatchContainer>()

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

if (task?.isCancelled == true) throw CancellationException()
if (task?.isCancelled == true) return@executeOnPooledThread

val moduleManager = ModuleManager.getInstance(project)
if (fuzzierSettingsService.state.isProject) {
processProject(project, stringEvaluator, searchString, listModel)
} else {
processModules(moduleManager, stringEvaluator, searchString, listModel)
}
process(project, stringEvaluator, searchString, listModel, task)

if (task?.isCancelled == true) throw CancellationException()
if (task?.isCancelled == true) return@executeOnPooledThread

listModel = fuzzierUtil.sortAndLimit(listModel)

if (task?.isCancelled == true) throw CancellationException()
if (task?.isCancelled == true) return@executeOnPooledThread

ApplicationManager.getApplication().invokeLater {
component.fileList.model = listModel
Expand All @@ -199,23 +184,54 @@ open class Fuzzier : FuzzyAction() {
component.fileList.setSelectedValue(listModel[0], true)
}
}
} catch (e: InterruptedException) {
return@executeOnPooledThread
} catch (e: CancellationException) {
// Do nothing
return@executeOnPooledThread
}
}
}

private fun handleEmptySearchString(project: Project) {
if (fuzzierSettingsService.state.recentFilesMode != NONE) {
createInitialView(project)
} else {
ApplicationManager.getApplication().invokeLater {
component.fileList.model = DefaultListModel()
defaultDoc?.let { (component as FuzzyFinderComponent).previewPane.updateFile(it) }
}
}
}

private fun getStringEvaluator(): StringEvaluator {
return StringEvaluator(
fuzzierSettingsService.state.exclusionSet,
fuzzierSettingsService.state.modules,
changeListManager
)
}

private fun process(project: Project, stringEvaluator: StringEvaluator, searchString: String,
listModel: DefaultListModel<FuzzyMatchContainer>, task: Future<*>?) {
val moduleManager = ModuleManager.getInstance(project)
if (fuzzierSettingsService.state.isProject) {
processProject(project, stringEvaluator, searchString, listModel, task)
} else {
processModules(moduleManager, stringEvaluator, searchString, listModel, task)
}
}

private fun processProject(project: Project, stringEvaluator: StringEvaluator,
searchString: String, listModel: DefaultListModel<FuzzyMatchContainer>) {
val contentIterator = stringEvaluator.getContentIterator(project.name, searchString, listModel)
searchString: String, listModel: DefaultListModel<FuzzyMatchContainer>, task: Future<*>?) {
val contentIterator = stringEvaluator.getContentIterator(project.name, searchString, listModel, task)
ProjectFileIndex.getInstance(project).iterateContent(contentIterator)
}

private fun processModules(moduleManager: ModuleManager, stringEvaluator: StringEvaluator,
searchString: String, listModel: DefaultListModel<FuzzyMatchContainer>) {
searchString: String, listModel: DefaultListModel<FuzzyMatchContainer>, task: Future<*>?) {
for (module in moduleManager.modules) {
val moduleFileIndex = module.rootManager.fileIndex
val contentIterator = stringEvaluator.getContentIterator(module.name, searchString, listModel)
val contentIterator = stringEvaluator.getContentIterator(module.name, searchString, listModel, task)
moduleFileIndex.iterateContent(contentIterator)
}
}
Expand Down
52 changes: 32 additions & 20 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 com.mituuz.fuzzier.entities.FuzzyMatchContainer
import org.apache.commons.lang3.StringUtils
import java.awt.event.*
import java.util.concurrent.CompletableFuture
import java.util.concurrent.Future
import javax.swing.*
import kotlin.coroutines.cancellation.CancellationException

Expand Down Expand Up @@ -203,25 +204,17 @@ class FuzzyMover : FuzzyAction() {
component.fileList.setPaintBusy(true)
var listModel = DefaultListModel<FuzzyMatchContainer>()

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

if (task?.isCancelled == true) throw CancellationException()
if (task?.isCancelled == true) return@executeOnPooledThread

val moduleManager = ModuleManager.getInstance(project)
if (fuzzierSettingsService.state.isProject) {
processProject(project, stringEvaluator, searchString, listModel)
} else {
processModules(moduleManager, stringEvaluator, searchString, listModel)
}
process(project, stringEvaluator, searchString, listModel, task)

if (task?.isCancelled == true) throw CancellationException()
if (task?.isCancelled == true) return@executeOnPooledThread

listModel = fuzzierUtil.sortAndLimit(listModel, true)

if (task?.isCancelled == true) throw CancellationException()
if (task?.isCancelled == true) return@executeOnPooledThread

ApplicationManager.getApplication().invokeLater {
component.fileList.model = listModel
Expand All @@ -231,31 +224,50 @@ class FuzzyMover : FuzzyAction() {
component.fileList.setSelectedValue(listModel[0], true)
}
}
} catch (e: InterruptedException) {
return@executeOnPooledThread
} catch (e: CancellationException) {
// Do nothing
return@executeOnPooledThread
}
}
}

private fun getStringEvaluator(): StringEvaluator {
return StringEvaluator(
fuzzierSettingsService.state.exclusionSet,
fuzzierSettingsService.state.modules
)
}

private fun process(project: Project, stringEvaluator: StringEvaluator, searchString: String,
listModel: DefaultListModel<FuzzyMatchContainer>, task: Future<*>?) {
val moduleManager = ModuleManager.getInstance(project)
if (fuzzierSettingsService.state.isProject) {
processProject(project, stringEvaluator, searchString, listModel, task)
} else {
processModules(moduleManager, stringEvaluator, searchString, listModel, task)
}
}

private fun processProject(project: Project, stringEvaluator: StringEvaluator,
searchString: String, listModel: DefaultListModel<FuzzyMatchContainer>) {
searchString: String, listModel: DefaultListModel<FuzzyMatchContainer>, task: Future<*>?) {
val contentIterator = if (!component.isDirSelector) {
stringEvaluator.getContentIterator(project.name, searchString, listModel)
stringEvaluator.getContentIterator(project.name, searchString, listModel, task)
} else {
stringEvaluator.getDirIterator(project.name, searchString, listModel)
stringEvaluator.getDirIterator(project.name, searchString, listModel, task)
}
ProjectFileIndex.getInstance(project).iterateContent(contentIterator)
}

private fun processModules(moduleManager: ModuleManager, stringEvaluator: StringEvaluator,
searchString: String, listModel: DefaultListModel<FuzzyMatchContainer>) {
searchString: String, listModel: DefaultListModel<FuzzyMatchContainer>, task: Future<*>?) {
for (module in moduleManager.modules) {
val moduleFileIndex = module.rootManager.fileIndex

val contentIterator = if (!component.isDirSelector) {
stringEvaluator.getContentIterator(module.name, searchString, listModel)
stringEvaluator.getContentIterator(module.name, searchString, listModel, task)
} else {
stringEvaluator.getDirIterator(module.name, searchString, listModel)
stringEvaluator.getDirIterator(module.name, searchString, listModel, task)
}
moduleFileIndex.iterateContent(contentIterator)
}
Expand Down
13 changes: 11 additions & 2 deletions src/main/kotlin/com/mituuz/fuzzier/StringEvaluator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.intellij.openapi.vcs.changes.ChangeListManager
import com.intellij.openapi.vfs.VirtualFile
import com.mituuz.fuzzier.entities.FuzzyMatchContainer
import com.mituuz.fuzzier.entities.ScoreCalculator
import java.util.concurrent.Future
import javax.swing.DefaultListModel

/**
Expand All @@ -42,9 +43,13 @@ class StringEvaluator(
) {
lateinit var scoreCalculator: ScoreCalculator

fun getContentIterator(moduleName: String, searchString: String, listModel: DefaultListModel<FuzzyMatchContainer>): ContentIterator {
fun getContentIterator(moduleName: String, searchString: String, listModel: DefaultListModel<FuzzyMatchContainer>,
task: Future<*>?): ContentIterator {
scoreCalculator = ScoreCalculator(searchString)
return ContentIterator { file: VirtualFile ->
if (task?.isCancelled == true) {
return@ContentIterator false
}
if (!file.isDirectory) {
val moduleBasePath = modules[moduleName] ?: return@ContentIterator true

Expand All @@ -63,9 +68,13 @@ class StringEvaluator(
}
}

fun getDirIterator(moduleName: String, searchString: String, listModel: DefaultListModel<FuzzyMatchContainer>): ContentIterator {
fun getDirIterator(moduleName: String, searchString: String, listModel: DefaultListModel<FuzzyMatchContainer>,
task: Future<*>?): ContentIterator {
scoreCalculator = ScoreCalculator(searchString)
return ContentIterator { file: VirtualFile ->
if (task?.isCancelled == true) {
return@ContentIterator false
}
if (file.isDirectory) {
val moduleBasePath = modules[moduleName] ?: return@ContentIterator true
val filePath = getDirPath(file, moduleBasePath, moduleName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,7 @@ class TestBenchComponent : JPanel() {
table.setPaintBusy(true)
val listModel = DefaultListModel<FuzzyMatchContainer>()

val moduleManager = ModuleManager.getInstance(project)
if (service<FuzzierSettingsService>().state.isProject) {
processProject(project, stringEvaluator, searchString, listModel)
} else {
processModules(moduleManager, stringEvaluator, searchString, listModel)
}
process(project, stringEvaluator, searchString, listModel)

val sortedList = listModel.elements().toList().sortedByDescending { it.getScore() }
val data = sortedList.map {
Expand All @@ -163,10 +158,20 @@ class TestBenchComponent : JPanel() {
table.setPaintBusy(false)
}
}

private fun process(project: Project, stringEvaluator: StringEvaluator, searchString: String,
listModel: DefaultListModel<FuzzyMatchContainer>) {
val moduleManager = ModuleManager.getInstance(project)
if (service<FuzzierSettingsService>().state.isProject) {
processProject(project, stringEvaluator, searchString, listModel)
} else {
processModules(moduleManager, stringEvaluator, searchString, listModel)
}
}

private fun processProject(project: Project, stringEvaluator: StringEvaluator,
searchString: String, listModel: DefaultListModel<FuzzyMatchContainer>) {
val contentIterator = stringEvaluator.getContentIterator(project.name, searchString, listModel)
val contentIterator = stringEvaluator.getContentIterator(project.name, searchString, listModel, null)

val scoreCalculator = stringEvaluator.scoreCalculator
scoreCalculator.setMultiMatch(liveSettingsComponent.multiMatchActive.getCheckBox().isSelected)
Expand All @@ -181,7 +186,7 @@ class TestBenchComponent : JPanel() {
searchString: String, listModel: DefaultListModel<FuzzyMatchContainer>) {
for (module in moduleManager.modules) {
val moduleFileIndex = module.rootManager.fileIndex
val contentIterator = stringEvaluator.getContentIterator(module.name, searchString, listModel)
val contentIterator = stringEvaluator.getContentIterator(module.name, searchString, listModel, null)

val scoreCalculator = stringEvaluator.scoreCalculator
scoreCalculator.setMultiMatch(liveSettingsComponent.multiMatchActive.getCheckBox().isSelected)
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/com/mituuz/fuzzier/TestUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class TestUtil {
stringEvaluator = StringEvaluator(exclusionList, map)
}

val contentIterator = stringEvaluator.getContentIterator(myFixture.module.name, "", filePathContainer)
val contentIterator = stringEvaluator.getContentIterator(myFixture.module.name, "", filePathContainer, null)
val index = myFixture.module.rootManager.fileIndex
runInEdtAndWait {
index.iterateContent(contentIterator)
Expand Down

0 comments on commit 7b0e0a9

Please sign in to comment.