mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-27 20:35:09 +00:00
parent
fcf15cc05a
commit
dcdbbddd16
6 changed files with 51 additions and 23 deletions
|
@ -51,10 +51,3 @@ export function selectAndApplySourceChange(ctx: Ctx): Cmd {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function reload(ctx: Ctx): Cmd {
|
|
||||||
return async () => {
|
|
||||||
vscode.window.showInformationMessage('Reloading rust-analyzer...');
|
|
||||||
await ctx.restartServer();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
import * as lc from 'vscode-languageclient';
|
import * as lc from 'vscode-languageclient';
|
||||||
|
import { strict as assert } from "assert";
|
||||||
|
|
||||||
import { Config } from './config';
|
import { Config } from './config';
|
||||||
import { createClient } from './client';
|
import { createClient } from './client';
|
||||||
|
@ -16,19 +17,16 @@ export class Ctx {
|
||||||
// on the event loop to get a better picture of what we can do here)
|
// on the event loop to get a better picture of what we can do here)
|
||||||
client: lc.LanguageClient | null = null;
|
client: lc.LanguageClient | null = null;
|
||||||
private extCtx: vscode.ExtensionContext;
|
private extCtx: vscode.ExtensionContext;
|
||||||
private onDidRestartHooks: Array<(client: lc.LanguageClient) => void> = [];
|
private onStartHooks: Array<(client: lc.LanguageClient) => void> = [];
|
||||||
|
|
||||||
constructor(extCtx: vscode.ExtensionContext) {
|
constructor(extCtx: vscode.ExtensionContext) {
|
||||||
this.config = new Config(extCtx);
|
this.config = new Config(extCtx);
|
||||||
this.extCtx = extCtx;
|
this.extCtx = extCtx;
|
||||||
}
|
}
|
||||||
|
|
||||||
async restartServer() {
|
async startServer() {
|
||||||
const old = this.client;
|
assert(this.client == null);
|
||||||
if (old) {
|
|
||||||
await old.stop();
|
|
||||||
}
|
|
||||||
this.client = null;
|
|
||||||
const client = await createClient(this.config);
|
const client = await createClient(this.config);
|
||||||
if (!client) {
|
if (!client) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
|
@ -41,7 +39,7 @@ export class Ctx {
|
||||||
await client.onReady();
|
await client.onReady();
|
||||||
|
|
||||||
this.client = client;
|
this.client = client;
|
||||||
for (const hook of this.onDidRestartHooks) {
|
for (const hook of this.onStartHooks) {
|
||||||
hook(client);
|
hook(client);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -72,8 +70,13 @@ export class Ctx {
|
||||||
this.extCtx.subscriptions.push(d);
|
this.extCtx.subscriptions.push(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
onDidRestart(hook: (client: lc.LanguageClient) => void) {
|
onStart(hook: (client: lc.LanguageClient) => void) {
|
||||||
this.onDidRestartHooks.push(hook);
|
const client = this.client;
|
||||||
|
if (client == null) {
|
||||||
|
this.onStartHooks.push(hook);
|
||||||
|
} else {
|
||||||
|
hook(client)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ import { Ctx, sendRequestWithRetry } from './ctx';
|
||||||
|
|
||||||
export function activateHighlighting(ctx: Ctx) {
|
export function activateHighlighting(ctx: Ctx) {
|
||||||
const highlighter = new Highlighter(ctx);
|
const highlighter = new Highlighter(ctx);
|
||||||
ctx.onDidRestart(client => {
|
ctx.onStart(client => {
|
||||||
client.onNotification(
|
client.onNotification(
|
||||||
'rust-analyzer/publishDecorations',
|
'rust-analyzer/publishDecorations',
|
||||||
(params: PublishDecorationsParams) => {
|
(params: PublishDecorationsParams) => {
|
||||||
|
|
|
@ -27,9 +27,15 @@ export function activateInlayHints(ctx: Ctx) {
|
||||||
ctx.subscriptions
|
ctx.subscriptions
|
||||||
);
|
);
|
||||||
|
|
||||||
|
ctx.pushCleanup({
|
||||||
|
dispose() {
|
||||||
|
hintsUpdater.clear()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
// We pass async function though it will not be awaited when called,
|
// We pass async function though it will not be awaited when called,
|
||||||
// thus Promise rejections won't be handled, but this should never throw in fact...
|
// thus Promise rejections won't be handled, but this should never throw in fact...
|
||||||
ctx.onDidRestart(async _ => hintsUpdater.setEnabled(ctx.config.displayInlayHints));
|
ctx.onStart(async _ => hintsUpdater.setEnabled(ctx.config.displayInlayHints));
|
||||||
}
|
}
|
||||||
|
|
||||||
interface InlayHintsParams {
|
interface InlayHintsParams {
|
||||||
|
@ -61,16 +67,23 @@ class HintsUpdater {
|
||||||
|
|
||||||
constructor(ctx: Ctx) {
|
constructor(ctx: Ctx) {
|
||||||
this.ctx = ctx;
|
this.ctx = ctx;
|
||||||
this.enabled = ctx.config.displayInlayHints;
|
this.enabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
async setEnabled(enabled: boolean): Promise<void> {
|
async setEnabled(enabled: boolean): Promise<void> {
|
||||||
|
console.log({ enabled, prev: this.enabled });
|
||||||
|
|
||||||
if (this.enabled == enabled) return;
|
if (this.enabled == enabled) return;
|
||||||
this.enabled = enabled;
|
this.enabled = enabled;
|
||||||
|
|
||||||
if (this.enabled) {
|
if (this.enabled) {
|
||||||
return await this.refresh();
|
return await this.refresh();
|
||||||
|
} else {
|
||||||
|
return this.clear();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
clear() {
|
||||||
this.allEditors.forEach(it => {
|
this.allEditors.forEach(it => {
|
||||||
this.setTypeDecorations(it, []);
|
this.setTypeDecorations(it, []);
|
||||||
this.setParameterDecorations(it, []);
|
this.setParameterDecorations(it, []);
|
||||||
|
@ -79,6 +92,8 @@ class HintsUpdater {
|
||||||
|
|
||||||
async refresh() {
|
async refresh() {
|
||||||
if (!this.enabled) return;
|
if (!this.enabled) return;
|
||||||
|
console.log("freshin!");
|
||||||
|
|
||||||
await Promise.all(this.allEditors.map(it => this.refreshEditor(it)));
|
await Promise.all(this.allEditors.map(it => this.refreshEditor(it)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,23 @@ let ctx: Ctx | undefined;
|
||||||
export async function activate(context: vscode.ExtensionContext) {
|
export async function activate(context: vscode.ExtensionContext) {
|
||||||
ctx = new Ctx(context);
|
ctx = new Ctx(context);
|
||||||
|
|
||||||
|
ctx.registerCommand('reload', (ctx) => {
|
||||||
|
return async () => {
|
||||||
|
vscode.window.showInformationMessage('Reloading rust-analyzer...');
|
||||||
|
// @DanTup maneuver
|
||||||
|
// https://github.com/microsoft/vscode/issues/45774#issuecomment-373423895
|
||||||
|
await deactivate()
|
||||||
|
for (const sub of ctx.subscriptions) {
|
||||||
|
try {
|
||||||
|
sub.dispose();
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await activate(context)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
// Commands which invokes manually via command palette, shortcut, etc.
|
// Commands which invokes manually via command palette, shortcut, etc.
|
||||||
ctx.registerCommand('analyzerStatus', commands.analyzerStatus);
|
ctx.registerCommand('analyzerStatus', commands.analyzerStatus);
|
||||||
ctx.registerCommand('collectGarbage', commands.collectGarbage);
|
ctx.registerCommand('collectGarbage', commands.collectGarbage);
|
||||||
|
@ -20,7 +37,6 @@ export async function activate(context: vscode.ExtensionContext) {
|
||||||
ctx.registerCommand('syntaxTree', commands.syntaxTree);
|
ctx.registerCommand('syntaxTree', commands.syntaxTree);
|
||||||
ctx.registerCommand('expandMacro', commands.expandMacro);
|
ctx.registerCommand('expandMacro', commands.expandMacro);
|
||||||
ctx.registerCommand('run', commands.run);
|
ctx.registerCommand('run', commands.run);
|
||||||
ctx.registerCommand('reload', commands.reload);
|
|
||||||
ctx.registerCommand('onEnter', commands.onEnter);
|
ctx.registerCommand('onEnter', commands.onEnter);
|
||||||
ctx.registerCommand('ssr', commands.ssr)
|
ctx.registerCommand('ssr', commands.ssr)
|
||||||
|
|
||||||
|
@ -38,7 +54,7 @@ export async function activate(context: vscode.ExtensionContext) {
|
||||||
//
|
//
|
||||||
// This a horribly, horribly wrong way to deal with this problem.
|
// This a horribly, horribly wrong way to deal with this problem.
|
||||||
try {
|
try {
|
||||||
await ctx.restartServer();
|
await ctx.startServer();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
vscode.window.showErrorMessage(e.message);
|
vscode.window.showErrorMessage(e.message);
|
||||||
}
|
}
|
||||||
|
@ -47,4 +63,5 @@ export async function activate(context: vscode.ExtensionContext) {
|
||||||
|
|
||||||
export async function deactivate() {
|
export async function deactivate() {
|
||||||
await ctx?.client?.stop();
|
await ctx?.client?.stop();
|
||||||
|
ctx = undefined;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '
|
||||||
export function activateStatusDisplay(ctx: Ctx) {
|
export function activateStatusDisplay(ctx: Ctx) {
|
||||||
const statusDisplay = new StatusDisplay(ctx.config.cargoWatchOptions.command);
|
const statusDisplay = new StatusDisplay(ctx.config.cargoWatchOptions.command);
|
||||||
ctx.pushCleanup(statusDisplay);
|
ctx.pushCleanup(statusDisplay);
|
||||||
ctx.onDidRestart(client => ctx.pushCleanup(client.onProgress(
|
ctx.onStart(client => ctx.pushCleanup(client.onProgress(
|
||||||
WorkDoneProgress.type,
|
WorkDoneProgress.type,
|
||||||
'rustAnalyzer/cargoWatcher',
|
'rustAnalyzer/cargoWatcher',
|
||||||
params => statusDisplay.handleProgressNotification(params)
|
params => statusDisplay.handleProgressNotification(params)
|
||||||
|
|
Loading…
Reference in a new issue