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 +- editors/code/src/ctx.ts | 30 ++++++++++++++++++++++++++ editors/code/src/main.ts | 32 +++++++++++++++++----------- 4 files changed, 63 insertions(+), 20 deletions(-) create mode 100644 editors/code/src/ctx.ts (limited to 'editors/code/src') 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'; diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts new file mode 100644 index 000000000..8581667b4 --- /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, + ) { + 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 1da10ebd0..048b9bbd4 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('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(); -- 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 +++++++ editors/code/src/ctx.ts | 4 +++- editors/code/src/main.ts | 10 ++-------- 4 files changed, 14 insertions(+), 11 deletions(-) (limited to 'editors/code/src') 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 }; diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index 8581667b4..9dd2b7d4f 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts @@ -16,7 +16,7 @@ export class Ctx { registerCommand( name: string, - factory: (ctx: Ctx) => () => Promise, + factory: (ctx: Ctx) => Cmd, ) { const fullName = `rust-analyzer.${name}` const cmd = factory(this); @@ -28,3 +28,5 @@ export class Ctx { this.extCtx.subscriptions.push(d) } } + +export type Cmd = (...args: any[]) => any; diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 048b9bbd4..9500219ca 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -15,11 +15,8 @@ let ctx!: Ctx; export async function activate(context: vscode.ExtensionContext) { ctx = new Ctx(context); - ctx.registerCommand( - 'analyzerStatus', - commands.analyzerStatus - ); - + ctx.registerCommand('analyzerStatus', commands.analyzerStatus); + ctx.registerCommand('collectGarbage', commands.collectGarbage); function disposeOnDeactivation(disposable: vscode.Disposable) { context.subscriptions.push(disposable); @@ -58,9 +55,6 @@ export async function activate(context: vscode.ExtensionContext) { } // Commands are requests from vscode to the language server - registerCommand('rust-analyzer.collectGarbage', () => - Server.client.sendRequest('rust-analyzer/collectGarbage', null), - ); registerCommand( 'rust-analyzer.matchingBrace', commands.matchingBrace.handle, -- cgit v1.2.3 From 57df9bed703bd0f8b7a7cc593152b65b543ad121 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Dec 2019 15:11:30 +0100 Subject: Run prettier --- editors/code/src/ctx.ts | 16 ++++++---------- editors/code/src/main.ts | 20 ++++++++++---------- 2 files changed, 16 insertions(+), 20 deletions(-) (limited to 'editors/code/src') diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index 9dd2b7d4f..87f1574d3 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts @@ -2,30 +2,26 @@ import * as vscode from 'vscode'; import * as lc from 'vscode-languageclient'; import { Server } from './server'; - export class Ctx { - private extCtx: vscode.ExtensionContext + private extCtx: vscode.ExtensionContext; constructor(extCtx: vscode.ExtensionContext) { - this.extCtx = extCtx + this.extCtx = extCtx; } get client(): lc.LanguageClient { - return Server.client + return Server.client; } - registerCommand( - name: string, - factory: (ctx: Ctx) => Cmd, - ) { - const fullName = `rust-analyzer.${name}` + registerCommand(name: string, factory: (ctx: Ctx) => Cmd) { + 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) + this.extCtx.subscriptions.push(d); } } diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 9500219ca..f96fb1962 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -9,7 +9,7 @@ import { StatusDisplay } from './commands/watch_status'; import * as events from './events'; import * as notifications from './notifications'; import { Server } from './server'; -import { Ctx } from './ctx' +import { Ctx } from './ctx'; let ctx!: Ctx; @@ -94,15 +94,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(); -- 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 ++++++++++++++--------------- editors/code/src/ctx.ts | 7 ++++ editors/code/src/main.ts | 5 +-- 4 files changed, 35 insertions(+), 32 deletions(-) (limited to 'editors/code/src') 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); -} diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index 87f1574d3..712337fe7 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts @@ -13,6 +13,13 @@ export class Ctx { return Server.client; } + get activeRustEditor(): vscode.TextEditor | undefined { + const editor = vscode.window.activeTextEditor; + return editor && editor.document.languageId === 'rust' + ? editor + : undefined; + } + registerCommand(name: string, factory: (ctx: Ctx) => Cmd) { const fullName = `rust-analyzer.${name}`; const cmd = factory(this); diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index f96fb1962..a4149a059 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -17,6 +17,7 @@ export async function activate(context: vscode.ExtensionContext) { ctx = new Ctx(context); ctx.registerCommand('analyzerStatus', commands.analyzerStatus); ctx.registerCommand('collectGarbage', commands.collectGarbage); + ctx.registerCommand('matchingBrace', commands.matchingBrace); function disposeOnDeactivation(disposable: vscode.Disposable) { context.subscriptions.push(disposable); @@ -55,10 +56,6 @@ export async function activate(context: vscode.ExtensionContext) { } // Commands are requests from vscode to the language server - registerCommand( - 'rust-analyzer.matchingBrace', - commands.matchingBrace.handle, - ); registerCommand('rust-analyzer.joinLines', commands.joinLines.handle); registerCommand('rust-analyzer.parentModule', commands.parentModule.handle); registerCommand('rust-analyzer.run', commands.runnables.handle); -- cgit v1.2.3