From 83d2527880d86653ce00940c65620319b36afcff Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Dec 2019 15:50:15 +0100 Subject: [PATCH 1/6] Move joinLines to the new Ctx --- editors/code/src/commands/index.ts | 2 +- editors/code/src/commands/join_lines.ts | 38 ++++++++++++------------- editors/code/src/main.ts | 2 +- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index 9d9b9c57549..8090c7e5b20 100644 --- a/editors/code/src/commands/index.ts +++ b/editors/code/src/commands/index.ts @@ -2,10 +2,10 @@ import { Ctx, Cmd } from '../ctx' import { analyzerStatus } from './analyzer_status'; import { matchingBrace } from './matching_brace'; +import { joinLines } from './join_lines'; 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 onEnter from './on_enter'; import * as parentModule from './parent_module'; import * as runnables from './runnables'; diff --git a/editors/code/src/commands/join_lines.ts b/editors/code/src/commands/join_lines.ts index 134ddc80164..7952fb0c006 100644 --- a/editors/code/src/commands/join_lines.ts +++ b/editors/code/src/commands/join_lines.ts @@ -1,29 +1,29 @@ -import * as vscode from 'vscode'; - import { Range, TextDocumentIdentifier } from 'vscode-languageclient'; -import { Server } from '../server'; +import { Ctx, Cmd } from '../ctx'; import { handle as applySourceChange, SourceChange, } from './apply_source_change'; +export function joinLines(ctx: Ctx): Cmd { + return async () => { + const editor = ctx.activeRustEditor; + if (!editor) { + return; + } + const request: JoinLinesParams = { + range: ctx.client.code2ProtocolConverter.asRange(editor.selection), + textDocument: { uri: editor.document.uri.toString() }, + }; + const change = await ctx.client.sendRequest( + 'rust-analyzer/joinLines', + request, + ); + await applySourceChange(change); + } +} + interface JoinLinesParams { textDocument: TextDocumentIdentifier; range: Range; } - -export async function handle() { - const editor = vscode.window.activeTextEditor; - if (editor == null || editor.document.languageId !== 'rust') { - return; - } - const request: JoinLinesParams = { - range: Server.client.code2ProtocolConverter.asRange(editor.selection), - textDocument: { uri: editor.document.uri.toString() }, - }; - const change = await Server.client.sendRequest( - 'rust-analyzer/joinLines', - request, - ); - await applySourceChange(change); -} diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index a4149a05999..95beb2d8fba 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -18,6 +18,7 @@ export async function activate(context: vscode.ExtensionContext) { ctx.registerCommand('analyzerStatus', commands.analyzerStatus); ctx.registerCommand('collectGarbage', commands.collectGarbage); ctx.registerCommand('matchingBrace', commands.matchingBrace); + ctx.registerCommand('joinLines', commands.joinLines); function disposeOnDeactivation(disposable: vscode.Disposable) { context.subscriptions.push(disposable); @@ -56,7 +57,6 @@ export async function activate(context: vscode.ExtensionContext) { } // Commands are requests from vscode to the language server - registerCommand('rust-analyzer.joinLines', commands.joinLines.handle); registerCommand('rust-analyzer.parentModule', commands.parentModule.handle); registerCommand('rust-analyzer.run', commands.runnables.handle); // Unlike the above this does not send requests to the language server From 5aebf1081dced95a71c674aba65fb5b3e40e6ff1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Dec 2019 16:43:34 +0100 Subject: [PATCH 2/6] Refactor applySourceChange --- editors/code/src/commands/analyzer_status.ts | 8 ++- editors/code/src/commands/index.ts | 4 +- editors/code/src/commands/join_lines.ts | 12 ++--- editors/code/src/commands/on_enter.ts | 49 ++++++++--------- editors/code/src/ctx.ts | 22 ++++++++ editors/code/src/main.ts | 52 ++++--------------- ...pply_source_change.ts => source_change.ts} | 10 ++-- 7 files changed, 68 insertions(+), 89 deletions(-) rename editors/code/src/{commands/apply_source_change.ts => source_change.ts} (83%) diff --git a/editors/code/src/commands/analyzer_status.ts b/editors/code/src/commands/analyzer_status.ts index c9d32fe070a..849c2ec6c6f 100644 --- a/editors/code/src/commands/analyzer_status.ts +++ b/editors/code/src/commands/analyzer_status.ts @@ -40,11 +40,10 @@ export function analyzerStatus(ctx: Ctx): Cmd { class TextDocumentContentProvider implements vscode.TextDocumentContentProvider { + ctx: Ctx uri = vscode.Uri.parse('rust-analyzer-status://status'); eventEmitter = new vscode.EventEmitter(); - ctx: Ctx - constructor(ctx: Ctx) { this.ctx = ctx } @@ -53,9 +52,8 @@ class TextDocumentContentProvider _uri: vscode.Uri, ): vscode.ProviderResult { const editor = vscode.window.activeTextEditor; - if (editor == null) { - return ''; - } + if (editor == null) return ''; + 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 8090c7e5b20..0a0a36e2307 100644 --- a/editors/code/src/commands/index.ts +++ b/editors/code/src/commands/index.ts @@ -3,10 +3,9 @@ import { Ctx, Cmd } from '../ctx' import { analyzerStatus } from './analyzer_status'; import { matchingBrace } from './matching_brace'; import { joinLines } from './join_lines'; -import * as applySourceChange from './apply_source_change'; +import { onEnter } from './on_enter'; import * as expandMacro from './expand_macro'; import * as inlayHints from './inlay_hints'; -import * as onEnter from './on_enter'; import * as parentModule from './parent_module'; import * as runnables from './runnables'; import * as syntaxTree from './syntaxTree'; @@ -17,7 +16,6 @@ function collectGarbage(ctx: Ctx): Cmd { export { analyzerStatus, - applySourceChange, expandMacro, joinLines, matchingBrace, diff --git a/editors/code/src/commands/join_lines.ts b/editors/code/src/commands/join_lines.ts index 7952fb0c006..1a4b8a2d808 100644 --- a/editors/code/src/commands/join_lines.ts +++ b/editors/code/src/commands/join_lines.ts @@ -1,16 +1,14 @@ import { Range, TextDocumentIdentifier } from 'vscode-languageclient'; import { Ctx, Cmd } from '../ctx'; import { - handle as applySourceChange, - SourceChange, -} from './apply_source_change'; + applySourceChange, SourceChange +} from '../source_change'; export function joinLines(ctx: Ctx): Cmd { return async () => { const editor = ctx.activeRustEditor; - if (!editor) { - return; - } + if (!editor) return; + const request: JoinLinesParams = { range: ctx.client.code2ProtocolConverter.asRange(editor.selection), textDocument: { uri: editor.document.uri.toString() }, @@ -19,7 +17,7 @@ export function joinLines(ctx: Ctx): Cmd { 'rust-analyzer/joinLines', request, ); - await applySourceChange(change); + await applySourceChange(ctx, change); } } diff --git a/editors/code/src/commands/on_enter.ts b/editors/code/src/commands/on_enter.ts index 772c64b3c78..4503e13f008 100644 --- a/editors/code/src/commands/on_enter.ts +++ b/editors/code/src/commands/on_enter.ts @@ -1,33 +1,28 @@ -import * as vscode from 'vscode'; import * as lc from 'vscode-languageclient'; -import { Server } from '../server'; import { - handle as applySourceChange, + applySourceChange, SourceChange, -} from './apply_source_change'; +} from '../source_change'; +import { Cmd, Ctx } from '../ctx'; -export async function handle(event: { text: string }): Promise { - const editor = vscode.window.activeTextEditor; - if ( - editor == null || - editor.document.languageId !== 'rust' || - event.text !== '\n' - ) { - return false; +export function onEnter(ctx: Ctx): Cmd { + return async (event: { text: string }) => { + const editor = ctx.activeRustEditor; + if (!editor || event.text !== '\n') return false; + + const request: lc.TextDocumentPositionParams = { + textDocument: { uri: editor.document.uri.toString() }, + position: ctx.client.code2ProtocolConverter.asPosition( + editor.selection.active, + ), + }; + const change = await ctx.client.sendRequest( + 'rust-analyzer/onEnter', + request, + ); + if (!change) return false; + + await applySourceChange(ctx, change); + return true; } - const request: lc.TextDocumentPositionParams = { - textDocument: { uri: editor.document.uri.toString() }, - position: Server.client.code2ProtocolConverter.asPosition( - editor.selection.active, - ), - }; - const change = await Server.client.sendRequest( - 'rust-analyzer/onEnter', - request, - ); - if (!change) { - return false; - } - await applySourceChange(change); - return true; } diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index 712337fe71c..22af5ef321a 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts @@ -27,6 +27,28 @@ export class Ctx { this.pushCleanup(d); } + overrideCommand(name: string, factory: (ctx: Ctx) => Cmd) { + const defaultCmd = `default:${name}`; + const override = factory(this); + const original = (...args: any[]) => + vscode.commands.executeCommand(defaultCmd, ...args); + try { + const d = vscode.commands.registerCommand( + name, + async (...args: any[]) => { + if (!(await override(...args))) { + return await original(...args); + } + }, + ); + this.pushCleanup(d); + } catch (_) { + vscode.window.showWarningMessage( + 'Enhanced typing feature is disabled because of incompatibility with VIM extension, consider turning off rust-analyzer.enableEnhancedTyping: https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/user/README.md#settings', + ); + } + } + pushCleanup(d: { dispose(): any }) { this.extCtx.subscriptions.push(d); } diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 95beb2d8fba..c3f2806302c 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -27,44 +27,12 @@ export async function activate(context: vscode.ExtensionContext) { function registerCommand(name: string, f: any) { disposeOnDeactivation(vscode.commands.registerCommand(name, f)); } - function overrideCommand( - name: string, - f: (...args: any[]) => Promise, - ) { - const defaultCmd = `default:${name}`; - const original = (...args: any[]) => - vscode.commands.executeCommand(defaultCmd, ...args); - - try { - registerCommand(name, async (...args: any[]) => { - const editor = vscode.window.activeTextEditor; - if ( - !editor || - !editor.document || - editor.document.languageId !== 'rust' - ) { - return await original(...args); - } - if (!(await f(...args))) { - return await original(...args); - } - }); - } catch (_) { - vscode.window.showWarningMessage( - 'Enhanced typing feature is disabled because of incompatibility with VIM extension, consider turning off rust-analyzer.enableEnhancedTyping: https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/user/README.md#settings', - ); - } - } // Commands are requests from vscode to the language server registerCommand('rust-analyzer.parentModule', commands.parentModule.handle); registerCommand('rust-analyzer.run', commands.runnables.handle); // Unlike the above this does not send requests to the language server registerCommand('rust-analyzer.runSingle', commands.runnables.handleSingle); - registerCommand( - 'rust-analyzer.applySourceChange', - commands.applySourceChange.handle, - ); registerCommand( 'rust-analyzer.showReferences', (uri: string, position: lc.Position, locations: lc.Location[]) => { @@ -78,7 +46,7 @@ export async function activate(context: vscode.ExtensionContext) { ); if (Server.config.enableEnhancedTyping) { - overrideCommand('type', commands.onEnter.handle); + ctx.overrideCommand('type', commands.onEnter); } const watchStatus = new StatusDisplay( @@ -91,15 +59,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(); diff --git a/editors/code/src/commands/apply_source_change.ts b/editors/code/src/source_change.ts similarity index 83% rename from editors/code/src/commands/apply_source_change.ts rename to editors/code/src/source_change.ts index 8167398b1ce..a4f9068b274 100644 --- a/editors/code/src/commands/apply_source_change.ts +++ b/editors/code/src/source_change.ts @@ -1,7 +1,7 @@ import * as vscode from 'vscode'; import * as lc from 'vscode-languageclient'; -import { Server } from '../server'; +import { Ctx } from './ctx'; export interface SourceChange { label: string; @@ -9,8 +9,8 @@ export interface SourceChange { cursorPosition?: lc.TextDocumentPositionParams; } -export async function handle(change: SourceChange) { - const wsEdit = Server.client.protocol2CodeConverter.asWorkspaceEdit( +export async function applySourceChange(ctx: Ctx, change: SourceChange) { + const wsEdit = ctx.client.protocol2CodeConverter.asWorkspaceEdit( change.workspaceEdit, ); let created; @@ -32,10 +32,10 @@ export async function handle(change: SourceChange) { const doc = await vscode.workspace.openTextDocument(toOpenUri); await vscode.window.showTextDocument(doc); } else if (toReveal) { - const uri = Server.client.protocol2CodeConverter.asUri( + const uri = ctx.client.protocol2CodeConverter.asUri( toReveal.textDocument.uri, ); - const position = Server.client.protocol2CodeConverter.asPosition( + const position = ctx.client.protocol2CodeConverter.asPosition( toReveal.position, ); const editor = vscode.window.activeTextEditor; From 9bfeac708d33056b37d780b948cb1f117cac19af Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Dec 2019 17:03:05 +0100 Subject: [PATCH 3/6] Move parentModule to the new Ctx --- editors/code/src/commands/index.ts | 2 +- editors/code/src/commands/parent_module.ts | 52 +++++++++++----------- editors/code/src/main.ts | 20 ++++----- 3 files changed, 37 insertions(+), 37 deletions(-) diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index 0a0a36e2307..03ca582105b 100644 --- a/editors/code/src/commands/index.ts +++ b/editors/code/src/commands/index.ts @@ -4,9 +4,9 @@ import { analyzerStatus } from './analyzer_status'; import { matchingBrace } from './matching_brace'; import { joinLines } from './join_lines'; import { onEnter } from './on_enter'; +import { parentModule } from './parent_module'; import * as expandMacro from './expand_macro'; import * as inlayHints from './inlay_hints'; -import * as parentModule from './parent_module'; import * as runnables from './runnables'; import * as syntaxTree from './syntaxTree'; diff --git a/editors/code/src/commands/parent_module.ts b/editors/code/src/commands/parent_module.ts index ad49e1bdbbf..2f986009e56 100644 --- a/editors/code/src/commands/parent_module.ts +++ b/editors/code/src/commands/parent_module.ts @@ -1,32 +1,32 @@ import * as vscode from 'vscode'; import * as lc from 'vscode-languageclient'; -import { Server } from '../server'; +import { Ctx, Cmd } from '../ctx'; -export async function handle() { - const editor = vscode.window.activeTextEditor; - if (editor == null || editor.document.languageId !== 'rust') { - return; - } - const request: lc.TextDocumentPositionParams = { - textDocument: { uri: editor.document.uri.toString() }, - position: Server.client.code2ProtocolConverter.asPosition( - editor.selection.active, - ), - }; - const response = await Server.client.sendRequest( - 'rust-analyzer/parentModule', - request, - ); - const loc = response[0]; - if (loc == null) { - return; - } - const uri = Server.client.protocol2CodeConverter.asUri(loc.uri); - const range = Server.client.protocol2CodeConverter.asRange(loc.range); +export function parentModule(ctx: Ctx): Cmd { + return async () => { + const editor = ctx.activeRustEditor; + if (!editor) return; - const doc = await vscode.workspace.openTextDocument(uri); - const e = await vscode.window.showTextDocument(doc); - e.selection = new vscode.Selection(range.start, range.start); - e.revealRange(range, vscode.TextEditorRevealType.InCenter); + const request: lc.TextDocumentPositionParams = { + textDocument: { uri: editor.document.uri.toString() }, + position: ctx.client.code2ProtocolConverter.asPosition( + editor.selection.active, + ), + }; + const response = await ctx.client.sendRequest( + 'rust-analyzer/parentModule', + request, + ); + const loc = response[0]; + if (loc == null) return; + + const uri = ctx.client.protocol2CodeConverter.asUri(loc.uri); + const range = ctx.client.protocol2CodeConverter.asRange(loc.range); + + const doc = await vscode.workspace.openTextDocument(uri); + const e = await vscode.window.showTextDocument(doc); + e.selection = new vscode.Selection(range.start, range.start); + e.revealRange(range, vscode.TextEditorRevealType.InCenter); + } } diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index c3f2806302c..55fedd8bba0 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -19,6 +19,7 @@ export async function activate(context: vscode.ExtensionContext) { ctx.registerCommand('collectGarbage', commands.collectGarbage); ctx.registerCommand('matchingBrace', commands.matchingBrace); ctx.registerCommand('joinLines', commands.joinLines); + ctx.registerCommand('parentModule', commands.parentModule); function disposeOnDeactivation(disposable: vscode.Disposable) { context.subscriptions.push(disposable); @@ -29,7 +30,6 @@ export async function activate(context: vscode.ExtensionContext) { } // Commands are requests from vscode to the language server - registerCommand('rust-analyzer.parentModule', commands.parentModule.handle); registerCommand('rust-analyzer.run', commands.runnables.handle); // Unlike the above this does not send requests to the language server registerCommand('rust-analyzer.runSingle', commands.runnables.handleSingle); @@ -59,15 +59,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(); From ac3d0e83403be22ec31d62a1501d726f6e6f81e1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Dec 2019 18:31:08 +0100 Subject: [PATCH 4/6] Run prettier on all files --- editors/code/package.json | 2 +- editors/code/src/commands/analyzer_status.ts | 10 +++------- editors/code/src/commands/index.ts | 8 +++++--- editors/code/src/commands/join_lines.ts | 6 ++---- editors/code/src/commands/matching_brace.ts | 6 ++++-- editors/code/src/commands/on_enter.ts | 7 ++----- editors/code/src/commands/parent_module.ts | 2 +- 7 files changed, 18 insertions(+), 23 deletions(-) diff --git a/editors/code/package.json b/editors/code/package.json index 6662747f65b..46d55e32d0f 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -19,7 +19,7 @@ "vscode:prepublish": "rollup -c", "package": "vsce package", "watch": "tsc -watch -p ./", - "prettier": "prettier --write **/*.ts" + "prettier": "prettier --write '**/*.ts'" }, "dependencies": { "jsonc-parser": "^2.1.0", diff --git a/editors/code/src/commands/analyzer_status.ts b/editors/code/src/commands/analyzer_status.ts index 849c2ec6c6f..830e40e8f9c 100644 --- a/editors/code/src/commands/analyzer_status.ts +++ b/editors/code/src/commands/analyzer_status.ts @@ -23,10 +23,7 @@ export function analyzerStatus(ctx: Ctx): Cmd { return async function handle() { if (poller == null) { - poller = setInterval( - () => tdcp.eventEmitter.fire(tdcp.uri), - 1000, - ); + poller = setInterval(() => tdcp.eventEmitter.fire(tdcp.uri), 1000); } const document = await vscode.workspace.openTextDocument(tdcp.uri); return vscode.window.showTextDocument( @@ -39,13 +36,12 @@ export function analyzerStatus(ctx: Ctx): Cmd { class TextDocumentContentProvider implements vscode.TextDocumentContentProvider { - - ctx: Ctx + ctx: Ctx; uri = vscode.Uri.parse('rust-analyzer-status://status'); eventEmitter = new vscode.EventEmitter(); constructor(ctx: Ctx) { - this.ctx = ctx + this.ctx = ctx; } provideTextDocumentContent( diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index 03ca582105b..a7f3bc4c10f 100644 --- a/editors/code/src/commands/index.ts +++ b/editors/code/src/commands/index.ts @@ -1,4 +1,4 @@ -import { Ctx, Cmd } from '../ctx' +import { Ctx, Cmd } from '../ctx'; import { analyzerStatus } from './analyzer_status'; import { matchingBrace } from './matching_brace'; @@ -11,7 +11,9 @@ import * as runnables from './runnables'; import * as syntaxTree from './syntaxTree'; function collectGarbage(ctx: Ctx): Cmd { - return async () => { ctx.client.sendRequest('rust-analyzer/collectGarbage', null) } + return async () => { + ctx.client.sendRequest('rust-analyzer/collectGarbage', null); + }; } export { @@ -24,5 +26,5 @@ export { syntaxTree, onEnter, inlayHints, - collectGarbage + collectGarbage, }; diff --git a/editors/code/src/commands/join_lines.ts b/editors/code/src/commands/join_lines.ts index 1a4b8a2d808..e906759c1cd 100644 --- a/editors/code/src/commands/join_lines.ts +++ b/editors/code/src/commands/join_lines.ts @@ -1,8 +1,6 @@ import { Range, TextDocumentIdentifier } from 'vscode-languageclient'; import { Ctx, Cmd } from '../ctx'; -import { - applySourceChange, SourceChange -} from '../source_change'; +import { applySourceChange, SourceChange } from '../source_change'; export function joinLines(ctx: Ctx): Cmd { return async () => { @@ -18,7 +16,7 @@ export function joinLines(ctx: Ctx): Cmd { request, ); await applySourceChange(ctx, change); - } + }; } interface JoinLinesParams { diff --git a/editors/code/src/commands/matching_brace.ts b/editors/code/src/commands/matching_brace.ts index 665b0c33cc4..a3febc5f425 100644 --- a/editors/code/src/commands/matching_brace.ts +++ b/editors/code/src/commands/matching_brace.ts @@ -10,7 +10,9 @@ export function matchingBrace(ctx: Ctx): Cmd { } const request: FindMatchingBraceParams = { textDocument: { uri: editor.document.uri.toString() }, - offsets: editor.selections.map(s => ctx.client.code2ProtocolConverter.asPosition(s.active)), + offsets: editor.selections.map(s => + ctx.client.code2ProtocolConverter.asPosition(s.active), + ), }; const response = await ctx.client.sendRequest( 'rust-analyzer/findMatchingBrace', @@ -24,7 +26,7 @@ export function matchingBrace(ctx: Ctx): Cmd { return new vscode.Selection(anchor, active); }); editor.revealRange(editor.selection); - } + }; } interface FindMatchingBraceParams { diff --git a/editors/code/src/commands/on_enter.ts b/editors/code/src/commands/on_enter.ts index 4503e13f008..efc0dfe1d1a 100644 --- a/editors/code/src/commands/on_enter.ts +++ b/editors/code/src/commands/on_enter.ts @@ -1,8 +1,5 @@ import * as lc from 'vscode-languageclient'; -import { - applySourceChange, - SourceChange, -} from '../source_change'; +import { applySourceChange, SourceChange } from '../source_change'; import { Cmd, Ctx } from '../ctx'; export function onEnter(ctx: Ctx): Cmd { @@ -24,5 +21,5 @@ export function onEnter(ctx: Ctx): Cmd { await applySourceChange(ctx, change); return true; - } + }; } diff --git a/editors/code/src/commands/parent_module.ts b/editors/code/src/commands/parent_module.ts index 2f986009e56..d641181fa88 100644 --- a/editors/code/src/commands/parent_module.ts +++ b/editors/code/src/commands/parent_module.ts @@ -28,5 +28,5 @@ export function parentModule(ctx: Ctx): Cmd { const e = await vscode.window.showTextDocument(doc); e.selection = new vscode.Selection(range.start, range.start); e.revealRange(range, vscode.TextEditorRevealType.InCenter); - } + }; } From ca5c59507f76b8e30658d6c815b823c9636d786a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Dec 2019 19:05:41 +0100 Subject: [PATCH 5/6] Refactor show syntax tree action --- editors/code/src/commands/index.ts | 2 +- editors/code/src/commands/syntaxTree.ts | 76 ------------- editors/code/src/commands/syntax_tree.ts | 106 ++++++++++++++++++ editors/code/src/ctx.ts | 4 + .../src/events/change_active_text_editor.ts | 9 +- .../code/src/events/change_text_document.ts | 24 ---- editors/code/src/events/index.ts | 3 +- editors/code/src/main.ts | 26 +---- 8 files changed, 116 insertions(+), 134 deletions(-) delete mode 100644 editors/code/src/commands/syntaxTree.ts create mode 100644 editors/code/src/commands/syntax_tree.ts delete mode 100644 editors/code/src/events/change_text_document.ts diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index a7f3bc4c10f..8f91b3b7d32 100644 --- a/editors/code/src/commands/index.ts +++ b/editors/code/src/commands/index.ts @@ -5,10 +5,10 @@ import { matchingBrace } from './matching_brace'; import { joinLines } from './join_lines'; import { onEnter } from './on_enter'; import { parentModule } from './parent_module'; +import { syntaxTree } from './syntax_tree'; import * as expandMacro from './expand_macro'; import * as inlayHints from './inlay_hints'; import * as runnables from './runnables'; -import * as syntaxTree from './syntaxTree'; function collectGarbage(ctx: Ctx): Cmd { return async () => { diff --git a/editors/code/src/commands/syntaxTree.ts b/editors/code/src/commands/syntaxTree.ts deleted file mode 100644 index 89a80550cee..00000000000 --- a/editors/code/src/commands/syntaxTree.ts +++ /dev/null @@ -1,76 +0,0 @@ -import * as vscode from 'vscode'; -import { Range, TextDocumentIdentifier } from 'vscode-languageclient'; - -import { Server } from '../server'; - -export const syntaxTreeUri = vscode.Uri.parse('rust-analyzer://syntaxtree'); - -export class SyntaxTreeContentProvider - implements vscode.TextDocumentContentProvider { - public eventEmitter = new vscode.EventEmitter(); - public syntaxTree: string = 'Not available'; - - public provideTextDocumentContent( - uri: vscode.Uri, - ): vscode.ProviderResult { - const editor = vscode.window.activeTextEditor; - if (editor == null) { - return ''; - } - - let range: Range | undefined; - - // When the range based query is enabled we take the range of the selection - if (uri.query === 'range=true') { - range = editor.selection.isEmpty - ? undefined - : Server.client.code2ProtocolConverter.asRange( - editor.selection, - ); - } - - const request: SyntaxTreeParams = { - textDocument: { uri: editor.document.uri.toString() }, - range, - }; - return Server.client.sendRequest( - 'rust-analyzer/syntaxTree', - request, - ); - } - - get onDidChange(): vscode.Event { - return this.eventEmitter.event; - } -} - -interface SyntaxTreeParams { - textDocument: TextDocumentIdentifier; - range?: Range; -} - -type SyntaxTreeResult = string; - -// Opens the virtual file that will show the syntax tree -// -// The contents of the file come from the `TextDocumentContentProvider` -export function createHandle(provider: SyntaxTreeContentProvider) { - return async () => { - const editor = vscode.window.activeTextEditor; - const rangeEnabled = !!(editor && !editor.selection.isEmpty); - - const uri = rangeEnabled - ? vscode.Uri.parse(`${syntaxTreeUri.toString()}?range=true`) - : syntaxTreeUri; - - const document = await vscode.workspace.openTextDocument(uri); - - provider.eventEmitter.fire(uri); - - return vscode.window.showTextDocument( - document, - vscode.ViewColumn.Two, - true, - ); - }; -} diff --git a/editors/code/src/commands/syntax_tree.ts b/editors/code/src/commands/syntax_tree.ts new file mode 100644 index 00000000000..e61fb36df82 --- /dev/null +++ b/editors/code/src/commands/syntax_tree.ts @@ -0,0 +1,106 @@ +import * as vscode from 'vscode'; +import * as lc from 'vscode-languageclient'; + +import { Ctx, Cmd } from '../ctx'; + +// Opens the virtual file that will show the syntax tree +// +// The contents of the file come from the `TextDocumentContentProvider` +export function syntaxTree(ctx: Ctx): Cmd { + const stcp = new SyntaxTreeContentProvider(ctx); + + ctx.pushCleanup( + vscode.workspace.registerTextDocumentContentProvider( + 'rust-analyzer', + stcp, + ), + ); + + vscode.workspace.onDidChangeTextDocument( + (event: vscode.TextDocumentChangeEvent) => { + const doc = event.document; + if (doc.languageId !== 'rust') return; + afterLs(() => stcp.eventEmitter.fire(stcp.uri)); + }, + ctx.subscriptions, + ); + + vscode.window.onDidChangeActiveTextEditor( + (editor: vscode.TextEditor | undefined) => { + if (!editor || editor.document.languageId !== 'rust') return; + stcp.eventEmitter.fire(stcp.uri); + }, + ctx.subscriptions, + ); + + return async () => { + const editor = vscode.window.activeTextEditor; + const rangeEnabled = !!(editor && !editor.selection.isEmpty); + + const uri = rangeEnabled + ? vscode.Uri.parse(`${stcp.uri.toString()}?range=true`) + : stcp.uri; + + const document = await vscode.workspace.openTextDocument(uri); + + stcp.eventEmitter.fire(uri); + + return vscode.window.showTextDocument( + document, + vscode.ViewColumn.Two, + true, + ); + }; +} + +// We need to order this after LS updates, but there's no API for that. +// Hence, good old setTimeout. +function afterLs(f: () => any) { + setTimeout(f, 10); +} + +interface SyntaxTreeParams { + textDocument: lc.TextDocumentIdentifier; + range?: lc.Range; +} + +export class SyntaxTreeContentProvider + implements vscode.TextDocumentContentProvider { + ctx: Ctx; + uri = vscode.Uri.parse('rust-analyzer://syntaxtree'); + eventEmitter = new vscode.EventEmitter(); + syntaxTree: string = 'Not available'; + + constructor(ctx: Ctx) { + this.ctx = ctx; + } + + provideTextDocumentContent(uri: vscode.Uri): vscode.ProviderResult { + const editor = vscode.window.activeTextEditor; + if (editor == null) return ''; + + let range: lc.Range | undefined; + + // When the range based query is enabled we take the range of the selection + if (uri.query === 'range=true') { + range = editor.selection.isEmpty + ? undefined + : this.ctx.client.code2ProtocolConverter.asRange( + editor.selection, + ); + } + + const request: SyntaxTreeParams = { + textDocument: { uri: editor.document.uri.toString() }, + range, + }; + return this.ctx.client.sendRequest( + 'rust-analyzer/syntaxTree', + request, + ); + } + + get onDidChange(): vscode.Event { + return this.eventEmitter.event; + } +} diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index 22af5ef321a..c3a3583b52d 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts @@ -49,6 +49,10 @@ export class Ctx { } } + get subscriptions(): { dispose(): any }[] { + return this.extCtx.subscriptions; + } + pushCleanup(d: { dispose(): any }) { this.extCtx.subscriptions.push(d); } diff --git a/editors/code/src/events/change_active_text_editor.ts b/editors/code/src/events/change_active_text_editor.ts index 74b91bd4871..4384ee56768 100644 --- a/editors/code/src/events/change_active_text_editor.ts +++ b/editors/code/src/events/change_active_text_editor.ts @@ -1,21 +1,14 @@ import { TextEditor } from 'vscode'; import { TextDocumentIdentifier } from 'vscode-languageclient'; - -import { - SyntaxTreeContentProvider, - syntaxTreeUri, -} from '../commands/syntaxTree'; import { Decoration } from '../highlighting'; import { Server } from '../server'; -export function makeHandler(syntaxTreeProvider: SyntaxTreeContentProvider) { +export function makeHandler() { return async function handle(editor: TextEditor | undefined) { if (!editor || editor.document.languageId !== 'rust') { return; } - syntaxTreeProvider.eventEmitter.fire(syntaxTreeUri); - if (!Server.config.highlightingOn) { return; } diff --git a/editors/code/src/events/change_text_document.ts b/editors/code/src/events/change_text_document.ts deleted file mode 100644 index 2e998e889ba..00000000000 --- a/editors/code/src/events/change_text_document.ts +++ /dev/null @@ -1,24 +0,0 @@ -import * as vscode from 'vscode'; - -import { - SyntaxTreeContentProvider, - syntaxTreeUri, -} from '../commands/syntaxTree'; - -export function createHandler(syntaxTreeProvider: SyntaxTreeContentProvider) { - return (event: vscode.TextDocumentChangeEvent) => { - const doc = event.document; - if (doc.languageId !== 'rust') { - return; - } - afterLs(() => { - syntaxTreeProvider.eventEmitter.fire(syntaxTreeUri); - }); - }; -} - -// We need to order this after LS updates, but there's no API for that. -// Hence, good old setTimeout. -function afterLs(f: () => any) { - setTimeout(f, 10); -} diff --git a/editors/code/src/events/index.ts b/editors/code/src/events/index.ts index 4c154563f51..be135474de6 100644 --- a/editors/code/src/events/index.ts +++ b/editors/code/src/events/index.ts @@ -1,4 +1,3 @@ import * as changeActiveTextEditor from './change_active_text_editor'; -import * as changeTextDocument from './change_text_document'; -export { changeActiveTextEditor, changeTextDocument }; +export { changeActiveTextEditor }; diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 55fedd8bba0..d92cd164fef 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -4,7 +4,6 @@ import * as lc from 'vscode-languageclient'; import * as commands from './commands'; import { ExpandMacroContentProvider } from './commands/expand_macro'; import { HintsUpdater } from './commands/inlay_hints'; -import { SyntaxTreeContentProvider } from './commands/syntaxTree'; import { StatusDisplay } from './commands/watch_status'; import * as events from './events'; import * as notifications from './notifications'; @@ -20,6 +19,7 @@ export async function activate(context: vscode.ExtensionContext) { ctx.registerCommand('matchingBrace', commands.matchingBrace); ctx.registerCommand('joinLines', commands.joinLines); ctx.registerCommand('parentModule', commands.parentModule); + ctx.registerCommand('syntaxTree', commands.syntaxTree); function disposeOnDeactivation(disposable: vscode.Disposable) { context.subscriptions.push(disposable); @@ -55,10 +55,7 @@ export async function activate(context: vscode.ExtensionContext) { disposeOnDeactivation(watchStatus); // Notifications are events triggered by the language server - const allNotifications: Iterable<[ - string, - lc.GenericNotificationHandler, - ]> = [ + const allNotifications: [string, lc.GenericNotificationHandler][] = [ [ 'rust-analyzer/publishDecorations', notifications.publishDecorations.handle, @@ -68,20 +65,13 @@ export async function activate(context: vscode.ExtensionContext) { params => watchStatus.handleProgressNotification(params), ], ]; - const syntaxTreeContentProvider = new SyntaxTreeContentProvider(); const expandMacroContentProvider = new ExpandMacroContentProvider(); // The events below are plain old javascript events, triggered and handled by vscode vscode.window.onDidChangeActiveTextEditor( - events.changeActiveTextEditor.makeHandler(syntaxTreeContentProvider), + events.changeActiveTextEditor.makeHandler(), ); - disposeOnDeactivation( - vscode.workspace.registerTextDocumentContentProvider( - 'rust-analyzer', - syntaxTreeContentProvider, - ), - ); disposeOnDeactivation( vscode.workspace.registerTextDocumentContentProvider( 'rust-analyzer', @@ -89,21 +79,11 @@ export async function activate(context: vscode.ExtensionContext) { ), ); - registerCommand( - 'rust-analyzer.syntaxTree', - commands.syntaxTree.createHandle(syntaxTreeContentProvider), - ); registerCommand( 'rust-analyzer.expandMacro', commands.expandMacro.createHandle(expandMacroContentProvider), ); - vscode.workspace.onDidChangeTextDocument( - events.changeTextDocument.createHandler(syntaxTreeContentProvider), - null, - context.subscriptions, - ); - const startServer = () => Server.start(allNotifications); const reloadCommand = () => reloadServer(startServer); From 260df66b7742e76c76184388253552c5055b1945 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Dec 2019 19:07:28 +0100 Subject: [PATCH 6/6] Cleanup imports --- editors/code/src/commands/analyzer_status.ts | 1 + editors/code/src/commands/join_lines.ts | 7 ++++--- editors/code/src/commands/matching_brace.ts | 9 +++++---- editors/code/src/commands/on_enter.ts | 1 + editors/code/src/commands/parent_module.ts | 2 +- 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/editors/code/src/commands/analyzer_status.ts b/editors/code/src/commands/analyzer_status.ts index 830e40e8f9c..b2b624b75a7 100644 --- a/editors/code/src/commands/analyzer_status.ts +++ b/editors/code/src/commands/analyzer_status.ts @@ -1,4 +1,5 @@ import * as vscode from 'vscode'; + import { Ctx, Cmd } from '../ctx'; // Shows status of rust-analyzer (for debugging) diff --git a/editors/code/src/commands/join_lines.ts b/editors/code/src/commands/join_lines.ts index e906759c1cd..f4f902cf96e 100644 --- a/editors/code/src/commands/join_lines.ts +++ b/editors/code/src/commands/join_lines.ts @@ -1,4 +1,5 @@ -import { Range, TextDocumentIdentifier } from 'vscode-languageclient'; +import * as lc from 'vscode-languageclient'; + import { Ctx, Cmd } from '../ctx'; import { applySourceChange, SourceChange } from '../source_change'; @@ -20,6 +21,6 @@ export function joinLines(ctx: Ctx): Cmd { } interface JoinLinesParams { - textDocument: TextDocumentIdentifier; - range: Range; + textDocument: lc.TextDocumentIdentifier; + range: lc.Range; } diff --git a/editors/code/src/commands/matching_brace.ts b/editors/code/src/commands/matching_brace.ts index a3febc5f425..59c253f8866 100644 --- a/editors/code/src/commands/matching_brace.ts +++ b/editors/code/src/commands/matching_brace.ts @@ -1,5 +1,6 @@ import * as vscode from 'vscode'; -import { Position, TextDocumentIdentifier } from 'vscode-languageclient'; +import * as lc from 'vscode-languageclient'; + import { Ctx, Cmd } from '../ctx'; export function matchingBrace(ctx: Ctx): Cmd { @@ -14,7 +15,7 @@ export function matchingBrace(ctx: Ctx): Cmd { ctx.client.code2ProtocolConverter.asPosition(s.active), ), }; - const response = await ctx.client.sendRequest( + const response = await ctx.client.sendRequest( 'rust-analyzer/findMatchingBrace', request, ); @@ -30,6 +31,6 @@ export function matchingBrace(ctx: Ctx): Cmd { } interface FindMatchingBraceParams { - textDocument: TextDocumentIdentifier; - offsets: Position[]; + textDocument: lc.TextDocumentIdentifier; + offsets: lc.Position[]; } diff --git a/editors/code/src/commands/on_enter.ts b/editors/code/src/commands/on_enter.ts index efc0dfe1d1a..8324060e88e 100644 --- a/editors/code/src/commands/on_enter.ts +++ b/editors/code/src/commands/on_enter.ts @@ -1,4 +1,5 @@ import * as lc from 'vscode-languageclient'; + import { applySourceChange, SourceChange } from '../source_change'; import { Cmd, Ctx } from '../ctx'; diff --git a/editors/code/src/commands/parent_module.ts b/editors/code/src/commands/parent_module.ts index d641181fa88..258b61b21fe 100644 --- a/editors/code/src/commands/parent_module.ts +++ b/editors/code/src/commands/parent_module.ts @@ -1,6 +1,6 @@ import * as vscode from 'vscode'; - import * as lc from 'vscode-languageclient'; + import { Ctx, Cmd } from '../ctx'; export function parentModule(ctx: Ctx): Cmd {