Skip to content

Commit 795ec44

Browse files
committed
Move progressbar while importing/exporting type contracts
1 parent d9f9310 commit 795ec44

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

ide-plugin/src/org/jetbrains/plugins/ruby/ruby/actions/ImportExportContractsAction.kt

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.intellij.openapi.actionSystem.AnActionEvent
44
import com.intellij.openapi.fileChooser.FileChooserDescriptor
55
import com.intellij.openapi.fileChooser.ex.FileChooserDialogImpl
66
import com.intellij.openapi.module.Module
7+
import com.intellij.openapi.progress.ProgressIndicator
78
import com.intellij.openapi.progress.ProgressManager
89
import com.intellij.openapi.project.DumbAwareAction
910
import com.intellij.openapi.project.Project
@@ -22,7 +23,15 @@ import java.io.File
2223

2324
const val CHUNK_SIZE = 1500
2425

25-
fun Database.copyTo(destination: Database) {
26+
fun Database.copyTo(destination: Database, moveProgressBar: Boolean) {
27+
var progressIndicator: ProgressIndicator? = null
28+
var count: Int? = null
29+
30+
if (moveProgressBar) {
31+
progressIndicator = ProgressManager.getInstance().progressIndicator
32+
count = transaction(this) { CallInfoTable.selectAll().count() }
33+
}
34+
2635
var offset = 0
2736
while (true) {
2837
val info: List<CallInfo> = transaction(this) {
@@ -35,7 +44,12 @@ fun Database.copyTo(destination: Database) {
3544
transaction(destination) {
3645
info.forEach { CallInfoTable.insertInfoIfNotContains(it) }
3746
}
47+
3848
offset += CHUNK_SIZE
49+
50+
if (moveProgressBar) {
51+
progressIndicator!!.fraction = offset.toDouble() / count!!
52+
}
3953
}
4054
}
4155

@@ -45,11 +59,11 @@ class ExportContractsAction : ExportFileActionBase(
4559
extensions = arrayOf("mv.db")
4660
) {
4761
override fun backgroundProcess(absoluteFilePath: String, module: Module?, sdk: Sdk?, project: Project) {
48-
exportContractsToFile(absoluteFilePath)
62+
exportContractsToFile(absoluteFilePath, moveProgressBar = true)
4963
}
5064

5165
companion object {
52-
fun exportContractsToFile(pathToExport: String) {
66+
fun exportContractsToFile(pathToExport: String, moveProgressBar: Boolean) {
5367
check(pathToExport.endsWith(DatabaseProvider.H2_DB_FILE_EXTENSION)) {
5468
"Path to export must end with .mv.db"
5569
}
@@ -58,7 +72,7 @@ class ExportContractsAction : ExportFileActionBase(
5872
val databaseToExportTo = DatabaseProvider.connectToDB(pathToExport)
5973
DatabaseProvider.createAllDatabases(databaseToExportTo)
6074

61-
DatabaseProvider.defaultDatabase!!.copyTo(databaseToExportTo)
75+
DatabaseProvider.defaultDatabase!!.copyTo(databaseToExportTo, moveProgressBar)
6276
}
6377
}
6478
}
@@ -76,25 +90,24 @@ class ImportContractsAction : DumbAwareAction() {
7690

7791
try {
7892
ProgressManager.getInstance().runProcessWithProgressSynchronously(ThrowableComputable<Unit, Exception> {
79-
files.forEach { importContractsFromFile(it.path) }
93+
files.forEach { importContractsFromFile(it.path, moveProgressBar = true) }
8094
return@ThrowableComputable
8195
}, "Importing type contracts", false, project)
8296
resetAllRubyTypeProviderAndIDEACaches(project)
83-
Messages.showDialog("Type contracts imported successfully", "Info", arrayOf("OK"), 0, null)
8497
} catch (ex: Exception) {
8598
Messages.showErrorDialog(ex.message, "Error while importing type contracts")
8699
}
87100
}
88101

89102
companion object {
90-
fun importContractsFromFile(pathToImportFrom: String) {
103+
fun importContractsFromFile(pathToImportFrom: String, moveProgressBar: Boolean) {
91104
check(pathToImportFrom.endsWith(DatabaseProvider.H2_DB_FILE_EXTENSION)) {
92105
"Path to import from must end with .mv.db"
93106
}
94107

95108
val dbToImportFrom = DatabaseProvider.connectToDB(pathToImportFrom)
96109

97-
dbToImportFrom.copyTo(DatabaseProvider.defaultDatabase!!)
110+
dbToImportFrom.copyTo(DatabaseProvider.defaultDatabase!!, moveProgressBar)
98111
}
99112
}
100113
}

ide-plugin/src/test/java/org/jetbrains/plugins/ruby/ruby/actions/ImportExportTests.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.jetbrains.plugins.ruby.ruby.actions
22

3-
import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase
43
import junit.framework.Assert
54
import junit.framework.TestCase
65
import org.jetbrains.exposed.sql.Database
@@ -27,7 +26,7 @@ class ImportExportTests : TestCase() {
2726
}
2827

2928
val exportedDB = generateTempDBFilePath().let { pathToExport: String ->
30-
ExportContractsAction.exportContractsToFile(pathToExport)
29+
ExportContractsAction.exportContractsToFile(pathToExport, moveProgressBar = false)
3130

3231
return@let DatabaseProvider.connectToDB(pathToExport)
3332
}
@@ -52,7 +51,7 @@ class ImportExportTests : TestCase() {
5251
data.forEach { CallInfoTable.insertInfoIfNotContains(it) }
5352
}
5453

55-
ImportContractsAction.importContractsFromFile(pathToImport)
54+
ImportContractsAction.importContractsFromFile(pathToImport, moveProgressBar = false)
5655

5756
return@let db
5857
}
@@ -89,7 +88,7 @@ class ImportExportTests : TestCase() {
8988
data.forEach { CallInfoTable.insertInfoIfNotContains(it) }
9089
}
9190

92-
ImportContractsAction.importContractsFromFile(pathToImport)
91+
ImportContractsAction.importContractsFromFile(pathToImport, moveProgressBar = false)
9392

9493
return@let db
9594
}

0 commit comments

Comments
 (0)