src/Tools/VSCode/src/lsp.scala
author Thomas Lindae <thomas.lindae@in.tum.de>
Wed, 24 Apr 2024 18:48:24 +0200
changeset 81021 89bfada3a16d
parent 75393 87ebf5a50283
child 81026 c4bc259393f6
permissions -rw-r--r--
lsp: added State_Init_Response message;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
72761
4519eeefe3b5 avoid conflicting base names;
wenzelm
parents: 71774
diff changeset
     1
/*  Title:      Tools/VSCode/src/lsp.scala
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     3
75201
8f6b8a46f54c clarified modules: more uniform .scala vs. ts (amending 4519eeefe3b5);
wenzelm
parents: 75199
diff changeset
     4
Message formats for Language Server Protocol, with adhoc PIDE extensions.
8f6b8a46f54c clarified modules: more uniform .scala vs. ts (amending 4519eeefe3b5);
wenzelm
parents: 75199
diff changeset
     5
See https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     6
*/
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     7
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     8
package isabelle.vscode
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     9
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    10
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    11
import isabelle._
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    12
64777
ca09695eb43c clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents: 64767
diff changeset
    13
import java.io.{File => JFile}
ca09695eb43c clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents: 64767
diff changeset
    14
ca09695eb43c clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents: 64767
diff changeset
    15
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
    16
object LSP {
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    17
  /* abstract message */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    18
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
    19
  object Message {
67850
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
    20
    val empty: JSON.Object.T = JSON.Object("jsonrpc" -> "2.0")
65922
d2f19f05c0e9 clarified message logging;
wenzelm
parents: 65229
diff changeset
    21
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
    22
    def log(prefix: String, json: JSON.T, logger: Logger, verbose: Boolean): Unit = {
67851
wenzelm
parents: 67850
diff changeset
    23
      val header =
65922
d2f19f05c0e9 clarified message logging;
wenzelm
parents: 65229
diff changeset
    24
        json match {
67851
wenzelm
parents: 67850
diff changeset
    25
          case JSON.Object(obj) => obj -- (obj.keySet - "method" - "id")
67850
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
    26
          case _ => JSON.Object.empty
65922
d2f19f05c0e9 clarified message logging;
wenzelm
parents: 65229
diff changeset
    27
        }
d2f19f05c0e9 clarified message logging;
wenzelm
parents: 65229
diff changeset
    28
      if (verbose || header.isEmpty) logger(prefix + "\n" + JSON.Format(json))
d2f19f05c0e9 clarified message logging;
wenzelm
parents: 65229
diff changeset
    29
      else logger(prefix + " " + JSON.Format(header))
d2f19f05c0e9 clarified message logging;
wenzelm
parents: 65229
diff changeset
    30
    }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    31
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    32
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    33
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    34
  /* notification */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    35
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
    36
  object Notification {
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    37
    def apply(method: String, params: JSON.T): JSON.T =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    38
      Message.empty + ("method" -> method) + ("params" -> params)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    39
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    40
    def unapply(json: JSON.T): Option[(String, Option[JSON.T])] =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    41
      for {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    42
        method <- JSON.string(json, "method")
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    43
        params = JSON.value(json, "params")
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    44
      } yield (method, params)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    45
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    46
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
    47
  class Notification0(name: String) {
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    48
    def unapply(json: JSON.T): Option[Unit] =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    49
      json match {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    50
        case Notification(method, _) if method == name => Some(())
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    51
        case _ => None
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    52
      }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    53
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    54
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    55
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    56
  /* request message */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    57
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
    58
  object Id {
66675
6f4613dbfef6 support for workspace edits;
wenzelm
parents: 66235
diff changeset
    59
    def empty: Id = Id("")
6f4613dbfef6 support for workspace edits;
wenzelm
parents: 66235
diff changeset
    60
  }
6f4613dbfef6 support for workspace edits;
wenzelm
parents: 66235
diff changeset
    61
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
    62
  sealed case class Id(id: Any) {
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    63
    require(
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    64
      id.isInstanceOf[Int] ||
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    65
      id.isInstanceOf[Long] ||
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    66
      id.isInstanceOf[Double] ||
73120
c3589f2dff31 more informative errors: simplify diagnosis of spurious failures reported by users;
wenzelm
parents: 72761
diff changeset
    67
      id.isInstanceOf[String], "bad id type")
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    68
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    69
    override def toString: String = id.toString
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    70
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    71
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
    72
  object RequestMessage {
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    73
    def apply(id: Id, method: String, params: JSON.T): JSON.T =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    74
      Message.empty + ("id" -> id.id) + ("method" -> method) + ("params" -> params)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    75
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    76
    def unapply(json: JSON.T): Option[(Id, String, Option[JSON.T])] =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    77
      for {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    78
        id <- JSON.long(json, "id") orElse JSON.double(json, "id") orElse JSON.string(json, "id")
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    79
        method <- JSON.string(json, "method")
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    80
        params = JSON.value(json, "params")
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    81
      } yield (Id(id), method, params)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    82
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    83
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
    84
  class Request0(name: String) {
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    85
    def unapply(json: JSON.T): Option[Id] =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    86
      json match {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    87
        case RequestMessage(id, method, _) if method == name => Some(id)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    88
        case _ => None
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    89
      }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    90
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    91
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
    92
  class RequestTextDocumentPosition(name: String) {
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
    93
    def unapply(json: JSON.T): Option[(Id, Line.Node_Position)] =
64648
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64605
diff changeset
    94
      json match {
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
    95
        case RequestMessage(id, method, Some(TextDocumentPosition(node_pos))) if method == name =>
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
    96
          Some((id, node_pos))
64648
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64605
diff changeset
    97
        case _ => None
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64605
diff changeset
    98
      }
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64605
diff changeset
    99
  }
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64605
diff changeset
   100
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   101
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   102
  /* response message */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   103
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   104
  object ResponseMessage {
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   105
    def apply(id: Id, result: Option[JSON.T] = None, error: Option[ResponseError] = None): JSON.T =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   106
      Message.empty + ("id" -> id.id) ++
64878
wenzelm
parents: 64877
diff changeset
   107
        JSON.optional("result" -> result) ++
wenzelm
parents: 64877
diff changeset
   108
        JSON.optional("error" -> error.map(_.json))
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   109
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   110
    def strict(id: Id, result: Option[JSON.T] = None, error: String = ""): JSON.T =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   111
      if (error == "") apply(id, result = result)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   112
      else apply(id, error = Some(ResponseError(code = ErrorCodes.serverErrorEnd, message = error)))
66675
6f4613dbfef6 support for workspace edits;
wenzelm
parents: 66235
diff changeset
   113
6f4613dbfef6 support for workspace edits;
wenzelm
parents: 66235
diff changeset
   114
    def is_empty(json: JSON.T): Boolean =
6f4613dbfef6 support for workspace edits;
wenzelm
parents: 66235
diff changeset
   115
      JSON.string(json, "id") == Some("") && JSON.value(json, "result").isDefined
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   116
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   117
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   118
  sealed case class ResponseError(code: Int, message: String, data: Option[JSON.T] = None) {
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   119
    def json: JSON.T =
67850
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
   120
      JSON.Object("code" -> code, "message" -> message) ++ JSON.optional("data" -> data)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   121
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   122
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   123
  object ErrorCodes {
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   124
    val ParseError = -32700
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   125
    val InvalidRequest = -32600
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   126
    val MethodNotFound = -32601
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   127
    val InvalidParams = -32602
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   128
    val InternalError = -32603
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   129
    val serverErrorStart = -32099
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   130
    val serverErrorEnd = -32000
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   131
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   132
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   133
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   134
  /* init and exit */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   135
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   136
  object Initialize extends Request0("initialize") {
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   137
    def reply(id: Id, error: String): JSON.T =
67850
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
   138
      ResponseMessage.strict(
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
   139
        id, Some(JSON.Object("capabilities" -> ServerCapabilities.json)), error)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   140
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   141
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   142
  object ServerCapabilities {
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   143
    val json: JSON.T =
67850
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
   144
      JSON.Object(
65160
6e042537555d incremental document changes;
wenzelm
parents: 65106
diff changeset
   145
        "textDocumentSync" -> 2,
75126
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73541
diff changeset
   146
        "completionProvider" -> JSON.Object(
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73541
diff changeset
   147
          "resolveProvider" -> false,
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73541
diff changeset
   148
          "triggerCharacters" ->
75199
1ced8ee860e2 clarified signature;
wenzelm
parents: 75197
diff changeset
   149
            Symbol.symbols.entries.flatMap(_.abbrevs).flatMap(_.toList).map(_.toString).distinct
75126
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73541
diff changeset
   150
        ),
64648
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64605
diff changeset
   151
        "hoverProvider" -> true,
64767
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64762
diff changeset
   152
        "definitionProvider" -> true,
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64762
diff changeset
   153
        "documentHighlightProvider" -> true)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   154
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   155
65229
cc96b8c3b8cb explicitly ignore "initialized" message;
wenzelm
parents: 65201
diff changeset
   156
  object Initialized extends Notification0("initialized")
cc96b8c3b8cb explicitly ignore "initialized" message;
wenzelm
parents: 65201
diff changeset
   157
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   158
  object Shutdown extends Request0("shutdown") {
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   159
    def reply(id: Id, error: String): JSON.T =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   160
      ResponseMessage.strict(id, Some("OK"), error)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   161
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   162
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   163
  object Exit extends Notification0("exit")
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   164
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   165
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   166
  /* document positions */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   167
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   168
  object Position {
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   169
    def apply(pos: Line.Position): JSON.T =
67850
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
   170
      JSON.Object("line" -> pos.line, "character" -> pos.column)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   171
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   172
    def unapply(json: JSON.T): Option[Line.Position] =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   173
      for {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   174
        line <- JSON.int(json, "line")
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   175
        column <- JSON.int(json, "character")
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   176
      } yield Line.Position(line, column)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   177
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   178
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   179
  object Range {
65173
3700be571a01 more compact protocol message;
wenzelm
parents: 65165
diff changeset
   180
    def compact(range: Line.Range): List[Int] =
3700be571a01 more compact protocol message;
wenzelm
parents: 65165
diff changeset
   181
      List(range.start.line, range.start.column, range.stop.line, range.stop.column)
3700be571a01 more compact protocol message;
wenzelm
parents: 65165
diff changeset
   182
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   183
    def apply(range: Line.Range): JSON.T =
67850
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
   184
      JSON.Object("start" -> Position(range.start), "end" -> Position(range.stop))
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   185
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   186
    def unapply(json: JSON.T): Option[Line.Range] =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   187
      (JSON.value(json, "start"), JSON.value(json, "end")) match {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   188
        case (Some(Position(start)), Some(Position(stop))) => Some(Line.Range(start, stop))
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   189
        case _ => None
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   190
      }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   191
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   192
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   193
  object Location {
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   194
    def apply(loc: Line.Node_Range): JSON.T =
67850
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
   195
      JSON.Object("uri" -> Url.print_file_name(loc.name), "range" -> Range(loc.range))
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   196
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   197
    def unapply(json: JSON.T): Option[Line.Node_Range] =
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   198
      for {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   199
        uri <- JSON.string(json, "uri")
75126
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73541
diff changeset
   200
        if Url.is_wellformed_file(uri)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   201
        range_json <- JSON.value(json, "range")
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   202
        range <- Range.unapply(range_json)
66235
d4fa51e7c4ff retain symlinks in file names from VSCode: relevant for proper file locations in decorations etc.;
wenzelm
parents: 66216
diff changeset
   203
      } yield Line.Node_Range(Url.absolute_file_name(uri), range)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   204
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   205
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   206
  object TextDocumentPosition {
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   207
    def unapply(json: JSON.T): Option[Line.Node_Position] =
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   208
      for {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   209
        doc <- JSON.value(json, "textDocument")
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   210
        uri <- JSON.string(doc, "uri")
75126
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73541
diff changeset
   211
        if Url.is_wellformed_file(uri)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   212
        pos_json <- JSON.value(json, "position")
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   213
        pos <- Position.unapply(pos_json)
66235
d4fa51e7c4ff retain symlinks in file names from VSCode: relevant for proper file locations in decorations etc.;
wenzelm
parents: 66216
diff changeset
   214
      } yield Line.Node_Position(Url.absolute_file_name(uri), pos)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   215
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   216
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   217
65103
0bf1836ce4b1 tight protocol messages;
wenzelm
parents: 65095
diff changeset
   218
  /* marked strings */
65093
5f08197206ce clarified signature;
wenzelm
parents: 64878
diff changeset
   219
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   220
  sealed case class MarkedString(text: String, language: String = "plaintext") {
67850
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
   221
    def json: JSON.T = JSON.Object("language" -> language, "value" -> text)
65093
5f08197206ce clarified signature;
wenzelm
parents: 64878
diff changeset
   222
  }
5f08197206ce clarified signature;
wenzelm
parents: 64878
diff changeset
   223
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   224
  object MarkedStrings {
65103
0bf1836ce4b1 tight protocol messages;
wenzelm
parents: 65095
diff changeset
   225
    def json(msgs: List[MarkedString]): Option[JSON.T] =
0bf1836ce4b1 tight protocol messages;
wenzelm
parents: 65095
diff changeset
   226
      msgs match {
0bf1836ce4b1 tight protocol messages;
wenzelm
parents: 65095
diff changeset
   227
        case Nil => None
0bf1836ce4b1 tight protocol messages;
wenzelm
parents: 65095
diff changeset
   228
        case List(msg) => Some(msg.json)
0bf1836ce4b1 tight protocol messages;
wenzelm
parents: 65095
diff changeset
   229
        case _ => Some(msgs.map(_.json))
0bf1836ce4b1 tight protocol messages;
wenzelm
parents: 65095
diff changeset
   230
      }
0bf1836ce4b1 tight protocol messages;
wenzelm
parents: 65095
diff changeset
   231
  }
0bf1836ce4b1 tight protocol messages;
wenzelm
parents: 65095
diff changeset
   232
65093
5f08197206ce clarified signature;
wenzelm
parents: 64878
diff changeset
   233
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   234
  /* diagnostic messages */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   235
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   236
  object MessageType {
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   237
    val Error = 1
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   238
    val Warning = 2
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   239
    val Info = 3
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   240
    val Log = 4
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   241
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   242
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   243
  object DisplayMessage {
73541
1240abf2e3f5 tuned signature;
wenzelm
parents: 73340
diff changeset
   244
    def apply(message_type: Int, message: String, show: Boolean): JSON.T =
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   245
      Notification(if (show) "window/showMessage" else "window/logMessage",
67850
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
   246
        JSON.Object("type" -> message_type, "message" -> message))
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   247
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   248
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   249
66138
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66096
diff changeset
   250
  /* commands */
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66096
diff changeset
   251
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   252
  sealed case class Command(title: String, command: String, arguments: List[JSON.T] = Nil) {
66138
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66096
diff changeset
   253
    def json: JSON.T =
67850
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
   254
      JSON.Object("title" -> title, "command" -> command, "arguments" -> arguments)
66138
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66096
diff changeset
   255
  }
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66096
diff changeset
   256
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66096
diff changeset
   257
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   258
  /* document edits */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   259
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   260
  object DidOpenTextDocument {
64777
ca09695eb43c clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents: 64767
diff changeset
   261
    def unapply(json: JSON.T): Option[(JFile, String, Long, String)] =
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   262
      json match {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   263
        case Notification("textDocument/didOpen", Some(params)) =>
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   264
          for {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   265
            doc <- JSON.value(params, "textDocument")
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   266
            uri <- JSON.string(doc, "uri")
75126
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73541
diff changeset
   267
            if Url.is_wellformed_file(uri)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   268
            lang <- JSON.string(doc, "languageId")
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   269
            version <- JSON.long(doc, "version")
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   270
            text <- JSON.string(doc, "text")
66235
d4fa51e7c4ff retain symlinks in file names from VSCode: relevant for proper file locations in decorations etc.;
wenzelm
parents: 66216
diff changeset
   271
          } yield (Url.absolute_file(uri), lang, version, text)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   272
        case _ => None
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   273
      }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   274
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   275
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   276
65160
6e042537555d incremental document changes;
wenzelm
parents: 65106
diff changeset
   277
  sealed case class TextDocumentChange(range: Option[Line.Range], text: String)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   278
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   279
  object DidChangeTextDocument {
65160
6e042537555d incremental document changes;
wenzelm
parents: 65106
diff changeset
   280
    def unapply_change(json: JSON.T): Option[TextDocumentChange] =
6e042537555d incremental document changes;
wenzelm
parents: 65106
diff changeset
   281
      for { text <- JSON.string(json, "text") }
75204
b0910e6c1320 tuned, based on suggestions by IntelliJ IDEA;
wenzelm
parents: 75201
diff changeset
   282
      yield TextDocumentChange(JSON.value(json, "range", Range.unapply), text)
65160
6e042537555d incremental document changes;
wenzelm
parents: 65106
diff changeset
   283
6e042537555d incremental document changes;
wenzelm
parents: 65106
diff changeset
   284
    def unapply(json: JSON.T): Option[(JFile, Long, List[TextDocumentChange])] =
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   285
      json match {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   286
        case Notification("textDocument/didChange", Some(params)) =>
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   287
          for {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   288
            doc <- JSON.value(params, "textDocument")
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   289
            uri <- JSON.string(doc, "uri")
75126
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73541
diff changeset
   290
            if Url.is_wellformed_file(uri)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   291
            version <- JSON.long(doc, "version")
75204
b0910e6c1320 tuned, based on suggestions by IntelliJ IDEA;
wenzelm
parents: 75201
diff changeset
   292
            changes <- JSON.list(params, "contentChanges", unapply_change)
66235
d4fa51e7c4ff retain symlinks in file names from VSCode: relevant for proper file locations in decorations etc.;
wenzelm
parents: 66216
diff changeset
   293
          } yield (Url.absolute_file(uri), version, changes)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   294
        case _ => None
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   295
      }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   296
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   297
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   298
  class TextDocumentNotification(name: String) {
64777
ca09695eb43c clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents: 64767
diff changeset
   299
    def unapply(json: JSON.T): Option[JFile] =
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   300
      json match {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   301
        case Notification(method, Some(params)) if method == name =>
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   302
          for {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   303
            doc <- JSON.value(params, "textDocument")
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   304
            uri <- JSON.string(doc, "uri")
75126
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73541
diff changeset
   305
            if Url.is_wellformed_file(uri)
66235
d4fa51e7c4ff retain symlinks in file names from VSCode: relevant for proper file locations in decorations etc.;
wenzelm
parents: 66216
diff changeset
   306
          } yield Url.absolute_file(uri)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   307
        case _ => None
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   308
      }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   309
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   310
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   311
  object DidCloseTextDocument extends TextDocumentNotification("textDocument/didClose")
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   312
  object DidSaveTextDocument extends TextDocumentNotification("textDocument/didSave")
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   313
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   314
66675
6f4613dbfef6 support for workspace edits;
wenzelm
parents: 66235
diff changeset
   315
  /* workspace edits */
6f4613dbfef6 support for workspace edits;
wenzelm
parents: 66235
diff changeset
   316
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   317
  sealed case class TextEdit(range: Line.Range, new_text: String) {
67850
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
   318
    def json: JSON.T = JSON.Object("range" -> Range(range), "newText" -> new_text)
66675
6f4613dbfef6 support for workspace edits;
wenzelm
parents: 66235
diff changeset
   319
  }
6f4613dbfef6 support for workspace edits;
wenzelm
parents: 66235
diff changeset
   320
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   321
  sealed case class TextDocumentEdit(file: JFile, version: Long, edits: List[TextEdit]) {
66675
6f4613dbfef6 support for workspace edits;
wenzelm
parents: 66235
diff changeset
   322
    def json: JSON.T =
67850
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
   323
      JSON.Object(
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
   324
        "textDocument" -> JSON.Object("uri" -> Url.print_file(file), "version" -> version),
66675
6f4613dbfef6 support for workspace edits;
wenzelm
parents: 66235
diff changeset
   325
        "edits" -> edits.map(_.json))
6f4613dbfef6 support for workspace edits;
wenzelm
parents: 66235
diff changeset
   326
  }
6f4613dbfef6 support for workspace edits;
wenzelm
parents: 66235
diff changeset
   327
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   328
  object WorkspaceEdit {
66675
6f4613dbfef6 support for workspace edits;
wenzelm
parents: 66235
diff changeset
   329
    def apply(edits: List[TextDocumentEdit]): JSON.T =
6f4613dbfef6 support for workspace edits;
wenzelm
parents: 66235
diff changeset
   330
      RequestMessage(Id.empty, "workspace/applyEdit",
67850
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
   331
        JSON.Object("edit" -> JSON.Object("documentChanges" -> edits.map(_.json))))
66675
6f4613dbfef6 support for workspace edits;
wenzelm
parents: 66235
diff changeset
   332
  }
6f4613dbfef6 support for workspace edits;
wenzelm
parents: 66235
diff changeset
   333
6f4613dbfef6 support for workspace edits;
wenzelm
parents: 66235
diff changeset
   334
64877
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64777
diff changeset
   335
  /* completion */
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64777
diff changeset
   336
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64777
diff changeset
   337
  sealed case class CompletionItem(
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64777
diff changeset
   338
    label: String,
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64777
diff changeset
   339
    kind: Option[Int] = None,
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64777
diff changeset
   340
    detail: Option[String] = None,
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64777
diff changeset
   341
    documentation: Option[String] = None,
66140
cdb6c10122b6 tuned signature;
wenzelm
parents: 66139
diff changeset
   342
    text: Option[String] = None,
66138
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66096
diff changeset
   343
    range: Option[Line.Range] = None,
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   344
    command: Option[Command] = None
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   345
  ) {
64877
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64777
diff changeset
   346
    def json: JSON.T =
67850
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
   347
      JSON.Object("label" -> label) ++
66061
wenzelm
parents: 66052
diff changeset
   348
      JSON.optional("kind" -> kind) ++
wenzelm
parents: 66052
diff changeset
   349
      JSON.optional("detail" -> detail) ++
wenzelm
parents: 66052
diff changeset
   350
      JSON.optional("documentation" -> documentation) ++
66140
cdb6c10122b6 tuned signature;
wenzelm
parents: 66139
diff changeset
   351
      JSON.optional("insertText" -> text) ++
66062
175772371cd0 use old-style "textEdit" for the sake of the external protocol (see also vscode-languageserver-node/issues/188);
wenzelm
parents: 66061
diff changeset
   352
      JSON.optional("range" -> range.map(Range(_))) ++
66675
6f4613dbfef6 support for workspace edits;
wenzelm
parents: 66235
diff changeset
   353
      JSON.optional("textEdit" -> range.map(r => TextEdit(r, text.getOrElse(label)).json)) ++
66138
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66096
diff changeset
   354
      JSON.optional("command" -> command.map(_.json))
64877
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64777
diff changeset
   355
  }
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64777
diff changeset
   356
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   357
  object Completion extends RequestTextDocumentPosition("textDocument/completion") {
64877
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64777
diff changeset
   358
    def reply(id: Id, result: List[CompletionItem]): JSON.T =
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64777
diff changeset
   359
      ResponseMessage(id, Some(result.map(_.json)))
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64777
diff changeset
   360
  }
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64777
diff changeset
   361
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64777
diff changeset
   362
66138
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66096
diff changeset
   363
  /* spell checker */
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66096
diff changeset
   364
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   365
  object Include_Word extends Notification0("PIDE/include_word") {
66139
6a8f8be2741c provide spell-checker menu via completion commands;
wenzelm
parents: 66138
diff changeset
   366
    val command = Command("Include word", "isabelle.include-word")
6a8f8be2741c provide spell-checker menu via completion commands;
wenzelm
parents: 66138
diff changeset
   367
  }
6a8f8be2741c provide spell-checker menu via completion commands;
wenzelm
parents: 66138
diff changeset
   368
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   369
  object Include_Word_Permanently extends Notification0("PIDE/include_word_permanently") {
66139
6a8f8be2741c provide spell-checker menu via completion commands;
wenzelm
parents: 66138
diff changeset
   370
    val command = Command("Include word permanently", "isabelle.include-word-permanently")
6a8f8be2741c provide spell-checker menu via completion commands;
wenzelm
parents: 66138
diff changeset
   371
  }
6a8f8be2741c provide spell-checker menu via completion commands;
wenzelm
parents: 66138
diff changeset
   372
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   373
  object Exclude_Word extends Notification0("PIDE/exclude_word") {
66139
6a8f8be2741c provide spell-checker menu via completion commands;
wenzelm
parents: 66138
diff changeset
   374
    val command = Command("Exclude word", "isabelle.exclude-word")
6a8f8be2741c provide spell-checker menu via completion commands;
wenzelm
parents: 66138
diff changeset
   375
  }
6a8f8be2741c provide spell-checker menu via completion commands;
wenzelm
parents: 66138
diff changeset
   376
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   377
  object Exclude_Word_Permanently extends Notification0("PIDE/exclude_word_permanently") {
66139
6a8f8be2741c provide spell-checker menu via completion commands;
wenzelm
parents: 66138
diff changeset
   378
    val command = Command("Exclude word permanently", "isabelle.exclude-word-permanently")
6a8f8be2741c provide spell-checker menu via completion commands;
wenzelm
parents: 66138
diff changeset
   379
  }
6a8f8be2741c provide spell-checker menu via completion commands;
wenzelm
parents: 66138
diff changeset
   380
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   381
  object Reset_Words extends Notification0("PIDE/reset_words") {
66139
6a8f8be2741c provide spell-checker menu via completion commands;
wenzelm
parents: 66138
diff changeset
   382
    val command = Command("Reset non-permanent words", "isabelle.reset-words")
6a8f8be2741c provide spell-checker menu via completion commands;
wenzelm
parents: 66138
diff changeset
   383
  }
66138
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66096
diff changeset
   384
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66096
diff changeset
   385
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   386
  /* hover request */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   387
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   388
  object Hover extends RequestTextDocumentPosition("textDocument/hover") {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   389
    def reply(id: Id, result: Option[(Line.Range, List[MarkedString])]): JSON.T = {
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   390
      val res =
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   391
        result match {
64747
54afac94f52b proper content format;
wenzelm
parents: 64693
diff changeset
   392
          case Some((range, contents)) =>
67850
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
   393
            JSON.Object(
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
   394
              "contents" -> MarkedStrings.json(contents).getOrElse(Nil),
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
   395
              "range" -> Range(range))
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
   396
          case None => JSON.Object("contents" -> Nil)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   397
        }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   398
      ResponseMessage(id, Some(res))
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   399
    }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   400
  }
64648
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64605
diff changeset
   401
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64605
diff changeset
   402
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64605
diff changeset
   403
  /* goto definition request */
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64605
diff changeset
   404
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   405
  object GotoDefinition extends RequestTextDocumentPosition("textDocument/definition") {
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   406
    def reply(id: Id, result: List[Line.Node_Range]): JSON.T =
75204
b0910e6c1320 tuned, based on suggestions by IntelliJ IDEA;
wenzelm
parents: 75201
diff changeset
   407
      ResponseMessage(id, Some(result.map(Location.apply)))
64648
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64605
diff changeset
   408
  }
64675
c557279d93f2 support for diagnostics;
wenzelm
parents: 64651
diff changeset
   409
c557279d93f2 support for diagnostics;
wenzelm
parents: 64651
diff changeset
   410
64767
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64762
diff changeset
   411
  /* document highlights request */
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64762
diff changeset
   412
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   413
  object DocumentHighlight {
64767
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64762
diff changeset
   414
    def text(range: Line.Range): DocumentHighlight = DocumentHighlight(range, Some(1))
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64762
diff changeset
   415
    def read(range: Line.Range): DocumentHighlight = DocumentHighlight(range, Some(2))
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64762
diff changeset
   416
    def write(range: Line.Range): DocumentHighlight = DocumentHighlight(range, Some(3))
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64762
diff changeset
   417
  }
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64762
diff changeset
   418
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   419
  sealed case class DocumentHighlight(range: Line.Range, kind: Option[Int] = None) {
64767
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64762
diff changeset
   420
    def json: JSON.T =
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64762
diff changeset
   421
      kind match {
67850
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
   422
        case None => JSON.Object("range" -> Range(range))
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
   423
        case Some(k) => JSON.Object("range" -> Range(range), "kind" -> k)
64767
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64762
diff changeset
   424
      }
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64762
diff changeset
   425
  }
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64762
diff changeset
   426
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   427
  object DocumentHighlights extends RequestTextDocumentPosition("textDocument/documentHighlight") {
64767
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64762
diff changeset
   428
    def reply(id: Id, result: List[DocumentHighlight]): JSON.T =
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64762
diff changeset
   429
      ResponseMessage(id, Some(result.map(_.json)))
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64762
diff changeset
   430
  }
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64762
diff changeset
   431
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64762
diff changeset
   432
64675
c557279d93f2 support for diagnostics;
wenzelm
parents: 64651
diff changeset
   433
  /* diagnostics */
c557279d93f2 support for diagnostics;
wenzelm
parents: 64651
diff changeset
   434
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   435
  sealed case class Diagnostic(
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   436
    range: Line.Range,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   437
    message: String,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   438
    severity: Option[Int] = None,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   439
    code: Option[Int] = None,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   440
    source: Option[String] = None
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   441
  ) {
64675
c557279d93f2 support for diagnostics;
wenzelm
parents: 64651
diff changeset
   442
    def json: JSON.T =
c557279d93f2 support for diagnostics;
wenzelm
parents: 64651
diff changeset
   443
      Message.empty + ("range" -> Range(range)) + ("message" -> message) ++
64878
wenzelm
parents: 64877
diff changeset
   444
      JSON.optional("severity" -> severity) ++
wenzelm
parents: 64877
diff changeset
   445
      JSON.optional("code" -> code) ++
wenzelm
parents: 64877
diff changeset
   446
      JSON.optional("source" -> source)
64675
c557279d93f2 support for diagnostics;
wenzelm
parents: 64651
diff changeset
   447
  }
c557279d93f2 support for diagnostics;
wenzelm
parents: 64651
diff changeset
   448
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   449
  object DiagnosticSeverity {
64762
cd545bec3fd0 clarified message severity, based on empirical observation of VSCode 1.8.1;
wenzelm
parents: 64747
diff changeset
   450
    val Error = 1
cd545bec3fd0 clarified message severity, based on empirical observation of VSCode 1.8.1;
wenzelm
parents: 64747
diff changeset
   451
    val Warning = 2
cd545bec3fd0 clarified message severity, based on empirical observation of VSCode 1.8.1;
wenzelm
parents: 64747
diff changeset
   452
    val Information = 3
cd545bec3fd0 clarified message severity, based on empirical observation of VSCode 1.8.1;
wenzelm
parents: 64747
diff changeset
   453
    val Hint = 4
64675
c557279d93f2 support for diagnostics;
wenzelm
parents: 64651
diff changeset
   454
  }
c557279d93f2 support for diagnostics;
wenzelm
parents: 64651
diff changeset
   455
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   456
  object PublishDiagnostics {
64777
ca09695eb43c clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents: 64767
diff changeset
   457
    def apply(file: JFile, diagnostics: List[Diagnostic]): JSON.T =
64675
c557279d93f2 support for diagnostics;
wenzelm
parents: 64651
diff changeset
   458
      Notification("textDocument/publishDiagnostics",
67850
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
   459
        JSON.Object("uri" -> Url.print_file(file), "diagnostics" -> diagnostics.map(_.json)))
64675
c557279d93f2 support for diagnostics;
wenzelm
parents: 64651
diff changeset
   460
  }
65094
386d9d487f62 support for decorations;
wenzelm
parents: 65093
diff changeset
   461
386d9d487f62 support for decorations;
wenzelm
parents: 65093
diff changeset
   462
386d9d487f62 support for decorations;
wenzelm
parents: 65093
diff changeset
   463
  /* decorations */
386d9d487f62 support for decorations;
wenzelm
parents: 65093
diff changeset
   464
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   465
  sealed case class Decoration_Options(range: Line.Range, hover_message: List[MarkedString]) {
65094
386d9d487f62 support for decorations;
wenzelm
parents: 65093
diff changeset
   466
    def json: JSON.T =
67850
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
   467
      JSON.Object("range" -> Range.compact(range)) ++
65173
3700be571a01 more compact protocol message;
wenzelm
parents: 65165
diff changeset
   468
      JSON.optional("hover_message" -> MarkedStrings.json(hover_message))
65094
386d9d487f62 support for decorations;
wenzelm
parents: 65093
diff changeset
   469
  }
386d9d487f62 support for decorations;
wenzelm
parents: 65093
diff changeset
   470
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   471
  sealed case class Decoration(decorations: List[(String, List[Decoration_Options])]) {
65095
eb21a4f70b0e publish decorations like diagnostics;
wenzelm
parents: 65094
diff changeset
   472
    def json(file: JFile): JSON.T =
65094
386d9d487f62 support for decorations;
wenzelm
parents: 65093
diff changeset
   473
      Notification("PIDE/decoration",
75126
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73541
diff changeset
   474
        JSON.Object(
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73541
diff changeset
   475
          "uri" -> Url.print_file(file),
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73541
diff changeset
   476
          "entries" -> decorations.map(decoration => JSON.Object(
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73541
diff changeset
   477
            "type" -> decoration._1,
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73541
diff changeset
   478
            "content" -> decoration._2.map(_.json))
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73541
diff changeset
   479
          ))
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73541
diff changeset
   480
      )
65094
386d9d487f62 support for decorations;
wenzelm
parents: 65093
diff changeset
   481
  }
65189
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65173
diff changeset
   482
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65173
diff changeset
   483
66215
9a8b6b86350c proper hyperlink_command, notably for locate_query;
wenzelm
parents: 66211
diff changeset
   484
  /* caret update: bidirectional */
65189
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65173
diff changeset
   485
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   486
  object Caret_Update {
66216
d4949bae0bad clarified editor focus;
wenzelm
parents: 66215
diff changeset
   487
    def apply(node_pos: Line.Node_Position, focus: Boolean): JSON.T =
66215
9a8b6b86350c proper hyperlink_command, notably for locate_query;
wenzelm
parents: 66211
diff changeset
   488
      Notification("PIDE/caret_update",
67850
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
   489
        JSON.Object(
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
   490
          "uri" -> Url.print_file_name(node_pos.name),
66215
9a8b6b86350c proper hyperlink_command, notably for locate_query;
wenzelm
parents: 66211
diff changeset
   491
          "line" -> node_pos.pos.line,
66216
d4949bae0bad clarified editor focus;
wenzelm
parents: 66215
diff changeset
   492
          "character" -> node_pos.pos.column,
d4949bae0bad clarified editor focus;
wenzelm
parents: 66215
diff changeset
   493
          "focus" -> focus))
66215
9a8b6b86350c proper hyperlink_command, notably for locate_query;
wenzelm
parents: 66211
diff changeset
   494
65189
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65173
diff changeset
   495
    def unapply(json: JSON.T): Option[Option[(JFile, Line.Position)]] =
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65173
diff changeset
   496
      json match {
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65173
diff changeset
   497
        case Notification("PIDE/caret_update", Some(params)) =>
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65173
diff changeset
   498
          val caret =
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65173
diff changeset
   499
            for {
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65173
diff changeset
   500
              uri <- JSON.string(params, "uri")
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65173
diff changeset
   501
              if Url.is_wellformed_file(uri)
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65173
diff changeset
   502
              pos <- Position.unapply(params)
66235
d4fa51e7c4ff retain symlinks in file names from VSCode: relevant for proper file locations in decorations etc.;
wenzelm
parents: 66216
diff changeset
   503
            } yield (Url.absolute_file(uri), pos)
65189
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65173
diff changeset
   504
          Some(caret)
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65173
diff changeset
   505
        case _ => None
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65173
diff changeset
   506
      }
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65173
diff changeset
   507
  }
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65173
diff changeset
   508
65201
2d01b30e6ac6 clarified modules;
wenzelm
parents: 65200
diff changeset
   509
2d01b30e6ac6 clarified modules;
wenzelm
parents: 65200
diff changeset
   510
  /* dynamic output */
2d01b30e6ac6 clarified modules;
wenzelm
parents: 65200
diff changeset
   511
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   512
  object Dynamic_Output {
66090
wenzelm
parents: 66062
diff changeset
   513
    def apply(content: String): JSON.T =
67850
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
   514
      Notification("PIDE/dynamic_output", JSON.Object("content" -> content))
65189
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65173
diff changeset
   515
  }
65977
c51b74be23b6 provide preview content on Scala side (similar to output);
wenzelm
parents: 65924
diff changeset
   516
c51b74be23b6 provide preview content on Scala side (similar to output);
wenzelm
parents: 65924
diff changeset
   517
66096
6187612e83c1 support for separate proof state output;
wenzelm
parents: 66090
diff changeset
   518
  /* state output */
6187612e83c1 support for separate proof state output;
wenzelm
parents: 66090
diff changeset
   519
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   520
  object State_Output {
75126
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73541
diff changeset
   521
    def apply(id: Counter.ID, content: String, auto_update: Boolean): JSON.T =
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   522
      Notification("PIDE/state_output",
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   523
        JSON.Object("id" -> id, "content" -> content, "auto_update" -> auto_update))
66096
6187612e83c1 support for separate proof state output;
wenzelm
parents: 66090
diff changeset
   524
  }
6187612e83c1 support for separate proof state output;
wenzelm
parents: 66090
diff changeset
   525
81021
89bfada3a16d lsp: added State_Init_Response message;
Thomas Lindae <thomas.lindae@in.tum.de>
parents: 75393
diff changeset
   526
  object State_Init_Response {
89bfada3a16d lsp: added State_Init_Response message;
Thomas Lindae <thomas.lindae@in.tum.de>
parents: 75393
diff changeset
   527
    def apply(id: Counter.ID): JSON.T =
89bfada3a16d lsp: added State_Init_Response message;
Thomas Lindae <thomas.lindae@in.tum.de>
parents: 75393
diff changeset
   528
      Notification("PIDE/state_init_response", JSON.Object("id" -> id))
89bfada3a16d lsp: added State_Init_Response message;
Thomas Lindae <thomas.lindae@in.tum.de>
parents: 75393
diff changeset
   529
  }
89bfada3a16d lsp: added State_Init_Response message;
Thomas Lindae <thomas.lindae@in.tum.de>
parents: 75393
diff changeset
   530
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   531
  class State_Id_Notification(name: String) {
66096
6187612e83c1 support for separate proof state output;
wenzelm
parents: 66090
diff changeset
   532
    def unapply(json: JSON.T): Option[Counter.ID] =
6187612e83c1 support for separate proof state output;
wenzelm
parents: 66090
diff changeset
   533
      json match {
6187612e83c1 support for separate proof state output;
wenzelm
parents: 66090
diff changeset
   534
        case Notification(method, Some(params)) if method == name => JSON.long(params, "id")
6187612e83c1 support for separate proof state output;
wenzelm
parents: 66090
diff changeset
   535
        case _ => None
6187612e83c1 support for separate proof state output;
wenzelm
parents: 66090
diff changeset
   536
      }
6187612e83c1 support for separate proof state output;
wenzelm
parents: 66090
diff changeset
   537
  }
6187612e83c1 support for separate proof state output;
wenzelm
parents: 66090
diff changeset
   538
6187612e83c1 support for separate proof state output;
wenzelm
parents: 66090
diff changeset
   539
  object State_Init extends Notification0("PIDE/state_init")
6187612e83c1 support for separate proof state output;
wenzelm
parents: 66090
diff changeset
   540
  object State_Exit extends State_Id_Notification("PIDE/state_exit")
6187612e83c1 support for separate proof state output;
wenzelm
parents: 66090
diff changeset
   541
  object State_Locate extends State_Id_Notification("PIDE/state_locate")
6187612e83c1 support for separate proof state output;
wenzelm
parents: 66090
diff changeset
   542
  object State_Update extends State_Id_Notification("PIDE/state_update")
6187612e83c1 support for separate proof state output;
wenzelm
parents: 66090
diff changeset
   543
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   544
  object State_Auto_Update {
66211
100c9c997e2b HTML GUI actions via JavaScript;
wenzelm
parents: 66140
diff changeset
   545
    def unapply(json: JSON.T): Option[(Counter.ID, Boolean)] =
100c9c997e2b HTML GUI actions via JavaScript;
wenzelm
parents: 66140
diff changeset
   546
      json match {
100c9c997e2b HTML GUI actions via JavaScript;
wenzelm
parents: 66140
diff changeset
   547
        case Notification("PIDE/state_auto_update", Some(params)) =>
100c9c997e2b HTML GUI actions via JavaScript;
wenzelm
parents: 66140
diff changeset
   548
          for {
100c9c997e2b HTML GUI actions via JavaScript;
wenzelm
parents: 66140
diff changeset
   549
            id <- JSON.long(params, "id")
100c9c997e2b HTML GUI actions via JavaScript;
wenzelm
parents: 66140
diff changeset
   550
            enabled <- JSON.bool(params, "enabled")
100c9c997e2b HTML GUI actions via JavaScript;
wenzelm
parents: 66140
diff changeset
   551
          } yield (id, enabled)
100c9c997e2b HTML GUI actions via JavaScript;
wenzelm
parents: 66140
diff changeset
   552
        case _ => None
100c9c997e2b HTML GUI actions via JavaScript;
wenzelm
parents: 66140
diff changeset
   553
      }
100c9c997e2b HTML GUI actions via JavaScript;
wenzelm
parents: 66140
diff changeset
   554
  }
100c9c997e2b HTML GUI actions via JavaScript;
wenzelm
parents: 66140
diff changeset
   555
66096
6187612e83c1 support for separate proof state output;
wenzelm
parents: 66090
diff changeset
   556
65983
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
   557
  /* preview */
65977
c51b74be23b6 provide preview content on Scala side (similar to output);
wenzelm
parents: 65924
diff changeset
   558
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   559
  object Preview_Request {
65983
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
   560
    def unapply(json: JSON.T): Option[(JFile, Int)] =
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
   561
      json match {
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
   562
        case Notification("PIDE/preview_request", Some(params)) =>
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
   563
          for {
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
   564
            uri <- JSON.string(params, "uri")
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
   565
            if Url.is_wellformed_file(uri)
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
   566
            column <- JSON.int(params, "column")
66235
d4fa51e7c4ff retain symlinks in file names from VSCode: relevant for proper file locations in decorations etc.;
wenzelm
parents: 66216
diff changeset
   567
          } yield (Url.absolute_file(uri), column)
65983
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
   568
        case _ => None
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
   569
      }
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
   570
  }
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
   571
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75263
diff changeset
   572
  object Preview_Response {
65983
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
   573
    def apply(file: JFile, column: Int, label: String, content: String): JSON.T =
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
   574
      Notification("PIDE/preview_response",
67850
3e9fe7a84b5d tuned signature;
wenzelm
parents: 67843
diff changeset
   575
        JSON.Object(
65983
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
   576
          "uri" -> Url.print_file(file),
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
   577
          "column" -> column,
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
   578
          "label" -> label,
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
   579
          "content" -> content))
65977
c51b74be23b6 provide preview content on Scala side (similar to output);
wenzelm
parents: 65924
diff changeset
   580
  }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   581
}