Refactor status activation

This commit is contained in:
Aleksey Kladov 2019-12-31 17:22:43 +01:00
parent 1327aed7f6
commit e4b588868f
5 changed files with 36 additions and 43 deletions

View file

@ -84,6 +84,11 @@ export class Ctx {
}
throw 'unreachable';
}
onNotification(method: string, handler: lc.GenericNotificationHandler) {
this.client.onReady()
.then(() => this.client.onNotification(method, handler))
}
}
export type Cmd = (...args: any[]) => any;

View file

@ -10,28 +10,26 @@ import { Ctx } from './ctx';
export function activateHighlighting(ctx: Ctx) {
const highlighter = new Highlighter(ctx);
ctx.client.onReady().then(() => {
ctx.client.onNotification(
'rust-analyzer/publishDecorations',
(params: PublishDecorationsParams) => {
if (!ctx.config.highlightingOn) return;
ctx.onNotification(
'rust-analyzer/publishDecorations',
(params: PublishDecorationsParams) => {
if (!ctx.config.highlightingOn) return;
const targetEditor = vscode.window.visibleTextEditors.find(
editor => {
const unescapedUri = unescape(
editor.document.uri.toString(),
);
// Unescaped URI looks like:
// file:///c:/Workspace/ra-test/src/main.rs
return unescapedUri === params.uri;
},
);
if (!targetEditor) return;
const targetEditor = vscode.window.visibleTextEditors.find(
editor => {
const unescapedUri = unescape(
editor.document.uri.toString(),
);
// Unescaped URI looks like:
// file:///c:/Workspace/ra-test/src/main.rs
return unescapedUri === params.uri;
},
);
if (!targetEditor) return;
highlighter.setHighlights(targetEditor, params.decorations);
},
);
});
highlighter.setHighlights(targetEditor, params.decorations);
},
);
vscode.workspace.onDidChangeConfiguration(
_ => highlighter.removeHighlights(),

View file

@ -1,9 +1,8 @@
import * as vscode from 'vscode';
import * as lc from 'vscode-languageclient';
import * as commands from './commands';
import { activateInlayHints } from './inlay_hints';
import { StatusDisplay } from './status_display';
import { activateStatusDisplay } from './status_display';
import { Server } from './server';
import { Ctx } from './ctx';
import { activateHighlighting } from './highlighting';
@ -32,18 +31,7 @@ export async function activate(context: vscode.ExtensionContext) {
ctx.overrideCommand('type', commands.onEnter);
}
const watchStatus = new StatusDisplay(ctx.config.cargoWatchOptions.command);
ctx.pushCleanup(watchStatus);
// Notifications are events triggered by the language server
const allNotifications: [string, lc.GenericNotificationHandler][] = [
[
'$/progress',
params => watchStatus.handleProgressNotification(params),
],
];
const startServer = () => Server.start(allNotifications);
const startServer = () => Server.start();
const reloadCommand = () => reloadServer(startServer);
vscode.commands.registerCommand('rust-analyzer.reload', reloadCommand);
@ -55,6 +43,7 @@ export async function activate(context: vscode.ExtensionContext) {
vscode.window.showErrorMessage(e.message);
}
activateStatusDisplay(ctx);
activateHighlighting(ctx);
if (ctx.config.displayInlayHints) {

View file

@ -15,9 +15,7 @@ export class Server {
public static config = new Config();
public static client: lc.LanguageClient;
public static async start(
notificationHandlers: Iterable<[string, lc.GenericNotificationHandler]>,
) {
public static async start() {
// '.' Is the fallback if no folder is open
// TODO?: Workspace folders support Uri's (eg: file://test.txt). It might be a good idea to test if the uri points to a file.
let folder: string = '.';
@ -92,11 +90,6 @@ export class Server {
},
};
Server.client.registerProposedFeatures();
Server.client.onReady().then(() => {
for (const [type, handler] of notificationHandlers) {
Server.client.onNotification(type, handler);
}
});
Server.client.start();
}
}

View file

@ -1,8 +1,16 @@
import * as vscode from 'vscode';
import { Ctx } from './ctx';
const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
export class StatusDisplay implements vscode.Disposable {
export function activateStatusDisplay(ctx: Ctx) {
const statusDisplay = new StatusDisplay(ctx.config.cargoWatchOptions.command);
ctx.pushCleanup(statusDisplay);
ctx.onNotification('$/progress', params => statusDisplay.handleProgressNotification(params));
}
class StatusDisplay implements vscode.Disposable {
packageName?: string;
private i = 0;