1 'use strict'; |
|
2 |
|
3 import * as timers from 'timers' |
|
4 import { ViewColumn, TextDocument, TextEditor, TextDocumentContentProvider, |
|
5 ExtensionContext, Event, EventEmitter, Uri, Position, workspace, |
|
6 window, commands } from 'vscode' |
|
7 import { LanguageClient } from 'vscode-languageclient'; |
|
8 import * as library from './library' |
|
9 import * as protocol from './protocol' |
|
10 import { Content_Provider } from './content_provider' |
|
11 |
|
12 |
|
13 /* HTML content */ |
|
14 |
|
15 const content_provider = new Content_Provider("isabelle-preview") |
|
16 |
|
17 function encode_preview(document_uri: Uri | undefined): Uri | undefined |
|
18 { |
|
19 if (document_uri && library.is_file(document_uri)) { |
|
20 return content_provider.uri_template.with({ query: document_uri.fsPath }) |
|
21 } |
|
22 else undefined |
|
23 } |
|
24 |
|
25 function decode_preview(preview_uri: Uri | undefined): Uri | undefined |
|
26 { |
|
27 if (preview_uri && preview_uri.scheme === content_provider.uri_scheme) { |
|
28 return Uri.file(preview_uri.query) |
|
29 } |
|
30 else undefined |
|
31 } |
|
32 |
|
33 |
|
34 /* setup */ |
|
35 |
|
36 let language_client: LanguageClient |
|
37 |
|
38 export function setup(context: ExtensionContext, client: LanguageClient) |
|
39 { |
|
40 context.subscriptions.push(content_provider.register()) |
|
41 |
|
42 language_client = client |
|
43 language_client.onNotification(protocol.preview_response_type, params => |
|
44 { |
|
45 const preview_uri = encode_preview(Uri.parse(params.uri)) |
|
46 if (preview_uri) { |
|
47 content_provider.set_content(preview_uri, params.content) |
|
48 content_provider.update(preview_uri) |
|
49 |
|
50 const existing_document = |
|
51 workspace.textDocuments.find(document => |
|
52 document.uri.scheme === preview_uri.scheme && |
|
53 document.uri.query === preview_uri.query) |
|
54 if (!existing_document && params.column != 0) { |
|
55 commands.executeCommand("vscode.previewHtml", preview_uri, params.column, params.label) |
|
56 } |
|
57 } |
|
58 }) |
|
59 } |
|
60 |
|
61 |
|
62 /* commands */ |
|
63 |
|
64 export function request(uri?: Uri, split: boolean = false) |
|
65 { |
|
66 const document_uri = uri || window.activeTextEditor.document.uri |
|
67 const preview_uri = encode_preview(document_uri) |
|
68 if (preview_uri && language_client) { |
|
69 language_client.sendNotification(protocol.preview_request_type, |
|
70 { uri: document_uri.toString(), |
|
71 column: library.adjacent_editor_column(window.activeTextEditor, split) }) |
|
72 } |
|
73 } |
|
74 |
|
75 export function update(preview_uri: Uri) |
|
76 { |
|
77 const document_uri = decode_preview(preview_uri) |
|
78 if (document_uri && language_client) { |
|
79 language_client.sendNotification(protocol.preview_request_type, |
|
80 { uri: document_uri.toString(), column: 0 }) |
|
81 } |
|
82 } |
|
83 |
|
84 export function source(preview_uri: Uri) |
|
85 { |
|
86 const document_uri = decode_preview(preview_uri) |
|
87 if (document_uri) { |
|
88 const editor = |
|
89 window.visibleTextEditors.find(editor => |
|
90 library.is_file(editor.document.uri) && |
|
91 editor.document.uri.fsPath === document_uri.fsPath) |
|
92 if (editor) window.showTextDocument(editor.document, editor.viewColumn) |
|
93 else workspace.openTextDocument(document_uri).then(window.showTextDocument) |
|
94 } |
|
95 else commands.executeCommand("workbench.action.navigateBack") |
|
96 } |
|