Files
rust/editors/code/src/highlighting.ts

83 lines
2.6 KiB
TypeScript
Raw Normal View History

2018-10-07 22:44:25 +02:00
import * as vscode from 'vscode';
2018-10-07 22:59:02 +02:00
import * as lc from 'vscode-languageclient';
2018-10-07 22:44:25 +02:00
import { Server } from './server';
export interface Decoration {
2018-10-07 22:59:02 +02:00
range: lc.Range;
tag: string;
2018-10-07 22:44:25 +02:00
}
export class Highlighter {
2018-10-08 20:18:55 +02:00
private static initDecorations(): Map<string, vscode.TextEditorDecorationType> {
const decor = (color: string) => vscode.window.createTextEditorDecorationType({ color });
const decorations: Iterable<[string, vscode.TextEditorDecorationType]> = [
['background', decor('#3F3F3F')],
['error', vscode.window.createTextEditorDecorationType({
borderColor: 'red',
borderStyle: 'none none dashed none',
})],
['comment', decor('#7F9F7F')],
['string', decor('#CC9393')],
['keyword', decor('#F0DFAF')],
['function', decor('#93E0E3')],
['parameter', decor('#94BFF3')],
['builtin', decor('#DD6718')],
['text', decor('#DCDCCC')],
['attribute', decor('#BFEBBF')],
['literal', decor('#DFAF8F')],
];
return new Map<string, vscode.TextEditorDecorationType>(decorations);
2018-10-07 22:44:25 +02:00
}
2018-10-08 20:18:55 +02:00
private decorations: (Map<string, vscode.TextEditorDecorationType> | null) = null;
2018-10-07 22:59:02 +02:00
public removeHighlights() {
2018-10-08 20:18:55 +02:00
if (this.decorations == null) {
return;
2018-10-07 22:44:25 +02:00
}
2018-10-08 20:18:55 +02:00
// Decorations are removed when the object is disposed
for (const decoration of this.decorations.values()) {
decoration.dispose();
}
this.decorations = null;
2018-10-07 22:44:25 +02:00
}
2018-10-07 22:59:02 +02:00
public setHighlights(
2018-10-07 22:44:25 +02:00
editor: vscode.TextEditor,
2018-10-07 22:59:02 +02:00
highlights: Decoration[],
2018-10-07 22:44:25 +02:00
) {
// Initialize decorations if necessary
//
// Note: decoration objects need to be kept around so we can dispose them
// if the user disables syntax highlighting
2018-10-08 20:18:55 +02:00
if (this.decorations == null) {
this.decorations = Highlighter.initDecorations();
2018-10-07 22:44:25 +02:00
}
2018-10-07 22:59:02 +02:00
const byTag: Map<string, vscode.Range[]> = new Map();
2018-10-08 20:18:55 +02:00
for (const tag of this.decorations.keys()) {
2018-10-07 22:59:02 +02:00
byTag.set(tag, []);
2018-10-07 22:44:25 +02:00
}
2018-10-07 22:59:02 +02:00
for (const d of highlights) {
2018-10-07 22:44:25 +02:00
if (!byTag.get(d.tag)) {
2018-10-07 22:59:02 +02:00
continue;
2018-10-07 22:44:25 +02:00
}
byTag.get(d.tag)!.push(
2018-10-07 22:59:02 +02:00
Server.client.protocol2CodeConverter.asRange(d.range),
);
2018-10-07 22:44:25 +02:00
}
2018-10-07 22:59:02 +02:00
for (const tag of byTag.keys()) {
2018-10-08 20:18:55 +02:00
const dec = this.decorations.get(tag) as vscode.TextEditorDecorationType;
2018-10-07 22:59:02 +02:00
const ranges = byTag.get(tag)!;
editor.setDecorations(dec, ranges);
2018-10-07 22:44:25 +02:00
}
}
}