Skip to content

Commit 153b2f2

Browse files
committed
Fix scala#6208, scala#6605: Properly support toplevel defs in the IDE
Toplevel defs are enclosed in an object named `<source>$package` by `Desugar#packageDef`, this didn't work correctly in the IDE because the name of the VirtualFile was the whole URI of the file. In particular this lead to double-vision problems ("cannot merge ...") when the toplevel definition was also present on the classpath.
1 parent 9116ecb commit 153b2f2

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,6 @@ 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)
244-
val writer = new BufferedWriter(new OutputStreamWriter(virtualFile.output, "UTF-8")) // buffering is still advised by javadoc
245-
writer.write(sourceCode)
246-
writer.close()
247-
compileSources(List(new SourceFile(virtualFile, Codec.UTF8)))
248-
}
249-
250242
/** Print summary; return # of errors encountered */
251243
def printSummary(): Reporter = {
252244
printMaxConstraint()

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

Lines changed: 2 additions & 1 deletion
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()

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

Lines changed: 9 additions & 0 deletions
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)