src/Tools/VSCode/extension/src/preview.ts
author wenzelm
Mon, 29 May 2017 19:50:46 +0200
changeset 65960 38fbf13f7986
parent 65959 47309113ee4d
child 65961 7f87310d6c09
permissions -rw-r--r--
proper HTML preview;
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
65959
47309113ee4d clarified view column;
wenzelm
parents: 65958
diff changeset
     3
import { TextDocumentContentProvider, ExtensionContext, Uri, Position, ViewColumn,
65958
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
     4
  workspace, window, commands } from 'vscode'
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
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    13
  dispose() { }
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    14
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    15
  provideTextDocumentContent(uri: Uri): string | Thenable<string>
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    16
  {
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    17
    const [name, pos] = decode_location(uri)
65960
38fbf13f7986 proper HTML preview;
wenzelm
parents: 65959
diff changeset
    18
    return `
38fbf13f7986 proper HTML preview;
wenzelm
parents: 65959
diff changeset
    19
      <html>
38fbf13f7986 proper HTML preview;
wenzelm
parents: 65959
diff changeset
    20
      <head>
38fbf13f7986 proper HTML preview;
wenzelm
parents: 65959
diff changeset
    21
        <meta http-equiv="Content-type" content="text/html; charset=UTF-8">
38fbf13f7986 proper HTML preview;
wenzelm
parents: 65959
diff changeset
    22
      </head>
38fbf13f7986 proper HTML preview;
wenzelm
parents: 65959
diff changeset
    23
      <body>
38fbf13f7986 proper HTML preview;
wenzelm
parents: 65959
diff changeset
    24
        <h1>Isabelle Preview</h1>
38fbf13f7986 proper HTML preview;
wenzelm
parents: 65959
diff changeset
    25
        <p>${JSON.stringify([name, pos])}</p>
38fbf13f7986 proper HTML preview;
wenzelm
parents: 65959
diff changeset
    26
      </body>
38fbf13f7986 proper HTML preview;
wenzelm
parents: 65959
diff changeset
    27
      </html>`
65958
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    28
  }
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    29
}
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    30
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    31
export function encode_location(uri: Uri, pos: Position): Uri
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    32
{
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    33
  const query = JSON.stringify([uri.toString(), pos.line, pos.character])
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    34
  return Uri.parse(uri_scheme + ":Preview?" + query)
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    35
}
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    36
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    37
export function decode_location(uri: Uri): [Uri, Position]
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    38
{
65959
47309113ee4d clarified view column;
wenzelm
parents: 65958
diff changeset
    39
  let [name, line, character] = <[string, number, number]>JSON.parse(uri.query)
47309113ee4d clarified view column;
wenzelm
parents: 65958
diff changeset
    40
  return [Uri.parse(name), new Position(line, character)]
47309113ee4d clarified view column;
wenzelm
parents: 65958
diff changeset
    41
}
47309113ee4d clarified view column;
wenzelm
parents: 65958
diff changeset
    42
47309113ee4d clarified view column;
wenzelm
parents: 65958
diff changeset
    43
function view_column(side_by_side: boolean = true): ViewColumn | undefined
47309113ee4d clarified view column;
wenzelm
parents: 65958
diff changeset
    44
{
47309113ee4d clarified view column;
wenzelm
parents: 65958
diff changeset
    45
  const active = window.activeTextEditor
47309113ee4d clarified view column;
wenzelm
parents: 65958
diff changeset
    46
  if (!active) { return ViewColumn.One }
47309113ee4d clarified view column;
wenzelm
parents: 65958
diff changeset
    47
  if (!side_by_side) { return active.viewColumn }
47309113ee4d clarified view column;
wenzelm
parents: 65958
diff changeset
    48
47309113ee4d clarified view column;
wenzelm
parents: 65958
diff changeset
    49
  if (active.viewColumn == ViewColumn.One) return ViewColumn.Two
47309113ee4d clarified view column;
wenzelm
parents: 65958
diff changeset
    50
  else if (active.viewColumn == ViewColumn.Two) return ViewColumn.Three
47309113ee4d clarified view column;
wenzelm
parents: 65958
diff changeset
    51
  else return ViewColumn.One
65958
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    52
}
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    53
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    54
export function init(context: ExtensionContext)
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    55
{
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    56
  const provider = new Provider()
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    57
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    58
  const provider_registration =
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    59
    workspace.registerTextDocumentContentProvider(uri_scheme, provider)
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    60
65959
47309113ee4d clarified view column;
wenzelm
parents: 65958
diff changeset
    61
  const command_registration =
65958
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    62
    commands.registerTextEditorCommand("isabelle.preview", editor =>
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    63
    {
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    64
      const uri = encode_location(editor.document.uri, editor.selection.active)
65960
38fbf13f7986 proper HTML preview;
wenzelm
parents: 65959
diff changeset
    65
      return workspace.openTextDocument(uri).then(doc =>
38fbf13f7986 proper HTML preview;
wenzelm
parents: 65959
diff changeset
    66
        commands.executeCommand("vscode.previewHtml", uri, view_column(), "Isabelle Preview"))
65959
47309113ee4d clarified view column;
wenzelm
parents: 65958
diff changeset
    67
    })
65958
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    68
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    69
  context.subscriptions.push(provider, provider_registration, command_registration)
6338355b2a88 basic setup for document preview;
wenzelm
parents:
diff changeset
    70
}