Skip to content

Commit 9465001

Browse files
authored
Merge pull request #6611 from dotty-staging/fix-ide-toplevel
Fix #6208, #6605: Properly support toplevel defs in the IDE
2 parents f680859 + 91950d1 commit 9465001

File tree

5 files changed

+15
-12
lines changed

5 files changed

+15
-12
lines changed

compiler/src/dotty/tools/dotc/Run.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
239239
}
240240
}
241241

242-
def compile(sourceCode: String): Unit = {
243-
val virtualFile = new VirtualFile(sourceCode)
242+
def compileFromString(sourceCode: String): Unit = {
243+
val virtualFile = new VirtualFile("compileFromString-${java.util.UUID.randomUUID().toString}")
244244
val writer = new BufferedWriter(new OutputStreamWriter(virtualFile.output, "UTF-8")) // buffering is still advised by javadoc
245245
writer.write(sourceCode)
246246
writer.close()

compiler/src/dotty/tools/dotc/interactive/InteractiveDriver.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,8 @@ class InteractiveDriver(val settings: List[String]) extends Driver {
296296
}
297297

298298
private def toSource(uri: URI, sourceCode: String): SourceFile = {
299-
val virtualFile = new VirtualFile(uri.toString, Paths.get(uri).toString)
299+
val path = Paths.get(uri)
300+
val virtualFile = new VirtualFile(path.getFileName.toString, path.toString)
300301
val writer = new BufferedWriter(new OutputStreamWriter(virtualFile.output, "UTF-8"))
301302
writer.write(sourceCode)
302303
writer.close()

compiler/test/dotty/tools/DottyTest.scala

+1-8
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,7 @@ trait DottyTest extends ContextEscapeDetection {
6464
def checkCompile(checkAfterPhase: String, source: String)(assertion: (tpd.Tree, Context) => Unit): Context = {
6565
val c = compilerWithChecker(checkAfterPhase)(assertion)
6666
val run = c.newRun
67-
run.compile(source)
68-
run.runContext
69-
}
70-
71-
def checkCompile(checkAfterPhase: String, sources: List[String])(assertion: (tpd.Tree, Context) => Unit): Context = {
72-
val c = compilerWithChecker(checkAfterPhase)(assertion)
73-
val run = c.newRun
74-
run.compile(sources)
67+
run.compileFromString(source)
7568
run.runContext
7669
}
7770

compiler/test/dotty/tools/backend/jvm/DottyBytecodeTest.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ trait DottyBytecodeTest {
5858

5959
val compiler = new Compiler
6060
val run = compiler.newRun
61-
compiler.newRun.compile(source)
61+
compiler.newRun.compileFromString(source)
6262

6363
checkOutput(ctx.settings.outputDir.value)
6464
}

language-server/test/dotty/tools/languageserver/HoverTest.scala

+9
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,13 @@ class HoverTest {
200200
.hover(m3 to m4, hoverContent("Test.🤪"))
201201

202202
}
203+
204+
@Test def topLevel: Unit = {
205+
code"""package hello
206+
|val x: Int = 1
207+
|val y = ${m1}this${m2}.x""".withSource
208+
// The test framework will place the code above in a virtual file called Source0.scala,
209+
// sp the top-level definitions should be enclosed in an object called `Source0$package`.
210+
.hover(m1 to m2, hoverContent("hello.Source0$package.type(hello.Source0$package)"))
211+
}
203212
}

0 commit comments

Comments
 (0)