-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use RFC 3986-compliant URIs for file paths (#226)
Fixes #218 The Java URIs uses a different standard (RFC 2396) for encoding than the library vscode-uri used by Cody agent (RFC 3986). Due to the fact that I was unable to find a reasonable library, this patch merely imitates the encoding and normalization of such addresses. Additional change: Remove unused CodyAgentDocuments. ## Test Plan 1. Verify basic features on Windows 2. Verify basic features on Linux
- Loading branch information
Showing
9 changed files
with
57 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 0 additions & 49 deletions
49
src/main/java/com/sourcegraph/cody/agent/CodyAgentDocuments.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 17 additions & 15 deletions
32
src/main/kotlin/com/sourcegraph/cody/agent/protocol/TextDocument.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,23 @@ | ||
package com.sourcegraph.cody.agent.protocol | ||
|
||
import java.net.URI | ||
import com.sourcegraph.cody.agent.protocol.util.Rfc3986UriEncoder | ||
import java.nio.file.Paths | ||
|
||
data class TextDocument | ||
// JvmOverloads needed until CodyAgentFocusListener | ||
// and CodyFileEditorListener are converted to Kotlin. | ||
@JvmOverloads | ||
constructor( | ||
var uri: URI, | ||
var content: String? = null, | ||
var selection: Range? = null, | ||
class TextDocument | ||
private constructor( | ||
var uri: String, | ||
var content: String?, | ||
var selection: Range?, | ||
) { | ||
@JvmOverloads | ||
constructor( | ||
filePath: String, | ||
content: String? = null, | ||
selection: Range? = null, | ||
) : this(Paths.get(filePath).toUri(), content, selection) | ||
|
||
companion object { | ||
|
||
@JvmStatic | ||
@JvmOverloads | ||
fun fromPath(path: String, content: String? = null, selection: Range? = null): TextDocument { | ||
val uri = Paths.get(path).toUri().toString() | ||
val rfc3986Uri = Rfc3986UriEncoder.encode(uri) | ||
return TextDocument(rfc3986Uri, content, selection) | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/com/sourcegraph/cody/agent/protocol/util/Rfc3986UriEncoder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.sourcegraph.cody.agent.protocol.util | ||
|
||
object Rfc3986UriEncoder { | ||
|
||
// todo solve this with library | ||
fun encode(uri: String): String { | ||
val isWindowsPath = uri.matches("^file:///[A-Za-z]:.*".toRegex()) | ||
if (isWindowsPath) { | ||
val found = "file:///([A-Za-z]):".toRegex().find(uri)!! | ||
val partition = found.groups[1]!!.value | ||
return uri.replace("file:///$partition:", "file:///${partition.lowercase()}%3A") | ||
} | ||
return uri | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/test/kotlin/com/sourcegraph/cody/agent/protocol/util/Rfc3986UriEncoderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.sourcegraph.cody.agent.protocol.util | ||
|
||
import junit.framework.TestCase | ||
|
||
class Rfc3986UriEncoderTest : TestCase() { | ||
|
||
fun `test encode Windows path`() { | ||
val fixedUri = Rfc3986UriEncoder.encode("file:///C:/Users/user/Test.java") | ||
assertEquals("file:///c%3A/Users/user/Test.java", fixedUri) | ||
} | ||
|
||
fun `test encode Windows path with lowercase partition`() { | ||
val fixedUri = Rfc3986UriEncoder.encode("file:///c:/Users/user/Test.java") | ||
assertEquals("file:///c%3A/Users/user/Test.java", fixedUri) | ||
} | ||
|
||
fun `test encode Linux path`() { | ||
val uri = Rfc3986UriEncoder.encode("file:///home/user/Test.java") | ||
assertEquals("file:///home/user/Test.java", uri) | ||
} | ||
} |