src/Tools/VSCode/src/dynamic_output.scala
author wenzelm
Tue, 22 Feb 2022 11:53:06 +0100
changeset 75126 da1108a6d249
parent 73340 0ffcad1f6130
child 75154 3b5aa38282bd
permissions -rw-r--r--
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
65191
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
     1
/*  Title:      Tools/VSCode/src/dynamic_output.scala
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
     3
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
     4
Dynamic output, depending on caret focus: messages, state etc.
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
     5
*/
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
     6
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
     7
package isabelle.vscode
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
     8
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
     9
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    10
import isabelle._
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    11
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    12
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    13
object Dynamic_Output
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    14
{
65912
wenzelm
parents: 65199
diff changeset
    15
  sealed case class State(do_update: Boolean = true, output: List[XML.Tree] = Nil)
65193
352d40b389ef misc tuning and simplification;
wenzelm
parents: 65191
diff changeset
    16
  {
352d40b389ef misc tuning and simplification;
wenzelm
parents: 65191
diff changeset
    17
    def handle_update(
352d40b389ef misc tuning and simplification;
wenzelm
parents: 65191
diff changeset
    18
      resources: VSCode_Resources, channel: Channel, restriction: Option[Set[Command]]): State =
352d40b389ef misc tuning and simplification;
wenzelm
parents: 65191
diff changeset
    19
    {
352d40b389ef misc tuning and simplification;
wenzelm
parents: 65191
diff changeset
    20
      val st1 =
65976
1448d71fbd22 tuned signature;
wenzelm
parents: 65912
diff changeset
    21
        resources.get_caret() match {
65193
352d40b389ef misc tuning and simplification;
wenzelm
parents: 65191
diff changeset
    22
          case None => copy(output = Nil)
66086
3f7067ba5df3 clarified signature;
wenzelm
parents: 65976
diff changeset
    23
          case Some(caret) =>
3f7067ba5df3 clarified signature;
wenzelm
parents: 65976
diff changeset
    24
            val snapshot = caret.model.snapshot()
65193
352d40b389ef misc tuning and simplification;
wenzelm
parents: 65191
diff changeset
    25
            if (do_update && !snapshot.is_outdated) {
66086
3f7067ba5df3 clarified signature;
wenzelm
parents: 65976
diff changeset
    26
              snapshot.current_command(caret.node_name, caret.offset) match {
65193
352d40b389ef misc tuning and simplification;
wenzelm
parents: 65191
diff changeset
    27
                case None => copy(output = Nil)
352d40b389ef misc tuning and simplification;
wenzelm
parents: 65191
diff changeset
    28
                case Some(command) =>
352d40b389ef misc tuning and simplification;
wenzelm
parents: 65191
diff changeset
    29
                  copy(output =
71383
8313dca6dee9 misc tuning, following hint by IntelliJ;
wenzelm
parents: 70302
diff changeset
    30
                    if (restriction.isEmpty || restriction.get.contains(command))
65195
wenzelm
parents: 65193
diff changeset
    31
                      Rendering.output_messages(snapshot.command_results(command))
65193
352d40b389ef misc tuning and simplification;
wenzelm
parents: 65191
diff changeset
    32
                    else output)
352d40b389ef misc tuning and simplification;
wenzelm
parents: 65191
diff changeset
    33
              }
352d40b389ef misc tuning and simplification;
wenzelm
parents: 65191
diff changeset
    34
            }
352d40b389ef misc tuning and simplification;
wenzelm
parents: 65191
diff changeset
    35
            else this
352d40b389ef misc tuning and simplification;
wenzelm
parents: 65191
diff changeset
    36
        }
352d40b389ef misc tuning and simplification;
wenzelm
parents: 65191
diff changeset
    37
      if (st1.output != output) {
75126
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73340
diff changeset
    38
        val elements = Presentation.Elements(
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73340
diff changeset
    39
          html = Presentation.elements2.html,
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73340
diff changeset
    40
          language = Presentation.elements2.language,
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73340
diff changeset
    41
          entity = Markup.Elements.full)
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73340
diff changeset
    42
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73340
diff changeset
    43
        def entity_link(props: Properties.T, body: XML.Body): Option[XML.Tree] =
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73340
diff changeset
    44
          for {
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73340
diff changeset
    45
            thy_file <- Position.Def_File.unapply(props)
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73340
diff changeset
    46
            def_line <- Position.Def_Line.unapply(props)
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73340
diff changeset
    47
            source <- resources.source_file(thy_file)
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73340
diff changeset
    48
            uri = Path.explode(source).absolute_file.toURI
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73340
diff changeset
    49
          } yield HTML.link(uri.toString + "#" + def_line, body)
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73340
diff changeset
    50
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73340
diff changeset
    51
        val htmlBody = Presentation.make_html(
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73340
diff changeset
    52
          Presentation.Entity_Context.empty,  // FIXME
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73340
diff changeset
    53
          elements,
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73340
diff changeset
    54
          Pretty.separate(st1.output))
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73340
diff changeset
    55
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 73340
diff changeset
    56
        channel.write(LSP.Dynamic_Output(HTML.source(htmlBody).toString))
65193
352d40b389ef misc tuning and simplification;
wenzelm
parents: 65191
diff changeset
    57
      }
352d40b389ef misc tuning and simplification;
wenzelm
parents: 65191
diff changeset
    58
      st1
352d40b389ef misc tuning and simplification;
wenzelm
parents: 65191
diff changeset
    59
    }
352d40b389ef misc tuning and simplification;
wenzelm
parents: 65191
diff changeset
    60
  }
65191
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    61
72761
4519eeefe3b5 avoid conflicting base names;
wenzelm
parents: 71774
diff changeset
    62
  def apply(server: Language_Server): Dynamic_Output = new Dynamic_Output(server)
65191
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    63
}
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    64
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    65
72761
4519eeefe3b5 avoid conflicting base names;
wenzelm
parents: 71774
diff changeset
    66
class Dynamic_Output private(server: Language_Server)
65191
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    67
{
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    68
  private val state = Synchronized(Dynamic_Output.State())
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    69
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 72761
diff changeset
    70
  private def handle_update(restriction: Option[Set[Command]]): Unit =
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 72761
diff changeset
    71
    state.change(_.handle_update(server.resources, server.channel, restriction))
65191
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    72
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    73
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    74
  /* main */
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    75
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    76
  private val main =
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    77
    Session.Consumer[Any](getClass.getName) {
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    78
      case changed: Session.Commands_Changed =>
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    79
        handle_update(if (changed.assignment) None else Some(changed.commands))
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    80
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    81
      case Session.Caret_Focus =>
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    82
        handle_update(None)
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    83
    }
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    84
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 72761
diff changeset
    85
  def init(): Unit =
65191
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    86
  {
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    87
    server.session.commands_changed += main
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    88
    server.session.caret_focus += main
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    89
    handle_update(None)
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    90
  }
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    91
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 72761
diff changeset
    92
  def exit(): Unit =
65191
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    93
  {
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    94
    server.session.commands_changed -= main
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    95
    server.session.caret_focus -= main
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    96
  }
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
diff changeset
    97
}