Skip to content

Commit

Permalink
fix: uri encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamil Podsiadlo committed Apr 5, 2022
1 parent c3fbfe9 commit 7be48da
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package scala.meta.internal.metals
package scala.meta.internal.mtags

object URIEncoderDecoder {

// https://en.wikipedia.org/wiki/Percent-encoding
private val toEscape: Map[Char, String] =
Set('"', '<', '>', '&', '\'', '[', ']', '{', '}', ' ')
.map(char => char -> ("%" + char.toHexString))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,13 @@ trait MtagsEnrichments extends CommonMtagsEnrichments {
URLDecoder.decode(value, "UTF-8").toAbsolutePath(followSymlink)
else if (value.toUpperCase.startsWith("JAR:FILE%3A"))
URLDecoder.decode(value, "UTF-8").toAbsolutePath(followSymlink)
else
URI.create(value.stripPrefix("metals:")).toAbsolutePath(followSymlink)
else if (value.toUpperCase.startsWith("JAR"))
URI.create(value).toAbsolutePath(followSymlink)
else {
val stripped = value.stripPrefix("metals:")
val percentEncoded = URIEncoderDecoder.encode(stripped)
URI.create(percentEncoded).toAbsolutePath(followSymlink)
}
}
def lastIndexBetween(
char: Char,
Expand Down
9 changes: 8 additions & 1 deletion tests/unit/src/test/scala/tests/MetalsEnrichmentsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ class MetalsEnrichmentsSuite extends BaseSuite {
assertEquals(relativeCreated, expected)
}

test("uri-to-absolute-path") {
test("dont-decode-uri") {
val uri =
"file:///Users/happyMetalsUser/hello%20space%20world/src/main/scala/Main.scala"
val path = uri.toAbsolutePath
assert(path.toString().contains("hello space world"))
}

test("encode-uri-space") {
val uri =
"file:///Users/happyMetalsUser/hello space+world/src/main/scala/Main.scala"
val path = uri.toAbsolutePath
assert(path.toString().contains("hello space+world"))
}
}

0 comments on commit 7be48da

Please sign in to comment.