mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-08-30 23:27:24 +00:00
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?
This commit is contained in:
parent
4b48cff022
commit
5bf3e949e8
9 changed files with 409 additions and 39 deletions
|
@ -1,3 +1,4 @@
|
|||
import seedrandom = require('seedrandom');
|
||||
import * as vscode from 'vscode';
|
||||
import * as lc from 'vscode-languageclient';
|
||||
|
||||
|
@ -6,6 +7,20 @@ import { Server } from './server';
|
|||
export interface Decoration {
|
||||
range: lc.Range;
|
||||
tag: string;
|
||||
id?: string;
|
||||
}
|
||||
|
||||
// Based on this HSL-based color generator: https://gist.github.com/bendc/76c48ce53299e6078a76
|
||||
function fancify(seed: string, shade: 'light' | 'dark') {
|
||||
const random = seedrandom(seed);
|
||||
const randomInt = (min: number, max: number) => {
|
||||
return Math.floor(random() * (max - min + 1)) + min;
|
||||
};
|
||||
|
||||
const h = randomInt(0, 360);
|
||||
const s = randomInt(42, 98);
|
||||
const l = shade === 'light' ? randomInt(15, 40) : randomInt(40, 90);
|
||||
return `hsl(${h},${s}%,${l}%)`;
|
||||
}
|
||||
|
||||
export class Highlighter {
|
||||
|
@ -76,6 +91,8 @@ export class Highlighter {
|
|||
}
|
||||
|
||||
const byTag: Map<string, vscode.Range[]> = new Map();
|
||||
const colorfulIdents: Map<string, vscode.Range[]> = new Map();
|
||||
|
||||
for (const tag of this.decorations.keys()) {
|
||||
byTag.set(tag, []);
|
||||
}
|
||||
|
@ -84,9 +101,23 @@ export class Highlighter {
|
|||
if (!byTag.get(d.tag)) {
|
||||
continue;
|
||||
}
|
||||
byTag
|
||||
.get(d.tag)!
|
||||
.push(Server.client.protocol2CodeConverter.asRange(d.range));
|
||||
|
||||
if (d.id) {
|
||||
if (!colorfulIdents.has(d.id)) {
|
||||
colorfulIdents.set(d.id, []);
|
||||
}
|
||||
colorfulIdents
|
||||
.get(d.id)!
|
||||
.push(
|
||||
Server.client.protocol2CodeConverter.asRange(d.range)
|
||||
);
|
||||
} else {
|
||||
byTag
|
||||
.get(d.tag)!
|
||||
.push(
|
||||
Server.client.protocol2CodeConverter.asRange(d.range)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
for (const tag of byTag.keys()) {
|
||||
|
@ -96,5 +127,13 @@ export class Highlighter {
|
|||
const ranges = byTag.get(tag)!;
|
||||
editor.setDecorations(dec, ranges);
|
||||
}
|
||||
|
||||
for (const [hash, ranges] of colorfulIdents.entries()) {
|
||||
const dec = vscode.window.createTextEditorDecorationType({
|
||||
light: { color: fancify(hash, 'light') },
|
||||
dark: { color: fancify(hash, 'dark') }
|
||||
});
|
||||
editor.setDecorations(dec, ranges);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue