src/Tools/VSCode/extension/src/extension.ts
author wenzelm
Wed, 31 May 2017 20:33:26 +0200
changeset 65986 d2b2f08533c5
parent 65985 1be7135917a6
child 65987 44e44bfc738a
permissions -rw-r--r--
added update operation;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     1
'use strict';
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     2
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     3
import * as path from 'path';
65172
365e97f009ed default cygwin_root from Isabelle distribution;
wenzelm
parents: 65171
diff changeset
     4
import * as fs from 'fs';
65968
44e703278dfd clarified modules;
wenzelm
parents: 65958
diff changeset
     5
import * as library from './library'
65094
386d9d487f62 support for decorations;
wenzelm
parents: 64874
diff changeset
     6
import * as decorations from './decorations';
65958
6338355b2a88 basic setup for document preview;
wenzelm
parents: 65233
diff changeset
     7
import * as preview from './preview';
65201
2d01b30e6ac6 clarified modules;
wenzelm
parents: 65200
diff changeset
     8
import * as protocol from './protocol';
65984
8e6a833da7db register commands earlier, before prover startup;
wenzelm
parents: 65983
diff changeset
     9
import { ExtensionContext, workspace, window, commands } from 'vscode';
65165
d98ede9e5917 updated to vscode-languageclient 3.0;
wenzelm
parents: 65153
diff changeset
    10
import { LanguageClient, LanguageClientOptions, SettingMonitor, ServerOptions, TransportKind, NotificationType }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    11
  from 'vscode-languageclient';
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    12
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    13
65201
2d01b30e6ac6 clarified modules;
wenzelm
parents: 65200
diff changeset
    14
let last_caret_update: protocol.Caret_Update = {}
65180
b5a8f27a4980 tuned signature;
wenzelm
parents: 65172
diff changeset
    15
65969
1f93eb5c3d77 tuned imports;
wenzelm
parents: 65968
diff changeset
    16
export function activate(context: ExtensionContext)
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    17
{
65968
44e703278dfd clarified modules;
wenzelm
parents: 65958
diff changeset
    18
  const isabelle_home = library.get_configuration<string>("home")
44e703278dfd clarified modules;
wenzelm
parents: 65958
diff changeset
    19
  const isabelle_args = library.get_configuration<Array<string>>("args")
44e703278dfd clarified modules;
wenzelm
parents: 65958
diff changeset
    20
  const cygwin_root = library.get_configuration<string>("cygwin_root")
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    21
65182
973b7669e7d9 proper Map operations;
wenzelm
parents: 65180
diff changeset
    22
973b7669e7d9 proper Map operations;
wenzelm
parents: 65180
diff changeset
    23
  /* server */
973b7669e7d9 proper Map operations;
wenzelm
parents: 65180
diff changeset
    24
65172
365e97f009ed default cygwin_root from Isabelle distribution;
wenzelm
parents: 65171
diff changeset
    25
  if (isabelle_home === "")
65969
1f93eb5c3d77 tuned imports;
wenzelm
parents: 65968
diff changeset
    26
    window.showErrorMessage("Missing user settings: isabelle.home")
64753
79ed396709e4 more robust startup;
wenzelm
parents: 64750
diff changeset
    27
  else {
65168
9eabc312a2a2 prefer immutable bindings;
wenzelm
parents: 65167
diff changeset
    28
    const isabelle_tool = isabelle_home + "/bin/isabelle"
9eabc312a2a2 prefer immutable bindings;
wenzelm
parents: 65167
diff changeset
    29
    const standard_args = ["-o", "vscode_unicode_symbols", "-o", "vscode_pide_extensions"]
65167
wenzelm
parents: 65165
diff changeset
    30
65168
9eabc312a2a2 prefer immutable bindings;
wenzelm
parents: 65167
diff changeset
    31
    const server_options: ServerOptions =
65970
05e317e291a8 clarified modules;
wenzelm
parents: 65969
diff changeset
    32
      library.platform_is_windows() ?
65172
365e97f009ed default cygwin_root from Isabelle distribution;
wenzelm
parents: 65171
diff changeset
    33
        { command:
365e97f009ed default cygwin_root from Isabelle distribution;
wenzelm
parents: 65171
diff changeset
    34
            (cygwin_root === "" ? path.join(isabelle_home, "contrib", "cygwin") : cygwin_root) +
365e97f009ed default cygwin_root from Isabelle distribution;
wenzelm
parents: 65171
diff changeset
    35
            "/bin/bash",
64874
e13ff666af96 enable vscode_unicode_symbols by default, despite asymmetry of input and output;
wenzelm
parents: 64833
diff changeset
    36
          args: ["-l", isabelle_tool, "vscode_server"].concat(standard_args, isabelle_args) } :
64755
ceb81f4928ea support for Windows;
wenzelm
parents: 64753
diff changeset
    37
        { command: isabelle_tool,
64874
e13ff666af96 enable vscode_unicode_symbols by default, despite asymmetry of input and output;
wenzelm
parents: 64833
diff changeset
    38
          args: ["vscode_server"].concat(standard_args, isabelle_args) };
65983
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
    39
    const language_client_options: LanguageClientOptions = {
64833
0f410e3b1d20 support for bibtex entries;
wenzelm
parents: 64756
diff changeset
    40
      documentSelector: ["isabelle", "isabelle-ml", "bibtex"]
64753
79ed396709e4 more robust startup;
wenzelm
parents: 64750
diff changeset
    41
    };
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    42
65983
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
    43
    const language_client =
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
    44
      new LanguageClient("Isabelle", server_options, language_client_options, false)
65094
386d9d487f62 support for decorations;
wenzelm
parents: 64874
diff changeset
    45
65182
973b7669e7d9 proper Map operations;
wenzelm
parents: 65180
diff changeset
    46
65201
2d01b30e6ac6 clarified modules;
wenzelm
parents: 65200
diff changeset
    47
    /* decorations */
2d01b30e6ac6 clarified modules;
wenzelm
parents: 65200
diff changeset
    48
2d01b30e6ac6 clarified modules;
wenzelm
parents: 65200
diff changeset
    49
    decorations.init(context)
65975
f20739a63a44 more careful treatment of context.subscriptions;
wenzelm
parents: 65974
diff changeset
    50
    context.subscriptions.push(
f20739a63a44 more careful treatment of context.subscriptions;
wenzelm
parents: 65974
diff changeset
    51
      workspace.onDidChangeConfiguration(() => decorations.init(context)),
f20739a63a44 more careful treatment of context.subscriptions;
wenzelm
parents: 65974
diff changeset
    52
      workspace.onDidChangeTextDocument(event => decorations.touch_document(event.document)),
f20739a63a44 more careful treatment of context.subscriptions;
wenzelm
parents: 65974
diff changeset
    53
      window.onDidChangeActiveTextEditor(decorations.update_editor),
f20739a63a44 more careful treatment of context.subscriptions;
wenzelm
parents: 65974
diff changeset
    54
      workspace.onDidCloseTextDocument(decorations.close_document))
65201
2d01b30e6ac6 clarified modules;
wenzelm
parents: 65200
diff changeset
    55
65983
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
    56
    language_client.onReady().then(() =>
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
    57
      language_client.onNotification(protocol.decoration_type, decorations.apply_decoration))
65201
2d01b30e6ac6 clarified modules;
wenzelm
parents: 65200
diff changeset
    58
2d01b30e6ac6 clarified modules;
wenzelm
parents: 65200
diff changeset
    59
65978
wenzelm
parents: 65977
diff changeset
    60
    /* caret handling */
65191
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents: 65189
diff changeset
    61
65189
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    62
    function update_caret()
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    63
    {
65969
1f93eb5c3d77 tuned imports;
wenzelm
parents: 65968
diff changeset
    64
      const editor = window.activeTextEditor
65201
2d01b30e6ac6 clarified modules;
wenzelm
parents: 65200
diff changeset
    65
      let caret_update: protocol.Caret_Update = {}
65189
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    66
      if (editor) {
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    67
        const uri = editor.document.uri
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    68
        const cursor = editor.selection.active
65972
9f6a154c6ca0 clarified modules;
wenzelm
parents: 65970
diff changeset
    69
        if (library.is_file(uri) && cursor)
65189
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    70
          caret_update = { uri: uri.toString(), line: cursor.line, character: cursor.character }
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    71
      }
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    72
      if (last_caret_update !== caret_update) {
65202
187277b77d50 suppress vacuous messages;
wenzelm
parents: 65201
diff changeset
    73
        if (caret_update.uri)
65983
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
    74
          language_client.sendNotification(protocol.caret_update_type, caret_update)
65189
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    75
        last_caret_update = caret_update
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    76
      }
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    77
    }
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    78
65983
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
    79
    language_client.onReady().then(() =>
65189
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    80
    {
65975
f20739a63a44 more careful treatment of context.subscriptions;
wenzelm
parents: 65974
diff changeset
    81
      context.subscriptions.push(
f20739a63a44 more careful treatment of context.subscriptions;
wenzelm
parents: 65974
diff changeset
    82
        window.onDidChangeActiveTextEditor(_ => update_caret()),
f20739a63a44 more careful treatment of context.subscriptions;
wenzelm
parents: 65974
diff changeset
    83
        window.onDidChangeTextEditorSelection(_ => update_caret()))
65189
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    84
      update_caret()
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    85
    })
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    86
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    87
65978
wenzelm
parents: 65977
diff changeset
    88
    /* dynamic output */
wenzelm
parents: 65977
diff changeset
    89
wenzelm
parents: 65977
diff changeset
    90
    const dynamic_output = window.createOutputChannel("Isabelle Output")
wenzelm
parents: 65977
diff changeset
    91
    context.subscriptions.push(dynamic_output)
wenzelm
parents: 65977
diff changeset
    92
    dynamic_output.show(true)
wenzelm
parents: 65977
diff changeset
    93
    dynamic_output.hide()
wenzelm
parents: 65977
diff changeset
    94
65983
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
    95
    language_client.onReady().then(() =>
65978
wenzelm
parents: 65977
diff changeset
    96
    {
65983
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
    97
      language_client.onNotification(protocol.dynamic_output_type,
65979
c208fcf369b7 tuned -- like Dynamic_Preview;
wenzelm
parents: 65978
diff changeset
    98
        params => { dynamic_output.clear(); dynamic_output.appendLine(params.content) })
65978
wenzelm
parents: 65977
diff changeset
    99
    })
wenzelm
parents: 65977
diff changeset
   100
wenzelm
parents: 65977
diff changeset
   101
65983
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
   102
    /* preview */
65958
6338355b2a88 basic setup for document preview;
wenzelm
parents: 65233
diff changeset
   103
65984
8e6a833da7db register commands earlier, before prover startup;
wenzelm
parents: 65983
diff changeset
   104
    context.subscriptions.push(
8e6a833da7db register commands earlier, before prover startup;
wenzelm
parents: 65983
diff changeset
   105
      commands.registerCommand("isabelle.preview", uri => preview.request_preview(uri, false)),
65985
1be7135917a6 clarified name;
wenzelm
parents: 65984
diff changeset
   106
      commands.registerCommand("isabelle.preview-split", uri => preview.request_preview(uri, true)),
65986
d2b2f08533c5 added update operation;
wenzelm
parents: 65985
diff changeset
   107
      commands.registerCommand("isabelle.preview-source", preview.show_source),
d2b2f08533c5 added update operation;
wenzelm
parents: 65985
diff changeset
   108
      commands.registerCommand("isabelle.preview-update", preview.update_preview))
65984
8e6a833da7db register commands earlier, before prover startup;
wenzelm
parents: 65983
diff changeset
   109
65983
d8c5603c1732 explicit preview request/response;
wenzelm
parents: 65979
diff changeset
   110
    language_client.onReady().then(() => preview.init(context, language_client))
65958
6338355b2a88 basic setup for document preview;
wenzelm
parents: 65233
diff changeset
   111
6338355b2a88 basic setup for document preview;
wenzelm
parents: 65233
diff changeset
   112
65182
973b7669e7d9 proper Map operations;
wenzelm
parents: 65180
diff changeset
   113
    /* start server */
973b7669e7d9 proper Map operations;
wenzelm
parents: 65180
diff changeset
   114
65986
d2b2f08533c5 added update operation;
wenzelm
parents: 65985
diff changeset
   115
    context.subscriptions.push(language_client.start())
65094
386d9d487f62 support for decorations;
wenzelm
parents: 64874
diff changeset
   116
  }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   117
}
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   118
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   119
export function deactivate() { }