mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
Rearrange code
This commit is contained in:
parent
44d6ab2650
commit
8346bdc04d
2 changed files with 106 additions and 107 deletions
|
@ -65,7 +65,7 @@ export class Ctx {
|
|||
async sendRequestWithRetry<R>(
|
||||
method: string,
|
||||
param: any,
|
||||
token: vscode.CancellationToken,
|
||||
token?: vscode.CancellationToken,
|
||||
): Promise<R> {
|
||||
await this.client.onReady();
|
||||
for (const delay of [2, 4, 6, 8, 10, null]) {
|
||||
|
|
|
@ -47,7 +47,7 @@ export function activateHighlighting(ctx: Ctx) {
|
|||
const params: lc.TextDocumentIdentifier = {
|
||||
uri: editor.document.uri.toString(),
|
||||
};
|
||||
const decorations = await ctx.client.sendRequest<Decoration[]>(
|
||||
const decorations = await ctx.sendRequestWithRetry<Decoration[]>(
|
||||
'rust-analyzer/decorationsRequest',
|
||||
params,
|
||||
);
|
||||
|
@ -62,7 +62,7 @@ interface PublishDecorationsParams {
|
|||
decorations: Decoration[];
|
||||
}
|
||||
|
||||
export interface Decoration {
|
||||
interface Decoration {
|
||||
range: lc.Range;
|
||||
tag: string;
|
||||
bindingHash?: string;
|
||||
|
@ -81,52 +81,97 @@ function fancify(seed: string, shade: 'light' | 'dark') {
|
|||
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 {
|
||||
private ctx: Ctx;
|
||||
private decorations: Map<
|
||||
string,
|
||||
vscode.TextEditorDecorationType
|
||||
> | null = null;
|
||||
|
||||
constructor(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,
|
||||
vscode.TextEditorDecorationType
|
||||
> {
|
||||
> {
|
||||
const decoration = (
|
||||
tag: string,
|
||||
textDecoration?: string,
|
||||
|
@ -184,85 +229,39 @@ class Highlighter {
|
|||
];
|
||||
|
||||
return new Map<string, vscode.TextEditorDecorationType>(decorations);
|
||||
}
|
||||
|
||||
private decorations: Map<
|
||||
string,
|
||||
vscode.TextEditorDecorationType
|
||||
> | null = null;
|
||||
|
||||
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 = Highlighter.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 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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue