1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
import seedrandom = require('seedrandom');
import * as vscode from 'vscode';
import * as lc from 'vscode-languageclient';
import { Server } from './server';
export interface Decoration {
range: lc.Range;
tag: string;
bindingHash?: string;
}
// Based on this HSL-based color generator: https://gist.github.com/bendc/76c48ce53299e6078a76
function fancify(seed: string, shade: 'light' | 'dark') {
const random = seedrandom(seed);
const randomInt = (min: number, max: number) => {
return Math.floor(random() * (max - min + 1)) + min;
};
const h = randomInt(0, 360);
const s = randomInt(42, 98);
const l = shade === 'light' ? randomInt(15, 40) : randomInt(40, 90);
return `hsl(${h},${s}%,${l}%)`;
}
export class Highlighter {
private static initDecorations(): Map<
string,
vscode.TextEditorDecorationType
> {
const decoration = (
tag: string,
textDecoration?: string
): [string, vscode.TextEditorDecorationType] => {
const color = new vscode.ThemeColor('ralsp.' + tag);
const decor = vscode.window.createTextEditorDecorationType({
color,
textDecoration
});
return [tag, decor];
};
const decorations: Iterable<
[string, vscode.TextEditorDecorationType]
> = [
decoration('comment'),
decoration('string'),
decoration('keyword'),
decoration('keyword.control'),
decoration('keyword.unsafe'),
decoration('function'),
decoration('parameter'),
decoration('constant'),
decoration('type'),
decoration('builtin'),
decoration('text'),
decoration('attribute'),
decoration('literal'),
decoration('macro'),
decoration('variable'),
decoration('variable.mut', 'underline'),
decoration('field'),
decoration('module')
];
return new Map<string, vscode.TextEditorDecorationType>(decorations);
}
private decorations: Map<
string,
vscode.TextEditorDecorationType
> | null = null;
public removeHighlights() {
if (this.decorations == null) {
return;
}
// Decorations are removed when the object is disposed
for (const decoration of this.decorations.values()) {
decoration.dispose();
}
this.decorations = null;
}
public setHighlights(editor: vscode.TextEditor, highlights: Decoration[]) {
// Initialize decorations if necessary
//
// Note: decoration objects need to be kept around so we can dispose them
// if the user disables syntax highlighting
if (this.decorations == null) {
this.decorations = Highlighter.initDecorations();
}
const byTag: Map<string, vscode.Range[]> = new Map();
const colorfulIdents: Map<
string,
[vscode.Range[], boolean]
> = new Map();
const rainbowTime = Server.config.rainbowHighlightingOn;
for (const tag of this.decorations.keys()) {
byTag.set(tag, []);
}
for (const d of highlights) {
if (!byTag.get(d.tag)) {
continue;
}
if (rainbowTime && d.bindingHash) {
if (!colorfulIdents.has(d.bindingHash)) {
const mut = d.tag.endsWith('.mut');
colorfulIdents.set(d.bindingHash, [[], mut]);
}
colorfulIdents
.get(d.bindingHash)![0]
.push(
Server.client.protocol2CodeConverter.asRange(d.range)
);
} else {
byTag
.get(d.tag)!
.push(
Server.client.protocol2CodeConverter.asRange(d.range)
);
}
}
for (const tag of byTag.keys()) {
const dec = this.decorations.get(
tag
) as vscode.TextEditorDecorationType;
const ranges = byTag.get(tag)!;
editor.setDecorations(dec, ranges);
}
for (const [hash, [ranges, mut]] of colorfulIdents.entries()) {
const textDecoration = mut ? 'underline' : undefined;
const dec = vscode.window.createTextEditorDecorationType({
light: { color: fancify(hash, 'light'), textDecoration },
dark: { color: fancify(hash, 'dark'), textDecoration }
});
editor.setDecorations(dec, ranges);
}
}
}
|