src/Tools/VSCode/extension/src/state_panel.ts
author wenzelm
Mon, 07 Mar 2022 12:37:03 +0100
changeset 75234 57de0062dc1c
parent 75201 8f6b8a46f54c
child 81035 56594fac1dab
permissions -rw-r--r--
proper file headers;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
75234
57de0062dc1c proper file headers;
wenzelm
parents: 75201
diff changeset
     1
/*  Author:     Makarius
57de0062dc1c proper file headers;
wenzelm
parents: 75201
diff changeset
     2
57de0062dc1c proper file headers;
wenzelm
parents: 75201
diff changeset
     3
State panel via HTML webview inside VSCode.
57de0062dc1c proper file headers;
wenzelm
parents: 75201
diff changeset
     4
*/
57de0062dc1c proper file headers;
wenzelm
parents: 75201
diff changeset
     5
66203
5763d9a2f47d added missing files (cf. 5aa9cb83e70e);
wenzelm
parents:
diff changeset
     6
'use strict';
5763d9a2f47d added missing files (cf. 5aa9cb83e70e);
wenzelm
parents:
diff changeset
     7
75181
98fbc9accb51 clarified module;
wenzelm
parents: 75134
diff changeset
     8
import * as vscode_lib from './vscode_lib'
75201
8f6b8a46f54c clarified modules: more uniform .scala vs. ts (amending 4519eeefe3b5);
wenzelm
parents: 75185
diff changeset
     9
import * as lsp from './lsp'
75134
c04ccea8bdd2 update VSCode plugin dependencies;
Fabian Huch <huch@in.tum.de>
parents: 75126
diff changeset
    10
import {LanguageClient} from 'vscode-languageclient/node'
75126
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    11
import {ExtensionContext, Uri, ViewColumn, WebviewPanel, window} from 'vscode'
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    12
import {get_webview_html, open_webview_link} from './output_view'
66203
5763d9a2f47d added missing files (cf. 5aa9cb83e70e);
wenzelm
parents:
diff changeset
    13
5763d9a2f47d added missing files (cf. 5aa9cb83e70e);
wenzelm
parents:
diff changeset
    14
71475
7a867a38712a clarified Panel;
wenzelm
parents: 71473
diff changeset
    15
let language_client: LanguageClient
66203
5763d9a2f47d added missing files (cf. 5aa9cb83e70e);
wenzelm
parents:
diff changeset
    16
71476
ecefde4f9103 proper message passing -- discontinued obsolete auxiliary commands;
wenzelm
parents: 71475
diff changeset
    17
function panel_column(): ViewColumn
ecefde4f9103 proper message passing -- discontinued obsolete auxiliary commands;
wenzelm
parents: 71475
diff changeset
    18
{
75181
98fbc9accb51 clarified module;
wenzelm
parents: 75134
diff changeset
    19
  return vscode_lib.adjacent_editor_column(window.activeTextEditor, true)
71476
ecefde4f9103 proper message passing -- discontinued obsolete auxiliary commands;
wenzelm
parents: 71475
diff changeset
    20
}
ecefde4f9103 proper message passing -- discontinued obsolete auxiliary commands;
wenzelm
parents: 71475
diff changeset
    21
71475
7a867a38712a clarified Panel;
wenzelm
parents: 71473
diff changeset
    22
class Panel
66203
5763d9a2f47d added missing files (cf. 5aa9cb83e70e);
wenzelm
parents:
diff changeset
    23
{
71475
7a867a38712a clarified Panel;
wenzelm
parents: 71473
diff changeset
    24
  private state_id: number
7a867a38712a clarified Panel;
wenzelm
parents: 71473
diff changeset
    25
  private webview_panel: WebviewPanel
75126
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    26
  private _extension_path: string
71475
7a867a38712a clarified Panel;
wenzelm
parents: 71473
diff changeset
    27
71476
ecefde4f9103 proper message passing -- discontinued obsolete auxiliary commands;
wenzelm
parents: 71475
diff changeset
    28
  public get_id(): number { return this.state_id }
75096
37bd912c8765 prefer strict equality, without implicit type conversion;
wenzelm
parents: 71478
diff changeset
    29
  public check_id(id: number): boolean { return this.state_id === id }
71475
7a867a38712a clarified Panel;
wenzelm
parents: 71473
diff changeset
    30
75201
8f6b8a46f54c clarified modules: more uniform .scala vs. ts (amending 4519eeefe3b5);
wenzelm
parents: 75185
diff changeset
    31
  public set_content(state: lsp.State_Output)
71475
7a867a38712a clarified Panel;
wenzelm
parents: 71473
diff changeset
    32
  {
75126
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    33
    this.state_id = state.id
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    34
    this.webview_panel.webview.html = this._get_html(state.content, state.auto_update)
71475
7a867a38712a clarified Panel;
wenzelm
parents: 71473
diff changeset
    35
  }
66203
5763d9a2f47d added missing files (cf. 5aa9cb83e70e);
wenzelm
parents:
diff changeset
    36
71476
ecefde4f9103 proper message passing -- discontinued obsolete auxiliary commands;
wenzelm
parents: 71475
diff changeset
    37
  public reveal()
ecefde4f9103 proper message passing -- discontinued obsolete auxiliary commands;
wenzelm
parents: 71475
diff changeset
    38
  {
ecefde4f9103 proper message passing -- discontinued obsolete auxiliary commands;
wenzelm
parents: 71475
diff changeset
    39
    this.webview_panel.reveal(panel_column())
ecefde4f9103 proper message passing -- discontinued obsolete auxiliary commands;
wenzelm
parents: 71475
diff changeset
    40
  }
ecefde4f9103 proper message passing -- discontinued obsolete auxiliary commands;
wenzelm
parents: 71475
diff changeset
    41
75126
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    42
  constructor(extension_path: string)
71475
7a867a38712a clarified Panel;
wenzelm
parents: 71473
diff changeset
    43
  {
75126
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    44
    this._extension_path = extension_path
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    45
    this.webview_panel = window.createWebviewPanel("isabelle-state", "State",
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    46
      panel_column(), { enableScripts: true })
71475
7a867a38712a clarified Panel;
wenzelm
parents: 71473
diff changeset
    47
    this.webview_panel.onDidDispose(exit_panel)
71476
ecefde4f9103 proper message passing -- discontinued obsolete auxiliary commands;
wenzelm
parents: 71475
diff changeset
    48
    this.webview_panel.webview.onDidReceiveMessage(message =>
75126
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    49
    {
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    50
      switch (message.command) {
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    51
        case "auto_update":
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    52
          language_client.sendNotification(
75201
8f6b8a46f54c clarified modules: more uniform .scala vs. ts (amending 4519eeefe3b5);
wenzelm
parents: 75185
diff changeset
    53
            lsp.state_auto_update_type, { id: this.state_id, enabled: message.enabled })
75126
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    54
          break
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    55
        case "update":
75201
8f6b8a46f54c clarified modules: more uniform .scala vs. ts (amending 4519eeefe3b5);
wenzelm
parents: 75185
diff changeset
    56
          language_client.sendNotification(lsp.state_update_type, { id: this.state_id })
75126
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    57
          break
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    58
        case "locate":
75201
8f6b8a46f54c clarified modules: more uniform .scala vs. ts (amending 4519eeefe3b5);
wenzelm
parents: 75185
diff changeset
    59
          language_client.sendNotification(lsp.state_locate_type, { id: this.state_id })
75126
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    60
          break
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    61
        case "open":
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    62
          open_webview_link(message.link)
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    63
          break
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    64
        default:
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    65
          break
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    66
      }
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    67
    })
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    68
  }
71475
7a867a38712a clarified Panel;
wenzelm
parents: 71473
diff changeset
    69
75126
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    70
  private _get_html(content: string, auto_update: boolean): string
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    71
  {
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    72
    const webview = this.webview_panel.webview
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    73
    const checked = auto_update ? "checked" : ""
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    74
    const content_with_buttons = `<div id="controls">
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    75
      <input type="checkbox" id="auto_update" ${checked}/>
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    76
      <label for="auto_update">Auto update</label>
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    77
      <button id="update_button">Update</button>
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    78
      <button id="locate_button">Locate</button>
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    79
    </div>
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    80
    ${content}`
71475
7a867a38712a clarified Panel;
wenzelm
parents: 71473
diff changeset
    81
75126
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
    82
    return get_webview_html(content_with_buttons, webview, this._extension_path)
71475
7a867a38712a clarified Panel;
wenzelm
parents: 71473
diff changeset
    83
  }
66203
5763d9a2f47d added missing files (cf. 5aa9cb83e70e);
wenzelm
parents:
diff changeset
    84
}
5763d9a2f47d added missing files (cf. 5aa9cb83e70e);
wenzelm
parents:
diff changeset
    85
71475
7a867a38712a clarified Panel;
wenzelm
parents: 71473
diff changeset
    86
let panel: Panel
66203
5763d9a2f47d added missing files (cf. 5aa9cb83e70e);
wenzelm
parents:
diff changeset
    87
71475
7a867a38712a clarified Panel;
wenzelm
parents: 71473
diff changeset
    88
function exit_panel()
7a867a38712a clarified Panel;
wenzelm
parents: 71473
diff changeset
    89
{
7a867a38712a clarified Panel;
wenzelm
parents: 71473
diff changeset
    90
  if (panel) {
75201
8f6b8a46f54c clarified modules: more uniform .scala vs. ts (amending 4519eeefe3b5);
wenzelm
parents: 75185
diff changeset
    91
    language_client.sendNotification(lsp.state_exit_type, { id: panel.get_id() })
71475
7a867a38712a clarified Panel;
wenzelm
parents: 71473
diff changeset
    92
    panel = null
7a867a38712a clarified Panel;
wenzelm
parents: 71473
diff changeset
    93
  }
7a867a38712a clarified Panel;
wenzelm
parents: 71473
diff changeset
    94
}
66203
5763d9a2f47d added missing files (cf. 5aa9cb83e70e);
wenzelm
parents:
diff changeset
    95
71476
ecefde4f9103 proper message passing -- discontinued obsolete auxiliary commands;
wenzelm
parents: 71475
diff changeset
    96
export function init(uri: Uri)
ecefde4f9103 proper message passing -- discontinued obsolete auxiliary commands;
wenzelm
parents: 71475
diff changeset
    97
{
71478
5c0293826dc8 misc tuning and clarification;
wenzelm
parents: 71477
diff changeset
    98
  if (language_client) {
5c0293826dc8 misc tuning and clarification;
wenzelm
parents: 71477
diff changeset
    99
    if (panel) panel.reveal()
75201
8f6b8a46f54c clarified modules: more uniform .scala vs. ts (amending 4519eeefe3b5);
wenzelm
parents: 75185
diff changeset
   100
    else language_client.sendNotification(lsp.state_init_type)
71478
5c0293826dc8 misc tuning and clarification;
wenzelm
parents: 71477
diff changeset
   101
  }
71476
ecefde4f9103 proper message passing -- discontinued obsolete auxiliary commands;
wenzelm
parents: 71475
diff changeset
   102
}
ecefde4f9103 proper message passing -- discontinued obsolete auxiliary commands;
wenzelm
parents: 71475
diff changeset
   103
66203
5763d9a2f47d added missing files (cf. 5aa9cb83e70e);
wenzelm
parents:
diff changeset
   104
export function setup(context: ExtensionContext, client: LanguageClient)
5763d9a2f47d added missing files (cf. 5aa9cb83e70e);
wenzelm
parents:
diff changeset
   105
{
5763d9a2f47d added missing files (cf. 5aa9cb83e70e);
wenzelm
parents:
diff changeset
   106
  language_client = client
75201
8f6b8a46f54c clarified modules: more uniform .scala vs. ts (amending 4519eeefe3b5);
wenzelm
parents: 75185
diff changeset
   107
  language_client.onNotification(lsp.state_output_type, params =>
66203
5763d9a2f47d added missing files (cf. 5aa9cb83e70e);
wenzelm
parents:
diff changeset
   108
    {
75126
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
   109
      if (!panel) {
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
   110
        panel = new Panel(context.extensionPath)
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
   111
      }
da1108a6d249 various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents: 75096
diff changeset
   112
      panel.set_content(params)
66203
5763d9a2f47d added missing files (cf. 5aa9cb83e70e);
wenzelm
parents:
diff changeset
   113
    })
5763d9a2f47d added missing files (cf. 5aa9cb83e70e);
wenzelm
parents:
diff changeset
   114
}