diff options
Diffstat (limited to 'editors/code/src')
-rw-r--r-- | editors/code/src/highlighting.ts | 45 |
1 files changed, 42 insertions, 3 deletions
diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts index 8389d94b8..4597db08f 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 | id?: 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,8 @@ 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 | |||
79 | for (const tag of this.decorations.keys()) { | 96 | for (const tag of this.decorations.keys()) { |
80 | byTag.set(tag, []); | 97 | byTag.set(tag, []); |
81 | } | 98 | } |
@@ -84,9 +101,23 @@ export class Highlighter { | |||
84 | if (!byTag.get(d.tag)) { | 101 | if (!byTag.get(d.tag)) { |
85 | continue; | 102 | continue; |
86 | } | 103 | } |
87 | byTag | 104 | |
88 | .get(d.tag)! | 105 | if (d.id) { |
89 | .push(Server.client.protocol2CodeConverter.asRange(d.range)); | 106 | if (!colorfulIdents.has(d.id)) { |
107 | colorfulIdents.set(d.id, []); | ||
108 | } | ||
109 | colorfulIdents | ||
110 | .get(d.id)! | ||
111 | .push( | ||
112 | Server.client.protocol2CodeConverter.asRange(d.range) | ||
113 | ); | ||
114 | } else { | ||
115 | byTag | ||
116 | .get(d.tag)! | ||
117 | .push( | ||
118 | Server.client.protocol2CodeConverter.asRange(d.range) | ||
119 | ); | ||
120 | } | ||
90 | } | 121 | } |
91 | 122 | ||
92 | for (const tag of byTag.keys()) { | 123 | for (const tag of byTag.keys()) { |
@@ -96,5 +127,13 @@ export class Highlighter { | |||
96 | const ranges = byTag.get(tag)!; | 127 | const ranges = byTag.get(tag)!; |
97 | editor.setDecorations(dec, ranges); | 128 | editor.setDecorations(dec, ranges); |
98 | } | 129 | } |
130 | |||
131 | for (const [hash, ranges] of colorfulIdents.entries()) { | ||
132 | const dec = vscode.window.createTextEditorDecorationType({ | ||
133 | light: { color: fancify(hash, 'light') }, | ||
134 | dark: { color: fancify(hash, 'dark') } | ||
135 | }); | ||
136 | editor.setDecorations(dec, ranges); | ||
137 | } | ||
99 | } | 138 | } |
100 | } | 139 | } |