author | wenzelm |
Mon, 07 Mar 2022 12:37:03 +0100 | |
changeset 75234 | 57de0062dc1c |
parent 75201 | 8f6b8a46f54c |
child 81035 | 56594fac1dab |
permissions | -rw-r--r-- |
75234 | 1 |
/* Author: Makarius |
2 |
||
3 |
State panel via HTML webview inside VSCode. |
|
4 |
*/ |
|
5 |
||
66203 | 6 |
'use strict'; |
7 |
||
75181 | 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 | 13 |
|
14 |
||
71475 | 15 |
let language_client: LanguageClient |
66203 | 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 | 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 | 22 |
class Panel |
66203 | 23 |
{ |
71475 | 24 |
private state_id: number |
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 | 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 | 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 | 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 | 35 |
} |
66203 | 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 | 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 | 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 | 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 | 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 | 83 |
} |
66203 | 84 |
} |
85 |
||
71475 | 86 |
let panel: Panel |
66203 | 87 |
|
71475 | 88 |
function exit_panel() |
89 |
{ |
|
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 | 92 |
panel = null |
93 |
} |
|
94 |
} |
|
66203 | 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 | 98 |
if (language_client) { |
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 | 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 | 104 |
export function setup(context: ExtensionContext, client: LanguageClient) |
105 |
{ |
|
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 | 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 | 113 |
}) |
114 |
} |