author | wenzelm |
Fri, 30 Jun 2017 16:16:45 +0200 | |
changeset 66236 | 8ae7c5ba1a85 |
parent 66097 | ee4c2d5b650e |
child 71473 | be84312a2d53 |
permissions | -rw-r--r-- |
65958 | 1 |
'use strict'; |
2 |
||
65974 | 3 |
import * as timers from 'timers' |
65983 | 4 |
import { ViewColumn, TextDocument, TextEditor, TextDocumentContentProvider, |
5 |
ExtensionContext, Event, EventEmitter, Uri, Position, workspace, |
|
6 |
window, commands } from 'vscode' |
|
7 |
import { LanguageClient } from 'vscode-languageclient'; |
|
65966 | 8 |
import * as library from './library' |
66092 | 9 |
import * as protocol from './protocol' |
10 |
import { Content_Provider } from './content_provider' |
|
65983 | 11 |
|
12 |
||
66092 | 13 |
/* HTML content */ |
65983 | 14 |
|
66092 | 15 |
const content_provider = new Content_Provider("isabelle-preview") |
65983 | 16 |
|
17 |
function encode_preview(document_uri: Uri | undefined): Uri | undefined |
|
18 |
{ |
|
19 |
if (document_uri && library.is_file(document_uri)) { |
|
66092 | 20 |
return content_provider.uri_template.with({ query: document_uri.fsPath }) |
65983 | 21 |
} |
22 |
else undefined |
|
23 |
} |
|
24 |
||
25 |
function decode_preview(preview_uri: Uri | undefined): Uri | undefined |
|
26 |
{ |
|
66092 | 27 |
if (preview_uri && preview_uri.scheme === content_provider.uri_scheme) { |
65983 | 28 |
return Uri.file(preview_uri.query) |
29 |
} |
|
30 |
else undefined |
|
31 |
} |
|
65958 | 32 |
|
33 |
||
66097 | 34 |
/* setup */ |
65959 | 35 |
|
65983 | 36 |
let language_client: LanguageClient |
65974 | 37 |
|
66097 | 38 |
export function setup(context: ExtensionContext, client: LanguageClient) |
65958 | 39 |
{ |
66092 | 40 |
context.subscriptions.push(content_provider.register()) |
65983 | 41 |
|
66093 | 42 |
language_client = client |
65987 | 43 |
language_client.onNotification(protocol.preview_response_type, params => |
44 |
{ |
|
45 |
const preview_uri = encode_preview(Uri.parse(params.uri)) |
|
46 |
if (preview_uri) { |
|
66092 | 47 |
content_provider.set_content(preview_uri, params.content) |
65994 | 48 |
content_provider.update(preview_uri) |
66027 | 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) { |
|
65994 | 55 |
commands.executeCommand("vscode.previewHtml", preview_uri, params.column, params.label) |
56 |
} |
|
65987 | 57 |
} |
58 |
}) |
|
65983 | 59 |
} |
60 |
||
61 |
||
62 |
/* commands */ |
|
63 |
||
65987 | 64 |
export function request(uri?: Uri, split: boolean = false) |
65983 | 65 |
{ |
66 |
const document_uri = uri || window.activeTextEditor.document.uri |
|
67 |
const preview_uri = encode_preview(document_uri) |
|
65984
8e6a833da7db
register commands earlier, before prover startup;
wenzelm
parents:
65983
diff
changeset
|
68 |
if (preview_uri && language_client) { |
65983 | 69 |
language_client.sendNotification(protocol.preview_request_type, |
66081 | 70 |
{ uri: document_uri.toString(), |
71 |
column: library.adjacent_editor_column(window.activeTextEditor, split) }) |
|
65983 | 72 |
} |
73 |
} |
|
74 |
||
65987 | 75 |
export function update(preview_uri: Uri) |
65983 | 76 |
{ |
65987 | 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 }) |
|
65983 | 81 |
} |
82 |
} |
|
83 |
||
65987 | 84 |
export function source(preview_uri: Uri) |
65983 | 85 |
{ |
86 |
const document_uri = decode_preview(preview_uri) |
|
87 |
if (document_uri) { |
|
88 |
const editor = |
|
89 |
window.visibleTextEditors.find(editor => |
|
66064 | 90 |
library.is_file(editor.document.uri) && |
65983 | 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 |
} |