Files
rust/editors/code/src/commands/syntaxTree.ts

77 lines
2.2 KiB
TypeScript
Raw Normal View History

2018-10-07 22:44:25 +02:00
import * as vscode from 'vscode';
import { Range, TextDocumentIdentifier } from 'vscode-languageclient';
2018-10-07 22:44:25 +02:00
import { Server } from '../server';
2019-01-28 14:43:07 +03:00
export const syntaxTreeUri = vscode.Uri.parse('rust-analyzer://syntaxtree');
2018-10-07 22:44:25 +02:00
2018-10-08 22:38:33 +01:00
export class TextDocumentContentProvider
implements vscode.TextDocumentContentProvider {
2018-10-07 22:59:02 +02:00
public eventEmitter = new vscode.EventEmitter<vscode.Uri>();
public syntaxTree: string = 'Not available';
2018-10-07 22:44:25 +02:00
2018-10-08 22:38:33 +01:00
public provideTextDocumentContent(
uri: vscode.Uri
): vscode.ProviderResult<string> {
2018-10-07 22:59:02 +02:00
const editor = vscode.window.activeTextEditor;
2018-10-08 22:38:33 +01:00
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
);
}
2018-10-07 22:59:02 +02:00
const request: SyntaxTreeParams = {
textDocument: { uri: editor.document.uri.toString() },
range
2018-10-07 22:44:25 +02:00
};
2018-10-08 22:38:33 +01:00
return Server.client.sendRequest<SyntaxTreeResult>(
2019-01-28 14:43:07 +03:00
'rust-analyzer/syntaxTree',
2018-10-08 22:38:33 +01:00
request
);
2018-10-07 22:44:25 +02:00
}
get onDidChange(): vscode.Event<vscode.Uri> {
2018-10-07 22:59:02 +02:00
return this.eventEmitter.event;
2018-10-07 22:44:25 +02:00
}
}
interface SyntaxTreeParams {
textDocument: TextDocumentIdentifier;
range?: Range;
2018-10-07 22:44:25 +02:00
}
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) {
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
);
};
2018-10-07 22:44:25 +02:00
}