src/Tools/VSCode/extension/src/preview.ts
author wenzelm
Mon, 29 May 2017 22:49:43 +0200
changeset 65961 7f87310d6c09
parent 65960 38fbf13f7986
child 65966 169d2928cce1
permissions -rw-r--r--
update preview after document change; clarified encode_name/decode_name; clarified other_column;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
65958
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
     1
'use strict';
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
     2
65961
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
     3
import { TextDocumentContentProvider, ExtensionContext, Event, EventEmitter, Uri, Position,
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
     4
  ViewColumn, workspace, window, commands } from 'vscode'
65958
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
     5
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
     6
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
     7
const uri_scheme = 'isabelle-preview';
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
     8
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
     9
class Provider implements TextDocumentContentProvider
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    10
{
65959
47309113ee4d clarified view column;
wenzelm
parents: 65958
diff changeset
    11
  constructor() { }
65958
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    12
  dispose() { }
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    13
65961
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    14
  private emitter = new EventEmitter<Uri>()
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    15
  private waiting: boolean = false;
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    16
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    17
  get onDidChange(): Event<Uri> { return this.emitter.event }
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    18
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    19
  public update(document_uri: Uri)
65958
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    20
  {
65961
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    21
    if (!this.waiting) {
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    22
      this.waiting = true
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    23
      setTimeout(() =>
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    24
      {
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    25
        this.waiting = false
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    26
        this.emitter.fire(encode_name(document_uri))
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    27
      }, 300)
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    28
    }
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    29
  }
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    30
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    31
  provideTextDocumentContent(preview_uri: Uri): string | Thenable<string>
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    32
  {
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    33
    const document_uri = decode_name(preview_uri)
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    34
    const editor =
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    35
      window.visibleTextEditors.find(editor =>
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    36
        document_uri.toString() === editor.document.uri.toString())
65960
38fbf13f7986 proper HTML preview;
wenzelm
parents: 65959
diff changeset
    37
    return `
38fbf13f7986 proper HTML preview;
wenzelm
parents: 65959
diff changeset
    38
      <html>
38fbf13f7986 proper HTML preview;
wenzelm
parents: 65959
diff changeset
    39
      <head>
38fbf13f7986 proper HTML preview;
wenzelm
parents: 65959
diff changeset
    40
        <meta http-equiv="Content-type" content="text/html; charset=UTF-8">
38fbf13f7986 proper HTML preview;
wenzelm
parents: 65959
diff changeset
    41
      </head>
38fbf13f7986 proper HTML preview;
wenzelm
parents: 65959
diff changeset
    42
      <body>
38fbf13f7986 proper HTML preview;
wenzelm
parents: 65959
diff changeset
    43
        <h1>Isabelle Preview</h1>
65961
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    44
        <ul>
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    45
        <li><b>file</b> = <code>${document_uri.fsPath}</code></li>` +
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    46
        (editor ? `<li><b>line count</b> = ${editor.document.lineCount}</li>` : "") +
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    47
        `</ul>
65960
38fbf13f7986 proper HTML preview;
wenzelm
parents: 65959
diff changeset
    48
      </body>
38fbf13f7986 proper HTML preview;
wenzelm
parents: 65959
diff changeset
    49
      </html>`
65958
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    50
  }
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    51
}
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    52
65961
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    53
export function encode_name(document_uri: Uri): Uri
65958
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    54
{
65961
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    55
  return Uri.parse(uri_scheme + ":Preview?" + JSON.stringify([document_uri.toString()]))
65958
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    56
}
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    57
65961
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    58
export function decode_name(preview_uri: Uri): Uri
65958
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    59
{
65961
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    60
  const [name] = <[string]>JSON.parse(preview_uri.query)
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    61
  return Uri.parse(name)
65959
47309113ee4d clarified view column;
wenzelm
parents: 65958
diff changeset
    62
}
47309113ee4d clarified view column;
wenzelm
parents: 65958
diff changeset
    63
65961
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    64
function other_column(): ViewColumn
65959
47309113ee4d clarified view column;
wenzelm
parents: 65958
diff changeset
    65
{
47309113ee4d clarified view column;
wenzelm
parents: 65958
diff changeset
    66
  const active = window.activeTextEditor
65961
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    67
  if (!active || active.viewColumn === ViewColumn.Three) return ViewColumn.One
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    68
  else if (active.viewColumn === ViewColumn.One) return ViewColumn.Two
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    69
  else return ViewColumn.Three
65958
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    70
}
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    71
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    72
export function init(context: ExtensionContext)
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    73
{
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    74
  const provider = new Provider()
65961
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    75
  context.subscriptions.push(workspace.registerTextDocumentContentProvider(uri_scheme, provider))
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    76
  context.subscriptions.push(provider)
65958
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    77
65961
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    78
  context.subscriptions.push(
65958
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    79
    commands.registerTextEditorCommand("isabelle.preview", editor =>
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    80
    {
65961
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    81
      const preview_uri = encode_name(editor.document.uri)
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    82
      return workspace.openTextDocument(preview_uri).then(doc =>
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    83
        commands.executeCommand("vscode.previewHtml", preview_uri, other_column(), "Isabelle Preview"))
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    84
    }))
65958
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    85
65961
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    86
  context.subscriptions.push(
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    87
    workspace.onDidChangeTextDocument(event =>
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    88
    {
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    89
      if (event.document.languageId === 'isabelle') {
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    90
        provider.update(event.document.uri)
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    91
      }
7f87310d6c09 update preview after document change;
wenzelm
parents: 65960
diff changeset
    92
    }))
65958
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    93
}