From e53ccb6e99bb0e92ebea19f150c8fbf9b6958634 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Dec 2019 14:42:59 +0100 Subject: Start new ctx module --- editors/code/src/commands/analyzer_status.ts | 19 +++++++++++++------ editors/code/src/commands/index.ts | 2 +- 2 files changed, 14 insertions(+), 7 deletions(-) (limited to 'editors/code/src/commands') diff --git a/editors/code/src/commands/analyzer_status.ts b/editors/code/src/commands/analyzer_status.ts index 5840e8fc0..6e92c50ef 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(); + ctx: Ctx + + constructor(ctx: Ctx) { + this.ctx = ctx + } + provideTextDocumentContent( _uri: vscode.Uri, ): vscode.ProviderResult { @@ -49,7 +56,7 @@ class TextDocumentContentProvider if (editor == null) { return ''; } - return Server.client.sendRequest( + return this.ctx.client.sendRequest( 'rust-analyzer/analyzerStatus', null, ); diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index 13a696758..ec1995396 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'; -- cgit v1.2.3 From 29e86c0c726c20cf1add94a322b9c147b67ca1f6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Dec 2019 14:53:43 +0100 Subject: More second command to Ctx --- editors/code/src/commands/analyzer_status.ts | 4 ++-- editors/code/src/commands/index.ts | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) (limited to 'editors/code/src/commands') diff --git a/editors/code/src/commands/analyzer_status.ts b/editors/code/src/commands/analyzer_status.ts index 6e92c50ef..c9d32fe07 100644 --- a/editors/code/src/commands/analyzer_status.ts +++ b/editors/code/src/commands/analyzer_status.ts @@ -1,8 +1,8 @@ import * as vscode from 'vscode'; -import { Ctx } from '../ctx'; +import { Ctx, Cmd } from '../ctx'; // Shows status of rust-analyzer (for debugging) -export function analyzerStatus(ctx: Ctx) { +export function analyzerStatus(ctx: Ctx): Cmd { let poller: NodeJS.Timer | null = null; const tdcp = new TextDocumentContentProvider(ctx); diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index ec1995396..ed56f5a4e 100644 --- a/editors/code/src/commands/index.ts +++ b/editors/code/src/commands/index.ts @@ -1,3 +1,5 @@ +import { Ctx, Cmd } from '../ctx' + import { analyzerStatus } from './analyzer_status'; import * as applySourceChange from './apply_source_change'; import * as expandMacro from './expand_macro'; @@ -9,6 +11,10 @@ import * as parentModule from './parent_module'; import * as runnables from './runnables'; import * as syntaxTree from './syntaxTree'; +function collectGarbage(ctx: Ctx): Cmd { + return async () => { ctx.client.sendRequest('rust-analyzer/collectGarbage', null) } +} + export { analyzerStatus, applySourceChange, @@ -20,4 +26,5 @@ export { syntaxTree, onEnter, inlayHints, + collectGarbage }; -- cgit v1.2.3 From 5dd9edaeafde3d5b5975cefe8dc1a65ccd9cd59f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Dec 2019 15:20:13 +0100 Subject: Move matching brace to new Ctx --- editors/code/src/commands/index.ts | 2 +- editors/code/src/commands/matching_brace.ts | 53 ++++++++++++++--------------- 2 files changed, 27 insertions(+), 28 deletions(-) (limited to 'editors/code/src/commands') diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index ed56f5a4e..9d9b9c575 100644 --- a/editors/code/src/commands/index.ts +++ b/editors/code/src/commands/index.ts @@ -1,11 +1,11 @@ import { Ctx, Cmd } from '../ctx' import { analyzerStatus } from './analyzer_status'; +import { matchingBrace } from './matching_brace'; import * as applySourceChange from './apply_source_change'; import * as expandMacro from './expand_macro'; import * as inlayHints from './inlay_hints'; import * as joinLines from './join_lines'; -import * as matchingBrace from './matching_brace'; import * as onEnter from './on_enter'; import * as parentModule from './parent_module'; import * as runnables from './runnables'; diff --git a/editors/code/src/commands/matching_brace.ts b/editors/code/src/commands/matching_brace.ts index 364208cc7..665b0c33c 100644 --- a/editors/code/src/commands/matching_brace.ts +++ b/editors/code/src/commands/matching_brace.ts @@ -1,34 +1,33 @@ import * as vscode from 'vscode'; - import { Position, TextDocumentIdentifier } from 'vscode-languageclient'; -import { Server } from '../server'; +import { Ctx, Cmd } from '../ctx'; + +export function matchingBrace(ctx: Ctx): Cmd { + return async () => { + const editor = ctx.activeRustEditor; + if (!editor) { + return; + } + const request: FindMatchingBraceParams = { + textDocument: { uri: editor.document.uri.toString() }, + offsets: editor.selections.map(s => ctx.client.code2ProtocolConverter.asPosition(s.active)), + }; + const response = await ctx.client.sendRequest( + 'rust-analyzer/findMatchingBrace', + request, + ); + editor.selections = editor.selections.map((sel, idx) => { + const active = ctx.client.protocol2CodeConverter.asPosition( + response[idx], + ); + const anchor = sel.isEmpty ? active : sel.anchor; + return new vscode.Selection(anchor, active); + }); + editor.revealRange(editor.selection); + } +} interface FindMatchingBraceParams { textDocument: TextDocumentIdentifier; offsets: Position[]; } - -export async function handle() { - const editor = vscode.window.activeTextEditor; - if (editor == null || editor.document.languageId !== 'rust') { - return; - } - const request: FindMatchingBraceParams = { - textDocument: { uri: editor.document.uri.toString() }, - offsets: editor.selections.map(s => { - return Server.client.code2ProtocolConverter.asPosition(s.active); - }), - }; - const response = await Server.client.sendRequest( - 'rust-analyzer/findMatchingBrace', - request, - ); - editor.selections = editor.selections.map((sel, idx) => { - const active = Server.client.protocol2CodeConverter.asPosition( - response[idx], - ); - const anchor = sel.isEmpty ? active : sel.anchor; - return new vscode.Selection(anchor, active); - }); - editor.revealRange(editor.selection); -} -- cgit v1.2.3