src/Tools/VSCode/extension/src/decorations.ts
author wenzelm
Sun, 12 Mar 2017 18:45:53 +0100
changeset 65201 2d01b30e6ac6
parent 65182 973b7669e7d9
child 65233 e37209c0a42a
permissions -rw-r--r--
clarified modules;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
65094
386d9d487f62 support for decorations;
wenzelm
parents:
diff changeset
     1
'use strict';
386d9d487f62 support for decorations;
wenzelm
parents:
diff changeset
     2
386d9d487f62 support for decorations;
wenzelm
parents:
diff changeset
     3
import * as vscode from 'vscode'
65173
3700be571a01 more compact protocol message;
wenzelm
parents: 65171
diff changeset
     4
import { Position, Range, MarkedString, DecorationOptions, DecorationRenderOptions,
65135
158cba86140f maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents: 65134
diff changeset
     5
  TextDocument, TextEditor, TextEditorDecorationType, ExtensionContext, Uri } from 'vscode'
65180
b5a8f27a4980 tuned signature;
wenzelm
parents: 65176
diff changeset
     6
import { get_color } from './extension'
65201
2d01b30e6ac6 clarified modules;
wenzelm
parents: 65182
diff changeset
     7
import { Decoration } from './protocol'
65094
386d9d487f62 support for decorations;
wenzelm
parents:
diff changeset
     8
386d9d487f62 support for decorations;
wenzelm
parents:
diff changeset
     9
386d9d487f62 support for decorations;
wenzelm
parents:
diff changeset
    10
/* known decoration types */
386d9d487f62 support for decorations;
wenzelm
parents:
diff changeset
    11
65104
66b19d05dcee decorations for background and foreground colors;
wenzelm
parents: 65098
diff changeset
    12
const background_colors = [
66b19d05dcee decorations for background and foreground colors;
wenzelm
parents: 65098
diff changeset
    13
  "unprocessed1",
66b19d05dcee decorations for background and foreground colors;
wenzelm
parents: 65098
diff changeset
    14
  "running1",
66b19d05dcee decorations for background and foreground colors;
wenzelm
parents: 65098
diff changeset
    15
  "bad",
66b19d05dcee decorations for background and foreground colors;
wenzelm
parents: 65098
diff changeset
    16
  "intensify",
66b19d05dcee decorations for background and foreground colors;
wenzelm
parents: 65098
diff changeset
    17
  "quoted",
66b19d05dcee decorations for background and foreground colors;
wenzelm
parents: 65098
diff changeset
    18
  "antiquoted",
66b19d05dcee decorations for background and foreground colors;
wenzelm
parents: 65098
diff changeset
    19
  "markdown_item1",
66b19d05dcee decorations for background and foreground colors;
wenzelm
parents: 65098
diff changeset
    20
  "markdown_item2",
66b19d05dcee decorations for background and foreground colors;
wenzelm
parents: 65098
diff changeset
    21
  "markdown_item3",
66b19d05dcee decorations for background and foreground colors;
wenzelm
parents: 65098
diff changeset
    22
  "markdown_item4"
66b19d05dcee decorations for background and foreground colors;
wenzelm
parents: 65098
diff changeset
    23
]
66b19d05dcee decorations for background and foreground colors;
wenzelm
parents: 65098
diff changeset
    24
66b19d05dcee decorations for background and foreground colors;
wenzelm
parents: 65098
diff changeset
    25
const foreground_colors = [
66b19d05dcee decorations for background and foreground colors;
wenzelm
parents: 65098
diff changeset
    26
  "quoted",
66b19d05dcee decorations for background and foreground colors;
wenzelm
parents: 65098
diff changeset
    27
  "antiquoted"
66b19d05dcee decorations for background and foreground colors;
wenzelm
parents: 65098
diff changeset
    28
]
66b19d05dcee decorations for background and foreground colors;
wenzelm
parents: 65098
diff changeset
    29
65122
1edb570f5b17 decorations for dotted underline: less intrusive;
wenzelm
parents: 65106
diff changeset
    30
const dotted_colors = [
1edb570f5b17 decorations for dotted underline: less intrusive;
wenzelm
parents: 65106
diff changeset
    31
  "writeln",
65134
511bf19348d3 clarified messages (with improved scalability): legacy/error as diagnostics, writeln/information/warning/bad as tooltips (dynamic hover);
wenzelm
parents: 65122
diff changeset
    32
  "information",
511bf19348d3 clarified messages (with improved scalability): legacy/error as diagnostics, writeln/information/warning/bad as tooltips (dynamic hover);
wenzelm
parents: 65122
diff changeset
    33
  "warning"
65122
1edb570f5b17 decorations for dotted underline: less intrusive;
wenzelm
parents: 65106
diff changeset
    34
]
1edb570f5b17 decorations for dotted underline: less intrusive;
wenzelm
parents: 65106
diff changeset
    35
65146
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    36
const text_colors = [
65181
b4105202751c tuned colors according to Light+ and Dark+ themes;
wenzelm
parents: 65180
diff changeset
    37
  "main",
65146
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    38
  "keyword1",
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    39
  "keyword2",
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    40
  "keyword3",
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    41
  "quasi_keyword",
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    42
  "improper",
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    43
  "operator",
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    44
  "tfree",
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    45
  "tvar",
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    46
  "free",
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    47
  "skolem",
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    48
  "bound",
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    49
  "var",
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    50
  "inner_numeral",
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    51
  "inner_quoted",
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    52
  "inner_cartouche",
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    53
  "inner_comment",
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    54
  "dynamic",
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    55
  "class_parameter",
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    56
  "antiquote"
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    57
]
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    58
65182
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
    59
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
    60
/* init */
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
    61
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
    62
const types = new Map<string, TextEditorDecorationType>()
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
    63
65094
386d9d487f62 support for decorations;
wenzelm
parents:
diff changeset
    64
export function init(context: ExtensionContext)
386d9d487f62 support for decorations;
wenzelm
parents:
diff changeset
    65
{
386d9d487f62 support for decorations;
wenzelm
parents:
diff changeset
    66
  function decoration(options: DecorationRenderOptions): TextEditorDecorationType
386d9d487f62 support for decorations;
wenzelm
parents:
diff changeset
    67
  {
386d9d487f62 support for decorations;
wenzelm
parents:
diff changeset
    68
    const typ = vscode.window.createTextEditorDecorationType(options)
386d9d487f62 support for decorations;
wenzelm
parents:
diff changeset
    69
    context.subscriptions.push(typ)
386d9d487f62 support for decorations;
wenzelm
parents:
diff changeset
    70
    return typ
386d9d487f62 support for decorations;
wenzelm
parents:
diff changeset
    71
  }
386d9d487f62 support for decorations;
wenzelm
parents:
diff changeset
    72
65104
66b19d05dcee decorations for background and foreground colors;
wenzelm
parents: 65098
diff changeset
    73
  function background(color: string): TextEditorDecorationType
66b19d05dcee decorations for background and foreground colors;
wenzelm
parents: 65098
diff changeset
    74
  {
65122
1edb570f5b17 decorations for dotted underline: less intrusive;
wenzelm
parents: 65106
diff changeset
    75
    return decoration(
1edb570f5b17 decorations for dotted underline: less intrusive;
wenzelm
parents: 65106
diff changeset
    76
      { light: { backgroundColor: get_color(color, true) },
1edb570f5b17 decorations for dotted underline: less intrusive;
wenzelm
parents: 65106
diff changeset
    77
        dark: { backgroundColor: get_color(color, false) } })
1edb570f5b17 decorations for dotted underline: less intrusive;
wenzelm
parents: 65106
diff changeset
    78
  }
1edb570f5b17 decorations for dotted underline: less intrusive;
wenzelm
parents: 65106
diff changeset
    79
65146
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    80
  function text_color(color: string): TextEditorDecorationType
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    81
  {
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    82
    return decoration(
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    83
      { light: { color: get_color(color, true) },
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    84
        dark: { color: get_color(color, false) } })
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    85
  }
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
    86
65142
368004bed323 decorations for spell-checker;
wenzelm
parents: 65135
diff changeset
    87
  function bottom_border(width: string, style: string, color: string): TextEditorDecorationType
65122
1edb570f5b17 decorations for dotted underline: less intrusive;
wenzelm
parents: 65106
diff changeset
    88
  {
65142
368004bed323 decorations for spell-checker;
wenzelm
parents: 65135
diff changeset
    89
    const border = `${width} none; border-bottom-style: ${style}; border-color: `
65122
1edb570f5b17 decorations for dotted underline: less intrusive;
wenzelm
parents: 65106
diff changeset
    90
    return decoration(
65153
wenzelm
parents: 65146
diff changeset
    91
      { light: { border: border + get_color(color, true) },
wenzelm
parents: 65146
diff changeset
    92
        dark: { border: border + get_color(color, false) } })
65104
66b19d05dcee decorations for background and foreground colors;
wenzelm
parents: 65098
diff changeset
    93
  }
66b19d05dcee decorations for background and foreground colors;
wenzelm
parents: 65098
diff changeset
    94
65182
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
    95
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
    96
  /* reset */
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
    97
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
    98
  types.forEach(typ =>
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
    99
  {
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
   100
    for (const editor of vscode.window.visibleTextEditors) {
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
   101
      editor.setDecorations(typ, [])
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
   102
    }
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
   103
    const i = context.subscriptions.indexOf(typ)
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
   104
    if (i >= 0) context.subscriptions.splice(i, 1)
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
   105
    typ.dispose()
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
   106
  })
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
   107
  types.clear()
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
   108
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
   109
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
   110
  /* init */
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
   111
65168
9eabc312a2a2 prefer immutable bindings;
wenzelm
parents: 65153
diff changeset
   112
  for (const color of background_colors) {
65182
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
   113
    types.set("background_" + color, background(color))
65104
66b19d05dcee decorations for background and foreground colors;
wenzelm
parents: 65098
diff changeset
   114
  }
65168
9eabc312a2a2 prefer immutable bindings;
wenzelm
parents: 65153
diff changeset
   115
  for (const color of foreground_colors) {
65182
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
   116
    types.set("foreground_" + color, background(color)) // approximation
65104
66b19d05dcee decorations for background and foreground colors;
wenzelm
parents: 65098
diff changeset
   117
  }
65168
9eabc312a2a2 prefer immutable bindings;
wenzelm
parents: 65153
diff changeset
   118
  for (const color of dotted_colors) {
65182
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
   119
    types.set("dotted_" + color, bottom_border("2px", "dotted", color))
65122
1edb570f5b17 decorations for dotted underline: less intrusive;
wenzelm
parents: 65106
diff changeset
   120
  }
65168
9eabc312a2a2 prefer immutable bindings;
wenzelm
parents: 65153
diff changeset
   121
  for (const color of text_colors) {
65182
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
   122
    types.set("text_" + color, text_color(color))
65146
69ea3f1715be decorations for text color;
wenzelm
parents: 65142
diff changeset
   123
  }
65182
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
   124
  types.set("spell_checker", bottom_border("1px", "solid", "spell_checker"))
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
   125
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
   126
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
   127
  /* update editors */
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
   128
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
   129
  for (const editor of vscode.window.visibleTextEditors) {
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
   130
    update_editor(editor)
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
   131
  }
65094
386d9d487f62 support for decorations;
wenzelm
parents:
diff changeset
   132
}
386d9d487f62 support for decorations;
wenzelm
parents:
diff changeset
   133
386d9d487f62 support for decorations;
wenzelm
parents:
diff changeset
   134
386d9d487f62 support for decorations;
wenzelm
parents:
diff changeset
   135
/* decoration for document node */
386d9d487f62 support for decorations;
wenzelm
parents:
diff changeset
   136
65135
158cba86140f maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents: 65134
diff changeset
   137
type Content = Range[] | DecorationOptions[]
158cba86140f maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents: 65134
diff changeset
   138
const document_decorations = new Map<string, Map<string, Content>>()
158cba86140f maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents: 65134
diff changeset
   139
158cba86140f maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents: 65134
diff changeset
   140
export function close_document(document: TextDocument)
65094
386d9d487f62 support for decorations;
wenzelm
parents:
diff changeset
   141
{
65135
158cba86140f maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents: 65134
diff changeset
   142
  document_decorations.delete(document.uri.toString())
158cba86140f maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents: 65134
diff changeset
   143
}
158cba86140f maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents: 65134
diff changeset
   144
65173
3700be571a01 more compact protocol message;
wenzelm
parents: 65171
diff changeset
   145
export function apply_decoration(decoration: Decoration)
65135
158cba86140f maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents: 65134
diff changeset
   146
{
65182
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
   147
  const typ = types.get(decoration.type)
65094
386d9d487f62 support for decorations;
wenzelm
parents:
diff changeset
   148
  if (typ) {
65173
3700be571a01 more compact protocol message;
wenzelm
parents: 65171
diff changeset
   149
    const uri = Uri.parse(decoration.uri).toString()
3700be571a01 more compact protocol message;
wenzelm
parents: 65171
diff changeset
   150
    const content: DecorationOptions[] = decoration.content.map(opt =>
3700be571a01 more compact protocol message;
wenzelm
parents: 65171
diff changeset
   151
      {
3700be571a01 more compact protocol message;
wenzelm
parents: 65171
diff changeset
   152
        const r = opt.range
3700be571a01 more compact protocol message;
wenzelm
parents: 65171
diff changeset
   153
        return {
3700be571a01 more compact protocol message;
wenzelm
parents: 65171
diff changeset
   154
          range: new Range(new Position(r[0], r[1]), new Position(r[2], r[3])),
3700be571a01 more compact protocol message;
wenzelm
parents: 65171
diff changeset
   155
          hoverMessage: opt.hover_message
3700be571a01 more compact protocol message;
wenzelm
parents: 65171
diff changeset
   156
        }
3700be571a01 more compact protocol message;
wenzelm
parents: 65171
diff changeset
   157
      })
65135
158cba86140f maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents: 65134
diff changeset
   158
65173
3700be571a01 more compact protocol message;
wenzelm
parents: 65171
diff changeset
   159
    const document = document_decorations.get(uri) || new Map<string, Content>()
3700be571a01 more compact protocol message;
wenzelm
parents: 65171
diff changeset
   160
    document.set(decoration.type, content)
3700be571a01 more compact protocol message;
wenzelm
parents: 65171
diff changeset
   161
    document_decorations.set(uri, document)
65135
158cba86140f maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents: 65134
diff changeset
   162
65168
9eabc312a2a2 prefer immutable bindings;
wenzelm
parents: 65153
diff changeset
   163
    for (const editor of vscode.window.visibleTextEditors) {
65173
3700be571a01 more compact protocol message;
wenzelm
parents: 65171
diff changeset
   164
      if (uri === editor.document.uri.toString()) {
3700be571a01 more compact protocol message;
wenzelm
parents: 65171
diff changeset
   165
        editor.setDecorations(typ, content)
65135
158cba86140f maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents: 65134
diff changeset
   166
      }
158cba86140f maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents: 65134
diff changeset
   167
    }
65094
386d9d487f62 support for decorations;
wenzelm
parents:
diff changeset
   168
  }
65135
158cba86140f maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents: 65134
diff changeset
   169
}
158cba86140f maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents: 65134
diff changeset
   170
158cba86140f maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents: 65134
diff changeset
   171
export function update_editor(editor: TextEditor)
158cba86140f maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents: 65134
diff changeset
   172
{
158cba86140f maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents: 65134
diff changeset
   173
  if (editor) {
65168
9eabc312a2a2 prefer immutable bindings;
wenzelm
parents: 65153
diff changeset
   174
    const decorations = document_decorations.get(editor.document.uri.toString())
65135
158cba86140f maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents: 65134
diff changeset
   175
    if (decorations) {
65168
9eabc312a2a2 prefer immutable bindings;
wenzelm
parents: 65153
diff changeset
   176
      for (const [typ, content] of decorations) {
65182
973b7669e7d9 proper Map operations;
wenzelm
parents: 65181
diff changeset
   177
        editor.setDecorations(types.get(typ), content)
65135
158cba86140f maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents: 65134
diff changeset
   178
      }
158cba86140f maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents: 65134
diff changeset
   179
    }
158cba86140f maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents: 65134
diff changeset
   180
  }
158cba86140f maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents: 65134
diff changeset
   181
}