@@ -8,22 +8,24 @@ import com.intellij.openapi.project.guessProjectDir
8
8
import com.intellij.openapi.ui.ComboBox
9
9
import com.intellij.openapi.ui.ComponentWithBrowseButton
10
10
import com.intellij.openapi.ui.FixedSizeButton
11
+ import com.intellij.openapi.util.text.StringUtil
11
12
import com.intellij.openapi.vfs.VirtualFile
12
13
import com.intellij.openapi.vfs.newvfs.impl.FakeVirtualFile
13
14
import com.intellij.ui.ColoredListCellRenderer
14
15
import com.intellij.ui.SimpleTextAttributes
15
16
import com.intellij.util.ArrayUtil
16
17
import com.intellij.util.ui.UIUtil
17
18
import java.io.File
19
+ import java.util.Comparator
18
20
import javax.swing.DefaultComboBoxModel
19
21
import javax.swing.JList
20
- import org.jetbrains.kotlin.idea.util.projectStructure.allModules
22
+ import org.jetbrains.kotlin.idea.util.rootManager
21
23
import org.utbot.common.PathUtil
24
+ import org.utbot.intellij.plugin.generator.CodeGenerationController.getAllTestSourceRoots
22
25
import org.utbot.intellij.plugin.models.GenerateTestsModel
23
26
import org.utbot.intellij.plugin.ui.utils.TestSourceRoot
24
27
import org.utbot.intellij.plugin.ui.utils.addDedicatedTestRoot
25
28
import org.utbot.intellij.plugin.ui.utils.isBuildWithGradle
26
- import org.utbot.intellij.plugin.ui.utils.suitableTestSourceRoots
27
29
28
30
class TestFolderComboWithBrowseButton (private val model : GenerateTestsModel ) :
29
31
ComponentWithBrowseButton <ComboBox <Any >>(ComboBox (), null ) {
@@ -57,20 +59,42 @@ class TestFolderComboWithBrowseButton(private val model: GenerateTestsModel) :
57
59
}
58
60
}
59
61
60
- val suggestedModules =
61
- if (model.project.isBuildWithGradle) model.project.allModules() else model.potentialTestModules
62
+ var commonModuleSourceDirectory = " "
63
+ for ((i, sourceRoot) in model.srcModule.rootManager.sourceRoots.withIndex()) {
64
+ commonModuleSourceDirectory = if (i == 0 ) {
65
+ sourceRoot.toNioPath().toString()
66
+ } else {
67
+ StringUtil .commonPrefix(commonModuleSourceDirectory, sourceRoot.toNioPath().toString())
68
+ }
69
+ }
70
+ // The first sorting to obtain the best candidate
71
+ val testRoots = model.getAllTestSourceRoots().distinct().sortedWith(object : Comparator <TestSourceRoot > {
72
+ override fun compare (o1 : TestSourceRoot , o2 : TestSourceRoot ): Int {
73
+ // Heuristics: Dirs with language == codegenLanguage should go first
74
+ val languageOrder = (o1.expectedLanguage == model.codegenLanguage).compareTo(o2.expectedLanguage == model.codegenLanguage)
75
+ if (languageOrder != 0 ) return - languageOrder
76
+ // Heuristics: move root that is 'closer' to module 'common' directory to the first position
77
+ return - StringUtil .commonPrefixLength(commonModuleSourceDirectory, o1.dir.toNioPath().toString())
78
+ .compareTo(StringUtil .commonPrefixLength(commonModuleSourceDirectory, o2.dir.toNioPath().toString()))
79
+ }
80
+ }).toMutableList()
81
+
82
+ val theBest = if (testRoots.isNotEmpty()) testRoots[0 ] else null
62
83
63
- val testRoots = suggestedModules.flatMap {
64
- it.suitableTestSourceRoots()
65
- }.sortedWith(
66
- compareByDescending<TestSourceRoot > {
84
+ // The second sorting to make full list ordered
85
+ testRoots.sortWith(compareByDescending<TestSourceRoot > {
67
86
// Heuristics: Dirs with language == codegenLanguage should go first
68
87
it.expectedLanguage == model.codegenLanguage
69
88
}.thenBy {
70
- // Heuristics: User is more likely to choose the shorter path
71
- it.dir.path.length
89
+ // ABC-sorting
90
+ it.dir.toNioPath()
72
91
}
73
- ).toMutableList()
92
+ )
93
+ // The best candidate should go first to be pre-selected
94
+ theBest?.let {
95
+ testRoots.remove(it)
96
+ testRoots.add(0 , it)
97
+ }
74
98
75
99
// this method is blocked for Gradle, where multiple test modules can exist
76
100
model.testModule.addDedicatedTestRoot(testRoots, model.codegenLanguage)
0 commit comments