Skip to content

Commit 4edd547

Browse files
authored
normalize file uri from resharper (#173)
1 parent beb28a8 commit 4edd547

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

rider/src/main/kotlin/org/digma/intellij/plugin/rider/protocol/CodeObjectHost.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ class CodeObjectHost(project: Project): LifetimedProjectComponent(project) {
2626

2727
//Note: using the file uri as the document key in rider protocol proved to be unstable because of
2828
// differences in conversion between linux and windows.
29-
// so the document key is just its full path without the URI schema 'file:///'
30-
// the Document has a fileUri field which is a uri with schema and is used to find a psi file in rider the frontend.
29+
//when running on windows the file:// schema has different number of slashes between resharper and the jvm,
30+
// and so it's not possible to use it as a map key in both sides.
31+
// so the document key is just its full path without the URI schema 'file:///', in resharper we use just the path
32+
// as the map key and in java we take the path from the PsiFile.
33+
// the Document has a fileUri field which is an uri with schema and is used to find a psi file in rider frontend.
34+
// PsuUtil.uriToPsiFile work ok with both the uri from resharper and the uri in the jvm side.
3135

3236

3337
fun getDocument(psiFile: PsiFile): DocumentInfo? {
@@ -121,7 +125,7 @@ class CodeObjectHost(project: Project): LifetimedProjectComponent(project) {
121125

122126

123127
private fun Document.toDocumentInfo() = DocumentInfo(
124-
fileUri = fileUri,
128+
fileUri = normalizeFileUri(fileUri,project),
125129
methods = toMethodInfoMap(methods)
126130

127131
)
@@ -140,7 +144,7 @@ class CodeObjectHost(project: Project): LifetimedProjectComponent(project) {
140144
name = name,
141145
containingClass = containingClass,
142146
containingNamespace = containingNamespace,
143-
containingFileUri = containingFileUri,
147+
containingFileUri = normalizeFileUri(containingFileUri,project),
144148
offsetAtFileUri = offsetAtFileUri,
145149
spans = toSpansList(spans)
146150
)
@@ -157,7 +161,7 @@ class CodeObjectHost(project: Project): LifetimedProjectComponent(project) {
157161
id = id,
158162
name = name,
159163
containingMethod = containingMethod,
160-
containingFileUri = containingFileUri
164+
containingFileUri = normalizeFileUri(containingFileUri,project)
161165
)
162166

163167

rider/src/main/kotlin/org/digma/intellij/plugin/rider/protocol/ElementUnderCaretDetector.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,11 @@ class ElementUnderCaretDetector(project: Project) : LifetimedProjectComponent(pr
8585
id = fqn,
8686
name = name,
8787
className = className,
88-
fileUri = fileUri,
88+
fileUri = normalizeFileUri(fileUri,project),
8989
isSupportedFile = isSupportedFile
9090
)
9191

9292

93+
94+
9395
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
@file:JvmName("Protocol")
22
package org.digma.intellij.plugin.rider.protocol
33

4+
import com.intellij.openapi.project.Project
5+
import org.digma.intellij.plugin.psi.PsiUtils
6+
47

58
//extensions to protocol classes can be written here
9+
10+
/*
11+
There is an issue with file uri when running on windows,
12+
the uri has a file:// schema,
13+
when calling in resharper IPsiSourceFile.GetLocation().ToUri().ToString() it will return a file schema with different
14+
number of slashes then when calling in the java side to PsiFile.getVirtualFile().getUrl().
15+
it causes issues when using the uri in various maps.
16+
the solution here is to always normalize the uri that comes from the resharper side to a uri as comes in the jvm side.
17+
so actually every object that passes from resharper to the jvm and has a uri that was taken from
18+
IPsiSourceFile.GetLocation().ToUri().ToString() needs to be normalized.
19+
PsiUtils.uriToPsiFile(fileUri,project) will always work , with 2 or with 3 slashes.
20+
not all file uri in all objects is used, the main one that causes issues if the MethodUnderCaretEvent.fileUri,
21+
but we normalize all the objects.
22+
*/
23+
fun normalizeFileUri(fileUri: String,project:Project): String {
24+
25+
if (fileUri.isBlank()){
26+
return fileUri
27+
}
28+
29+
val psiFile = PsiUtils.uriToPsiFile(fileUri,project)
30+
return PsiUtils.psiFileToUri(psiFile)
31+
}

0 commit comments

Comments
 (0)