Skip to content

Commit bd6a6c4

Browse files
Deliver holes for literate Effekt files (#1035)
Previously, we only published holes for `.effekt` but not for `effekt.md`. The reason for this is that for `*.md` files we construct a new `MarkdownSource` in `compileSource` and we attach the annotations to that, but we pass the old `StringSource` back to `afterCompilation`, meaning we don't find the annotations. This PR fixes this issue by passing the correct source to `afterCompilation`.
1 parent 3f9be5e commit bd6a6c4

File tree

2 files changed

+79
-54
lines changed

2 files changed

+79
-54
lines changed

effekt/jvm/src/main/scala/effekt/Driver.scala

Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -35,64 +35,65 @@ trait Driver extends kiama.util.Compiler[EffektConfig, EffektError] { outer =>
3535
*
3636
* In LSP mode: invoked for each file opened in an editor
3737
*/
38-
override def compileSource(source: Source, config: EffektConfig): Unit = try {
39-
val src = if (source.name.endsWith(".md")) { MarkdownSource(source) } else { source }
40-
41-
// remember that we have seen this source, this is used by LSP (kiama.util.Server)
42-
sources(source.name) = src
43-
44-
implicit val C = context
45-
C.setup(config)
46-
47-
def saveOutput(path: String, doc: String): Unit =
48-
if (C.config.requiresCompilation()) {
49-
val out = C.config.outputPath()
50-
out.mkdirs
51-
IO.createFile((out / path).unixPath, doc)
52-
}
53-
54-
C.backend match {
38+
override def compileSource(source: Source, config: EffektConfig): Unit = {
39+
val src = if (source.name.endsWith(".md")) { MarkdownSource(source) } else { source }
40+
try {
41+
// remember that we have seen this source, this is used by LSP (kiama.util.Server)
42+
sources(source.name) = src
43+
44+
implicit val C = context
45+
C.setup(config)
46+
47+
def saveOutput(path: String, doc: String): Unit =
48+
if (C.config.requiresCompilation()) {
49+
val out = C.config.outputPath()
50+
out.mkdirs
51+
IO.createFile((out / path).unixPath, doc)
52+
}
5553

56-
case Backend(name, compiler, runner) =>
57-
// measure the total compilation time here
58-
def compile() = C.timed("total", source.name) {
59-
compiler.compile(src) map {
60-
case (outputFiles, exec) =>
61-
outputFiles.foreach {
62-
case (filename, doc) =>
63-
saveOutput(filename, doc)
54+
C.backend match {
55+
56+
case Backend(name, compiler, runner) =>
57+
// measure the total compilation time here
58+
def compile() = C.timed("total", source.name) {
59+
compiler.compile(src) map {
60+
case (outputFiles, exec) =>
61+
outputFiles.foreach {
62+
case (filename, doc) =>
63+
saveOutput(filename, doc)
64+
}
65+
exec
6466
}
65-
exec
66-
}
67+
}
68+
69+
// we are in one of four exclusive modes: Documenter, LSPServer, Compile, Run
70+
if (config.documenter()) { documenter(source, config)(context) }
71+
else if (config.server()) { compiler.runFrontend(src) }
72+
else if (config.interpret()) { compile() foreach runner.eval }
73+
else if (config.build()) { compile() foreach runner.build }
74+
else if (config.compile()) { compile() }
6775
}
68-
69-
// we are in one of four exclusive modes: Documenter, LSPServer, Compile, Run
70-
if (config.documenter()) { documenter(source, config)(context) }
71-
else if (config.server()) { compiler.runFrontend(src) }
72-
else if (config.interpret()) { compile() foreach runner.eval }
73-
else if (config.build()) { compile() foreach runner.build }
74-
else if (config.compile()) { compile() }
75-
}
76-
} catch {
77-
case FatalPhaseError(msg) => context.report(msg)
78-
case e @ CompilerPanic(msg) =>
79-
context.report(msg)
80-
e.getStackTrace.foreach { line =>
81-
context.info(" at " + line)
82-
}
83-
// when in server-mode, do not crash but report the error to avoid
84-
// restarting the server.
85-
case e if config.server() =>
86-
context.info("Effekt Compiler Crash: " + e.getMessage)
87-
e.getStackTrace.foreach { line =>
88-
context.info(" at " + line)
76+
} catch {
77+
case FatalPhaseError(msg) => context.report(msg)
78+
case e @ CompilerPanic(msg) =>
79+
context.report(msg)
80+
e.getStackTrace.foreach { line =>
81+
context.info(" at " + line)
82+
}
83+
// when in server-mode, do not crash but report the error to avoid
84+
// restarting the server.
85+
case e if config.server() =>
86+
context.info("Effekt Compiler Crash: " + e.getMessage)
87+
e.getStackTrace.foreach { line =>
88+
context.info(" at " + line)
89+
}
90+
} finally {
91+
outputTimes(src, config)(context)
92+
showIR(src, config)(context)
93+
writeIRs(src, config)(context)
94+
// This reports error messages
95+
afterCompilation(src, config)(context)
8996
}
90-
} finally {
91-
outputTimes(source, config)(context)
92-
showIR(source, config)(context)
93-
writeIRs(source, config)(context)
94-
// This reports error messages
95-
afterCompilation(source, config)(context)
9697
}
9798

9899
/**

effekt/jvm/src/test/scala/effekt/LSPTests.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,30 @@ class LSPTests extends FunSuite {
13331333
}
13341334
}
13351335

1336+
test("Server publishes holes for literate Effekt") {
1337+
withClientAndServer { (client, server) =>
1338+
val source = new TextDocumentItem("file://path/to/test.effekt.md", "literate effekt", 0,
1339+
raw"""# Some title
1340+
|```effekt
1341+
|def foo(x: Int): Bool = <>
1342+
|```
1343+
|""".stripMargin)
1344+
1345+
val initializeParams = new InitializeParams()
1346+
val initializationOptions = """{"showHoles": true}"""
1347+
initializeParams.setInitializationOptions(JsonParser.parseString(initializationOptions))
1348+
server.initialize(initializeParams).get()
1349+
1350+
val didOpenParams = new DidOpenTextDocumentParams()
1351+
didOpenParams.setTextDocument(source)
1352+
server.getTextDocumentService().didOpen(didOpenParams)
1353+
1354+
val receivedHoles = client.receivedHoles()
1355+
assertEquals(receivedHoles.length, 1)
1356+
assertEquals(receivedHoles.head.holes.length, 1)
1357+
}
1358+
}
1359+
13361360
test("Server publishes hole id for nested defs") {
13371361
withClientAndServer { (client, server) =>
13381362
val source =

0 commit comments

Comments
 (0)