about summary refs log tree commit diff
path: root/editors/code
diff options
context:
space:
mode:
authorAleksey Kladov <aleksey.kladov@gmail.com>2019-12-30 14:42:59 +0100
committerAleksey Kladov <aleksey.kladov@gmail.com>2019-12-30 14:42:59 +0100
commite53ccb6e99bb0e92ebea19f150c8fbf9b6958634 (patch)
treeac0f868655e52002f8a40015d094e0d55effa764 /editors/code
parent9cad88dd95773f9ede6233fd7d0f3a076c5cda61 (diff)
downloadrust-e53ccb6e99bb0e92ebea19f150c8fbf9b6958634.tar.gz
rust-e53ccb6e99bb0e92ebea19f150c8fbf9b6958634.zip
Start new ctx module
Diffstat (limited to 'editors/code')
-rw-r--r--editors/code/src/commands/analyzer_status.ts19
-rw-r--r--editors/code/src/commands/index.ts2
-rw-r--r--editors/code/src/ctx.ts30
-rw-r--r--editors/code/src/main.ts32
4 files changed, 63 insertions, 20 deletions
diff --git a/editors/code/src/commands/analyzer_status.ts b/editors/code/src/commands/analyzer_status.ts
index 5840e8fc082..6e92c50ef1e 100644
--- a/editors/code/src/commands/analyzer_status.ts
+++ b/editors/code/src/commands/analyzer_status.ts
@@ -1,19 +1,19 @@
 import * as vscode from 'vscode';
-import { Server } from '../server';
+import { Ctx } from '../ctx';
 // Shows status of rust-analyzer (for debugging)
 
-export function makeCommand(context: vscode.ExtensionContext) {
+export function analyzerStatus(ctx: Ctx) {
     let poller: NodeJS.Timer | null = null;
-    const tdcp = new TextDocumentContentProvider();
+    const tdcp = new TextDocumentContentProvider(ctx);
 
-    context.subscriptions.push(
+    ctx.pushCleanup(
         vscode.workspace.registerTextDocumentContentProvider(
             'rust-analyzer-status',
             tdcp,
         ),
     );
 
-    context.subscriptions.push({
+    ctx.pushCleanup({
         dispose() {
             if (poller != null) {
                 clearInterval(poller);
@@ -39,9 +39,16 @@ export function makeCommand(context: vscode.ExtensionContext) {
 
 class TextDocumentContentProvider
     implements vscode.TextDocumentContentProvider {
+
     uri = vscode.Uri.parse('rust-analyzer-status://status');
     eventEmitter = new vscode.EventEmitter<vscode.Uri>();
 
+    ctx: Ctx
+
+    constructor(ctx: Ctx) {
+        this.ctx = ctx
+    }
+
     provideTextDocumentContent(
         _uri: vscode.Uri,
     ): vscode.ProviderResult<string> {
@@ -49,7 +56,7 @@ class TextDocumentContentProvider
         if (editor == null) {
             return '';
         }
-        return Server.client.sendRequest<string>(
+        return this.ctx.client.sendRequest<string>(
             'rust-analyzer/analyzerStatus',
             null,
         );
diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts
index 13a696758b4..ec1995396dc 100644
--- a/editors/code/src/commands/index.ts
+++ b/editors/code/src/commands/index.ts
@@ -1,4 +1,4 @@
-import * as analyzerStatus from './analyzer_status';
+import { analyzerStatus } from './analyzer_status';
 import * as applySourceChange from './apply_source_change';
 import * as expandMacro from './expand_macro';
 import * as inlayHints from './inlay_hints';
diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts
new file mode 100644
index 00000000000..8581667b4e3
--- /dev/null
+++ b/editors/code/src/ctx.ts
@@ -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)
+    }
+}
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts
index 1da10ebd06f..048b9bbd424 100644
--- a/editors/code/src/main.ts
+++ b/editors/code/src/main.ts
@@ -9,8 +9,18 @@ import { StatusDisplay } from './commands/watch_status';
 import * as events from './events';
 import * as notifications from './notifications';
 import { Server } from './server';
+import { Ctx } from './ctx'
+
+let ctx!: Ctx;
 
 export async function activate(context: vscode.ExtensionContext) {
+    ctx = new Ctx(context);
+    ctx.registerCommand(
+        'analyzerStatus',
+        commands.analyzerStatus
+    );
+
+
     function disposeOnDeactivation(disposable: vscode.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
-    registerCommand(
-        'rust-analyzer.analyzerStatus',
-        commands.analyzerStatus.makeCommand(context),
-    );
     registerCommand('rust-analyzer.collectGarbage', () =>
         Server.client.sendRequest<null>('rust-analyzer/collectGarbage', null),
     );
@@ -94,15 +100,15 @@ export async function activate(context: vscode.ExtensionContext) {
         string,
         lc.GenericNotificationHandler,
     ]> = [
-        [
-            'rust-analyzer/publishDecorations',
-            notifications.publishDecorations.handle,
-        ],
-        [
-            '$/progress',
-            params => watchStatus.handleProgressNotification(params),
-        ],
-    ];
+            [
+                'rust-analyzer/publishDecorations',
+                notifications.publishDecorations.handle,
+            ],
+            [
+                '$/progress',
+                params => watchStatus.handleProgressNotification(params),
+            ],
+        ];
     const syntaxTreeContentProvider = new SyntaxTreeContentProvider();
     const expandMacroContentProvider = new ExpandMacroContentProvider();