diff options
author | Kevin DeLorey <[email protected]> | 2020-02-09 16:25:47 +0000 |
---|---|---|
committer | Kevin DeLorey <[email protected]> | 2020-02-09 16:37:43 +0000 |
commit | a957c473fdb79880c39b73dc9e0c923093cf16ac (patch) | |
tree | f998b548f530ce604651e0e6af314ed2ec74b3b5 /editors/code/src/highlighting.ts | |
parent | 22caf982b99c54058e2e9200aeea0e61cada284a (diff) | |
parent | 1b9b13b4b4a75b5531c3f046ce6bf72d681f2732 (diff) |
Merge branch 'master' into kdelorey/complete-trait-impl
Diffstat (limited to 'editors/code/src/highlighting.ts')
-rw-r--r-- | editors/code/src/highlighting.ts | 34 |
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 @@ | |||
1 | import * as vscode from 'vscode'; | 1 | import * as vscode from 'vscode'; |
2 | import * as lc from 'vscode-languageclient'; | 2 | import * as lc from 'vscode-languageclient'; |
3 | import * as seedrandom_ from 'seedrandom'; | ||
4 | const seedrandom = seedrandom_; // https://github.com/jvandemo/generator-angular2-library/issues/221#issuecomment-355945207 | ||
5 | 3 | ||
6 | import { ColorTheme, TextMateRuleSettings } from './color_theme'; | 4 | import { 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 |
73 | function fancify(seed: string, shade: 'light' | 'dark') { | 73 | function 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 | |||
251 | function 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 | |||
262 | function 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 | } | ||