Skip to content

Commit f597624

Browse files
committed
fix(intellij): make comparing testitem uri's platform independend
1 parent f7b797c commit f597624

File tree

4 files changed

+52
-10
lines changed

4 files changed

+52
-10
lines changed

Diff for: intellij-client/src/main/kotlin/dev/robotcode/robotcode4ij/RobotIcons.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package dev.robotcode.robotcode4ij
22

3-
import com.intellij.openapi.util.IconLoader
43
import com.intellij.ui.IconManager
54

65
class RobotIcons {
76
companion object {
87
@JvmField
98
val Resource = IconManager.getInstance().getIcon("/images/resource.svg", Companion::class.java.classLoader)
9+
1010
@JvmField
1111
val Suite = IconManager.getInstance().getIcon("/images/suite.svg", Companion::class.java.classLoader)
12+
1213
@JvmField
1314
val RobotCode = IconManager.getInstance().getIcon("/images/robotcode.svg", Companion::class.java.classLoader)
15+
1416
@JvmField
1517
val Robot = IconManager.getInstance().getIcon("/images/suite.robot", Companion::class.java.classLoader)
1618
}

Diff for: intellij-client/src/main/kotlin/dev/robotcode/robotcode4ij/lsp/RobotCodeServerApi.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ import org.eclipse.lsp4j.services.LanguageServer
55
import java.util.concurrent.CompletableFuture
66

77

8-
interface RobotCodeServerApi: LanguageServer {
8+
interface RobotCodeServerApi : LanguageServer {
99
@JsonRequest("robot/cache/clear") fun clearCache(): CompletableFuture<Void>?
1010
}

Diff for: intellij-client/src/main/kotlin/dev/robotcode/robotcode4ij/testing/DataItems.kt

+28
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
package dev.robotcode.robotcode4ij.testing
22

33
import kotlinx.serialization.Serializable
4+
import kotlinx.serialization.Transient
45
import kotlinx.serialization.json.JsonElement
6+
import java.net.URI
7+
8+
private object UriCache {
9+
const val MAX_SIZE = 512
10+
11+
private val cache = object : LinkedHashMap<String, URI>(MAX_SIZE, 0.75f, true) {
12+
override fun removeEldestEntry(eldest: MutableMap.MutableEntry<String, URI>?): Boolean {
13+
return size >= MAX_SIZE
14+
}
15+
}
16+
17+
fun get(uri: String): URI {
18+
return cache.getOrPut(uri) { URI.create(uri) }
19+
}
20+
}
521

622
@Serializable
723
data class Position(val line: UInt, val character: UInt)
@@ -68,6 +84,18 @@ data class Position(val line: UInt, val character: UInt)
6884
result = 31 * result + (tags?.contentHashCode() ?: 0)
6985
return result
7086
}
87+
88+
@Transient
89+
val realUri: URI? = if (uri != null) UriCache.get(uri) else null
90+
91+
fun isSameUri(uri: String?): Boolean {
92+
if (realUri == null || uri == null)
93+
return false
94+
95+
val otherUri = UriCache.get(uri)
96+
97+
return otherUri.scheme == realUri.scheme && otherUri.path == realUri.path
98+
}
7199
}
72100

73101
@Serializable data class RobotCodeDiscoverResult(

Diff for: intellij-client/src/main/kotlin/dev/robotcode/robotcode4ij/testing/RobotCodeTestManager.kt

+20-8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import com.intellij.psi.PsiDocumentManager
2525
import com.intellij.psi.PsiElement
2626
import com.intellij.psi.util.elementType
2727
import com.intellij.psi.util.startOffset
28-
import com.intellij.util.io.URLUtil
2928
import dev.robotcode.robotcode4ij.RobotSuiteFileType
3029
import dev.robotcode.robotcode4ij.buildRobotCodeCommandLine
3130
import dev.robotcode.robotcode4ij.psi.IRobotFrameworkElementType
@@ -38,10 +37,9 @@ import kotlinx.coroutines.launch
3837
import kotlinx.coroutines.runBlocking
3938
import kotlinx.serialization.encodeToString
4039
import kotlinx.serialization.json.Json
41-
import java.net.URI
40+
import java.nio.file.Paths
4241
import java.util.*
4342

44-
4543
@Service(Service.Level.PROJECT) class RobotCodeTestManager(private val project: Project) : Disposable, DocumentListener,
4644
AsyncFileListener,
4745
FileEditorManagerListener {
@@ -229,7 +227,7 @@ import java.util.*
229227
arrayOf(*defaultPaths, "discover", "--read-from-stdin", "all"), format = "json"
230228
).withCharset(Charsets.UTF_8).withWorkDirectory(project.basePath)
231229

232-
var openFiles = mutableMapOf<String, String>()
230+
val openFiles = mutableMapOf<String, String>()
233231

234232
ApplicationManager.getApplication().runReadAction {
235233
FileEditorManagerEx.getInstanceEx(project).openFiles.forEach { file ->
@@ -239,7 +237,7 @@ import java.util.*
239237
}
240238
}
241239

242-
var openFilesAsString = Json.encodeToString(openFiles)
240+
val openFilesAsString = Json.encodeToString(openFiles)
243241

244242
val result = CapturingProcessHandler(cmdLine).apply {
245243
process.outputStream.bufferedWriter().apply {
@@ -276,11 +274,11 @@ import java.util.*
276274
): RobotCodeTestItem? {
277275

278276
if (line == null) {
279-
if (root.uri == uri) {
277+
if (root.isSameUri(uri)) {
280278
return root
281279
}
282280
} else {
283-
if (root.uri == uri && root.range != null && root.range.start.line == line) {
281+
if (root.isSameUri(uri) && root.range != null && root.range.start.line == line) {
284282
return root
285283
}
286284
}
@@ -334,9 +332,23 @@ import java.util.*
334332
}
335333
}
336334

335+
private fun getRfcCompliantUri(virtualFile: VirtualFile): String {
336+
val filePath = virtualFile.path
337+
338+
val normalizedPath = if (isWindows()) {
339+
filePath.replace("\\", "/")
340+
} else {
341+
filePath
342+
}
343+
344+
return Paths.get(normalizedPath).toUri().toString()
345+
}
346+
347+
private fun isWindows(): Boolean = System.getProperty("os.name").lowercase().contains("win")
348+
337349
val VirtualFile.uri: String
338350
get() {
339-
return URI.create(fileSystem.protocol + URLUtil.SCHEME_SEPARATOR + "/" + path.replace(":", "%3A")).toString()
351+
return getRfcCompliantUri(this)
340352
}
341353

342354
val Project.testManger: RobotCodeTestManager

0 commit comments

Comments
 (0)