2018-10-07 20:44:25 +00:00
|
|
|
import * as vscode from 'vscode';
|
2018-10-07 20:59:02 +00:00
|
|
|
import * as lc from 'vscode-languageclient';
|
2018-10-07 20:44:25 +00:00
|
|
|
|
|
|
|
import { Server } from './server';
|
|
|
|
|
|
|
|
export interface Decoration {
|
2018-10-07 20:59:02 +00:00
|
|
|
range: lc.Range;
|
|
|
|
tag: string;
|
2018-10-07 20:44:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class Highlighter {
|
2018-10-08 21:38:33 +00:00
|
|
|
private static initDecorations(): Map<
|
|
|
|
string,
|
|
|
|
vscode.TextEditorDecorationType
|
|
|
|
> {
|
2019-05-21 11:04:54 +00:00
|
|
|
const colorContrib = (
|
|
|
|
tag: string
|
|
|
|
): [string, vscode.TextEditorDecorationType] => {
|
|
|
|
const color = new vscode.ThemeColor('ralsp.' + tag);
|
|
|
|
const decor = vscode.window.createTextEditorDecorationType({
|
|
|
|
color
|
|
|
|
});
|
|
|
|
return [tag, decor];
|
|
|
|
};
|
2018-10-08 18:18:55 +00:00
|
|
|
|
2018-10-08 21:38:33 +00:00
|
|
|
const decorations: Iterable<
|
|
|
|
[string, vscode.TextEditorDecorationType]
|
|
|
|
> = [
|
2019-05-21 11:04:54 +00:00
|
|
|
colorContrib('comment'),
|
|
|
|
colorContrib('string'),
|
|
|
|
colorContrib('keyword'),
|
2019-05-21 13:28:10 +00:00
|
|
|
colorContrib('keyword.control'),
|
|
|
|
colorContrib('keyword.unsafe'),
|
2019-05-21 11:04:54 +00:00
|
|
|
colorContrib('function'),
|
|
|
|
colorContrib('parameter'),
|
2019-05-23 10:26:38 +00:00
|
|
|
colorContrib('constant'),
|
|
|
|
colorContrib('type'),
|
2019-05-21 11:04:54 +00:00
|
|
|
colorContrib('builtin'),
|
|
|
|
colorContrib('text'),
|
|
|
|
colorContrib('attribute'),
|
|
|
|
colorContrib('literal'),
|
2019-05-23 10:26:38 +00:00
|
|
|
colorContrib('macro'),
|
|
|
|
colorContrib('variable'),
|
|
|
|
colorContrib('field'),
|
|
|
|
colorContrib('module')
|
2018-10-08 18:18:55 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
return new Map<string, vscode.TextEditorDecorationType>(decorations);
|
2018-10-07 20:44:25 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 21:38:33 +00:00
|
|
|
private decorations: Map<
|
|
|
|
string,
|
|
|
|
vscode.TextEditorDecorationType
|
|
|
|
> | null = null;
|
2018-10-08 18:18:55 +00:00
|
|
|
|
2018-10-07 20:59:02 +00:00
|
|
|
public removeHighlights() {
|
2018-10-08 18:18:55 +00:00
|
|
|
if (this.decorations == null) {
|
|
|
|
return;
|
2018-10-07 20:44:25 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 18:18:55 +00:00
|
|
|
// Decorations are removed when the object is disposed
|
|
|
|
for (const decoration of this.decorations.values()) {
|
|
|
|
decoration.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.decorations = null;
|
2018-10-07 20:44:25 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 21:38:33 +00:00
|
|
|
public setHighlights(editor: vscode.TextEditor, highlights: Decoration[]) {
|
2018-10-07 20:44:25 +00: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 18:18:55 +00:00
|
|
|
if (this.decorations == null) {
|
|
|
|
this.decorations = Highlighter.initDecorations();
|
2018-10-07 20:44:25 +00:00
|
|
|
}
|
|
|
|
|
2018-10-07 20:59:02 +00:00
|
|
|
const byTag: Map<string, vscode.Range[]> = new Map();
|
2018-10-08 18:18:55 +00:00
|
|
|
for (const tag of this.decorations.keys()) {
|
2018-10-07 20:59:02 +00:00
|
|
|
byTag.set(tag, []);
|
2018-10-07 20:44:25 +00:00
|
|
|
}
|
|
|
|
|
2018-10-07 20:59:02 +00:00
|
|
|
for (const d of highlights) {
|
2018-10-07 20:44:25 +00:00
|
|
|
if (!byTag.get(d.tag)) {
|
2018-10-07 20:59:02 +00:00
|
|
|
continue;
|
2018-10-07 20:44:25 +00:00
|
|
|
}
|
2018-10-08 21:38:33 +00:00
|
|
|
byTag
|
|
|
|
.get(d.tag)!
|
|
|
|
.push(Server.client.protocol2CodeConverter.asRange(d.range));
|
2018-10-07 20:44:25 +00:00
|
|
|
}
|
|
|
|
|
2018-10-07 20:59:02 +00:00
|
|
|
for (const tag of byTag.keys()) {
|
2018-10-08 21:38:33 +00:00
|
|
|
const dec = this.decorations.get(
|
|
|
|
tag
|
|
|
|
) as vscode.TextEditorDecorationType;
|
2018-10-07 20:59:02 +00:00
|
|
|
const ranges = byTag.get(tag)!;
|
|
|
|
editor.setDecorations(dec, ranges);
|
2018-10-07 20:44:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|