diff options
author | Pascal Hertleif <[email protected]> | 2019-05-23 18:42:42 +0100 |
---|---|---|
committer | Pascal Hertleif <[email protected]> | 2019-05-27 10:26:33 +0100 |
commit | 5bf3e949e8470a138a61c806769e1a329761cab6 (patch) | |
tree | 9885346944b4aa82804514580944673a53605ee2 /editors/code | |
parent | 4b48cff022a1606bde596f01fbf44361640b10d8 (diff) |
Semantic highlighting spike
Very simple approach: For each identifier, set the hash of the range
where it's defined as its 'id' and use it in the VSCode extension to
generate unique colors.
Thus, the generated colors are per-file. They are also quite fragile,
and I'm not entirely sure why. Looks like we need to make sure the
same ranges aren't overwritten by a later request?
Diffstat (limited to 'editors/code')
-rw-r--r-- | editors/code/package-lock.json | 10 | ||||
-rw-r--r-- | editors/code/package.json | 2 | ||||
-rw-r--r-- | editors/code/src/highlighting.ts | 45 |
3 files changed, 54 insertions, 3 deletions
diff --git a/editors/code/package-lock.json b/editors/code/package-lock.json index 29cd260a4..6b3a12f91 100644 --- a/editors/code/package-lock.json +++ b/editors/code/package-lock.json | |||
@@ -36,6 +36,11 @@ | |||
36 | "integrity": "sha512-Ja7d4s0qyGFxjGeDq5S7Si25OFibSAHUi6i17UWnwNnpitADN7hah9q0Tl25gxuV5R1u2Bx+np6w4LHXfHyj/g==", | 36 | "integrity": "sha512-Ja7d4s0qyGFxjGeDq5S7Si25OFibSAHUi6i17UWnwNnpitADN7hah9q0Tl25gxuV5R1u2Bx+np6w4LHXfHyj/g==", |
37 | "dev": true | 37 | "dev": true |
38 | }, | 38 | }, |
39 | "@types/seedrandom": { | ||
40 | "version": "2.4.28", | ||
41 | "resolved": "https://registry.npmjs.org/@types/seedrandom/-/seedrandom-2.4.28.tgz", | ||
42 | "integrity": "sha512-SMA+fUwULwK7sd/ZJicUztiPs8F1yCPwF3O23Z9uQ32ME5Ha0NmDK9+QTsYE4O2tHXChzXomSWWeIhCnoN1LqA==" | ||
43 | }, | ||
39 | "agent-base": { | 44 | "agent-base": { |
40 | "version": "4.2.1", | 45 | "version": "4.2.1", |
41 | "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-4.2.1.tgz", | 46 | "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-4.2.1.tgz", |
@@ -984,6 +989,11 @@ | |||
984 | "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==", | 989 | "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==", |
985 | "dev": true | 990 | "dev": true |
986 | }, | 991 | }, |
992 | "seedrandom": { | ||
993 | "version": "3.0.1", | ||
994 | "resolved": "https://registry.npmjs.org/seedrandom/-/seedrandom-3.0.1.tgz", | ||
995 | "integrity": "sha512-1/02Y/rUeU1CJBAGLebiC5Lbo5FnB22gQbIFFYTLkwvp1xdABZJH1sn4ZT1MzXmPpzv+Rf/Lu2NcsLJiK4rcDg==" | ||
996 | }, | ||
987 | "semver": { | 997 | "semver": { |
988 | "version": "5.7.0", | 998 | "version": "5.7.0", |
989 | "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.0.tgz", | 999 | "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.0.tgz", |
diff --git a/editors/code/package.json b/editors/code/package.json index cde5fbcb8..d8ba914f5 100644 --- a/editors/code/package.json +++ b/editors/code/package.json | |||
@@ -31,11 +31,13 @@ | |||
31 | "singleQuote": true | 31 | "singleQuote": true |
32 | }, | 32 | }, |
33 | "dependencies": { | 33 | "dependencies": { |
34 | "seedrandom": "^3.0.1", | ||
34 | "vscode-languageclient": "^5.3.0-next.4" | 35 | "vscode-languageclient": "^5.3.0-next.4" |
35 | }, | 36 | }, |
36 | "devDependencies": { | 37 | "devDependencies": { |
37 | "@types/mocha": "^5.2.6", | 38 | "@types/mocha": "^5.2.6", |
38 | "@types/node": "^10.14.5", | 39 | "@types/node": "^10.14.5", |
40 | "@types/seedrandom": "^2.4.28", | ||
39 | "prettier": "^1.17.0", | 41 | "prettier": "^1.17.0", |
40 | "shx": "^0.3.1", | 42 | "shx": "^0.3.1", |
41 | "tslint": "^5.16.0", | 43 | "tslint": "^5.16.0", |
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 | } |