From 8dc74e53a73bf2fb68920b2ea9a66a86b9178632 Mon Sep 17 00:00:00 2001 From: ComFreek Date: Wed, 5 Jul 2023 01:24:13 +0200 Subject: [PATCH] fix mmt lsp --- .../src/info/kwarc/mmt/lsp/Server.scala | 17 +++++++++++++---- .../src/info/kwarc/mmt/lsp/Utils.scala | 19 ------------------- .../src/info/kwarc/mmt/lsp/mmt/Server.scala | 15 ++++++--------- .../src/info/kwarc/mmt/stex/lsp/Server.scala | 5 ++--- 4 files changed, 21 insertions(+), 35 deletions(-) delete mode 100644 src/mmt-lsp/src/info/kwarc/mmt/lsp/Utils.scala diff --git a/src/mmt-lsp/src/info/kwarc/mmt/lsp/Server.scala b/src/mmt-lsp/src/info/kwarc/mmt/lsp/Server.scala index 8c8053f55..fbb9d0063 100644 --- a/src/mmt-lsp/src/info/kwarc/mmt/lsp/Server.scala +++ b/src/mmt-lsp/src/info/kwarc/mmt/lsp/Server.scala @@ -17,6 +17,8 @@ import java.util.logging.Logger import info.kwarc.mmt.api.frontend.{Controller, Extension} import info.kwarc.mmt.api.parser.SourceRef import info.kwarc.mmt.api.presentation.Presenter +import info.kwarc.mmt.api.utils.{File, FileURI, URI} +import info.kwarc.mmt.api.utils.File.read import info.kwarc.mmt.api.utils.time.Time import info.kwarc.mmt.api.web.ServerExtension import org.eclipse.jetty.server.{Server, ServerConnector} @@ -422,12 +424,19 @@ class LSPServer[+ClientType <: LSPClient](clct : Class[ClientType]) { object LSPServer { def URItoVSCode(s : String) : String = URLEncoder.encode(s.replace("+","%2B"),"UTF-8") - def VSCodeToURI(s : String) : String = { + def VSCodeToURI(s: String): String = { val dec = URLDecoder.decode(s,"UTF-8") - if (dec.startsWith("file:///") && dec(9) == ':') { + if (dec.startsWith("file:///")) { dec.take(8) + dec(8).toUpper + dec.drop(9) - } else dec - }//s.replace("%3A",":") + } else { + dec + } + } + + def VSCodeToFile(s: String): Option[File] = URI(VSCodeToURI(s)) match { + case FileURI(f) => Some(f) + case _ => None + } } class AbstractLSPServer[A <: LSPClient, B <: LSPServer[A], C <: LSPWebsocket[A,B]](val server : B,lsp:LSP[A,B,C]) diff --git a/src/mmt-lsp/src/info/kwarc/mmt/lsp/Utils.scala b/src/mmt-lsp/src/info/kwarc/mmt/lsp/Utils.scala deleted file mode 100644 index 6a6f7ff48..000000000 --- a/src/mmt-lsp/src/info/kwarc/mmt/lsp/Utils.scala +++ /dev/null @@ -1,19 +0,0 @@ -package info.kwarc.mmt.lsp - -import info.kwarc.mmt.api.utils.{File, URI} - -object Utils { - def vscodeURIToFile(uri: URI): File = { - if (uri.scheme.contains("file")) { - val filePath = (uri.path match { - case driveLetter :: xs if driveLetter.matches("[a-zA-Z](:|%3A)") => - (driveLetter.charAt(0).toUpper + ":") :: xs - case x => x - }).mkString(java.io.File.separator) - - File(filePath) - } else { // fallback - File(uri.toString) - } - } -} diff --git a/src/mmt-lsp/src/info/kwarc/mmt/lsp/mmt/Server.scala b/src/mmt-lsp/src/info/kwarc/mmt/lsp/mmt/Server.scala index b2645a178..8b592299c 100644 --- a/src/mmt-lsp/src/info/kwarc/mmt/lsp/mmt/Server.scala +++ b/src/mmt-lsp/src/info/kwarc/mmt/lsp/mmt/Server.scala @@ -69,9 +69,12 @@ class MMTLSPServer(style : RunStyle) extends LSPServer(classOf[MMTClient]) }) params.getWorkspaceFolders.asScala - .map(_.getUri) - .map(URI(_)) - .map(Utils.vscodeURIToFile) + .flatMap(workspaceFolder => LSPServer.VSCodeToFile(workspaceFolder.getUri) match { + case Some(f) => Some(f) + case None => + client.log(s"Could not parse `${workspaceFolder.getUri} as a file path on your operating system.") + None + }) .foreach(workspacePath => { client.log(s"Registering `$workspacePath` as mathpath archive.") controller.addArchive(workspacePath) @@ -92,12 +95,6 @@ class MMTLSPServer(style : RunStyle) extends LSPServer(classOf[MMTClient]) }) } - private object fg extends ReportHandler("lsp-frontend") { - override def apply(ind: Int, caller: => String, group: String, msgParts: List[String]): Unit = { - // TODO client.client. - } - } - override val triggerChar: Char = 'j' val completionls = { diff --git a/src/mmt-stex/src/info/kwarc/mmt/stex/lsp/Server.scala b/src/mmt-stex/src/info/kwarc/mmt/stex/lsp/Server.scala index d6dbdfd19..0f1687863 100644 --- a/src/mmt-stex/src/info/kwarc/mmt/stex/lsp/Server.scala +++ b/src/mmt-stex/src/info/kwarc/mmt/stex/lsp/Server.scala @@ -597,9 +597,8 @@ class STeXLSPServer(style:RunStyle) extends LSPServer(classOf[STeXClient]) with result.getCapabilities.setWorkspaceSymbolProvider(true) if (params.getWorkspaceFolders != null) { params.getWorkspaceFolders.asScala - .map(_.getUri) - .map(URI(_)) - .map(info.kwarc.mmt.lsp.Utils.vscodeURIToFile).foreach { + .flatMap(workspaceFolder => LSPServer.VSCodeToFile(workspaceFolder.getUri)) + .foreach { case f if f.exists() => workspacefolders ::= f } }