diff options
Diffstat (limited to 'editors/code/src/highlighting.ts')
-rw-r--r-- | editors/code/src/highlighting.ts | 46 |
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 @@ | |||
1 | import seedrandom = require('seedrandom'); | ||
1 | import * as vscode from 'vscode'; | 2 | import * as vscode from 'vscode'; |
2 | import * as lc from 'vscode-languageclient'; | 3 | import * as lc from 'vscode-languageclient'; |
3 | 4 | ||
@@ -6,6 +7,20 @@ import { Server } from './server'; | |||
6 | export interface Decoration { | 7 | export 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 | ||
14 | function 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 | ||
11 | export class Highlighter { | 26 | export 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 | } |