Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle colons in file names when producing SemanticDB #15863

Merged
merged 1 commit into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/semanticdb/Tools.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ object Tools:
def mkURIstring(path: Path): String =
// Calling `.toUri` on a relative path will convert it to absolute. Iteration through its parts instead preserves
// the resulting URI as relative.
val uriParts = for part <- path.asScala yield new java.net.URI(null, null, part.toString, null)
uriParts.mkString("/")
// To prevent colon `:` from being treated as a scheme separator, prepend a slash `/` to each part to trick the URI
// parser into treating it as an absolute path, and then strip the spurious leading slash from the final result.
val uriParts = for part <- path.asScala yield new java.net.URI(null, null, "/" + part.toString, null)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like this URI constructor is just not the right tool for the job...

Copy link
Contributor Author

@kierendavies kierendavies Aug 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a poor fit for sure, but sadly the Java standard library doesn't provide anything better. I considered copy-pasting the OpenJDK private method on java.net.URI that does the quoting, but it's, uh, very Java. And then there's also license compatibility.

This comment was marked as duplicate.

uriParts.mkString.stripPrefix("/")

/** Load SemanticDB TextDocument for a single Scala source file
*
Expand Down
3 changes: 3 additions & 0 deletions tests/semanticdb/expect/example-dir/FileInDir.expect.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package example

class FileInDir/*<-example::FileInDir#*/
3 changes: 3 additions & 0 deletions tests/semanticdb/expect/example-dir/FileInDir.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package example

class FileInDir
19 changes: 19 additions & 0 deletions tests/semanticdb/metac.expect
Original file line number Diff line number Diff line change
Expand Up @@ -3811,6 +3811,25 @@ Occurrences:
[6:19..6:20): U -> local0
[6:24..6:27): ??? -> scala/Predef.`???`().

expect/example-dir/FileInDir.scala
----------------------------------

Summary:
Schema => SemanticDB v4
Uri => example-dir/FileInDir.scala
Text => empty
Language => Scala
Symbols => 2 entries
Occurrences => 2 entries

Symbols:
example/FileInDir# => class FileInDir extends Object { self: FileInDir => +1 decls }
example/FileInDir#`<init>`(). => primary ctor <init> (): FileInDir

Occurrences:
[0:8..0:15): example <- example/
[2:6..2:15): FileInDir <- example/FileInDir#

expect/exports-example-Codec.scala
----------------------------------

Expand Down