src/Tools/VSCode/src/server.scala
author wenzelm
Wed, 14 Mar 2018 16:48:05 +0100
changeset 67856 ec9f1ec763a0
parent 67846 bdf6933f7ac9
child 67870 586be47e00b3
permissions -rw-r--r--
tuned signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     1
/*  Title:      Tools/VSCode/src/server.scala
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
65160
6e042537555d incremental document changes;
wenzelm
parents: 65134
diff changeset
     4
Server for VS Code Language Server Protocol 2.0/3.0, see also
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     5
https://github.com/Microsoft/language-server-protocol
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     6
https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md
65160
6e042537555d incremental document changes;
wenzelm
parents: 65134
diff changeset
     7
6e042537555d incremental document changes;
wenzelm
parents: 65134
diff changeset
     8
PIDE protocol extensions depend on system option "vscode_pide_extensions".
64605
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
package isabelle.vscode
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    12
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    13
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    14
import isabelle._
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    15
64710
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
    16
import java.io.{PrintStream, OutputStream, File => JFile}
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    17
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    18
import scala.annotation.tailrec
65234
1d6e9048cb62 normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents: 65232
diff changeset
    19
import scala.collection.mutable
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    20
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    21
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    22
object Server
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    23
{
66100
d1ad5a7458c2 proper treatment of editor overlays;
wenzelm
parents: 66098
diff changeset
    24
  type Editor = isabelle.Editor[Unit]
d1ad5a7458c2 proper treatment of editor overlays;
wenzelm
parents: 66098
diff changeset
    25
d1ad5a7458c2 proper treatment of editor overlays;
wenzelm
parents: 66098
diff changeset
    26
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    27
  /* Isabelle tool wrapper */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    28
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    29
  private lazy val default_logic = Isabelle_System.getenv("ISABELLE_LOGIC")
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    30
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    31
  val isabelle_tool = Isabelle_Tool("vscode_server", "VSCode Language Server for PIDE", args =>
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    32
  {
64644
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    33
    try {
66241
8f39d60b943d all_known can cause timeout of VSCode server startup, notably on Windows;
wenzelm
parents: 66216
diff changeset
    34
      var all_known = false
64644
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    35
      var log_file: Option[Path] = None
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    36
      var dirs: List[Path] = Nil
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    37
      var logic = default_logic
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    38
      var modes: List[String] = Nil
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    39
      var options = Options.init()
64733
20174e871623 automatically build session image;
wenzelm
parents: 64727
diff changeset
    40
      var system_mode = false
65922
d2f19f05c0e9 clarified message logging;
wenzelm
parents: 65512
diff changeset
    41
      var verbose = false
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    42
64644
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    43
      val getopts = Getopts("""
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    44
Usage: isabelle vscode_server [OPTIONS]
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
  Options are:
66241
8f39d60b943d all_known can cause timeout of VSCode server startup, notably on Windows;
wenzelm
parents: 66216
diff changeset
    47
    -A           explore theory name space of all known sessions (potentially slow)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    48
    -L FILE      enable logging on FILE
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    49
    -d DIR       include session directory
64737
9fc965612459 proper default;
wenzelm
parents: 64733
diff changeset
    50
    -l NAME      logic session name (default ISABELLE_LOGIC=""" + quote(default_logic) + """)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    51
    -m MODE      add print mode for output
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    52
    -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
64733
20174e871623 automatically build session image;
wenzelm
parents: 64727
diff changeset
    53
    -s           system build mode for session image
65922
d2f19f05c0e9 clarified message logging;
wenzelm
parents: 65512
diff changeset
    54
    -v           verbose logging
64605
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
  Run the VSCode Language Server protocol (JSON RPC) over stdin/stdout.
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    57
""",
66241
8f39d60b943d all_known can cause timeout of VSCode server startup, notably on Windows;
wenzelm
parents: 66216
diff changeset
    58
        "A" -> (_ => all_known = true),
64755
ceb81f4928ea support for Windows;
wenzelm
parents: 64748
diff changeset
    59
        "L:" -> (arg => log_file = Some(Path.explode(File.standard_path(arg)))),
ceb81f4928ea support for Windows;
wenzelm
parents: 64748
diff changeset
    60
        "d:" -> (arg => dirs = dirs ::: List(Path.explode(File.standard_path(arg)))),
64644
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    61
        "l:" -> (arg => logic = arg),
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    62
        "m:" -> (arg => modes = arg :: modes),
64733
20174e871623 automatically build session image;
wenzelm
parents: 64727
diff changeset
    63
        "o:" -> (arg => options = options + arg),
65922
d2f19f05c0e9 clarified message logging;
wenzelm
parents: 65512
diff changeset
    64
        "s" -> (_ => system_mode = true),
d2f19f05c0e9 clarified message logging;
wenzelm
parents: 65512
diff changeset
    65
        "v" -> (_ => verbose = true))
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    66
64644
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    67
      val more_args = getopts(args)
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    68
      if (more_args.nonEmpty) getopts.usage()
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    69
64717
d2b50eb3d9ab tuned signature;
wenzelm
parents: 64710
diff changeset
    70
      val log = Logger.make(log_file)
65922
d2f19f05c0e9 clarified message logging;
wenzelm
parents: 65512
diff changeset
    71
      val channel = new Channel(System.in, System.out, log, verbose)
66241
8f39d60b943d all_known can cause timeout of VSCode server startup, notably on Windows;
wenzelm
parents: 66216
diff changeset
    72
      val server =
8f39d60b943d all_known can cause timeout of VSCode server startup, notably on Windows;
wenzelm
parents: 66216
diff changeset
    73
        new Server(channel, options, session_name = logic, session_dirs = dirs,
8f39d60b943d all_known can cause timeout of VSCode server startup, notably on Windows;
wenzelm
parents: 66216
diff changeset
    74
          all_known = all_known, modes = modes, system_mode = system_mode, log = log)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    75
64644
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    76
      // prevent spurious garbage on the main protocol channel
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    77
      val orig_out = System.out
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    78
      try {
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    79
        System.setOut(new PrintStream(new OutputStream { def write(n: Int) {} }))
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    80
        server.start()
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    81
      }
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    82
      finally { System.setOut(orig_out) }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    83
    }
64644
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    84
    catch {
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    85
      case exn: Throwable =>
64717
d2b50eb3d9ab tuned signature;
wenzelm
parents: 64710
diff changeset
    86
        val channel = new Channel(System.in, System.out, No_Logger)
64644
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    87
        channel.error_message(Exn.message(exn))
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    88
        throw(exn)
7dbc9485ed70 more explicit error;
wenzelm
parents: 64643
diff changeset
    89
    }
64605
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
}
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    92
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    93
class Server(
65191
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents: 65189
diff changeset
    94
  val channel: Channel,
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    95
  options: Options,
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    96
  session_name: String = Server.default_logic,
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    97
  session_dirs: List[Path] = Nil,
66241
8f39d60b943d all_known can cause timeout of VSCode server startup, notably on Windows;
wenzelm
parents: 66216
diff changeset
    98
  all_known: Boolean = false,
64717
d2b50eb3d9ab tuned signature;
wenzelm
parents: 64710
diff changeset
    99
  modes: List[String] = Nil,
64733
20174e871623 automatically build session image;
wenzelm
parents: 64727
diff changeset
   100
  system_mode: Boolean = false,
64717
d2b50eb3d9ab tuned signature;
wenzelm
parents: 64710
diff changeset
   101
  log: Logger = No_Logger)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   102
{
66085
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   103
  server =>
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   104
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   105
64725
wenzelm
parents: 64724
diff changeset
   106
  /* prover session */
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   107
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   108
  private val session_ = Synchronized(None: Option[Session])
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   109
  def session: Session = session_.value getOrElse error("Server inactive")
64623
83f012ce2567 clarified module name;
wenzelm
parents: 64622
diff changeset
   110
  def resources: VSCode_Resources = session.resources.asInstanceOf[VSCode_Resources]
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   111
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   112
  def rendering_offset(node_pos: Line.Node_Position): Option[(VSCode_Rendering, Text.Offset)] =
64649
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64648
diff changeset
   113
    for {
64777
ca09695eb43c clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents: 64767
diff changeset
   114
      model <- resources.get_model(new JFile(node_pos.name))
64704
08c2d80428ff re-use options from resources;
wenzelm
parents: 64703
diff changeset
   115
      rendering = model.rendering()
64830
9bc44bef99e6 more explocit Document_Model.Content;
wenzelm
parents: 64821
diff changeset
   116
      offset <- model.content.doc.offset(node_pos.pos)
64649
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64648
diff changeset
   117
    } yield (rendering, offset)
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64648
diff changeset
   118
66085
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   119
  private val dynamic_output = Dynamic_Output(server)
65191
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents: 65189
diff changeset
   120
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   121
64725
wenzelm
parents: 64724
diff changeset
   122
  /* input from client or file-system */
wenzelm
parents: 64724
diff changeset
   123
64800
415dafeb9669 manage document blobs as well;
wenzelm
parents: 64783
diff changeset
   124
  private val delay_input: Standard_Thread.Delay =
64810
05b29c8f0add more informative error for spurious crash;
wenzelm
parents: 64801
diff changeset
   125
    Standard_Thread.delay_last(options.seconds("vscode_input_delay"), channel.Error_Logger)
66676
39db5bb7eb0a recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents: 66675
diff changeset
   126
    { resources.flush_input(session, channel) }
64725
wenzelm
parents: 64724
diff changeset
   127
64800
415dafeb9669 manage document blobs as well;
wenzelm
parents: 64783
diff changeset
   128
  private val delay_load: Standard_Thread.Delay =
64810
05b29c8f0add more informative error for spurious crash;
wenzelm
parents: 64801
diff changeset
   129
    Standard_Thread.delay_last(options.seconds("vscode_load_delay"), channel.Error_Logger) {
66100
d1ad5a7458c2 proper treatment of editor overlays;
wenzelm
parents: 66098
diff changeset
   130
      val (invoke_input, invoke_load) =
d1ad5a7458c2 proper treatment of editor overlays;
wenzelm
parents: 66098
diff changeset
   131
        resources.resolve_dependencies(session, editor, file_watcher)
64800
415dafeb9669 manage document blobs as well;
wenzelm
parents: 64783
diff changeset
   132
      if (invoke_input) delay_input.invoke()
415dafeb9669 manage document blobs as well;
wenzelm
parents: 64783
diff changeset
   133
      if (invoke_load) delay_load.invoke
415dafeb9669 manage document blobs as well;
wenzelm
parents: 64783
diff changeset
   134
    }
64727
13e37567a0d6 automatically resolve dependencies from document models and file-system;
wenzelm
parents: 64725
diff changeset
   135
64857
wenzelm
parents: 64856
diff changeset
   136
  private val file_watcher =
64727
13e37567a0d6 automatically resolve dependencies from document models and file-system;
wenzelm
parents: 64725
diff changeset
   137
    File_Watcher(sync_documents(_), options.seconds("vscode_load_delay"))
64725
wenzelm
parents: 64724
diff changeset
   138
65111
3224a6f7bd6b more robust treatment of pending input/output: these are often correlated;
wenzelm
parents: 65107
diff changeset
   139
  private def close_document(file: JFile)
3224a6f7bd6b more robust treatment of pending input/output: these are often correlated;
wenzelm
parents: 65107
diff changeset
   140
  {
3224a6f7bd6b more robust treatment of pending input/output: these are often correlated;
wenzelm
parents: 65107
diff changeset
   141
    if (resources.close_model(file)) {
3224a6f7bd6b more robust treatment of pending input/output: these are often correlated;
wenzelm
parents: 65107
diff changeset
   142
      file_watcher.register_parent(file)
65116
06d9bcb66ef3 simplified;
wenzelm
parents: 65111
diff changeset
   143
      sync_documents(Set(file))
06d9bcb66ef3 simplified;
wenzelm
parents: 65111
diff changeset
   144
      delay_input.invoke()
06d9bcb66ef3 simplified;
wenzelm
parents: 65111
diff changeset
   145
      delay_output.invoke()
65111
3224a6f7bd6b more robust treatment of pending input/output: these are often correlated;
wenzelm
parents: 65107
diff changeset
   146
    }
3224a6f7bd6b more robust treatment of pending input/output: these are often correlated;
wenzelm
parents: 65107
diff changeset
   147
  }
3224a6f7bd6b more robust treatment of pending input/output: these are often correlated;
wenzelm
parents: 65107
diff changeset
   148
65116
06d9bcb66ef3 simplified;
wenzelm
parents: 65111
diff changeset
   149
  private def sync_documents(changed: Set[JFile])
06d9bcb66ef3 simplified;
wenzelm
parents: 65111
diff changeset
   150
  {
06d9bcb66ef3 simplified;
wenzelm
parents: 65111
diff changeset
   151
    resources.sync_models(changed)
06d9bcb66ef3 simplified;
wenzelm
parents: 65111
diff changeset
   152
    delay_input.invoke()
06d9bcb66ef3 simplified;
wenzelm
parents: 65111
diff changeset
   153
    delay_output.invoke()
06d9bcb66ef3 simplified;
wenzelm
parents: 65111
diff changeset
   154
  }
64679
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   155
66674
30d5195299e2 store document version;
wenzelm
parents: 66389
diff changeset
   156
  private def change_document(file: JFile, version: Long, changes: List[Protocol.TextDocumentChange])
65160
6e042537555d incremental document changes;
wenzelm
parents: 65134
diff changeset
   157
  {
65234
1d6e9048cb62 normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents: 65232
diff changeset
   158
    val norm_changes = new mutable.ListBuffer[Protocol.TextDocumentChange]
1d6e9048cb62 normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents: 65232
diff changeset
   159
    @tailrec def norm(chs: List[Protocol.TextDocumentChange])
1d6e9048cb62 normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents: 65232
diff changeset
   160
    {
1d6e9048cb62 normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents: 65232
diff changeset
   161
      if (chs.nonEmpty) {
1d6e9048cb62 normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents: 65232
diff changeset
   162
        val (full_texts, rest1) = chs.span(_.range.isEmpty)
1d6e9048cb62 normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents: 65232
diff changeset
   163
        val (edits, rest2) = rest1.span(_.range.nonEmpty)
1d6e9048cb62 normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents: 65232
diff changeset
   164
        norm_changes ++= full_texts
1d6e9048cb62 normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents: 65232
diff changeset
   165
        norm_changes ++= edits.sortBy(_.range.get.start)(Line.Position.Ordering).reverse
1d6e9048cb62 normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents: 65232
diff changeset
   166
        norm(rest2)
1d6e9048cb62 normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents: 65232
diff changeset
   167
      }
1d6e9048cb62 normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents: 65232
diff changeset
   168
    }
1d6e9048cb62 normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents: 65232
diff changeset
   169
    norm(changes)
1d6e9048cb62 normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents: 65232
diff changeset
   170
    norm_changes.foreach(change =>
66674
30d5195299e2 store document version;
wenzelm
parents: 66389
diff changeset
   171
      resources.change_model(session, editor, file, version, change.text, change.range))
65234
1d6e9048cb62 normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents: 65232
diff changeset
   172
64679
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   173
    delay_input.invoke()
65111
3224a6f7bd6b more robust treatment of pending input/output: these are often correlated;
wenzelm
parents: 65107
diff changeset
   174
    delay_output.invoke()
64679
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   175
  }
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   176
64710
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   177
65189
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65184
diff changeset
   178
  /* caret handling */
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65184
diff changeset
   179
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65184
diff changeset
   180
  private val delay_caret_update: Standard_Thread.Delay =
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65184
diff changeset
   181
    Standard_Thread.delay_last(options.seconds("vscode_input_delay"), channel.Error_Logger)
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65184
diff changeset
   182
    { session.caret_focus.post(Session.Caret_Focus) }
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65184
diff changeset
   183
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65184
diff changeset
   184
  private def update_caret(caret: Option[(JFile, Line.Position)])
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65184
diff changeset
   185
  {
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65184
diff changeset
   186
    resources.update_caret(caret)
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65184
diff changeset
   187
    delay_caret_update.invoke()
65926
0f7821a07aa9 restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents: 65922
diff changeset
   188
    delay_input.invoke()
65189
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65184
diff changeset
   189
  }
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65184
diff changeset
   190
64679
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   191
65983
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65977
diff changeset
   192
  /* preview */
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65977
diff changeset
   193
66098
5aa9cb83e70e clarified modules;
wenzelm
parents: 66096
diff changeset
   194
  private lazy val preview_panel = new Preview_Panel(resources)
65983
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65977
diff changeset
   195
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65977
diff changeset
   196
  private lazy val delay_preview: Standard_Thread.Delay =
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65977
diff changeset
   197
    Standard_Thread.delay_last(options.seconds("vscode_output_delay"), channel.Error_Logger)
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65977
diff changeset
   198
    {
66098
5aa9cb83e70e clarified modules;
wenzelm
parents: 66096
diff changeset
   199
      if (preview_panel.flush(channel)) delay_preview.invoke()
65983
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65977
diff changeset
   200
    }
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65977
diff changeset
   201
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65977
diff changeset
   202
  private def request_preview(file: JFile, column: Int)
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65977
diff changeset
   203
  {
66098
5aa9cb83e70e clarified modules;
wenzelm
parents: 66096
diff changeset
   204
    preview_panel.request(file, column)
65983
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65977
diff changeset
   205
    delay_preview.invoke()
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65977
diff changeset
   206
  }
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65977
diff changeset
   207
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65977
diff changeset
   208
64679
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   209
  /* output to client */
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   210
64691
db2b21a52f20 prefer stable state -- reduce repeated diagnostics;
wenzelm
parents: 64690
diff changeset
   211
  private val delay_output: Standard_Thread.Delay =
64810
05b29c8f0add more informative error for spurious crash;
wenzelm
parents: 64801
diff changeset
   212
    Standard_Thread.delay_last(options.seconds("vscode_output_delay"), channel.Error_Logger)
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   213
    {
65232
ca571c8c0788 avoid race condition between current_state().stable_tip_version and model.rendering();
wenzelm
parents: 65231
diff changeset
   214
      if (resources.flush_output(channel)) delay_output.invoke()
64679
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   215
    }
b2bf280b7e13 more uniform treatment of input/output wrt. client;
wenzelm
parents: 64673
diff changeset
   216
66138
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   217
  def update_output(changed_nodes: Traversable[JFile])
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   218
  {
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   219
    resources.update_output(changed_nodes)
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   220
    delay_output.invoke()
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   221
  }
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   222
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   223
  def update_output_visible()
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   224
  {
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   225
    resources.update_output_visible()
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   226
    delay_output.invoke()
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   227
  }
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   228
64725
wenzelm
parents: 64724
diff changeset
   229
  private val prover_output =
wenzelm
parents: 64724
diff changeset
   230
    Session.Consumer[Session.Commands_Changed](getClass.getName) {
65119
a7bedf94e71c always invoke output: pending_output may be present due to other reasons;
wenzelm
parents: 65116
diff changeset
   231
      case changed =>
66138
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   232
        update_output(changed.nodes.toList.map(resources.node_file(_)))
64725
wenzelm
parents: 64724
diff changeset
   233
    }
64710
72ca4e5f976e manage changes of external files;
wenzelm
parents: 64709
diff changeset
   234
66389
wenzelm
parents: 66241
diff changeset
   235
  private val syslog_messages =
wenzelm
parents: 66241
diff changeset
   236
    Session.Consumer[Prover.Output](getClass.getName) {
wenzelm
parents: 66241
diff changeset
   237
      case output => channel.log_writeln(resources.output_xml(output.message))
64692
ccf017e2f2b4 support for syslog messages;
wenzelm
parents: 64691
diff changeset
   238
    }
ccf017e2f2b4 support for syslog messages;
wenzelm
parents: 64691
diff changeset
   239
ccf017e2f2b4 support for syslog messages;
wenzelm
parents: 64691
diff changeset
   240
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   241
  /* init and exit */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   242
64641
wenzelm
parents: 64623
diff changeset
   243
  def init(id: Protocol.Id)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   244
  {
64641
wenzelm
parents: 64623
diff changeset
   245
    def reply(err: String)
wenzelm
parents: 64623
diff changeset
   246
    {
wenzelm
parents: 64623
diff changeset
   247
      channel.write(Protocol.Initialize.reply(id, err))
64642
c231206a84c8 display messages, according to regular Isabelle Output;
wenzelm
parents: 64641
diff changeset
   248
      if (err == "")
c231206a84c8 display messages, according to regular Isabelle Output;
wenzelm
parents: 64641
diff changeset
   249
        channel.writeln("Welcome to Isabelle/" + session_name + " (" + Distribution.version + ")")
c231206a84c8 display messages, according to regular Isabelle Output;
wenzelm
parents: 64641
diff changeset
   250
      else channel.error_message(err)
64641
wenzelm
parents: 64623
diff changeset
   251
    }
wenzelm
parents: 64623
diff changeset
   252
64643
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   253
    val try_session =
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   254
      try {
67846
wenzelm
parents: 67292
diff changeset
   255
        if (!Build.build(options, build_heap = true, no_build = true,
wenzelm
parents: 67292
diff changeset
   256
            system_mode = system_mode, dirs = session_dirs, sessions = List(session_name)).ok)
64733
20174e871623 automatically build session image;
wenzelm
parents: 64727
diff changeset
   257
        {
20174e871623 automatically build session image;
wenzelm
parents: 64727
diff changeset
   258
          val start_msg = "Build started for Isabelle/" + session_name + " ..."
20174e871623 automatically build session image;
wenzelm
parents: 64727
diff changeset
   259
          val fail_msg = "Session build failed -- prover process remains inactive!"
20174e871623 automatically build session image;
wenzelm
parents: 64727
diff changeset
   260
67856
ec9f1ec763a0 tuned signature;
wenzelm
parents: 67846
diff changeset
   261
          val progress = channel.progress(verbose = true)
64733
20174e871623 automatically build session image;
wenzelm
parents: 64727
diff changeset
   262
          progress.echo(start_msg); channel.writeln(start_msg)
20174e871623 automatically build session image;
wenzelm
parents: 64727
diff changeset
   263
67846
wenzelm
parents: 67292
diff changeset
   264
          if (!Build.build(options, progress = progress, build_heap = true,
64733
20174e871623 automatically build session image;
wenzelm
parents: 64727
diff changeset
   265
              system_mode = system_mode, dirs = session_dirs, sessions = List(session_name)).ok)
20174e871623 automatically build session image;
wenzelm
parents: 64727
diff changeset
   266
          {
20174e871623 automatically build session image;
wenzelm
parents: 64727
diff changeset
   267
            progress.echo(fail_msg); error(fail_msg)
20174e871623 automatically build session image;
wenzelm
parents: 64727
diff changeset
   268
          }
20174e871623 automatically build session image;
wenzelm
parents: 64727
diff changeset
   269
        }
20174e871623 automatically build session image;
wenzelm
parents: 64727
diff changeset
   270
65512
9fd620f2fa7d always explore all sessions;
wenzelm
parents: 65361
diff changeset
   271
        val session_base =
66976
806bc39550a5 tuned signature;
wenzelm
parents: 66964
diff changeset
   272
          Sessions.base_info(
66964
9f2de457b95e clarified signature;
wenzelm
parents: 66963
diff changeset
   273
            options, session_name, dirs = session_dirs, all_known = all_known).check_base
65361
ecefb68dc21d tuned signature;
wenzelm
parents: 65317
diff changeset
   274
        val resources = new VSCode_Resources(options, session_base, log)
64854
f5aa712e6250 tuned signature;
wenzelm
parents: 64830
diff changeset
   275
          {
64727
13e37567a0d6 automatically resolve dependencies from document models and file-system;
wenzelm
parents: 64725
diff changeset
   276
            override def commit(change: Session.Change): Unit =
64800
415dafeb9669 manage document blobs as well;
wenzelm
parents: 64783
diff changeset
   277
              if (change.deps_changed || undefined_blobs(change.version.nodes).nonEmpty)
415dafeb9669 manage document blobs as well;
wenzelm
parents: 64783
diff changeset
   278
                delay_load.invoke()
64727
13e37567a0d6 automatically resolve dependencies from document models and file-system;
wenzelm
parents: 64725
diff changeset
   279
          }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   280
65272
7611c55c39d0 always output proof state: there is only one output buffer in Isabelle/VSCode;
wenzelm
parents: 65264
diff changeset
   281
        val session_options = options.bool("editor_output_state") = true
7611c55c39d0 always output proof state: there is only one output buffer in Isabelle/VSCode;
wenzelm
parents: 65264
diff changeset
   282
        Some(new Session(session_options, resources))
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   283
      }
64643
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   284
      catch { case ERROR(msg) => reply(msg); None }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   285
64643
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   286
    for (session <- try_session) {
65191
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents: 65189
diff changeset
   287
      session_.change(_ => Some(session))
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents: 65189
diff changeset
   288
65227
wenzelm
parents: 65216
diff changeset
   289
      session.commands_changed += prover_output
66389
wenzelm
parents: 66241
diff changeset
   290
      session.syslog_messages += syslog_messages
65227
wenzelm
parents: 65216
diff changeset
   291
wenzelm
parents: 65216
diff changeset
   292
      dynamic_output.init()
wenzelm
parents: 65216
diff changeset
   293
64643
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   294
      var session_phase: Session.Consumer[Session.Phase] = null
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   295
      session_phase =
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   296
        Session.Consumer(getClass.getName) {
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   297
          case Session.Ready =>
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   298
            session.phase_changed -= session_phase
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   299
            reply("")
65317
b9f5cd845616 more informative session result;
wenzelm
parents: 65272
diff changeset
   300
          case Session.Terminated(result) if !result.ok =>
64643
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   301
            session.phase_changed -= session_phase
65317
b9f5cd845616 more informative session result;
wenzelm
parents: 65272
diff changeset
   302
            reply("Prover startup failed: return code " + result.rc)
64643
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   303
          case _ =>
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   304
        }
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   305
      session.phase_changed += session_phase
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   306
65216
060a8a1f2dec tuned signature;
wenzelm
parents: 65210
diff changeset
   307
      Isabelle_Process.start(session, options,
060a8a1f2dec tuned signature;
wenzelm
parents: 65210
diff changeset
   308
        logic = session_name, dirs = session_dirs, modes = modes)
64643
b755f6069ba2 more explicit error;
wenzelm
parents: 64642
diff changeset
   309
    }
64605
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
64641
wenzelm
parents: 64623
diff changeset
   312
  def shutdown(id: Protocol.Id)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   313
  {
64641
wenzelm
parents: 64623
diff changeset
   314
    def reply(err: String): Unit = channel.write(Protocol.Shutdown.reply(id, err))
wenzelm
parents: 64623
diff changeset
   315
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   316
    session_.change({
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   317
      case Some(session) =>
65228
b11099b7d868 clarified shutdown;
wenzelm
parents: 65227
diff changeset
   318
        session.commands_changed -= prover_output
66389
wenzelm
parents: 66241
diff changeset
   319
        session.syslog_messages -= syslog_messages
65228
b11099b7d868 clarified shutdown;
wenzelm
parents: 65227
diff changeset
   320
65191
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents: 65189
diff changeset
   321
        dynamic_output.exit()
65228
b11099b7d868 clarified shutdown;
wenzelm
parents: 65227
diff changeset
   322
b11099b7d868 clarified shutdown;
wenzelm
parents: 65227
diff changeset
   323
        delay_load.revoke()
b11099b7d868 clarified shutdown;
wenzelm
parents: 65227
diff changeset
   324
        file_watcher.shutdown()
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   325
        delay_input.revoke()
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   326
        delay_output.revoke()
65228
b11099b7d868 clarified shutdown;
wenzelm
parents: 65227
diff changeset
   327
        delay_caret_update.revoke()
65983
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65977
diff changeset
   328
        delay_preview.revoke()
65228
b11099b7d868 clarified shutdown;
wenzelm
parents: 65227
diff changeset
   329
66677
fa70edfcb6fa proper result type (cf. b9f5cd845616);
wenzelm
parents: 66676
diff changeset
   330
        val result = session.stop()
fa70edfcb6fa proper result type (cf. b9f5cd845616);
wenzelm
parents: 66676
diff changeset
   331
        if (result.ok) reply("") else reply("Prover shutdown failed: return code " + result.rc)
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   332
        None
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   333
      case None =>
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   334
        reply("Prover inactive")
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   335
        None
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   336
    })
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   337
  }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   338
64646
805c5e6fa430 tuned messages;
wenzelm
parents: 64645
diff changeset
   339
  def exit() {
64717
d2b50eb3d9ab tuned signature;
wenzelm
parents: 64710
diff changeset
   340
    log("\n")
64703
a115391494ed moved main state to VSCode_Resources;
wenzelm
parents: 64702
diff changeset
   341
    sys.exit(if (session_.value.isDefined) 1 else 0)
64646
805c5e6fa430 tuned messages;
wenzelm
parents: 64645
diff changeset
   342
  }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   343
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   344
64877
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64870
diff changeset
   345
  /* completion */
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64870
diff changeset
   346
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64870
diff changeset
   347
  def completion(id: Protocol.Id, node_pos: Line.Node_Position)
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64870
diff changeset
   348
  {
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64870
diff changeset
   349
    val result =
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64870
diff changeset
   350
      (for ((rendering, offset) <- rendering_offset(node_pos))
66054
fb0eea226a4d more uniform syntax_completion + semantic_completion;
wenzelm
parents: 66052
diff changeset
   351
        yield rendering.completion(node_pos.pos, offset)) getOrElse Nil
64877
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64870
diff changeset
   352
    channel.write(Protocol.Completion.reply(id, result))
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64870
diff changeset
   353
  }
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64870
diff changeset
   354
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64870
diff changeset
   355
66138
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   356
  /* spell-checker dictionary */
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   357
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   358
  def update_dictionary(include: Boolean, permanent: Boolean)
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   359
  {
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   360
    for {
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   361
      spell_checker <- resources.spell_checker.get
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   362
      caret <- resources.get_caret()
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   363
      rendering = caret.model.rendering()
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   364
      range = rendering.before_caret_range(caret.offset)
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   365
      Text.Info(_, word) <- Spell_Checker.current_word(rendering, range)
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   366
    } {
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   367
      spell_checker.update(word, include, permanent)
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   368
      update_output_visible()
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   369
    }
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   370
  }
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   371
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   372
  def reset_dictionary()
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   373
  {
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   374
    for (spell_checker <- resources.spell_checker.get)
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   375
    {
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   376
      spell_checker.reset()
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   377
      update_output_visible()
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   378
    }
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   379
  }
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   380
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   381
64622
529bbb8977c7 more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents: 64618
diff changeset
   382
  /* hover */
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   383
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   384
  def hover(id: Protocol.Id, node_pos: Line.Node_Position)
64641
wenzelm
parents: 64623
diff changeset
   385
  {
wenzelm
parents: 64623
diff changeset
   386
    val result =
wenzelm
parents: 64623
diff changeset
   387
      for {
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   388
        (rendering, offset) <- rendering_offset(node_pos)
65134
511bf19348d3 clarified messages (with improved scalability): legacy/error as diagnostics, writeln/information/warning/bad as tooltips (dynamic hover);
wenzelm
parents: 65129
diff changeset
   389
        info <- rendering.tooltips(VSCode_Rendering.tooltip_elements, Text.Range(offset, offset + 1))
64641
wenzelm
parents: 64623
diff changeset
   390
      } yield {
65197
8fada74d82be tuned signature;
wenzelm
parents: 65196
diff changeset
   391
        val range = rendering.model.content.doc.range(info.range)
64748
155bf8632104 clarified multiple tooltips;
wenzelm
parents: 64747
diff changeset
   392
        val contents =
65107
70b0113fa4ef clarified pretty margin;
wenzelm
parents: 65093
diff changeset
   393
          info.info.map(t => Protocol.MarkedString(resources.output_pretty_tooltip(List(t))))
64748
155bf8632104 clarified multiple tooltips;
wenzelm
parents: 64747
diff changeset
   394
        (range, contents)
64641
wenzelm
parents: 64623
diff changeset
   395
      }
wenzelm
parents: 64623
diff changeset
   396
    channel.write(Protocol.Hover.reply(id, result))
wenzelm
parents: 64623
diff changeset
   397
  }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   398
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   399
64648
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   400
  /* goto definition */
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   401
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   402
  def goto_definition(id: Protocol.Id, node_pos: Line.Node_Position)
64648
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   403
  {
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   404
    val result =
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   405
      (for ((rendering, offset) <- rendering_offset(node_pos))
64649
d67c3094a0c2 tuned signature -- more explicit types;
wenzelm
parents: 64648
diff changeset
   406
        yield rendering.hyperlinks(Text.Range(offset, offset + 1))) getOrElse Nil
64648
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   407
    channel.write(Protocol.GotoDefinition.reply(id, result))
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   408
  }
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   409
5d7f741aaccb basic support for hyperlinks / Goto Definition Request;
wenzelm
parents: 64646
diff changeset
   410
64767
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64755
diff changeset
   411
  /* document highlights */
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64755
diff changeset
   412
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64755
diff changeset
   413
  def document_highlights(id: Protocol.Id, node_pos: Line.Node_Position)
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64755
diff changeset
   414
  {
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64755
diff changeset
   415
    val result =
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64755
diff changeset
   416
      (for ((rendering, offset) <- rendering_offset(node_pos))
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64755
diff changeset
   417
        yield {
65197
8fada74d82be tuned signature;
wenzelm
parents: 65196
diff changeset
   418
          val model = rendering.model
8fada74d82be tuned signature;
wenzelm
parents: 65196
diff changeset
   419
          rendering.caret_focus_ranges(Text.Range(offset, offset + 1), model.content.text_range)
8fada74d82be tuned signature;
wenzelm
parents: 65196
diff changeset
   420
            .map(r => Protocol.DocumentHighlight.text(model.content.doc.range(r)))
64767
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64755
diff changeset
   421
        }) getOrElse Nil
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64755
diff changeset
   422
    channel.write(Protocol.DocumentHighlights.reply(id, result))
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64755
diff changeset
   423
  }
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64755
diff changeset
   424
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64755
diff changeset
   425
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   426
  /* main loop */
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   427
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   428
  def start()
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   429
  {
64717
d2b50eb3d9ab tuned signature;
wenzelm
parents: 64710
diff changeset
   430
    log("Server started " + Date.now())
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   431
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   432
    def handle(json: JSON.T)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   433
    {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   434
      try {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   435
        json match {
64641
wenzelm
parents: 64623
diff changeset
   436
          case Protocol.Initialize(id) => init(id)
65229
cc96b8c3b8cb explicitly ignore "initialized" message;
wenzelm
parents: 65228
diff changeset
   437
          case Protocol.Initialized(()) =>
64641
wenzelm
parents: 64623
diff changeset
   438
          case Protocol.Shutdown(id) => shutdown(id)
64645
0b513620d949 proper pattern match;
wenzelm
parents: 64644
diff changeset
   439
          case Protocol.Exit(()) => exit()
66674
30d5195299e2 store document version;
wenzelm
parents: 66389
diff changeset
   440
          case Protocol.DidOpenTextDocument(file, _, version, text) =>
30d5195299e2 store document version;
wenzelm
parents: 66389
diff changeset
   441
            change_document(file, version, List(Protocol.TextDocumentChange(None, text)))
67292
386ddccfccbf implicit thy_load context for bibtex files (VSCode);
wenzelm
parents: 66976
diff changeset
   442
            delay_load.invoke()
66674
30d5195299e2 store document version;
wenzelm
parents: 66389
diff changeset
   443
          case Protocol.DidChangeTextDocument(file, version, changes) =>
30d5195299e2 store document version;
wenzelm
parents: 66389
diff changeset
   444
            change_document(file, version, changes)
65160
6e042537555d incremental document changes;
wenzelm
parents: 65134
diff changeset
   445
          case Protocol.DidCloseTextDocument(file) => close_document(file)
64877
31e9920a0dc1 support for semantic completion;
wenzelm
parents: 64870
diff changeset
   446
          case Protocol.Completion(id, node_pos) => completion(id, node_pos)
66138
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   447
          case Protocol.Include_Word(()) => update_dictionary(true, false)
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   448
          case Protocol.Include_Word_Permanently(()) => update_dictionary(true, true)
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   449
          case Protocol.Exclude_Word(()) => update_dictionary(false, false)
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   450
          case Protocol.Exclude_Word_Permanently(()) => update_dictionary(false, true)
f7ef4c50b747 added commands for spell-checker dictionary;
wenzelm
parents: 66101
diff changeset
   451
          case Protocol.Reset_Words(()) => reset_dictionary()
64651
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   452
          case Protocol.Hover(id, node_pos) => hover(id, node_pos)
ea5620f7b0d8 clarified signature;
wenzelm
parents: 64649
diff changeset
   453
          case Protocol.GotoDefinition(id, node_pos) => goto_definition(id, node_pos)
64767
f5c4ffdb1124 support VSCode DocumentHighlights;
wenzelm
parents: 64755
diff changeset
   454
          case Protocol.DocumentHighlights(id, node_pos) => document_highlights(id, node_pos)
65189
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65184
diff changeset
   455
          case Protocol.Caret_Update(caret) => update_caret(caret)
66098
5aa9cb83e70e clarified modules;
wenzelm
parents: 66096
diff changeset
   456
          case Protocol.State_Init(()) => State_Panel.init(server)
5aa9cb83e70e clarified modules;
wenzelm
parents: 66096
diff changeset
   457
          case Protocol.State_Exit(id) => State_Panel.exit(id)
5aa9cb83e70e clarified modules;
wenzelm
parents: 66096
diff changeset
   458
          case Protocol.State_Locate(id) => State_Panel.locate(id)
5aa9cb83e70e clarified modules;
wenzelm
parents: 66096
diff changeset
   459
          case Protocol.State_Update(id) => State_Panel.update(id)
66211
100c9c997e2b HTML GUI actions via JavaScript;
wenzelm
parents: 66138
diff changeset
   460
          case Protocol.State_Auto_Update(id, enabled) => State_Panel.auto_update(id, enabled)
65983
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65977
diff changeset
   461
          case Protocol.Preview_Request(file, column) => request_preview(file, column)
66052
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents: 65983
diff changeset
   462
          case Protocol.Symbols_Request(()) => channel.write(Protocol.Symbols())
66675
6f4613dbfef6 support for workspace edits;
wenzelm
parents: 66674
diff changeset
   463
          case _ => if (!Protocol.ResponseMessage.is_empty(json)) log("### IGNORED")
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   464
        }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   465
      }
64687
04806ad1e43a clarified protocol errors;
wenzelm
parents: 64684
diff changeset
   466
      catch { case exn: Throwable => channel.log_error_message(Exn.message(exn)) }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   467
    }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   468
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   469
    @tailrec def loop()
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   470
    {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   471
      channel.read() match {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   472
        case Some(json) =>
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   473
          json match {
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   474
            case bulk: List[_] => bulk.foreach(handle(_))
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   475
            case _ => handle(json)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   476
          }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   477
          loop()
64717
d2b50eb3d9ab tuned signature;
wenzelm
parents: 64710
diff changeset
   478
        case None => log("### TERMINATE")
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   479
      }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   480
    }
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   481
    loop()
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   482
  }
66085
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   483
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   484
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   485
  /* abstract editor operations */
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   486
66100
d1ad5a7458c2 proper treatment of editor overlays;
wenzelm
parents: 66098
diff changeset
   487
  object editor extends Server.Editor
66085
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   488
  {
66101
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66100
diff changeset
   489
    /* session */
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66100
diff changeset
   490
66085
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   491
    override def session: Session = server.session
66676
39db5bb7eb0a recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents: 66675
diff changeset
   492
    override def flush(): Unit = resources.flush_input(session, channel)
66085
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   493
    override def invoke(): Unit = delay_input.invoke()
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   494
66101
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66100
diff changeset
   495
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66100
diff changeset
   496
    /* current situation */
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66100
diff changeset
   497
66085
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   498
    override def current_node(context: Unit): Option[Document.Node.Name] =
66086
3f7067ba5df3 clarified signature;
wenzelm
parents: 66085
diff changeset
   499
      resources.get_caret().map(_.model.node_name)
66085
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   500
    override def current_node_snapshot(context: Unit): Option[Document.Snapshot] =
66086
3f7067ba5df3 clarified signature;
wenzelm
parents: 66085
diff changeset
   501
      resources.get_caret().map(_.model.snapshot())
3f7067ba5df3 clarified signature;
wenzelm
parents: 66085
diff changeset
   502
66085
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   503
    override def node_snapshot(name: Document.Node.Name): Document.Snapshot =
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   504
    {
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   505
      resources.get_model(name) match {
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   506
        case Some(model) => model.snapshot()
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   507
        case None => session.snapshot(name)
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   508
      }
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   509
    }
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   510
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   511
    def current_command(snapshot: Document.Snapshot): Option[Command] =
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   512
    {
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   513
      resources.get_caret() match {
66086
3f7067ba5df3 clarified signature;
wenzelm
parents: 66085
diff changeset
   514
        case Some(caret) => snapshot.current_command(caret.node_name, caret.offset)
66085
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   515
        case None => None
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   516
      }
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   517
    }
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   518
    override def current_command(context: Unit, snapshot: Document.Snapshot): Option[Command] =
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   519
      current_command(snapshot)
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   520
66101
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66100
diff changeset
   521
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66100
diff changeset
   522
    /* overlays */
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66100
diff changeset
   523
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66100
diff changeset
   524
    override def node_overlays(name: Document.Node.Name): Document.Node.Overlays =
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66100
diff changeset
   525
      resources.node_overlays(name)
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66100
diff changeset
   526
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66100
diff changeset
   527
    override def insert_overlay(command: Command, fn: String, args: List[String]): Unit =
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66100
diff changeset
   528
      resources.insert_overlay(command, fn, args)
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66100
diff changeset
   529
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66100
diff changeset
   530
    override def remove_overlay(command: Command, fn: String, args: List[String]): Unit =
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66100
diff changeset
   531
      resources.remove_overlay(command, fn, args)
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66100
diff changeset
   532
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66100
diff changeset
   533
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66100
diff changeset
   534
    /* hyperlinks */
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66100
diff changeset
   535
66085
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   536
    override def hyperlink_command(
66215
9a8b6b86350c proper hyperlink_command, notably for locate_query;
wenzelm
parents: 66211
diff changeset
   537
      focus: Boolean, snapshot: Document.Snapshot, id: Document_ID.Generic,
9a8b6b86350c proper hyperlink_command, notably for locate_query;
wenzelm
parents: 66211
diff changeset
   538
      offset: Symbol.Offset = 0): Option[Hyperlink] =
9a8b6b86350c proper hyperlink_command, notably for locate_query;
wenzelm
parents: 66211
diff changeset
   539
    {
9a8b6b86350c proper hyperlink_command, notably for locate_query;
wenzelm
parents: 66211
diff changeset
   540
      if (snapshot.is_outdated) None
66216
d4949bae0bad clarified editor focus;
wenzelm
parents: 66215
diff changeset
   541
      else
d4949bae0bad clarified editor focus;
wenzelm
parents: 66215
diff changeset
   542
        snapshot.find_command_position(id, offset).map(node_pos =>
d4949bae0bad clarified editor focus;
wenzelm
parents: 66215
diff changeset
   543
          new Hyperlink {
d4949bae0bad clarified editor focus;
wenzelm
parents: 66215
diff changeset
   544
            def follow(unit: Unit) { channel.write(Protocol.Caret_Update(node_pos, focus)) }
d4949bae0bad clarified editor focus;
wenzelm
parents: 66215
diff changeset
   545
          })
66215
9a8b6b86350c proper hyperlink_command, notably for locate_query;
wenzelm
parents: 66211
diff changeset
   546
    }
66094
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66086
diff changeset
   547
66101
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66100
diff changeset
   548
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66100
diff changeset
   549
    /* dispatcher thread */
0f0f294e314f maintain overlays within main state of document models;
wenzelm
parents: 66100
diff changeset
   550
66094
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66086
diff changeset
   551
    override def assert_dispatcher[A](body: => A): A = session.assert_dispatcher(body)
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66086
diff changeset
   552
    override def require_dispatcher[A](body: => A): A = session.require_dispatcher(body)
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66086
diff changeset
   553
    override def send_dispatcher(body: => Unit): Unit = session.send_dispatcher(body)
24658c9d7c78 more general dispatcher operations;
wenzelm
parents: 66086
diff changeset
   554
    override def send_wait_dispatcher(body: => Unit): Unit = session.send_wait_dispatcher(body)
66085
100f02099532 added abstract editor operations, notably for Query_Operation;
wenzelm
parents: 66054
diff changeset
   555
  }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   556
}