Rearrange code

This commit is contained in:
Aleksey Kladov 2019-12-31 02:17:50 +01:00
parent 44d6ab2650
commit 8346bdc04d
2 changed files with 106 additions and 107 deletions

View file

@ -65,7 +65,7 @@ export class Ctx {
async sendRequestWithRetry<R>( async sendRequestWithRetry<R>(
method: string, method: string,
param: any, param: any,
token: vscode.CancellationToken, token?: vscode.CancellationToken,
): Promise<R> { ): Promise<R> {
await this.client.onReady(); await this.client.onReady();
for (const delay of [2, 4, 6, 8, 10, null]) { for (const delay of [2, 4, 6, 8, 10, null]) {

View file

@ -47,7 +47,7 @@ export function activateHighlighting(ctx: Ctx) {
const params: lc.TextDocumentIdentifier = { const params: lc.TextDocumentIdentifier = {
uri: editor.document.uri.toString(), uri: editor.document.uri.toString(),
}; };
const decorations = await ctx.client.sendRequest<Decoration[]>( const decorations = await ctx.sendRequestWithRetry<Decoration[]>(
'rust-analyzer/decorationsRequest', 'rust-analyzer/decorationsRequest',
params, params,
); );
@ -62,7 +62,7 @@ interface PublishDecorationsParams {
decorations: Decoration[]; decorations: Decoration[];
} }
export interface Decoration { interface Decoration {
range: lc.Range; range: lc.Range;
tag: string; tag: string;
bindingHash?: string; bindingHash?: string;
@ -81,52 +81,97 @@ function fancify(seed: string, shade: 'light' | 'dark') {
return `hsl(${h},${s}%,${l}%)`; return `hsl(${h},${s}%,${l}%)`;
} }
function createDecorationFromTextmate(
themeStyle: scopes.TextMateRuleSettings,
): vscode.TextEditorDecorationType {
const decorationOptions: vscode.DecorationRenderOptions = {};
decorationOptions.rangeBehavior = vscode.DecorationRangeBehavior.OpenOpen;
if (themeStyle.foreground) {
decorationOptions.color = themeStyle.foreground;
}
if (themeStyle.background) {
decorationOptions.backgroundColor = themeStyle.background;
}
if (themeStyle.fontStyle) {
const parts: string[] = themeStyle.fontStyle.split(' ');
parts.forEach(part => {
switch (part) {
case 'italic':
decorationOptions.fontStyle = 'italic';
break;
case 'bold':
decorationOptions.fontWeight = 'bold';
break;
case 'underline':
decorationOptions.textDecoration = 'underline';
break;
default:
break;
}
});
}
return vscode.window.createTextEditorDecorationType(decorationOptions);
}
class Highlighter { class Highlighter {
private ctx: Ctx; private ctx: Ctx;
private decorations: Map<
string,
vscode.TextEditorDecorationType
> | null = null;
constructor(ctx: Ctx) { constructor(ctx: Ctx) {
this.ctx = ctx; this.ctx = ctx;
} }
private static initDecorations(): Map< public removeHighlights() {
if (this.decorations == null) {
return;
}
// Decorations are removed when the object is disposed
for (const decoration of this.decorations.values()) {
decoration.dispose();
}
this.decorations = null;
}
public setHighlights(editor: vscode.TextEditor, highlights: Decoration[]) {
// Initialize decorations if necessary
//
// Note: decoration objects need to be kept around so we can dispose them
// if the user disables syntax highlighting
if (this.decorations == null) {
this.decorations = initDecorations();
}
const byTag: Map<string, vscode.Range[]> = new Map();
const colorfulIdents: Map<
string,
[vscode.Range[], boolean]
> = new Map();
const rainbowTime = this.ctx.config.rainbowHighlightingOn;
for (const tag of this.decorations.keys()) {
byTag.set(tag, []);
}
for (const d of highlights) {
if (!byTag.get(d.tag)) {
continue;
}
if (rainbowTime && d.bindingHash) {
if (!colorfulIdents.has(d.bindingHash)) {
const mut = d.tag.endsWith('.mut');
colorfulIdents.set(d.bindingHash, [[], mut]);
}
colorfulIdents
.get(d.bindingHash)![0]
.push(
this.ctx.client.protocol2CodeConverter.asRange(d.range),
);
} else {
byTag
.get(d.tag)!
.push(
this.ctx.client.protocol2CodeConverter.asRange(d.range),
);
}
}
for (const tag of byTag.keys()) {
const dec = this.decorations.get(
tag,
) as vscode.TextEditorDecorationType;
const ranges = byTag.get(tag)!;
editor.setDecorations(dec, ranges);
}
for (const [hash, [ranges, mut]] of colorfulIdents.entries()) {
const textDecoration = mut ? 'underline' : undefined;
const dec = vscode.window.createTextEditorDecorationType({
light: { color: fancify(hash, 'light'), textDecoration },
dark: { color: fancify(hash, 'dark'), textDecoration },
});
editor.setDecorations(dec, ranges);
}
}
}
function initDecorations(): Map<
string, string,
vscode.TextEditorDecorationType vscode.TextEditorDecorationType
> { > {
const decoration = ( const decoration = (
tag: string, tag: string,
textDecoration?: string, textDecoration?: string,
@ -184,85 +229,39 @@ class Highlighter {
]; ];
return new Map<string, vscode.TextEditorDecorationType>(decorations); return new Map<string, vscode.TextEditorDecorationType>(decorations);
} }
private decorations: Map< function createDecorationFromTextmate(
string, themeStyle: scopes.TextMateRuleSettings,
vscode.TextEditorDecorationType ): vscode.TextEditorDecorationType {
> | null = null; const decorationOptions: vscode.DecorationRenderOptions = {};
decorationOptions.rangeBehavior = vscode.DecorationRangeBehavior.OpenOpen;
public removeHighlights() {
if (this.decorations == null) { if (themeStyle.foreground) {
return; decorationOptions.color = themeStyle.foreground;
} }
// Decorations are removed when the object is disposed if (themeStyle.background) {
for (const decoration of this.decorations.values()) { decorationOptions.backgroundColor = themeStyle.background;
decoration.dispose(); }
}
if (themeStyle.fontStyle) {
this.decorations = null; const parts: string[] = themeStyle.fontStyle.split(' ');
} parts.forEach(part => {
switch (part) {
public setHighlights(editor: vscode.TextEditor, highlights: Decoration[]) { case 'italic':
// Initialize decorations if necessary decorationOptions.fontStyle = 'italic';
// break;
// Note: decoration objects need to be kept around so we can dispose them case 'bold':
// if the user disables syntax highlighting decorationOptions.fontWeight = 'bold';
if (this.decorations == null) { break;
this.decorations = Highlighter.initDecorations(); case 'underline':
} decorationOptions.textDecoration = 'underline';
break;
const byTag: Map<string, vscode.Range[]> = new Map(); default:
const colorfulIdents: Map< break;
string, }
[vscode.Range[], boolean] });
> = new Map(); }
const rainbowTime = this.ctx.config.rainbowHighlightingOn; return vscode.window.createTextEditorDecorationType(decorationOptions);
for (const tag of this.decorations.keys()) {
byTag.set(tag, []);
}
for (const d of highlights) {
if (!byTag.get(d.tag)) {
continue;
}
if (rainbowTime && d.bindingHash) {
if (!colorfulIdents.has(d.bindingHash)) {
const mut = d.tag.endsWith('.mut');
colorfulIdents.set(d.bindingHash, [[], mut]);
}
colorfulIdents
.get(d.bindingHash)![0]
.push(
this.ctx.client.protocol2CodeConverter.asRange(d.range),
);
} else {
byTag
.get(d.tag)!
.push(
this.ctx.client.protocol2CodeConverter.asRange(d.range),
);
}
}
for (const tag of byTag.keys()) {
const dec = this.decorations.get(
tag,
) as vscode.TextEditorDecorationType;
const ranges = byTag.get(tag)!;
editor.setDecorations(dec, ranges);
}
for (const [hash, [ranges, mut]] of colorfulIdents.entries()) {
const textDecoration = mut ? 'underline' : undefined;
const dec = vscode.window.createTextEditorDecorationType({
light: { color: fancify(hash, 'light'), textDecoration },
dark: { color: fancify(hash, 'dark'), textDecoration },
});
editor.setDecorations(dec, ranges);
}
}
} }