mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
vscode: minor refactorings
This commit is contained in:
parent
31ae646448
commit
bd113623a0
3 changed files with 28 additions and 30 deletions
|
@ -44,7 +44,6 @@ export class Config {
|
||||||
this.refreshConfig();
|
this.refreshConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private refreshConfig() {
|
private refreshConfig() {
|
||||||
this.cfg = vscode.workspace.getConfiguration(Config.rootSection);
|
this.cfg = vscode.workspace.getConfiguration(Config.rootSection);
|
||||||
console.log("Using configuration:", this.cfg);
|
console.log("Using configuration:", this.cfg);
|
||||||
|
|
|
@ -91,15 +91,11 @@ export async function sendRequestWithRetry<R>(
|
||||||
for (const delay of [2, 4, 6, 8, 10, null]) {
|
for (const delay of [2, 4, 6, 8, 10, null]) {
|
||||||
try {
|
try {
|
||||||
return await (token ? client.sendRequest(method, param, token) : client.sendRequest(method, param));
|
return await (token ? client.sendRequest(method, param, token) : client.sendRequest(method, param));
|
||||||
} catch (e) {
|
} catch (err) {
|
||||||
if (
|
if (delay === null || err.code !== lc.ErrorCodes.ContentModified) {
|
||||||
e.code === lc.ErrorCodes.ContentModified &&
|
throw err;
|
||||||
delay !== null
|
|
||||||
) {
|
|
||||||
await sleep(10 * (1 << delay));
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
throw e;
|
await sleep(10 * (1 << delay));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw 'unreachable';
|
throw 'unreachable';
|
||||||
|
|
|
@ -38,7 +38,7 @@ interface InlayHintsParams {
|
||||||
|
|
||||||
interface InlayHint {
|
interface InlayHint {
|
||||||
range: vscode.Range;
|
range: vscode.Range;
|
||||||
kind: string;
|
kind: "TypeHint" | "ParameterHint";
|
||||||
label: string;
|
label: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,7 +55,7 @@ const parameterHintDecorationType = vscode.window.createTextEditorDecorationType
|
||||||
});
|
});
|
||||||
|
|
||||||
class HintsUpdater {
|
class HintsUpdater {
|
||||||
private pending: Map<string, vscode.CancellationTokenSource> = new Map();
|
private pending = new Map<string, vscode.CancellationTokenSource>();
|
||||||
private ctx: Ctx;
|
private ctx: Ctx;
|
||||||
private enabled: boolean;
|
private enabled: boolean;
|
||||||
|
|
||||||
|
@ -64,30 +64,36 @@ class HintsUpdater {
|
||||||
this.enabled = ctx.config.displayInlayHints;
|
this.enabled = ctx.config.displayInlayHints;
|
||||||
}
|
}
|
||||||
|
|
||||||
async setEnabled(enabled: boolean) {
|
async setEnabled(enabled: boolean): Promise<void> {
|
||||||
if (this.enabled == enabled) return;
|
if (this.enabled == enabled) return;
|
||||||
this.enabled = enabled;
|
this.enabled = enabled;
|
||||||
|
|
||||||
if (this.enabled) {
|
if (this.enabled) {
|
||||||
await this.refresh();
|
return await this.refresh();
|
||||||
} else {
|
|
||||||
this.allEditors.forEach(it => {
|
|
||||||
this.setTypeDecorations(it, []);
|
|
||||||
this.setParameterDecorations(it, []);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
this.allEditors.forEach(it => {
|
||||||
|
this.setTypeDecorations(it, []);
|
||||||
|
this.setParameterDecorations(it, []);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async refresh() {
|
async refresh() {
|
||||||
if (!this.enabled) return;
|
if (!this.enabled) return;
|
||||||
const promises = this.allEditors.map(it => this.refreshEditor(it));
|
await Promise.all(this.allEditors.map(it => this.refreshEditor(it)));
|
||||||
await Promise.all(promises);
|
}
|
||||||
|
|
||||||
|
private get allEditors(): vscode.TextEditor[] {
|
||||||
|
return vscode.window.visibleTextEditors.filter(
|
||||||
|
editor => editor.document.languageId === 'rust',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async refreshEditor(editor: vscode.TextEditor): Promise<void> {
|
private async refreshEditor(editor: vscode.TextEditor): Promise<void> {
|
||||||
const newHints = await this.queryHints(editor.document.uri.toString());
|
const newHints = await this.queryHints(editor.document.uri.toString());
|
||||||
if (newHints == null) return;
|
if (newHints == null) return;
|
||||||
const newTypeDecorations = newHints.filter(hint => hint.kind === 'TypeHint')
|
|
||||||
|
const newTypeDecorations = newHints
|
||||||
|
.filter(hint => hint.kind === 'TypeHint')
|
||||||
.map(hint => ({
|
.map(hint => ({
|
||||||
range: hint.range,
|
range: hint.range,
|
||||||
renderOptions: {
|
renderOptions: {
|
||||||
|
@ -98,7 +104,8 @@ class HintsUpdater {
|
||||||
}));
|
}));
|
||||||
this.setTypeDecorations(editor, newTypeDecorations);
|
this.setTypeDecorations(editor, newTypeDecorations);
|
||||||
|
|
||||||
const newParameterDecorations = newHints.filter(hint => hint.kind === 'ParameterHint')
|
const newParameterDecorations = newHints
|
||||||
|
.filter(hint => hint.kind === 'ParameterHint')
|
||||||
.map(hint => ({
|
.map(hint => ({
|
||||||
range: hint.range,
|
range: hint.range,
|
||||||
renderOptions: {
|
renderOptions: {
|
||||||
|
@ -110,12 +117,6 @@ class HintsUpdater {
|
||||||
this.setParameterDecorations(editor, newParameterDecorations);
|
this.setParameterDecorations(editor, newParameterDecorations);
|
||||||
}
|
}
|
||||||
|
|
||||||
private get allEditors(): vscode.TextEditor[] {
|
|
||||||
return vscode.window.visibleTextEditors.filter(
|
|
||||||
editor => editor.document.languageId === 'rust',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private setTypeDecorations(
|
private setTypeDecorations(
|
||||||
editor: vscode.TextEditor,
|
editor: vscode.TextEditor,
|
||||||
decorations: vscode.DecorationOptions[],
|
decorations: vscode.DecorationOptions[],
|
||||||
|
@ -139,12 +140,14 @@ class HintsUpdater {
|
||||||
private async queryHints(documentUri: string): Promise<InlayHint[] | null> {
|
private async queryHints(documentUri: string): Promise<InlayHint[] | null> {
|
||||||
const client = this.ctx.client;
|
const client = this.ctx.client;
|
||||||
if (!client) return null;
|
if (!client) return null;
|
||||||
|
|
||||||
const request: InlayHintsParams = {
|
const request: InlayHintsParams = {
|
||||||
textDocument: { uri: documentUri },
|
textDocument: { uri: documentUri },
|
||||||
};
|
};
|
||||||
const tokenSource = new vscode.CancellationTokenSource();
|
const tokenSource = new vscode.CancellationTokenSource();
|
||||||
const prev = this.pending.get(documentUri);
|
const prevHintsRequest = this.pending.get(documentUri);
|
||||||
if (prev) prev.cancel();
|
prevHintsRequest?.cancel();
|
||||||
|
|
||||||
this.pending.set(documentUri, tokenSource);
|
this.pending.set(documentUri, tokenSource);
|
||||||
try {
|
try {
|
||||||
return await sendRequestWithRetry<InlayHint[] | null>(
|
return await sendRequestWithRetry<InlayHint[] | null>(
|
||||||
|
|
Loading…
Reference in a new issue