-
Notifications
You must be signed in to change notification settings - Fork 9
/
HtmlCoqView.ts
277 lines (229 loc) · 9.63 KB
/
HtmlCoqView.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
'use strict';
import * as vscode from 'vscode'
import * as view from './CoqView'
export {CoqView} from './CoqView'
import {extensionContext} from './extension'
import * as proto from './protocol'
import * as WebSocket from 'ws';
import * as http from 'http';
import * as path from 'path';
import * as docs from './CoqProject';
import * as nasync from './nodejs-async';
import * as webServer from './WebServer';
import * as psm from './prettify-symbols-mode';
const opener = require('opener');
interface ControllerEvent {
eventName: string;
params: ResizeEvent // | | | | | ;
}
interface ResizeEvent {
columns: number;
}
interface GoalUpdate {
command: 'goal-update',
goal: proto.CommandResult
}
interface SettingsUpdate extends SettingsState {
command: 'settings-update'
}
interface SettingsState {
fontFamily?: string,
fontSize?: string,
fontWeight?: string,
cssFile?: string,
prettifySymbolsMode?: boolean,
}
type ProofViewProtocol = GoalUpdate | SettingsUpdate;
const VIEW_PATH = 'html_views';
function proofViewCSSFile() {
const userDir = vscode.workspace.getConfiguration("coq.hacks")
.get("userSettingsLocation", null)
|| extensionContext.asAbsolutePath(path.join(VIEW_PATH,'goals'));
return vscode.Uri.file(path.join(userDir,'proof-view.css'));
}
function proofViewFile(file: string = "") {
return vscode.Uri.file(extensionContext.asAbsolutePath(path.join(VIEW_PATH,'goals',file)));
}
function proofViewHtmlPath() {
return proofViewFile('Coq.html');
}
function coqViewToFileUri(uri: vscode.Uri) {
return `file://${uri.path}?${uri.query}#${uri.fragment}`;
}
class IFrameDocumentProvider implements vscode.TextDocumentContentProvider {
private onDidChangeEmitter = new vscode.EventEmitter<vscode.Uri>();
public provideTextDocumentContent(uri: vscode.Uri): string {
return `<!DOCTYPE HTML><body style="margin:0px;padding:0px;width:100%;height:100vh;border:none;position:absolute;top:0px;left:0px;right:0px;bottom:0px">
<iframe src="${coqViewToFileUri(uri)}" seamless style="position:absolute;top:0px;left:0px;right:0px;bottom:0px;border:none;margin:0px;padding:0px;width:100%;height:100%" />
</body>`;
}
public get onDidChange(): vscode.Event<vscode.Uri> {
return this.onDidChangeEmitter.event;
}
}
var coqViewProvider : IFrameDocumentProvider|null = null;
/**
* Displays a Markdown-HTML file which contains javascript to connect to this view
* and update the goals and show other status info
*/
export class HtmlCoqView implements view.CoqView {
private docUri: vscode.Uri;
private server : WebSocket.Server;
private httpServer : http.Server;
// private connection : Promise<WebSocket>;
private serverReady : Promise<void>;
private currentState : proto.CommandResult = {type: 'not-running', reason: 'not-started'};
private coqViewUri : vscode.Uri;
private currentSettings : SettingsState = {};
private visible = false;
private resizeEvent = new vscode.EventEmitter<number>();
public get resize() : vscode.Event<number> { return this.resizeEvent.event; }
constructor(uri: vscode.Uri, context: vscode.ExtensionContext) {
if(coqViewProvider===null) {
coqViewProvider = new IFrameDocumentProvider();
var registration = vscode.workspace.registerTextDocumentContentProvider('coq-view', coqViewProvider);
context.subscriptions.push(registration);
context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(() => this.updateSettings()))
}
this.docUri = uri;
const httpServer = this.httpServer = http.createServer();
this.serverReady = new Promise<void>((resolve, reject) =>
httpServer.listen(0,'localhost',undefined,(e:any) => {
if(e)
reject(e)
else
resolve();
}));
this.server = new WebSocket.Server({server: httpServer});
this.server.on('connection', (ws: WebSocket) => {
ws.onmessage = (event) => this.handleClientMessage(event);
this.updateSettings([ws]);
this.sendMessage({command: 'goal-update', goal: this.currentState}, [ws]);
})
psm.onEnabledChange((enabled) => {
this.currentSettings.prettifySymbolsMode = enabled;
this.sendMessage(Object.assign<SettingsState,{command: 'settings-update'}>({prettifySymbolsMode: enabled},{command: 'settings-update'}));
});
}
private handleClientResize(event: ResizeEvent) {
this.resizeEvent.fire(event.columns);
}
private handleClientMessage(event: {data: any; type: string; target: WebSocket}) {
const message = <ControllerEvent>JSON.parse(event.data);
switch(message.eventName) {
case 'resize':
this.handleClientResize(message.params);
return;
case 'focus':
docs.getProject().setActiveDoc(this.docUri);
return;
}
}
private async createBuffer() : Promise<void> {
try {
await this.serverReady;
const serverAddress = this.httpServer.address();
await HtmlCoqView.prepareStyleSheet();
this.coqViewUri = vscode.Uri.parse(`coq-view://${proofViewHtmlPath().path.replace(/%3A/, ':')}?host=${serverAddress.address}&port=${serverAddress.port}`);
console.log("Goals: " + decodeURIComponent(this.coqViewUri.with({scheme: 'file'}).toString()));
} catch(err) {
vscode.window.showErrorMessage(err.toString());
}
}
public getUri() : vscode.Uri {
return this.coqViewUri;
}
public isVisible() {
return this.visible;
}
public async show(preserveFocus: boolean, pane: vscode.ViewColumn) {
if(!this.coqViewUri)
await this.createBuffer();
this.visible = true;
// const focusedDoc = vscode.window.activeTextEditor ? vscode.window.activeTextEditor.document : null;
await vscode.commands.executeCommand('vscode.previewHtml', this.coqViewUri, pane, "ProofView: " + path.basename(this.docUri.fsPath));
// if(preserveFocus && focusedDoc)
// await vscode.window.showTextDocument(focusedDoc);
}
public async showExternal(scheme: "file"|"http", command? : (url:string)=>{module: string, args: string[]}) : Promise<void> {
let url : string;
if(scheme === "file") {
url = decodeURIComponent(coqViewToFileUri(this.coqViewUri).toString());
} else {
// this.coqViewUri = vscode.Uri.parse(`coq-view://${proofViewHtmlPath().path.replace(/%3A/, ':')}?host=${serverAddress.address}&port=${serverAddress.port}`);
const uri = await webServer.serveDirectory("proof-view/", proofViewFile('..').fsPath, "**/*.{html,css,js}");
if(!uri)
return Promise.reject("Cannot find proof view script");
url = decodeURIComponent(uri.with({path: uri.path + 'goals/Coq.html', query: this.coqViewUri.query, fragment: this.coqViewUri.fragment }).toString());
}
if(!command)
return Promise.resolve(opener(url));
else {
const c = command(url);
return Promise.resolve(opener(c.args.join(' '), {command: c.module}));
}
}
public dispose() {
// this.editor.hide();
}
private async sendMessage(message: ProofViewProtocol, clients = this.server.clients) {
await this.serverReady;
if(!clients)
clients = this.server.clients;
for(const connection of clients) {
try {
connection.send(JSON.stringify(message));
} catch(error) {}
}
}
private async updateClients(state: proto.CommandResult, clients = this.server.clients) {
this.currentState = state;
await this.sendMessage({command: 'goal-update', goal: state}, clients);
}
public update(state: proto.CommandResult) {
this.updateClients(state, this.server.clients);
}
private async updateSettings(clients = this.server.clients) {
this.currentSettings.fontFamily = vscode.workspace.getConfiguration("editor").get("fontFamily") as string;
this.currentSettings.fontSize = `${vscode.workspace.getConfiguration("editor").get("fontSize") as number}px`;
this.currentSettings.fontWeight = vscode.workspace.getConfiguration("editor").get("fontWeight") as string;
this.currentSettings.cssFile = decodeURIComponent(proofViewCSSFile().toString());
this.currentSettings.prettifySymbolsMode = psm.isEnabled();
await this.sendMessage(Object.assign<SettingsState,{command: 'settings-update'}>(this.currentSettings,{command: 'settings-update'}), clients);
}
private static async shouldResetStyleSheet() : Promise<boolean> {
try {
const styleFile = proofViewCSSFile();
if(!await nasync.fs.exists(styleFile.fsPath))
return true;
const stat = await nasync.fs.stat(styleFile.fsPath);
if(stat.size < 5 && (await nasync.fs.readFile(styleFile.fsPath, 'utf8')).trim() === "")
return true;
} catch(err) {
console.error(err.toString());
}
return false;
}
/** makes sure that the style sheet is available */
private static async prepareStyleSheet() {
try {
const styleFile = proofViewCSSFile();
if(await HtmlCoqView.shouldResetStyleSheet() === true) {
const defaultFile = proofViewFile('default-proof-view.css');
await nasync.fs.copyFile(defaultFile.fsPath,styleFile.fsPath);
}
} catch(err) {
console.error(err.toString());
}
}
public static async customizeProofViewStyle() {
try {
await HtmlCoqView.prepareStyleSheet();
const styleFile = proofViewCSSFile();
const doc = await vscode.workspace.openTextDocument(styleFile.fsPath);
await vscode.window.showTextDocument(doc);
} catch(err) {
console.error(err.toString());
}
}
}