src/Tools/VSCode/extension/src/extension.ts
author wenzelm
Sun, 12 Mar 2017 18:05:06 +0100
changeset 65200 1227a68fac7a
parent 65191 4c9c83311cad
child 65201 2d01b30e6ac6
permissions -rw-r--r--
more explicit message type: allows body to become empty;
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 vscode from 'vscode';
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
     4
import * as path from 'path';
65172
365e97f009ed default cygwin_root from Isabelle distribution;
wenzelm
parents: 65171
diff changeset
     5
import * as fs from 'fs';
64755
ceb81f4928ea support for Windows;
wenzelm
parents: 64753
diff changeset
     6
import * as os from 'os';
65094
386d9d487f62 support for decorations;
wenzelm
parents: 64874
diff changeset
     7
import * as decorations from './decorations';
386d9d487f62 support for decorations;
wenzelm
parents: 64874
diff changeset
     8
import { Decoration } from './decorations'
65165
d98ede9e5917 updated to vscode-languageclient 3.0;
wenzelm
parents: 65153
diff changeset
     9
import { LanguageClient, LanguageClientOptions, SettingMonitor, ServerOptions, TransportKind, NotificationType }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    10
  from 'vscode-languageclient';
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    11
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    12
65180
b5a8f27a4980 tuned signature;
wenzelm
parents: 65172
diff changeset
    13
/* Isabelle configuration */
b5a8f27a4980 tuned signature;
wenzelm
parents: 65172
diff changeset
    14
65186
wenzelm
parents: 65182
diff changeset
    15
export function get_configuration<T>(name: string): T
65180
b5a8f27a4980 tuned signature;
wenzelm
parents: 65172
diff changeset
    16
{
65186
wenzelm
parents: 65182
diff changeset
    17
  return vscode.workspace.getConfiguration("isabelle").get<T>(name)
65180
b5a8f27a4980 tuned signature;
wenzelm
parents: 65172
diff changeset
    18
}
b5a8f27a4980 tuned signature;
wenzelm
parents: 65172
diff changeset
    19
b5a8f27a4980 tuned signature;
wenzelm
parents: 65172
diff changeset
    20
export function get_color(color: string, light: boolean): string
b5a8f27a4980 tuned signature;
wenzelm
parents: 65172
diff changeset
    21
{
b5a8f27a4980 tuned signature;
wenzelm
parents: 65172
diff changeset
    22
  const config = color + (light ? "_light" : "_dark") + "_color"
65186
wenzelm
parents: 65182
diff changeset
    23
  return get_configuration<string>(config)
65180
b5a8f27a4980 tuned signature;
wenzelm
parents: 65172
diff changeset
    24
}
b5a8f27a4980 tuned signature;
wenzelm
parents: 65172
diff changeset
    25
b5a8f27a4980 tuned signature;
wenzelm
parents: 65172
diff changeset
    26
65189
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    27
/* caret handling and dynamic output */
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    28
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    29
interface Caret_Update
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    30
{
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    31
  uri?: string
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    32
  line?: number
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    33
  character?: number
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    34
}
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    35
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    36
const caret_update_type = new NotificationType<Caret_Update, void>("PIDE/caret_update")
65200
1227a68fac7a more explicit message type: allows body to become empty;
wenzelm
parents: 65191
diff changeset
    37
let last_caret_update: Caret_Update = {}
1227a68fac7a more explicit message type: allows body to become empty;
wenzelm
parents: 65191
diff changeset
    38
65189
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    39
65200
1227a68fac7a more explicit message type: allows body to become empty;
wenzelm
parents: 65191
diff changeset
    40
interface Dynamic_Output
1227a68fac7a more explicit message type: allows body to become empty;
wenzelm
parents: 65191
diff changeset
    41
{
1227a68fac7a more explicit message type: allows body to become empty;
wenzelm
parents: 65191
diff changeset
    42
  body: string
1227a68fac7a more explicit message type: allows body to become empty;
wenzelm
parents: 65191
diff changeset
    43
}
1227a68fac7a more explicit message type: allows body to become empty;
wenzelm
parents: 65191
diff changeset
    44
1227a68fac7a more explicit message type: allows body to become empty;
wenzelm
parents: 65191
diff changeset
    45
const dynamic_output_type = new NotificationType<Dynamic_Output, void>("PIDE/dynamic_output")
1227a68fac7a more explicit message type: allows body to become empty;
wenzelm
parents: 65191
diff changeset
    46
65189
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    47
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    48
65180
b5a8f27a4980 tuned signature;
wenzelm
parents: 65172
diff changeset
    49
/* activate extension */
b5a8f27a4980 tuned signature;
wenzelm
parents: 65172
diff changeset
    50
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    51
export function activate(context: vscode.ExtensionContext)
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    52
{
65168
9eabc312a2a2 prefer immutable bindings;
wenzelm
parents: 65167
diff changeset
    53
  const is_windows = os.type().startsWith("Windows")
64755
ceb81f4928ea support for Windows;
wenzelm
parents: 64753
diff changeset
    54
65186
wenzelm
parents: 65182
diff changeset
    55
  const isabelle_home = get_configuration<string>("home")
wenzelm
parents: 65182
diff changeset
    56
  const isabelle_args = get_configuration<Array<string>>("args")
wenzelm
parents: 65182
diff changeset
    57
  const cygwin_root = get_configuration<string>("cygwin_root")
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    58
65182
973b7669e7d9 proper Map operations;
wenzelm
parents: 65180
diff changeset
    59
973b7669e7d9 proper Map operations;
wenzelm
parents: 65180
diff changeset
    60
  /* server */
973b7669e7d9 proper Map operations;
wenzelm
parents: 65180
diff changeset
    61
65172
365e97f009ed default cygwin_root from Isabelle distribution;
wenzelm
parents: 65171
diff changeset
    62
  if (isabelle_home === "")
64755
ceb81f4928ea support for Windows;
wenzelm
parents: 64753
diff changeset
    63
    vscode.window.showErrorMessage("Missing user settings: isabelle.home")
64753
79ed396709e4 more robust startup;
wenzelm
parents: 64750
diff changeset
    64
  else {
65168
9eabc312a2a2 prefer immutable bindings;
wenzelm
parents: 65167
diff changeset
    65
    const isabelle_tool = isabelle_home + "/bin/isabelle"
9eabc312a2a2 prefer immutable bindings;
wenzelm
parents: 65167
diff changeset
    66
    const standard_args = ["-o", "vscode_unicode_symbols", "-o", "vscode_pide_extensions"]
65167
wenzelm
parents: 65165
diff changeset
    67
65168
9eabc312a2a2 prefer immutable bindings;
wenzelm
parents: 65167
diff changeset
    68
    const server_options: ServerOptions =
64755
ceb81f4928ea support for Windows;
wenzelm
parents: 64753
diff changeset
    69
      is_windows ?
65172
365e97f009ed default cygwin_root from Isabelle distribution;
wenzelm
parents: 65171
diff changeset
    70
        { command:
365e97f009ed default cygwin_root from Isabelle distribution;
wenzelm
parents: 65171
diff changeset
    71
            (cygwin_root === "" ? path.join(isabelle_home, "contrib", "cygwin") : cygwin_root) +
365e97f009ed default cygwin_root from Isabelle distribution;
wenzelm
parents: 65171
diff changeset
    72
            "/bin/bash",
64874
e13ff666af96 enable vscode_unicode_symbols by default, despite asymmetry of input and output;
wenzelm
parents: 64833
diff changeset
    73
          args: ["-l", isabelle_tool, "vscode_server"].concat(standard_args, isabelle_args) } :
64755
ceb81f4928ea support for Windows;
wenzelm
parents: 64753
diff changeset
    74
        { command: isabelle_tool,
64874
e13ff666af96 enable vscode_unicode_symbols by default, despite asymmetry of input and output;
wenzelm
parents: 64833
diff changeset
    75
          args: ["vscode_server"].concat(standard_args, isabelle_args) };
65168
9eabc312a2a2 prefer immutable bindings;
wenzelm
parents: 65167
diff changeset
    76
    const client_options: LanguageClientOptions = {
64833
0f410e3b1d20 support for bibtex entries;
wenzelm
parents: 64756
diff changeset
    77
      documentSelector: ["isabelle", "isabelle-ml", "bibtex"]
64753
79ed396709e4 more robust startup;
wenzelm
parents: 64750
diff changeset
    78
    };
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
    79
65168
9eabc312a2a2 prefer immutable bindings;
wenzelm
parents: 65167
diff changeset
    80
    const client = new LanguageClient("Isabelle", server_options, client_options, false)
65094
386d9d487f62 support for decorations;
wenzelm
parents: 64874
diff changeset
    81
65182
973b7669e7d9 proper Map operations;
wenzelm
parents: 65180
diff changeset
    82
65189
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    83
    /* caret handling and dynamic output */
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    84
65191
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents: 65189
diff changeset
    85
    const dynamic_output = vscode.window.createOutputChannel("Isabelle Output")
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents: 65189
diff changeset
    86
    context.subscriptions.push(dynamic_output)
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents: 65189
diff changeset
    87
    dynamic_output.show(true)
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents: 65189
diff changeset
    88
    dynamic_output.hide()
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents: 65189
diff changeset
    89
65189
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    90
    function update_caret()
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    91
    {
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    92
      const editor = vscode.window.activeTextEditor
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    93
      let caret_update: Caret_Update = {}
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    94
      if (editor) {
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    95
        const uri = editor.document.uri
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    96
        const cursor = editor.selection.active
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    97
        if (uri.scheme === "file" && cursor)
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    98
          caret_update = { uri: uri.toString(), line: cursor.line, character: cursor.character }
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
    99
      }
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
   100
      if (last_caret_update !== caret_update) {
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
   101
        client.sendNotification(caret_update_type, caret_update)
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
   102
        last_caret_update = caret_update
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
   103
      }
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
   104
    }
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
   105
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
   106
    client.onReady().then(() =>
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
   107
    {
65191
4c9c83311cad dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents: 65189
diff changeset
   108
      client.onNotification(dynamic_output_type,
65200
1227a68fac7a more explicit message type: allows body to become empty;
wenzelm
parents: 65191
diff changeset
   109
        params => { dynamic_output.clear(); dynamic_output.appendLine(params.body) })
65189
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
   110
      vscode.window.onDidChangeActiveTextEditor(_ => update_caret())
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
   111
      vscode.window.onDidChangeTextEditorSelection(_ => update_caret())
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
   112
      update_caret()
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
   113
    })
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
   114
41d2452845fc support for caret handling and dynamic output;
wenzelm
parents: 65186
diff changeset
   115
65182
973b7669e7d9 proper Map operations;
wenzelm
parents: 65180
diff changeset
   116
    /* decorations */
973b7669e7d9 proper Map operations;
wenzelm
parents: 65180
diff changeset
   117
65094
386d9d487f62 support for decorations;
wenzelm
parents: 64874
diff changeset
   118
    decorations.init(context)
65182
973b7669e7d9 proper Map operations;
wenzelm
parents: 65180
diff changeset
   119
    vscode.workspace.onDidChangeConfiguration(() => decorations.init(context))
65135
158cba86140f maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents: 65096
diff changeset
   120
    vscode.window.onDidChangeActiveTextEditor(decorations.update_editor)
158cba86140f maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents: 65096
diff changeset
   121
    vscode.workspace.onDidCloseTextDocument(decorations.close_document)
65182
973b7669e7d9 proper Map operations;
wenzelm
parents: 65180
diff changeset
   122
65165
d98ede9e5917 updated to vscode-languageclient 3.0;
wenzelm
parents: 65153
diff changeset
   123
    client.onReady().then(() =>
d98ede9e5917 updated to vscode-languageclient 3.0;
wenzelm
parents: 65153
diff changeset
   124
      client.onNotification(
d98ede9e5917 updated to vscode-languageclient 3.0;
wenzelm
parents: 65153
diff changeset
   125
        new NotificationType<Decoration, void>("PIDE/decoration"), decorations.apply_decoration))
65094
386d9d487f62 support for decorations;
wenzelm
parents: 64874
diff changeset
   126
65182
973b7669e7d9 proper Map operations;
wenzelm
parents: 65180
diff changeset
   127
973b7669e7d9 proper Map operations;
wenzelm
parents: 65180
diff changeset
   128
    /* start server */
973b7669e7d9 proper Map operations;
wenzelm
parents: 65180
diff changeset
   129
65094
386d9d487f62 support for decorations;
wenzelm
parents: 64874
diff changeset
   130
    context.subscriptions.push(client.start());
386d9d487f62 support for decorations;
wenzelm
parents: 64874
diff changeset
   131
  }
64605
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   132
}
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   133
9c1173a7e4cb basic support for VSCode Language Server protocol;
wenzelm
parents:
diff changeset
   134
export function deactivate() { }