author | wenzelm |
Sat, 11 Mar 2017 14:03:46 +0100 | |
changeset 65182 | 973b7669e7d9 |
parent 65181 | b4105202751c |
child 65201 | 2d01b30e6ac6 |
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' |
65180 | 6 |
import { get_color } from './extension' |
65094 | 7 |
|
8 |
||
9 |
/* known decoration types */ |
|
10 |
||
65104
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65098
diff
changeset
|
11 |
const background_colors = [ |
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65098
diff
changeset
|
12 |
"unprocessed1", |
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65098
diff
changeset
|
13 |
"running1", |
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65098
diff
changeset
|
14 |
"bad", |
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65098
diff
changeset
|
15 |
"intensify", |
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65098
diff
changeset
|
16 |
"quoted", |
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65098
diff
changeset
|
17 |
"antiquoted", |
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65098
diff
changeset
|
18 |
"markdown_item1", |
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65098
diff
changeset
|
19 |
"markdown_item2", |
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65098
diff
changeset
|
20 |
"markdown_item3", |
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65098
diff
changeset
|
21 |
"markdown_item4" |
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65098
diff
changeset
|
22 |
] |
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 |
const foreground_colors = [ |
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65098
diff
changeset
|
25 |
"quoted", |
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65098
diff
changeset
|
26 |
"antiquoted" |
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65098
diff
changeset
|
27 |
] |
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65098
diff
changeset
|
28 |
|
65122
1edb570f5b17
decorations for dotted underline: less intrusive;
wenzelm
parents:
65106
diff
changeset
|
29 |
const dotted_colors = [ |
1edb570f5b17
decorations for dotted underline: less intrusive;
wenzelm
parents:
65106
diff
changeset
|
30 |
"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
|
31 |
"information", |
511bf19348d3
clarified messages (with improved scalability): legacy/error as diagnostics, writeln/information/warning/bad as tooltips (dynamic hover);
wenzelm
parents:
65122
diff
changeset
|
32 |
"warning" |
65122
1edb570f5b17
decorations for dotted underline: less intrusive;
wenzelm
parents:
65106
diff
changeset
|
33 |
] |
1edb570f5b17
decorations for dotted underline: less intrusive;
wenzelm
parents:
65106
diff
changeset
|
34 |
|
65146 | 35 |
const text_colors = [ |
65181
b4105202751c
tuned colors according to Light+ and Dark+ themes;
wenzelm
parents:
65180
diff
changeset
|
36 |
"main", |
65146 | 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 |
||
65182 | 58 |
|
59 |
/* init */ |
|
60 |
||
61 |
const types = new Map<string, TextEditorDecorationType>() |
|
62 |
||
65094 | 63 |
export function init(context: ExtensionContext) |
64 |
{ |
|
65 |
function decoration(options: DecorationRenderOptions): TextEditorDecorationType |
|
66 |
{ |
|
67 |
const typ = vscode.window.createTextEditorDecorationType(options) |
|
68 |
context.subscriptions.push(typ) |
|
69 |
return typ |
|
70 |
} |
|
71 |
||
65104
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65098
diff
changeset
|
72 |
function background(color: string): TextEditorDecorationType |
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65098
diff
changeset
|
73 |
{ |
65122
1edb570f5b17
decorations for dotted underline: less intrusive;
wenzelm
parents:
65106
diff
changeset
|
74 |
return decoration( |
1edb570f5b17
decorations for dotted underline: less intrusive;
wenzelm
parents:
65106
diff
changeset
|
75 |
{ light: { backgroundColor: get_color(color, true) }, |
1edb570f5b17
decorations for dotted underline: less intrusive;
wenzelm
parents:
65106
diff
changeset
|
76 |
dark: { backgroundColor: get_color(color, false) } }) |
1edb570f5b17
decorations for dotted underline: less intrusive;
wenzelm
parents:
65106
diff
changeset
|
77 |
} |
1edb570f5b17
decorations for dotted underline: less intrusive;
wenzelm
parents:
65106
diff
changeset
|
78 |
|
65146 | 79 |
function text_color(color: string): TextEditorDecorationType |
80 |
{ |
|
81 |
return decoration( |
|
82 |
{ light: { color: get_color(color, true) }, |
|
83 |
dark: { color: get_color(color, false) } }) |
|
84 |
} |
|
85 |
||
65142 | 86 |
function bottom_border(width: string, style: string, color: string): TextEditorDecorationType |
65122
1edb570f5b17
decorations for dotted underline: less intrusive;
wenzelm
parents:
65106
diff
changeset
|
87 |
{ |
65142 | 88 |
const border = `${width} none; border-bottom-style: ${style}; border-color: ` |
65122
1edb570f5b17
decorations for dotted underline: less intrusive;
wenzelm
parents:
65106
diff
changeset
|
89 |
return decoration( |
65153 | 90 |
{ light: { border: border + get_color(color, true) }, |
91 |
dark: { border: border + get_color(color, false) } }) |
|
65104
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65098
diff
changeset
|
92 |
} |
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65098
diff
changeset
|
93 |
|
65182 | 94 |
|
95 |
/* reset */ |
|
96 |
||
97 |
types.forEach(typ => |
|
98 |
{ |
|
99 |
for (const editor of vscode.window.visibleTextEditors) { |
|
100 |
editor.setDecorations(typ, []) |
|
101 |
} |
|
102 |
const i = context.subscriptions.indexOf(typ) |
|
103 |
if (i >= 0) context.subscriptions.splice(i, 1) |
|
104 |
typ.dispose() |
|
105 |
}) |
|
106 |
types.clear() |
|
107 |
||
108 |
||
109 |
/* init */ |
|
110 |
||
65168 | 111 |
for (const color of background_colors) { |
65182 | 112 |
types.set("background_" + color, background(color)) |
65104
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65098
diff
changeset
|
113 |
} |
65168 | 114 |
for (const color of foreground_colors) { |
65182 | 115 |
types.set("foreground_" + color, background(color)) // approximation |
65104
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65098
diff
changeset
|
116 |
} |
65168 | 117 |
for (const color of dotted_colors) { |
65182 | 118 |
types.set("dotted_" + color, bottom_border("2px", "dotted", color)) |
65122
1edb570f5b17
decorations for dotted underline: less intrusive;
wenzelm
parents:
65106
diff
changeset
|
119 |
} |
65168 | 120 |
for (const color of text_colors) { |
65182 | 121 |
types.set("text_" + color, text_color(color)) |
65146 | 122 |
} |
65182 | 123 |
types.set("spell_checker", bottom_border("1px", "solid", "spell_checker")) |
124 |
||
125 |
||
126 |
/* update editors */ |
|
127 |
||
128 |
for (const editor of vscode.window.visibleTextEditors) { |
|
129 |
update_editor(editor) |
|
130 |
} |
|
65094 | 131 |
} |
132 |
||
133 |
||
134 |
/* decoration for document node */ |
|
135 |
||
65173 | 136 |
interface DecorationOpts { |
137 |
range: number[], |
|
138 |
hover_message?: MarkedString | MarkedString[] |
|
139 |
} |
|
140 |
||
65094 | 141 |
export interface Decoration |
142 |
{ |
|
143 |
uri: string, |
|
144 |
"type": string, |
|
65173 | 145 |
content: DecorationOpts[] |
65094 | 146 |
} |
147 |
||
65135
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
148 |
type Content = Range[] | DecorationOptions[] |
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
149 |
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
|
150 |
|
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
151 |
export function close_document(document: TextDocument) |
65094 | 152 |
{ |
65135
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
153 |
document_decorations.delete(document.uri.toString()) |
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
154 |
} |
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
155 |
|
65173 | 156 |
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
|
157 |
{ |
65182 | 158 |
const typ = types.get(decoration.type) |
65094 | 159 |
if (typ) { |
65173 | 160 |
const uri = Uri.parse(decoration.uri).toString() |
161 |
const content: DecorationOptions[] = decoration.content.map(opt => |
|
162 |
{ |
|
163 |
const r = opt.range |
|
164 |
return { |
|
165 |
range: new Range(new Position(r[0], r[1]), new Position(r[2], r[3])), |
|
166 |
hoverMessage: opt.hover_message |
|
167 |
} |
|
168 |
}) |
|
65135
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
169 |
|
65173 | 170 |
const document = document_decorations.get(uri) || new Map<string, Content>() |
171 |
document.set(decoration.type, content) |
|
172 |
document_decorations.set(uri, document) |
|
65135
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
173 |
|
65168 | 174 |
for (const editor of vscode.window.visibleTextEditors) { |
65173 | 175 |
if (uri === editor.document.uri.toString()) { |
176 |
editor.setDecorations(typ, content) |
|
65135
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
177 |
} |
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
178 |
} |
65094 | 179 |
} |
65135
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 |
|
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
182 |
export function update_editor(editor: TextEditor) |
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
183 |
{ |
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
184 |
if (editor) { |
65168 | 185 |
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
|
186 |
if (decorations) { |
65168 | 187 |
for (const [typ, content] of decorations) { |
65182 | 188 |
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
|
189 |
} |
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
190 |
} |
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
191 |
} |
158cba86140f
maintain decorations for document (model) and update it for each editor (view);
wenzelm
parents:
65134
diff
changeset
|
192 |
} |