Skip to content

Commit 7c3ddc3

Browse files
authored
Add jpath to VirtualFile (for pc) (#23203)
scalameta/metals#7460
1 parent fd72f1a commit 7c3ddc3

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

compiler/src/dotty/tools/dotc/util/SourceFile.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,7 @@ object SourceFile {
229229
* It relies on SourceFile#virtual implementation to create the virtual file.
230230
*/
231231
def virtual(uri: URI, content: String): SourceFile =
232-
val path = Paths.get(uri).toString
233-
SourceFile.virtual(path, content)
232+
SourceFile(new VirtualFile(Paths.get(uri), content.getBytes(StandardCharsets.UTF_8)), content.toCharArray)
234233

235234
/** Returns the relative path of `source` within the `reference` path
236235
*

compiler/src/dotty/tools/io/VirtualFile.scala

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,34 @@ class VirtualFile(val name: String, override val path: String) extends AbstractF
4040
this.content = content
4141
}
4242

43+
/**
44+
* Initializes this instance with the specified path
45+
* and a name taken from the last path element.
46+
*
47+
* @param path the path of the virtual file to be created
48+
* @param content the initial contents of the virtual file
49+
* @return the created virtual file
50+
*/
51+
def this(path: JPath, content: Array[Byte]) = {
52+
this(path.getFileName().toString(), path.toString())
53+
this.content = content
54+
this.jpath_ = path
55+
}
56+
4357
private var content = Array.emptyByteArray
4458

59+
private var jpath_ : JPath = null
60+
4561
def absolute: AbstractFile = this
4662

47-
/** Returns null. */
48-
def jpath: JPath = null
63+
/** Returns path, which might be a non-existing file or null. */
64+
def jpath: JPath = jpath_
4965

5066
override def sizeOption: Option[Int] = Some(content.length)
5167

68+
/** Always returns true, even if jpath is a non-existing file. */
69+
override def exists: Boolean = true
70+
5271
def input : InputStream = new ByteArrayInputStream(content)
5372

5473
override def output: OutputStream = {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package tests.macros
2+
3+
import scala.quoted.*
4+
5+
object Macros7460 {
6+
7+
transparent inline def foo: String =
8+
${ fooImpl }
9+
10+
private def fooImpl(using Quotes): Expr[String] =
11+
Expr("foo...")
12+
13+
transparent inline def bar: String =
14+
${ barImpl }
15+
16+
private def barImpl(using Quotes): Expr[String] =
17+
quotes.reflect.Position.ofMacroExpansion.sourceFile.getJPath.get // this line is the culprit
18+
Expr("bar...")
19+
20+
}

presentation-compiler/test/dotty/tools/pc/tests/hover/HoverTermSuite.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,3 +799,19 @@ class HoverTermSuite extends BaseHoverSuite:
799799
|""".stripMargin,
800800
"def valueOf($name: String): Foo".hover
801801
)
802+
803+
@Test def `i7460` =
804+
check(
805+
"""|package tests.macros
806+
|def m = Macros7460.foo.sub@@string(2, 4)
807+
|""".stripMargin,
808+
"def substring(x$0: Int, x$1: Int): String".hover
809+
)
810+
811+
@Test def `i7460-2` =
812+
check(
813+
"""|package tests.macros
814+
|def m = Macros7460.bar.sub@@string(2, 4)
815+
|""".stripMargin,
816+
"def substring(x$0: Int, x$1: Int): String".hover
817+
)

0 commit comments

Comments
 (0)