Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functionality to execute and compute notebook cell contents #751

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 40 additions & 11 deletions effekt/jvm/src/main/scala/effekt/Server.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package effekt

import com.google.gson.JsonObject
import effekt.context.Context
import effekt.core.PrettyPrinter
import effekt.source.{ FunDef, Hole, ModuleDecl, Tree }
import effekt.util.{ PlainMessaging, getOrElseAborting }
import effekt.source.{FunDef, Hole, ModuleDecl, Tree}
import effekt.util.{PlainMessaging, getOrElseAborting}
import effekt.util.messages.EffektError

import kiama.util.{ Filenames, Position, Services, Source }
import kiama.util.{Filenames, Position, Services, Source}
import kiama.output.PrettyPrinterTypes.Document

import org.eclipse.lsp4j.{ Diagnostic, DocumentSymbol, SymbolKind, ExecuteCommandParams }
Expand Down Expand Up @@ -239,14 +239,43 @@ trait LSPServer extends kiama.util.Server[Tree, EffektConfig, EffektError] with

case class CaptureInfo(location: Location, captureText: String)

// changes ifelse to match case
override def executeCommand(src: Source, params: ExecuteCommandParams): Option[Any] =
if (params.getCommand == "inferredCaptures") {
val captures = getInferredCaptures(src)(using context).map {
case (p, c) => CaptureInfo(positionToLocation(p), TypePrinter.show(c))
}
if (captures.isEmpty) None else Some(captures.toArray)
} else {
None
params.getCommand match {
case "inferredCaptures" =>
val captures = getInferredCaptures(src)(using context).map {
case (p, c) => CaptureInfo(positionToLocation(p), TypePrinter.show(c))
}
if (captures.isEmpty) None else Some(captures.toArray)
case "compileCell" =>
val arg = params.getArguments()
if (arg.isEmpty) {
None
} else {
try {
val firstArg = arg.get(0)
firstArg match {
case jsonObject: JsonObject =>
val content = if (jsonObject.has("content")){
jsonObject.get("content").getAsString
} else {
"{}"
}
val result = compileNotebookContent(content)(using context)
result match {
case Some(compiled) => Some(compiled)
case None => Some("Compilation failed")
}
case _ =>
Some(s"Unexpected argument type: ${firstArg.getClass.getName}")
}
} catch {
case e: Exception =>
Some(s"Error during command execution: ${e.getMessage}")
}
}
//Some(arg.get(0)) hat funktioniert, get(1) nicht
case _ => None
}

override def createServices(config: EffektConfig) = new LSPServices(this, config)
Expand Down
17 changes: 16 additions & 1 deletion effekt/shared/src/main/scala/effekt/Intelligence.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package effekt

import effekt.context.{ Annotations, Context }
import effekt.source.{ FunDef, ModuleDecl, Tree }
import kiama.util.{ Position, Source }
import kiama.util.{ Position, Source, StringSource , Compiler}

trait Intelligence {

Expand Down Expand Up @@ -115,6 +115,21 @@ trait Intelligence {
capt <- C.inferredCaptureOption(block)
} yield (pos, capt)
}.flatten

// Compile the notebook content into Effekt code
def compileNotebookContent(content: String)(using C: Context): Option[String] = {
//val configs = createAndInitConfig(content)
C.compiler.compile(StringSource(
s"""def main() = {
| println($content)
|}
|""".stripMargin, "filename.effekt")
) match {
case Some((contents, mainFile)) =>
contents.get("filename.effekt")
case None => None
}
}

def getInfoOf(sym: Symbol)(implicit C: Context): Option[SymbolInfo] = PartialFunction.condOpt(resolveCallTarget(sym)) {

Expand Down