aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/highlighting.ts
diff options
context:
space:
mode:
Diffstat (limited to 'editors/code/src/highlighting.ts')
-rw-r--r--editors/code/src/highlighting.ts31
1 files changed, 25 insertions, 6 deletions
diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts
index fc7cd5a1c..66216e0fc 100644
--- a/editors/code/src/highlighting.ts
+++ b/editors/code/src/highlighting.ts
@@ -1,6 +1,5 @@
1import * as vscode from 'vscode'; 1import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient'; 2import * as lc from 'vscode-languageclient';
3import seedrandom from 'seedrandom';
4 3
5import { ColorTheme, TextMateRuleSettings } from './color_theme'; 4import { ColorTheme, TextMateRuleSettings } from './color_theme';
6 5
@@ -40,7 +39,7 @@ export function activateHighlighting(ctx: Ctx) {
40 async (editor: vscode.TextEditor | undefined) => { 39 async (editor: vscode.TextEditor | undefined) => {
41 if (!editor || editor.document.languageId !== 'rust') return; 40 if (!editor || editor.document.languageId !== 'rust') return;
42 if (!ctx.config.highlightingOn) return; 41 if (!ctx.config.highlightingOn) return;
43 let client = ctx.client; 42 const client = ctx.client;
44 if (!client) return; 43 if (!client) return;
45 44
46 const params: lc.TextDocumentIdentifier = { 45 const params: lc.TextDocumentIdentifier = {
@@ -70,9 +69,9 @@ interface Decoration {
70 69
71// Based on this HSL-based color generator: https://gist.github.com/bendc/76c48ce53299e6078a76 70// Based on this HSL-based color generator: https://gist.github.com/bendc/76c48ce53299e6078a76
72function fancify(seed: string, shade: 'light' | 'dark') { 71function fancify(seed: string, shade: 'light' | 'dark') {
73 const random = seedrandom(seed); 72 const random = randomU32Numbers(hashString(seed))
74 const randomInt = (min: number, max: number) => { 73 const randomInt = (min: number, max: number) => {
75 return Math.floor(random() * (max - min + 1)) + min; 74 return Math.abs(random()) % (max - min + 1) + min;
76 }; 75 };
77 76
78 const h = randomInt(0, 360); 77 const h = randomInt(0, 360);
@@ -106,7 +105,7 @@ class Highlighter {
106 } 105 }
107 106
108 public setHighlights(editor: vscode.TextEditor, highlights: Decoration[]) { 107 public setHighlights(editor: vscode.TextEditor, highlights: Decoration[]) {
109 let client = this.ctx.client; 108 const client = this.ctx.client;
110 if (!client) return; 109 if (!client) return;
111 // Initialize decorations if necessary 110 // Initialize decorations if necessary
112 // 111 //
@@ -175,7 +174,7 @@ function initDecorations(): Map<string, vscode.TextEditorDecorationType> {
175 const res = new Map(); 174 const res = new Map();
176 TAG_TO_SCOPES.forEach((scopes, tag) => { 175 TAG_TO_SCOPES.forEach((scopes, tag) => {
177 if (!scopes) throw `unmapped tag: ${tag}`; 176 if (!scopes) throw `unmapped tag: ${tag}`;
178 let rule = theme.lookup(scopes); 177 const rule = theme.lookup(scopes);
179 const decor = createDecorationFromTextmate(rule); 178 const decor = createDecorationFromTextmate(rule);
180 res.set(tag, decor); 179 res.set(tag, decor);
181 }); 180 });
@@ -246,3 +245,23 @@ const TAG_TO_SCOPES = new Map<string, string[]>([
246 ["keyword.unsafe", ["keyword.other.unsafe"]], 245 ["keyword.unsafe", ["keyword.other.unsafe"]],
247 ["keyword.control", ["keyword.control"]], 246 ["keyword.control", ["keyword.control"]],
248]); 247]);
248
249function randomU32Numbers(seed: number) {
250 let random = seed | 0;
251 return () => {
252 random ^= random << 13;
253 random ^= random >> 17;
254 random ^= random << 5;
255 random |= 0;
256 return random
257 }
258}
259
260function hashString(str: string): number {
261 let res = 0;
262 for (let i = 0; i < str.length; ++i) {
263 const c = str.codePointAt(i)!!;
264 res = (res * 31 + c) & ~0;
265 }
266 return res;
267}