From c2d3203d0c708dc2ccb7c0d017ae876180c0e5a8 Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Sun, 3 Mar 2019 21:21:40 +0200 Subject: Add vscode support for range in SyntaxTreeParams This enables the client to use a command to either show the live-updating version of the syntax tree for the current file. Or optionally when a selected range is provided, we then provide a snapshot of the syntax tree for the range. --- editors/code/package.json | 2 +- editors/code/src/commands/syntaxTree.ts | 44 ++++++++++++++++++++++++++------- editors/code/src/extension.ts | 6 ++++- 3 files changed, 41 insertions(+), 11 deletions(-) (limited to 'editors') diff --git a/editors/code/package.json b/editors/code/package.json index d4ce2ae2c..fda411810 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -75,7 +75,7 @@ "commands": [ { "command": "rust-analyzer.syntaxTree", - "title": "Show syntax tree for current file", + "title": "Show Syntax Tree", "category": "Rust Analyzer" }, { diff --git a/editors/code/src/commands/syntaxTree.ts b/editors/code/src/commands/syntaxTree.ts index c0baf08c5..7200ae823 100644 --- a/editors/code/src/commands/syntaxTree.ts +++ b/editors/code/src/commands/syntaxTree.ts @@ -1,5 +1,5 @@ import * as vscode from 'vscode'; -import { TextDocumentIdentifier } from 'vscode-languageclient'; +import { Range, TextDocumentIdentifier } from 'vscode-languageclient'; import { Server } from '../server'; @@ -17,8 +17,21 @@ export class TextDocumentContentProvider 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() } + textDocument: { uri: editor.document.uri.toString() }, + range }; return Server.client.sendRequest( 'rust-analyzer/syntaxTree', @@ -33,6 +46,7 @@ export class TextDocumentContentProvider interface SyntaxTreeParams { textDocument: TextDocumentIdentifier; + range?: Range; } type SyntaxTreeResult = string; @@ -40,11 +54,23 @@ type SyntaxTreeResult = string; // Opens the virtual file that will show the syntax tree // // The contents of the file come from the `TextDocumentContentProvider` -export async function handle() { - const document = await vscode.workspace.openTextDocument(syntaxTreeUri); - return vscode.window.showTextDocument( - document, - vscode.ViewColumn.Two, - true - ); +export function createHandle(provider: TextDocumentContentProvider) { + 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/extension.ts b/editors/code/src/extension.ts index 8b332eeb2..5134bb258 100644 --- a/editors/code/src/extension.ts +++ b/editors/code/src/extension.ts @@ -52,7 +52,6 @@ export function activate(context: vscode.ExtensionContext) { registerCommand('rust-analyzer.collectGarbage', () => Server.client.sendRequest('rust-analyzer/collectGarbage', null) ); - registerCommand('rust-analyzer.syntaxTree', commands.syntaxTree.handle); registerCommand( 'rust-analyzer.extendSelection', commands.extendSelection.handle @@ -109,6 +108,11 @@ export function activate(context: vscode.ExtensionContext) { ) ); + registerCommand( + 'rust-analyzer.syntaxTree', + commands.syntaxTree.createHandle(textDocumentContentProvider) + ); + vscode.workspace.onDidChangeTextDocument( events.changeTextDocument.createHandler(textDocumentContentProvider), null, -- cgit v1.2.3 From 1b4e0ec1c868c7f2a0eef1e59bfa382db85a6900 Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Sun, 3 Mar 2019 21:54:51 +0200 Subject: Rename syntaxtree text provider to SyntaxTreeContentProvider --- editors/code/src/commands/syntaxTree.ts | 4 ++-- editors/code/src/events/change_text_document.ts | 10 ++++------ editors/code/src/extension.ts | 10 +++++----- 3 files changed, 11 insertions(+), 13 deletions(-) (limited to 'editors') diff --git a/editors/code/src/commands/syntaxTree.ts b/editors/code/src/commands/syntaxTree.ts index 7200ae823..2f50fe14b 100644 --- a/editors/code/src/commands/syntaxTree.ts +++ b/editors/code/src/commands/syntaxTree.ts @@ -5,7 +5,7 @@ import { Server } from '../server'; export const syntaxTreeUri = vscode.Uri.parse('rust-analyzer://syntaxtree'); -export class TextDocumentContentProvider +export class SyntaxTreeContentProvider implements vscode.TextDocumentContentProvider { public eventEmitter = new vscode.EventEmitter(); public syntaxTree: string = 'Not available'; @@ -54,7 +54,7 @@ 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: TextDocumentContentProvider) { +export function createHandle(provider: SyntaxTreeContentProvider) { return async () => { const editor = vscode.window.activeTextEditor; const rangeEnabled = !!(editor && !editor.selection.isEmpty); diff --git a/editors/code/src/events/change_text_document.ts b/editors/code/src/events/change_text_document.ts index 6be057245..89488bc61 100644 --- a/editors/code/src/events/change_text_document.ts +++ b/editors/code/src/events/change_text_document.ts @@ -1,20 +1,18 @@ import * as vscode from 'vscode'; import { - syntaxTreeUri, - TextDocumentContentProvider + SyntaxTreeContentProvider, + syntaxTreeUri } from '../commands/syntaxTree'; -export function createHandler( - textDocumentContentProvider: TextDocumentContentProvider -) { +export function createHandler(syntaxTreeProvider: SyntaxTreeContentProvider) { return (event: vscode.TextDocumentChangeEvent) => { const doc = event.document; if (doc.languageId !== 'rust') { return; } afterLs(() => { - textDocumentContentProvider.eventEmitter.fire(syntaxTreeUri); + syntaxTreeProvider.eventEmitter.fire(syntaxTreeUri); }); }; } diff --git a/editors/code/src/extension.ts b/editors/code/src/extension.ts index 5134bb258..894334c55 100644 --- a/editors/code/src/extension.ts +++ b/editors/code/src/extension.ts @@ -2,7 +2,7 @@ import * as vscode from 'vscode'; import * as lc from 'vscode-languageclient'; import * as commands from './commands'; -import { TextDocumentContentProvider } from './commands/syntaxTree'; +import { SyntaxTreeContentProvider } from './commands/syntaxTree'; import * as events from './events'; import * as notifications from './notifications'; import { Server } from './server'; @@ -100,21 +100,21 @@ export function activate(context: vscode.ExtensionContext) { events.changeActiveTextEditor.handle ); - const textDocumentContentProvider = new TextDocumentContentProvider(); + const syntaxTreeContentProvider = new SyntaxTreeContentProvider(); disposeOnDeactivation( vscode.workspace.registerTextDocumentContentProvider( 'rust-analyzer', - textDocumentContentProvider + syntaxTreeContentProvider ) ); registerCommand( 'rust-analyzer.syntaxTree', - commands.syntaxTree.createHandle(textDocumentContentProvider) + commands.syntaxTree.createHandle(syntaxTreeContentProvider) ); vscode.workspace.onDidChangeTextDocument( - events.changeTextDocument.createHandler(textDocumentContentProvider), + events.changeTextDocument.createHandler(syntaxTreeContentProvider), null, context.subscriptions ); -- cgit v1.2.3 From 0db95fc812d2c839e847527b774dfda170266cec Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Sun, 3 Mar 2019 22:03:37 +0200 Subject: Allow syntax tree to update when changing files Previously when using the file based syntax tree, it would not update until a change had been made in the new file. Now we automatically update the syntax tree to match the current file. --- .../code/src/events/change_active_text_editor.ts | 39 +++++++++++++--------- editors/code/src/extension.ts | 4 +-- 2 files changed, 26 insertions(+), 17 deletions(-) (limited to 'editors') diff --git a/editors/code/src/events/change_active_text_editor.ts b/editors/code/src/events/change_active_text_editor.ts index af295b2ec..64be56225 100644 --- a/editors/code/src/events/change_active_text_editor.ts +++ b/editors/code/src/events/change_active_text_editor.ts @@ -1,23 +1,32 @@ import { TextEditor } from 'vscode'; import { TextDocumentIdentifier } from 'vscode-languageclient'; +import { + SyntaxTreeContentProvider, + syntaxTreeUri +} from '../commands/syntaxTree'; import { Decoration } from '../highlighting'; import { Server } from '../server'; -export async function handle(editor: TextEditor | undefined) { - if ( - !Server.config.highlightingOn || - !editor || - editor.document.languageId !== 'rust' - ) { - return; - } - const params: TextDocumentIdentifier = { - uri: editor.document.uri.toString() +export function makeHandler(syntaxTreeProvider: SyntaxTreeContentProvider) { + return async function handle(editor: TextEditor | undefined) { + if (!editor || editor.document.languageId !== 'rust') { + return; + } + + syntaxTreeProvider.eventEmitter.fire(syntaxTreeUri); + + if (!Server.config.highlightingOn) { + return; + } + + const params: TextDocumentIdentifier = { + uri: editor.document.uri.toString() + }; + const decorations = await Server.client.sendRequest( + 'rust-analyzer/decorationsRequest', + params + ); + Server.highlighter.setHighlights(editor, decorations); }; - const decorations = await Server.client.sendRequest( - 'rust-analyzer/decorationsRequest', - params - ); - Server.highlighter.setHighlights(editor, decorations); } diff --git a/editors/code/src/extension.ts b/editors/code/src/extension.ts index 894334c55..941beba18 100644 --- a/editors/code/src/extension.ts +++ b/editors/code/src/extension.ts @@ -94,13 +94,13 @@ export function activate(context: vscode.ExtensionContext) { notifications.publishDecorations.handle ] ]; + const syntaxTreeContentProvider = new SyntaxTreeContentProvider(); // The events below are plain old javascript events, triggered and handled by vscode vscode.window.onDidChangeActiveTextEditor( - events.changeActiveTextEditor.handle + events.changeActiveTextEditor.makeHandler(syntaxTreeContentProvider) ); - const syntaxTreeContentProvider = new SyntaxTreeContentProvider(); disposeOnDeactivation( vscode.workspace.registerTextDocumentContentProvider( 'rust-analyzer', -- cgit v1.2.3