aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/highlighting.ts
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-05-27 10:56:06 +0100
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-05-27 10:56:06 +0100
commit0d1c6076073c73f57340e256dc25da9d37311ef0 (patch)
tree60ff1f4a42f8ef297c07d5716af67e3057c8e1bd /editors/code/src/highlighting.ts
parent4b48cff022a1606bde596f01fbf44361640b10d8 (diff)
parent1e6ba1901550fb1610a1a464c48ec358cd3c339c (diff)
Merge #1319
1319: Rainbow highlighting spike 🌈 r=killercup a=killercup Very simple approach: For each identifier, set the hash of the range where it's defined as its 'id' and use it in the VSCode extension to generate unique colors. Thus, the generated colors are per-file. They are also quite fragile, and I'm not entirely sure why. Looks like we need to make sure the same ranges aren't overwritten by a later request? Co-authored-by: Pascal Hertleif <[email protected]>
Diffstat (limited to 'editors/code/src/highlighting.ts')
-rw-r--r--editors/code/src/highlighting.ts46
1 files changed, 43 insertions, 3 deletions
diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts
index 8389d94b8..52a0bd4bb 100644
--- a/editors/code/src/highlighting.ts
+++ b/editors/code/src/highlighting.ts
@@ -1,3 +1,4 @@
1import seedrandom = require('seedrandom');
1import * as vscode from 'vscode'; 2import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient'; 3import * as lc from 'vscode-languageclient';
3 4
@@ -6,6 +7,20 @@ import { Server } from './server';
6export interface Decoration { 7export interface Decoration {
7 range: lc.Range; 8 range: lc.Range;
8 tag: string; 9 tag: string;
10 bindingHash?: string;
11}
12
13// Based on this HSL-based color generator: https://gist.github.com/bendc/76c48ce53299e6078a76
14function fancify(seed: string, shade: 'light' | 'dark') {
15 const random = seedrandom(seed);
16 const randomInt = (min: number, max: number) => {
17 return Math.floor(random() * (max - min + 1)) + min;
18 };
19
20 const h = randomInt(0, 360);
21 const s = randomInt(42, 98);
22 const l = shade === 'light' ? randomInt(15, 40) : randomInt(40, 90);
23 return `hsl(${h},${s}%,${l}%)`;
9} 24}
10 25
11export class Highlighter { 26export class Highlighter {
@@ -76,6 +91,9 @@ export class Highlighter {
76 } 91 }
77 92
78 const byTag: Map<string, vscode.Range[]> = new Map(); 93 const byTag: Map<string, vscode.Range[]> = new Map();
94 const colorfulIdents: Map<string, vscode.Range[]> = new Map();
95 const rainbowTime = Server.config.rainbowHighlightingOn;
96
79 for (const tag of this.decorations.keys()) { 97 for (const tag of this.decorations.keys()) {
80 byTag.set(tag, []); 98 byTag.set(tag, []);
81 } 99 }
@@ -84,9 +102,23 @@ export class Highlighter {
84 if (!byTag.get(d.tag)) { 102 if (!byTag.get(d.tag)) {
85 continue; 103 continue;
86 } 104 }
87 byTag 105
88 .get(d.tag)! 106 if (rainbowTime && d.bindingHash) {
89 .push(Server.client.protocol2CodeConverter.asRange(d.range)); 107 if (!colorfulIdents.has(d.bindingHash)) {
108 colorfulIdents.set(d.bindingHash, []);
109 }
110 colorfulIdents
111 .get(d.bindingHash)!
112 .push(
113 Server.client.protocol2CodeConverter.asRange(d.range)
114 );
115 } else {
116 byTag
117 .get(d.tag)!
118 .push(
119 Server.client.protocol2CodeConverter.asRange(d.range)
120 );
121 }
90 } 122 }
91 123
92 for (const tag of byTag.keys()) { 124 for (const tag of byTag.keys()) {
@@ -96,5 +128,13 @@ export class Highlighter {
96 const ranges = byTag.get(tag)!; 128 const ranges = byTag.get(tag)!;
97 editor.setDecorations(dec, ranges); 129 editor.setDecorations(dec, ranges);
98 } 130 }
131
132 for (const [hash, ranges] of colorfulIdents.entries()) {
133 const dec = vscode.window.createTextEditorDecorationType({
134 light: { color: fancify(hash, 'light') },
135 dark: { color: fancify(hash, 'dark') }
136 });
137 editor.setDecorations(dec, ranges);
138 }
99 } 139 }
100} 140}