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'; throw 'unreachable';
} }
onNotification(method: string, handler: lc.GenericNotificationHandler) {
this.client.onReady()
.then(() => this.client.onNotification(method, handler))
}
} }
export type Cmd = (...args: any[]) => any; export type Cmd = (...args: any[]) => any;

View file

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

View file

@ -1,9 +1,8 @@
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import * as lc from 'vscode-languageclient';
import * as commands from './commands'; import * as commands from './commands';
import { activateInlayHints } from './inlay_hints'; import { activateInlayHints } from './inlay_hints';
import { StatusDisplay } from './status_display'; import { activateStatusDisplay } from './status_display';
import { Server } from './server'; import { Server } from './server';
import { Ctx } from './ctx'; import { Ctx } from './ctx';
import { activateHighlighting } from './highlighting'; import { activateHighlighting } from './highlighting';
@ -32,18 +31,7 @@ export async function activate(context: vscode.ExtensionContext) {
ctx.overrideCommand('type', commands.onEnter); ctx.overrideCommand('type', commands.onEnter);
} }
const watchStatus = new StatusDisplay(ctx.config.cargoWatchOptions.command); const startServer = () => Server.start();
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 reloadCommand = () => reloadServer(startServer); const reloadCommand = () => reloadServer(startServer);
vscode.commands.registerCommand('rust-analyzer.reload', reloadCommand); vscode.commands.registerCommand('rust-analyzer.reload', reloadCommand);
@ -55,6 +43,7 @@ export async function activate(context: vscode.ExtensionContext) {
vscode.window.showErrorMessage(e.message); vscode.window.showErrorMessage(e.message);
} }
activateStatusDisplay(ctx);
activateHighlighting(ctx); activateHighlighting(ctx);
if (ctx.config.displayInlayHints) { if (ctx.config.displayInlayHints) {

View file

@ -15,9 +15,7 @@ export class Server {
public static config = new Config(); public static config = new Config();
public static client: lc.LanguageClient; public static client: lc.LanguageClient;
public static async start( public static async start() {
notificationHandlers: Iterable<[string, lc.GenericNotificationHandler]>,
) {
// '.' Is the fallback if no folder is open // '.' 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. // 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 = '.'; let folder: string = '.';
@ -92,11 +90,6 @@ export class Server {
}, },
}; };
Server.client.registerProposedFeatures(); Server.client.registerProposedFeatures();
Server.client.onReady().then(() => {
for (const [type, handler] of notificationHandlers) {
Server.client.onNotification(type, handler);
}
});
Server.client.start(); Server.client.start();
} }
} }

View file

@ -1,8 +1,16 @@
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import { Ctx } from './ctx';
const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']; 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; packageName?: string;
private i = 0; private i = 0;