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.
This commit is contained in:
Ville Penttinen 2019-03-03 21:21:40 +02:00
parent ac52d9a1f1
commit c2d3203d0c
3 changed files with 41 additions and 11 deletions

View file

@ -75,7 +75,7 @@
"commands": [ "commands": [
{ {
"command": "rust-analyzer.syntaxTree", "command": "rust-analyzer.syntaxTree",
"title": "Show syntax tree for current file", "title": "Show Syntax Tree",
"category": "Rust Analyzer" "category": "Rust Analyzer"
}, },
{ {

View file

@ -1,5 +1,5 @@
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import { TextDocumentIdentifier } from 'vscode-languageclient'; import { Range, TextDocumentIdentifier } from 'vscode-languageclient';
import { Server } from '../server'; import { Server } from '../server';
@ -17,8 +17,21 @@ export class TextDocumentContentProvider
if (editor == null) { if (editor == null) {
return ''; 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 = { const request: SyntaxTreeParams = {
textDocument: { uri: editor.document.uri.toString() } textDocument: { uri: editor.document.uri.toString() },
range
}; };
return Server.client.sendRequest<SyntaxTreeResult>( return Server.client.sendRequest<SyntaxTreeResult>(
'rust-analyzer/syntaxTree', 'rust-analyzer/syntaxTree',
@ -33,6 +46,7 @@ export class TextDocumentContentProvider
interface SyntaxTreeParams { interface SyntaxTreeParams {
textDocument: TextDocumentIdentifier; textDocument: TextDocumentIdentifier;
range?: Range;
} }
type SyntaxTreeResult = string; type SyntaxTreeResult = string;
@ -40,11 +54,23 @@ type SyntaxTreeResult = string;
// Opens the virtual file that will show the syntax tree // Opens the virtual file that will show the syntax tree
// //
// The contents of the file come from the `TextDocumentContentProvider` // The contents of the file come from the `TextDocumentContentProvider`
export async function handle() { export function createHandle(provider: TextDocumentContentProvider) {
const document = await vscode.workspace.openTextDocument(syntaxTreeUri); return async () => {
return vscode.window.showTextDocument( const editor = vscode.window.activeTextEditor;
document, const rangeEnabled = !!(editor && !editor.selection.isEmpty);
vscode.ViewColumn.Two,
true 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
);
};
} }

View file

@ -52,7 +52,6 @@ export function activate(context: vscode.ExtensionContext) {
registerCommand('rust-analyzer.collectGarbage', () => registerCommand('rust-analyzer.collectGarbage', () =>
Server.client.sendRequest<null>('rust-analyzer/collectGarbage', null) Server.client.sendRequest<null>('rust-analyzer/collectGarbage', null)
); );
registerCommand('rust-analyzer.syntaxTree', commands.syntaxTree.handle);
registerCommand( registerCommand(
'rust-analyzer.extendSelection', 'rust-analyzer.extendSelection',
commands.extendSelection.handle commands.extendSelection.handle
@ -109,6 +108,11 @@ export function activate(context: vscode.ExtensionContext) {
) )
); );
registerCommand(
'rust-analyzer.syntaxTree',
commands.syntaxTree.createHandle(textDocumentContentProvider)
);
vscode.workspace.onDidChangeTextDocument( vscode.workspace.onDidChangeTextDocument(
events.changeTextDocument.createHandler(textDocumentContentProvider), events.changeTextDocument.createHandler(textDocumentContentProvider),
null, null,