From 83d2527880d86653ce00940c65620319b36afcff Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Dec 2019 15:50:15 +0100 Subject: Move joinLines to the new Ctx --- editors/code/src/commands/index.ts | 2 +- editors/code/src/commands/join_lines.ts | 38 ++++++++++++++++----------------- 2 files changed, 20 insertions(+), 20 deletions(-) (limited to 'editors/code/src/commands') diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index 9d9b9c575..8090c7e5b 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 134ddc801..7952fb0c0 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); -} -- cgit v1.2.3 From 5aebf1081dced95a71c674aba65fb5b3e40e6ff1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Dec 2019 16:43:34 +0100 Subject: Refactor applySourceChange --- editors/code/src/commands/analyzer_status.ts | 8 ++-- editors/code/src/commands/apply_source_change.ts | 54 ------------------------ editors/code/src/commands/index.ts | 4 +- editors/code/src/commands/join_lines.ts | 12 +++--- editors/code/src/commands/on_enter.ts | 49 ++++++++++----------- 5 files changed, 31 insertions(+), 96 deletions(-) delete mode 100644 editors/code/src/commands/apply_source_change.ts (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 c9d32fe07..849c2ec6c 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/apply_source_change.ts b/editors/code/src/commands/apply_source_change.ts deleted file mode 100644 index 8167398b1..000000000 --- a/editors/code/src/commands/apply_source_change.ts +++ /dev/null @@ -1,54 +0,0 @@ -import * as vscode from 'vscode'; -import * as lc from 'vscode-languageclient'; - -import { Server } from '../server'; - -export interface SourceChange { - label: string; - workspaceEdit: lc.WorkspaceEdit; - cursorPosition?: lc.TextDocumentPositionParams; -} - -export async function handle(change: SourceChange) { - const wsEdit = Server.client.protocol2CodeConverter.asWorkspaceEdit( - change.workspaceEdit, - ); - let created; - let moved; - if (change.workspaceEdit.documentChanges) { - for (const docChange of change.workspaceEdit.documentChanges) { - if (lc.CreateFile.is(docChange)) { - created = docChange.uri; - } else if (lc.RenameFile.is(docChange)) { - moved = docChange.newUri; - } - } - } - const toOpen = created || moved; - const toReveal = change.cursorPosition; - await vscode.workspace.applyEdit(wsEdit); - if (toOpen) { - const toOpenUri = vscode.Uri.parse(toOpen); - const doc = await vscode.workspace.openTextDocument(toOpenUri); - await vscode.window.showTextDocument(doc); - } else if (toReveal) { - const uri = Server.client.protocol2CodeConverter.asUri( - toReveal.textDocument.uri, - ); - const position = Server.client.protocol2CodeConverter.asPosition( - toReveal.position, - ); - const editor = vscode.window.activeTextEditor; - if (!editor || editor.document.uri.toString() !== uri.toString()) { - return; - } - if (!editor.selection.isEmpty) { - return; - } - editor.selection = new vscode.Selection(position, position); - editor.revealRange( - new vscode.Range(position, position), - vscode.TextEditorRevealType.Default, - ); - } -} diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index 8090c7e5b..0a0a36e23 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 7952fb0c0..1a4b8a2d8 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 772c64b3c..4503e13f0 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; - } - 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; +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; } - await applySourceChange(change); - return true; } -- cgit v1.2.3 From 9bfeac708d33056b37d780b948cb1f117cac19af Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Dec 2019 17:03:05 +0100 Subject: Move parentModule to the new Ctx --- editors/code/src/commands/index.ts | 2 +- editors/code/src/commands/parent_module.ts | 52 +++++++++++++++--------------- 2 files changed, 27 insertions(+), 27 deletions(-) (limited to 'editors/code/src/commands') diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index 0a0a36e23..03ca58210 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 ad49e1bdb..2f986009e 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 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 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 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); + } } -- cgit v1.2.3 From ac3d0e83403be22ec31d62a1501d726f6e6f81e1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Dec 2019 18:31:08 +0100 Subject: Run prettier on all files --- 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 +- 6 files changed, 17 insertions(+), 22 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 849c2ec6c..830e40e8f 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 03ca58210..a7f3bc4c1 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 1a4b8a2d8..e906759c1 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 665b0c33c..a3febc5f4 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 4503e13f0..efc0dfe1d 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 2f986009e..d641181fa 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); - } + }; } -- cgit v1.2.3 From ca5c59507f76b8e30658d6c815b823c9636d786a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Dec 2019 19:05:41 +0100 Subject: 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 +++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 77 deletions(-) delete mode 100644 editors/code/src/commands/syntaxTree.ts create mode 100644 editors/code/src/commands/syntax_tree.ts (limited to 'editors/code/src/commands') diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index a7f3bc4c1..8f91b3b7d 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 89a80550c..000000000 --- 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 000000000..e61fb36df --- /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; + } +} -- cgit v1.2.3 From 260df66b7742e76c76184388253552c5055b1945 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Dec 2019 19:07:28 +0100 Subject: 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(-) (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 830e40e8f..b2b624b75 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 e906759c1..f4f902cf9 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 a3febc5f4..59c253f88 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 efc0dfe1d..8324060e8 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 d641181fa..258b61b21 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 { -- cgit v1.2.3