2019-05-23 17:42:42 +00:00
|
|
|
import seedrandom = require('seedrandom');
|
2018-10-07 20:44:25 +00:00
|
|
|
import * as vscode from 'vscode';
|
2018-10-07 20:59:02 +00:00
|
|
|
import * as lc from 'vscode-languageclient';
|
2019-11-04 22:59:11 +00:00
|
|
|
import * as scopes from './scopes';
|
Introducing a Scopes Mapper to map from RA scopes to TextMate scopes with fallbacks.
Current scopes defined:
```
['keyword.unsafe', ['storage.modifier', 'keyword.other', 'keyword.control']],
['function', ['entity.name.function']],
['parameter', ['variable.parameter']],
['type', ['entity.name.type']],
['builtin', ['variable.language', 'support.type', 'support.type']],
['text', ['string', 'string.quoted', 'string.regexp']],
['attribute', ['keyword']],
['literal', ['string', 'string.quoted', 'string.regexp']],
['macro', ['support.other']],
['variable.mut', ['variable']],
['field', ['variable.object.property']],
['module', ['entity.name.section']]
```
Need to complement with further fallbacks as some themes fail.
2019-10-27 16:57:11 +00:00
|
|
|
import * as scopesMapper from './scopes_mapper';
|
2018-10-07 20:44:25 +00:00
|
|
|
|
|
|
|
import { Server } from './server';
|
|
|
|
|
|
|
|
export interface Decoration {
|
2018-10-07 20:59:02 +00:00
|
|
|
range: lc.Range;
|
|
|
|
tag: string;
|
2019-05-27 09:26:15 +00:00
|
|
|
bindingHash?: string;
|
2019-05-23 17:42:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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}%)`;
|
2018-10-07 20:44:25 +00:00
|
|
|
}
|
|
|
|
|
2019-11-04 22:59:11 +00:00
|
|
|
|
2019-10-24 15:25:23 +00:00
|
|
|
function createDecorationFromTextmate(themeStyle: scopes.TextMateRuleSettings): vscode.TextEditorDecorationType {
|
2019-11-04 22:59:11 +00:00
|
|
|
const options: vscode.DecorationRenderOptions = {};
|
|
|
|
options.rangeBehavior = vscode.DecorationRangeBehavior.OpenOpen;
|
2019-10-24 15:25:23 +00:00
|
|
|
if (themeStyle.foreground) {
|
2019-11-04 22:59:11 +00:00
|
|
|
options.color = themeStyle.foreground;
|
2019-10-24 15:25:23 +00:00
|
|
|
}
|
|
|
|
if (themeStyle.background) {
|
2019-11-04 22:59:11 +00:00
|
|
|
options.backgroundColor = themeStyle.background;
|
2019-10-24 15:25:23 +00:00
|
|
|
}
|
|
|
|
if (themeStyle.fontStyle) {
|
2019-11-04 22:59:11 +00:00
|
|
|
const parts: string[] = themeStyle.fontStyle.split(' ');
|
2019-10-24 15:25:23 +00:00
|
|
|
parts.forEach((part) => {
|
|
|
|
switch (part) {
|
|
|
|
case 'italic':
|
2019-11-04 22:59:11 +00:00
|
|
|
options.fontStyle = 'italic';
|
|
|
|
break;
|
2019-10-24 15:25:23 +00:00
|
|
|
case 'bold':
|
2019-11-04 22:59:11 +00:00
|
|
|
options.fontWeight = 'bold';
|
|
|
|
break;
|
2019-10-24 15:25:23 +00:00
|
|
|
case 'underline':
|
2019-11-04 22:59:11 +00:00
|
|
|
options.textDecoration = 'underline';
|
|
|
|
break;
|
2019-10-24 15:25:23 +00:00
|
|
|
default:
|
2019-11-04 22:59:11 +00:00
|
|
|
break;
|
2019-10-24 15:25:23 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-11-04 22:59:11 +00:00
|
|
|
return vscode.window.createTextEditorDecorationType(options);
|
2019-10-24 15:25:23 +00:00
|
|
|
}
|
|
|
|
|
2018-10-07 20:44:25 +00:00
|
|
|
export class Highlighter {
|
2018-10-08 21:38:33 +00:00
|
|
|
private static initDecorations(): Map<
|
|
|
|
string,
|
|
|
|
vscode.TextEditorDecorationType
|
|
|
|
> {
|
2019-07-19 11:43:36 +00:00
|
|
|
const decoration = (
|
|
|
|
tag: string,
|
|
|
|
textDecoration?: string
|
2019-05-21 11:04:54 +00:00
|
|
|
): [string, vscode.TextEditorDecorationType] => {
|
2019-10-24 15:25:23 +00:00
|
|
|
|
2019-11-04 22:59:11 +00:00
|
|
|
const rule = scopesMapper.toRule(tag, scopes.find);
|
Introducing a Scopes Mapper to map from RA scopes to TextMate scopes with fallbacks.
Current scopes defined:
```
['keyword.unsafe', ['storage.modifier', 'keyword.other', 'keyword.control']],
['function', ['entity.name.function']],
['parameter', ['variable.parameter']],
['type', ['entity.name.type']],
['builtin', ['variable.language', 'support.type', 'support.type']],
['text', ['string', 'string.quoted', 'string.regexp']],
['attribute', ['keyword']],
['literal', ['string', 'string.quoted', 'string.regexp']],
['macro', ['support.other']],
['variable.mut', ['variable']],
['field', ['variable.object.property']],
['module', ['entity.name.section']]
```
Need to complement with further fallbacks as some themes fail.
2019-10-27 16:57:11 +00:00
|
|
|
|
2019-10-27 22:49:41 +00:00
|
|
|
if (rule) {
|
|
|
|
const decor = createDecorationFromTextmate(rule);
|
2019-10-24 15:25:23 +00:00
|
|
|
return [tag, decor];
|
|
|
|
}
|
|
|
|
else {
|
2019-11-04 23:11:43 +00:00
|
|
|
const fallBackTag = 'ralsp.' + tag;
|
2019-11-05 13:22:09 +00:00
|
|
|
// console.log(' ');
|
|
|
|
// console.log('Missing theme for: <"' + tag + '"> for following mapped scopes:');
|
|
|
|
// console.log(scopesMapper.find(tag));
|
|
|
|
// console.log('Falling back to values defined in: ' + fallBackTag);
|
|
|
|
// console.log(' ');
|
2019-11-04 23:11:43 +00:00
|
|
|
const color = new vscode.ThemeColor(fallBackTag);
|
2019-10-24 15:25:23 +00:00
|
|
|
const decor = vscode.window.createTextEditorDecorationType({
|
|
|
|
color,
|
|
|
|
textDecoration
|
|
|
|
});
|
|
|
|
return [tag, decor];
|
|
|
|
}
|
2019-05-21 11:04:54 +00:00
|
|
|
};
|
2018-10-08 18:18:55 +00:00
|
|
|
|
2018-10-08 21:38:33 +00:00
|
|
|
const decorations: Iterable<
|
|
|
|
[string, vscode.TextEditorDecorationType]
|
|
|
|
> = [
|
2019-10-24 15:25:23 +00:00
|
|
|
decoration('comment'),
|
|
|
|
decoration('string'),
|
|
|
|
decoration('keyword'),
|
|
|
|
decoration('keyword.control'),
|
|
|
|
decoration('keyword.unsafe'),
|
|
|
|
decoration('function'),
|
|
|
|
decoration('parameter'),
|
|
|
|
decoration('constant'),
|
|
|
|
decoration('type'),
|
|
|
|
decoration('builtin'),
|
|
|
|
decoration('text'),
|
|
|
|
decoration('attribute'),
|
|
|
|
decoration('literal'),
|
|
|
|
decoration('macro'),
|
|
|
|
decoration('variable'),
|
|
|
|
decoration('variable.mut', 'underline'),
|
|
|
|
decoration('field'),
|
|
|
|
decoration('module')
|
|
|
|
];
|
2018-10-08 18:18:55 +00:00
|
|
|
|
|
|
|
return new Map<string, vscode.TextEditorDecorationType>(decorations);
|
2018-10-07 20:44:25 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 21:38:33 +00:00
|
|
|
private decorations: Map<
|
|
|
|
string,
|
|
|
|
vscode.TextEditorDecorationType
|
|
|
|
> | null = null;
|
2018-10-08 18:18:55 +00:00
|
|
|
|
2018-10-07 20:59:02 +00:00
|
|
|
public removeHighlights() {
|
2018-10-08 18:18:55 +00:00
|
|
|
if (this.decorations == null) {
|
|
|
|
return;
|
2018-10-07 20:44:25 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 18:18:55 +00:00
|
|
|
// Decorations are removed when the object is disposed
|
|
|
|
for (const decoration of this.decorations.values()) {
|
|
|
|
decoration.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.decorations = null;
|
2018-10-07 20:44:25 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 21:38:33 +00:00
|
|
|
public setHighlights(editor: vscode.TextEditor, highlights: Decoration[]) {
|
2018-10-07 20:44:25 +00:00
|
|
|
// Initialize decorations if necessary
|
|
|
|
//
|
|
|
|
// Note: decoration objects need to be kept around so we can dispose them
|
|
|
|
// if the user disables syntax highlighting
|
2019-10-24 15:25:23 +00:00
|
|
|
|
|
|
|
|
2018-10-08 18:18:55 +00:00
|
|
|
if (this.decorations == null) {
|
|
|
|
this.decorations = Highlighter.initDecorations();
|
2018-10-07 20:44:25 +00:00
|
|
|
}
|
|
|
|
|
2018-10-07 20:59:02 +00:00
|
|
|
const byTag: Map<string, vscode.Range[]> = new Map();
|
2019-07-19 11:43:36 +00:00
|
|
|
const colorfulIdents: Map<
|
|
|
|
string,
|
|
|
|
[vscode.Range[], boolean]
|
|
|
|
> = new Map();
|
2019-05-27 09:26:15 +00:00
|
|
|
const rainbowTime = Server.config.rainbowHighlightingOn;
|
2019-05-23 17:42:42 +00:00
|
|
|
|
2018-10-08 18:18:55 +00:00
|
|
|
for (const tag of this.decorations.keys()) {
|
2018-10-07 20:59:02 +00:00
|
|
|
byTag.set(tag, []);
|
2018-10-07 20:44:25 +00:00
|
|
|
}
|
|
|
|
|
2018-10-07 20:59:02 +00:00
|
|
|
for (const d of highlights) {
|
2018-10-07 20:44:25 +00:00
|
|
|
if (!byTag.get(d.tag)) {
|
2018-10-07 20:59:02 +00:00
|
|
|
continue;
|
2018-10-07 20:44:25 +00:00
|
|
|
}
|
2019-05-23 17:42:42 +00:00
|
|
|
|
2019-05-27 09:26:15 +00:00
|
|
|
if (rainbowTime && d.bindingHash) {
|
|
|
|
if (!colorfulIdents.has(d.bindingHash)) {
|
2019-07-19 11:43:36 +00:00
|
|
|
const mut = d.tag.endsWith('.mut');
|
|
|
|
colorfulIdents.set(d.bindingHash, [[], mut]);
|
2019-05-23 17:42:42 +00:00
|
|
|
}
|
|
|
|
colorfulIdents
|
2019-07-19 11:43:36 +00:00
|
|
|
.get(d.bindingHash)![0]
|
2019-05-23 17:42:42 +00:00
|
|
|
.push(
|
|
|
|
Server.client.protocol2CodeConverter.asRange(d.range)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
byTag
|
|
|
|
.get(d.tag)!
|
|
|
|
.push(
|
|
|
|
Server.client.protocol2CodeConverter.asRange(d.range)
|
|
|
|
);
|
|
|
|
}
|
2018-10-07 20:44:25 +00:00
|
|
|
}
|
|
|
|
|
2018-10-07 20:59:02 +00:00
|
|
|
for (const tag of byTag.keys()) {
|
2018-10-08 21:38:33 +00:00
|
|
|
const dec = this.decorations.get(
|
|
|
|
tag
|
|
|
|
) as vscode.TextEditorDecorationType;
|
2018-10-07 20:59:02 +00:00
|
|
|
const ranges = byTag.get(tag)!;
|
2019-10-24 15:25:23 +00:00
|
|
|
|
2018-10-07 20:59:02 +00:00
|
|
|
editor.setDecorations(dec, ranges);
|
2018-10-07 20:44:25 +00:00
|
|
|
}
|
2019-05-23 17:42:42 +00:00
|
|
|
|
2019-07-19 11:43:36 +00:00
|
|
|
for (const [hash, [ranges, mut]] of colorfulIdents.entries()) {
|
|
|
|
const textDecoration = mut ? 'underline' : undefined;
|
2019-05-23 17:42:42 +00:00
|
|
|
const dec = vscode.window.createTextEditorDecorationType({
|
2019-07-19 11:43:36 +00:00
|
|
|
light: { color: fancify(hash, 'light'), textDecoration },
|
|
|
|
dark: { color: fancify(hash, 'dark'), textDecoration }
|
2019-05-23 17:42:42 +00:00
|
|
|
});
|
|
|
|
editor.setDecorations(dec, ranges);
|
|
|
|
}
|
2018-10-07 20:44:25 +00:00
|
|
|
}
|
|
|
|
}
|