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