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

79 lines
2.3 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 {
private decorations: { [index: string]: vscode.TextEditorDecorationType };
constructor() {
this.decorations = {};
}
2018-10-07 22:59:02 +02:00
public removeHighlights() {
for (const tag in this.decorations) {
2018-10-07 22:44:25 +02:00
this.decorations[tag].dispose();
}
this.decorations = {};
}
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
if (Object.keys(this.decorations).length === 0) {
this.initDecorations();
}
2018-10-07 22:59:02 +02:00
const byTag: Map<string, vscode.Range[]> = new Map();
for (const tag in this.decorations) {
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
console.log(`unknown tag ${d.tag}`);
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()) {
const dec: vscode.TextEditorDecorationType = this.decorations[tag];
const ranges = byTag.get(tag)!;
editor.setDecorations(dec, ranges);
2018-10-07 22:44:25 +02:00
}
}
private initDecorations() {
2018-10-07 22:59:02 +02:00
const decor = (obj: any) => vscode.window.createTextEditorDecorationType({ color: obj });
2018-10-07 22:44:25 +02:00
this.decorations = {
2018-10-07 22:59:02 +02:00
background: decor('#3F3F3F'),
2018-10-07 22:44:25 +02:00
error: vscode.window.createTextEditorDecorationType({
2018-10-07 22:59:02 +02:00
borderColor: 'red',
borderStyle: 'none none dashed none',
2018-10-07 22:44:25 +02:00
}),
2018-10-07 22:59:02 +02:00
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'),
};
2018-10-07 22:44:25 +02:00
}
}