Skip to content

Commit 46b15c4

Browse files
authored
Merge pull request #439 from scala/backport-lts-3.3-23203
Backport "Add jpath to VirtualFile (for pc)" to 3.3 LTS
2 parents 06b8909 + eafc458 commit 46b15c4

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
@@ -228,8 +228,7 @@ object SourceFile {
228228
* It relies on SourceFile#virtual implementation to create the virtual file.
229229
*/
230230
def virtual(uri: URI, content: String): SourceFile =
231-
val path = Paths.get(uri).toString
232-
SourceFile.virtual(path, content)
231+
SourceFile(new VirtualFile(Paths.get(uri), content.getBytes(StandardCharsets.UTF_8)), content.toCharArray)
233232

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

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
@@ -731,3 +731,19 @@ class HoverTermSuite extends BaseHoverSuite:
731731
|""".stripMargin,
732732
"def valueOf($name: String): Foo".hover
733733
)
734+
735+
@Test def `i7460` =
736+
check(
737+
"""|package tests.macros
738+
|def m = Macros7460.foo.sub@@string(2, 4)
739+
|""".stripMargin,
740+
"def substring(x$0: Int, x$1: Int): String".hover
741+
)
742+
743+
@Test def `i7460-2` =
744+
check(
745+
"""|package tests.macros
746+
|def m = Macros7460.bar.sub@@string(2, 4)
747+
|""".stripMargin,
748+
"def substring(x$0: Int, x$1: Int): String".hover
749+
)

0 commit comments

Comments
 (0)