author | wenzelm |
Fri, 10 Mar 2017 21:47:48 +0100 | |
changeset 65176 | 908d8be90533 |
parent 65173 | 3700be571a01 |
child 65180 | b5a8f27a4980 |
permissions | -rw-r--r-- |
65094 | 1 |
'use strict'; |
2 |
||
3 |
import * as vscode from 'vscode' |
|
65173 | 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' |
65094 | 6 |
|
7 |
||
8 |
/* known decoration types */ |
|
9 |
||
65104
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65098
diff
changeset
|
10 |
export const types = new Map<string, TextEditorDecorationType>() |
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65098
diff
changeset
|
11 |
|
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 | 36 |
const text_colors = [ |
37 |
"keyword1", |
|
38 |
"keyword2", |
|
39 |
"keyword3", |
|
40 |
"quasi_keyword", |
|
41 |
"improper", |
|
42 |
"operator", |
|
43 |
"tfree", |
|
44 |
"tvar", |
|
45 |
"free", |
|
46 |
"skolem", |
|
47 |
"bound", |
|
48 |
"var", |
|
49 |
"inner_numeral", |
|
50 |
"inner_quoted", |
|
51 |
"inner_cartouche", |
|
52 |
"inner_comment", |
|
53 |
"dynamic", |
|
54 |
"class_parameter", |
|
55 |
"antiquote" |
|
56 |
] |
|
57 |
||
65122
1edb570f5b17
decorations for dotted underline: less intrusive;
wenzelm
parents:
65106
diff
changeset
|
58 |
function get_color(color: string, light: boolean): string |
65094 | 59 |
{ |
65153 | 60 |
const config = color + (light ? "_light" : "_dark") + "_color" |
65122
1edb570f5b17
decorations for dotted underline: less intrusive;
wenzelm
parents:
65106
diff
changeset
|
61 |
return vscode.workspace.getConfiguration("isabelle").get<string>(config) |
65094 | 62 |
} |
63 |
||
64 |
export function init(context: ExtensionContext) |
|
65 |
{ |
|
66 |
function decoration(options: DecorationRenderOptions): TextEditorDecorationType |
|
67 |
{ |
|
68 |
const typ = vscode.window.createTextEditorDecorationType(options) |
|
69 |
context.subscriptions.push(typ) |
|
70 |
return typ |
|
71 |
} |
|
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 | 80 |
function text_color(color: string): TextEditorDecorationType |
81 |
{ |
|
82 |
return decoration( |
|
83 |
{ light: { color: get_color(color, true) }, |
|
84 |
dark: { color: get_color(color, false) } }) |
|
85 |
} |
|
86 |
||
65142 | 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 | 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 | 91 |
{ light: { border: border + get_color(color, true) }, |
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 |
|
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65098
diff
changeset
|
95 |
types.clear |
65168 | 96 |
for (const color of background_colors) { |
65153 | 97 |
types["background_" + color] = background(color) |
65104
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65098
diff
changeset
|
98 |
} |
65168 | 99 |
for (const color of foreground_colors) { |
65153 | 100 |
types["foreground_" + color] = background(color) // approximation |
65104
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65098
diff
changeset
|
101 |
} |
65168 | 102 |
for (const color of dotted_colors) { |
65153 | 103 |
types["dotted_" + color] = bottom_border("2px", "dotted", color) |
65122
1edb570f5b17
decorations for dotted underline: less intrusive;
wenzelm
parents:
65106
diff
changeset
|
104 |
} |
65168 | 105 |
for (const color of text_colors) { |
65153 | 106 |
types["text_" + color] = text_color(color) |
65146 | 107 |
} |
65142 | 108 |
types["spell_checker"] = bottom_border("1px", "solid", "spell_checker") |
65094 | 109 |
} |
110 |
||
111 |
||
112 |
/* decoration for document node */ |
|
113 |
||
65173 | 114 |
interface DecorationOpts { |
115 |
range: number[], |
|
116 |
hover_message?: MarkedString | MarkedString[] |
|
117 |
} |
|
118 |
||
65094 | 119 |
export interface Decoration |
120 |
{ |
|
121 |
uri: string, |
|
122 |
"type": string, |
|
65173 | 123 |
content: DecorationOpts[] |
65094 | 124 |
} |
125 |
||
65135
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
126 |
type Content = Range[] | DecorationOptions[] |
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
127 |
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
|
128 |
|
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
129 |
export function close_document(document: TextDocument) |
65094 | 130 |
{ |
65135
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
131 |
document_decorations.delete(document.uri.toString()) |
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
132 |
} |
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
133 |
|
65173 | 134 |
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
|
135 |
{ |
65173 | 136 |
const typ = types[decoration.type] |
65094 | 137 |
if (typ) { |
65173 | 138 |
const uri = Uri.parse(decoration.uri).toString() |
139 |
const content: DecorationOptions[] = decoration.content.map(opt => |
|
140 |
{ |
|
141 |
const r = opt.range |
|
142 |
return { |
|
143 |
range: new Range(new Position(r[0], r[1]), new Position(r[2], r[3])), |
|
144 |
hoverMessage: opt.hover_message |
|
145 |
} |
|
146 |
}) |
|
65135
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
147 |
|
65173 | 148 |
const document = document_decorations.get(uri) || new Map<string, Content>() |
149 |
document.set(decoration.type, content) |
|
150 |
document_decorations.set(uri, document) |
|
65135
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
151 |
|
65168 | 152 |
for (const editor of vscode.window.visibleTextEditors) { |
65173 | 153 |
if (uri === editor.document.uri.toString()) { |
154 |
editor.setDecorations(typ, content) |
|
65135
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
155 |
} |
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
156 |
} |
65094 | 157 |
} |
65135
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
158 |
} |
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
159 |
|
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
160 |
export function update_editor(editor: TextEditor) |
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
161 |
{ |
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
162 |
if (editor) { |
65168 | 163 |
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
|
164 |
if (decorations) { |
65168 | 165 |
for (const [typ, content] of decorations) { |
65135
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
166 |
editor.setDecorations(types[typ], content) |
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
167 |
} |
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
168 |
} |
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 |
} |