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.ts34
1 files changed, 27 insertions, 7 deletions
diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts
index 014e96f75..4fbbe3ddc 100644
--- a/editors/code/src/highlighting.ts
+++ b/editors/code/src/highlighting.ts
@@ -1,7 +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 * as seedrandom_ from 'seedrandom';
4const seedrandom = seedrandom_; // https://github.com/jvandemo/generator-angular2-library/issues/221#issuecomment-355945207
5 3
6import { ColorTheme, TextMateRuleSettings } from './color_theme'; 4import { ColorTheme, TextMateRuleSettings } from './color_theme';
7 5
@@ -34,6 +32,7 @@ export function activateHighlighting(ctx: Ctx) {
34 32
35 vscode.workspace.onDidChangeConfiguration( 33 vscode.workspace.onDidChangeConfiguration(
36 _ => highlighter.removeHighlights(), 34 _ => highlighter.removeHighlights(),
35 null,
37 ctx.subscriptions, 36 ctx.subscriptions,
38 ); 37 );
39 38
@@ -41,7 +40,7 @@ export function activateHighlighting(ctx: Ctx) {
41 async (editor: vscode.TextEditor | undefined) => { 40 async (editor: vscode.TextEditor | undefined) => {
42 if (!editor || editor.document.languageId !== 'rust') return; 41 if (!editor || editor.document.languageId !== 'rust') return;
43 if (!ctx.config.highlightingOn) return; 42 if (!ctx.config.highlightingOn) return;
44 let client = ctx.client; 43 const client = ctx.client;
45 if (!client) return; 44 if (!client) return;
46 45
47 const params: lc.TextDocumentIdentifier = { 46 const params: lc.TextDocumentIdentifier = {
@@ -54,6 +53,7 @@ export function activateHighlighting(ctx: Ctx) {
54 ); 53 );
55 highlighter.setHighlights(editor, decorations); 54 highlighter.setHighlights(editor, decorations);
56 }, 55 },
56 null,
57 ctx.subscriptions, 57 ctx.subscriptions,
58 ); 58 );
59} 59}
@@ -71,9 +71,9 @@ interface Decoration {
71 71
72// Based on this HSL-based color generator: https://gist.github.com/bendc/76c48ce53299e6078a76 72// Based on this HSL-based color generator: https://gist.github.com/bendc/76c48ce53299e6078a76
73function fancify(seed: string, shade: 'light' | 'dark') { 73function fancify(seed: string, shade: 'light' | 'dark') {
74 const random = seedrandom(seed); 74 const random = randomU32Numbers(hashString(seed));
75 const randomInt = (min: number, max: number) => { 75 const randomInt = (min: number, max: number) => {
76 return Math.floor(random() * (max - min + 1)) + min; 76 return Math.abs(random()) % (max - min + 1) + min;
77 }; 77 };
78 78
79 const h = randomInt(0, 360); 79 const h = randomInt(0, 360);
@@ -107,7 +107,7 @@ class Highlighter {
107 } 107 }
108 108
109 public setHighlights(editor: vscode.TextEditor, highlights: Decoration[]) { 109 public setHighlights(editor: vscode.TextEditor, highlights: Decoration[]) {
110 let client = this.ctx.client; 110 const client = this.ctx.client;
111 if (!client) return; 111 if (!client) return;
112 // Initialize decorations if necessary 112 // Initialize decorations if necessary
113 // 113 //
@@ -176,7 +176,7 @@ function initDecorations(): Map<string, vscode.TextEditorDecorationType> {
176 const res = new Map(); 176 const res = new Map();
177 TAG_TO_SCOPES.forEach((scopes, tag) => { 177 TAG_TO_SCOPES.forEach((scopes, tag) => {
178 if (!scopes) throw `unmapped tag: ${tag}`; 178 if (!scopes) throw `unmapped tag: ${tag}`;
179 let rule = theme.lookup(scopes); 179 const rule = theme.lookup(scopes);
180 const decor = createDecorationFromTextmate(rule); 180 const decor = createDecorationFromTextmate(rule);
181 res.set(tag, decor); 181 res.set(tag, decor);
182 }); 182 });
@@ -247,3 +247,23 @@ const TAG_TO_SCOPES = new Map<string, string[]>([
247 ["keyword.unsafe", ["keyword.other.unsafe"]], 247 ["keyword.unsafe", ["keyword.other.unsafe"]],
248 ["keyword.control", ["keyword.control"]], 248 ["keyword.control", ["keyword.control"]],
249]); 249]);
250
251function randomU32Numbers(seed: number) {
252 let random = seed | 0;
253 return () => {
254 random ^= random << 13;
255 random ^= random >> 17;
256 random ^= random << 5;
257 random |= 0;
258 return random;
259 };
260}
261
262function hashString(str: string): number {
263 let res = 0;
264 for (let i = 0; i < str.length; ++i) {
265 const c = str.codePointAt(i)!;
266 res = (res * 31 + c) & ~0;
267 }
268 return res;
269}