mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
Start new ctx module
This commit is contained in:
parent
9cad88dd95
commit
e53ccb6e99
4 changed files with 63 additions and 20 deletions
|
@ -1,19 +1,19 @@
|
||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
import { Server } from '../server';
|
import { Ctx } from '../ctx';
|
||||||
// Shows status of rust-analyzer (for debugging)
|
// Shows status of rust-analyzer (for debugging)
|
||||||
|
|
||||||
export function makeCommand(context: vscode.ExtensionContext) {
|
export function analyzerStatus(ctx: Ctx) {
|
||||||
let poller: NodeJS.Timer | null = null;
|
let poller: NodeJS.Timer | null = null;
|
||||||
const tdcp = new TextDocumentContentProvider();
|
const tdcp = new TextDocumentContentProvider(ctx);
|
||||||
|
|
||||||
context.subscriptions.push(
|
ctx.pushCleanup(
|
||||||
vscode.workspace.registerTextDocumentContentProvider(
|
vscode.workspace.registerTextDocumentContentProvider(
|
||||||
'rust-analyzer-status',
|
'rust-analyzer-status',
|
||||||
tdcp,
|
tdcp,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
context.subscriptions.push({
|
ctx.pushCleanup({
|
||||||
dispose() {
|
dispose() {
|
||||||
if (poller != null) {
|
if (poller != null) {
|
||||||
clearInterval(poller);
|
clearInterval(poller);
|
||||||
|
@ -39,9 +39,16 @@ export function makeCommand(context: vscode.ExtensionContext) {
|
||||||
|
|
||||||
class TextDocumentContentProvider
|
class TextDocumentContentProvider
|
||||||
implements vscode.TextDocumentContentProvider {
|
implements vscode.TextDocumentContentProvider {
|
||||||
|
|
||||||
uri = vscode.Uri.parse('rust-analyzer-status://status');
|
uri = vscode.Uri.parse('rust-analyzer-status://status');
|
||||||
eventEmitter = new vscode.EventEmitter<vscode.Uri>();
|
eventEmitter = new vscode.EventEmitter<vscode.Uri>();
|
||||||
|
|
||||||
|
ctx: Ctx
|
||||||
|
|
||||||
|
constructor(ctx: Ctx) {
|
||||||
|
this.ctx = ctx
|
||||||
|
}
|
||||||
|
|
||||||
provideTextDocumentContent(
|
provideTextDocumentContent(
|
||||||
_uri: vscode.Uri,
|
_uri: vscode.Uri,
|
||||||
): vscode.ProviderResult<string> {
|
): vscode.ProviderResult<string> {
|
||||||
|
@ -49,7 +56,7 @@ class TextDocumentContentProvider
|
||||||
if (editor == null) {
|
if (editor == null) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
return Server.client.sendRequest<string>(
|
return this.ctx.client.sendRequest<string>(
|
||||||
'rust-analyzer/analyzerStatus',
|
'rust-analyzer/analyzerStatus',
|
||||||
null,
|
null,
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import * as analyzerStatus from './analyzer_status';
|
import { analyzerStatus } from './analyzer_status';
|
||||||
import * as applySourceChange from './apply_source_change';
|
import * as applySourceChange from './apply_source_change';
|
||||||
import * as expandMacro from './expand_macro';
|
import * as expandMacro from './expand_macro';
|
||||||
import * as inlayHints from './inlay_hints';
|
import * as inlayHints from './inlay_hints';
|
||||||
|
|
30
editors/code/src/ctx.ts
Normal file
30
editors/code/src/ctx.ts
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
import * as vscode from 'vscode';
|
||||||
|
import * as lc from 'vscode-languageclient';
|
||||||
|
import { Server } from './server';
|
||||||
|
|
||||||
|
|
||||||
|
export class Ctx {
|
||||||
|
private extCtx: vscode.ExtensionContext
|
||||||
|
|
||||||
|
constructor(extCtx: vscode.ExtensionContext) {
|
||||||
|
this.extCtx = extCtx
|
||||||
|
}
|
||||||
|
|
||||||
|
get client(): lc.LanguageClient {
|
||||||
|
return Server.client
|
||||||
|
}
|
||||||
|
|
||||||
|
registerCommand(
|
||||||
|
name: string,
|
||||||
|
factory: (ctx: Ctx) => () => Promise<vscode.TextEditor>,
|
||||||
|
) {
|
||||||
|
const fullName = `rust-analyzer.${name}`
|
||||||
|
const cmd = factory(this);
|
||||||
|
const d = vscode.commands.registerCommand(fullName, cmd);
|
||||||
|
this.pushCleanup(d);
|
||||||
|
}
|
||||||
|
|
||||||
|
pushCleanup(d: { dispose(): any }) {
|
||||||
|
this.extCtx.subscriptions.push(d)
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,8 +9,18 @@ import { StatusDisplay } from './commands/watch_status';
|
||||||
import * as events from './events';
|
import * as events from './events';
|
||||||
import * as notifications from './notifications';
|
import * as notifications from './notifications';
|
||||||
import { Server } from './server';
|
import { Server } from './server';
|
||||||
|
import { Ctx } from './ctx'
|
||||||
|
|
||||||
|
let ctx!: Ctx;
|
||||||
|
|
||||||
export async function activate(context: vscode.ExtensionContext) {
|
export async function activate(context: vscode.ExtensionContext) {
|
||||||
|
ctx = new Ctx(context);
|
||||||
|
ctx.registerCommand(
|
||||||
|
'analyzerStatus',
|
||||||
|
commands.analyzerStatus
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
function disposeOnDeactivation(disposable: vscode.Disposable) {
|
function disposeOnDeactivation(disposable: vscode.Disposable) {
|
||||||
context.subscriptions.push(disposable);
|
context.subscriptions.push(disposable);
|
||||||
}
|
}
|
||||||
|
@ -48,10 +58,6 @@ export async function activate(context: vscode.ExtensionContext) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Commands are requests from vscode to the language server
|
// Commands are requests from vscode to the language server
|
||||||
registerCommand(
|
|
||||||
'rust-analyzer.analyzerStatus',
|
|
||||||
commands.analyzerStatus.makeCommand(context),
|
|
||||||
);
|
|
||||||
registerCommand('rust-analyzer.collectGarbage', () =>
|
registerCommand('rust-analyzer.collectGarbage', () =>
|
||||||
Server.client.sendRequest<null>('rust-analyzer/collectGarbage', null),
|
Server.client.sendRequest<null>('rust-analyzer/collectGarbage', null),
|
||||||
);
|
);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue