Use DocumentProvider instead of Hover

This commit is contained in:
Edwin Cheng 2019-11-20 01:06:10 +08:00
parent 80fe467ce8
commit d16cc223e1
3 changed files with 75 additions and 33 deletions

View file

@ -90,6 +90,11 @@
"title": "Show Syntax Tree", "title": "Show Syntax Tree",
"category": "Rust Analyzer" "category": "Rust Analyzer"
}, },
{
"command": "rust-analyzer.expandMacro",
"title": "Expand macro recursively",
"category": "Rust Analyzer"
},
{ {
"command": "rust-analyzer.matchingBrace", "command": "rust-analyzer.matchingBrace",
"title": "Find matching brace", "title": "Find matching brace",

View file

@ -2,47 +2,82 @@ import * as vscode from 'vscode';
import { Position, TextDocumentIdentifier } from 'vscode-languageclient'; import { Position, TextDocumentIdentifier } from 'vscode-languageclient';
import { Server } from '../server'; import { Server } from '../server';
interface ExpandedMacro { export const expandMacroUri = vscode.Uri.parse(
name: string, 'rust-analyzer://expandMacro/[EXPANSION].rs'
expansion: string, );
}
function code_format(expanded: ExpandedMacro): vscode.MarkdownString { export class ExpandMacroContentProvider
const markdown = new vscode.MarkdownString( implements vscode.TextDocumentContentProvider {
`#### Recursive expansion of ${expanded.name}! macro` public eventEmitter = new vscode.EventEmitter<vscode.Uri>();
);
markdown.appendCodeblock(expanded.expansion, 'rust');
return markdown;
}
export class ExpandMacroHoverProvider implements vscode.HoverProvider { public provideTextDocumentContent(
public provideHover( uri: vscode.Uri
document: vscode.TextDocument, ): vscode.ProviderResult<string> {
position: vscode.Position,
token: vscode.CancellationToken
): Thenable<vscode.Hover | null> | null {
async function handle() { async function handle() {
const editor = vscode.window.activeTextEditor;
if (editor == null) {
return '';
}
const position = editor.selection.active;
const request: MacroExpandParams = { const request: MacroExpandParams = {
textDocument: { uri: document.uri.toString() }, textDocument: { uri: editor.document.uri.toString() },
position position
}; };
const result = await Server.client.sendRequest<ExpandedMacro>( const expanded = await Server.client.sendRequest<ExpandedMacro>(
'rust-analyzer/expandMacro', 'rust-analyzer/expandMacro',
request request
); );
if (result != null) {
const formated = code_format(result); if (expanded == null) {
return new vscode.Hover(formated); return 'Not available';
} }
return null; return code_format(expanded);
} }
return handle(); return handle();
} }
get onDidChange(): vscode.Event<vscode.Uri> {
return this.eventEmitter.event;
}
}
// Opens the virtual file that will show the syntax tree
//
// The contents of the file come from the `TextDocumentContentProvider`
export function createHandle(provider: ExpandMacroContentProvider) {
return async () => {
const uri = expandMacroUri;
const document = await vscode.workspace.openTextDocument(uri);
provider.eventEmitter.fire(uri);
return vscode.window.showTextDocument(
document,
vscode.ViewColumn.Two,
true
);
};
} }
interface MacroExpandParams { interface MacroExpandParams {
textDocument: TextDocumentIdentifier; textDocument: TextDocumentIdentifier;
position: Position; position: Position;
} }
interface ExpandedMacro {
name: string;
expansion: string;
}
function code_format(expanded: ExpandedMacro): string {
let result = `// Recursive expansion of ${expanded.name}! macro\n`;
result += '='.repeat(result.length);
result += '\n\n';
result += expanded.expansion;
return result;
}

View file

@ -3,7 +3,7 @@ import * as lc from 'vscode-languageclient';
import * as commands from './commands'; import * as commands from './commands';
import { CargoWatchProvider } from './commands/cargo_watch'; import { CargoWatchProvider } from './commands/cargo_watch';
import { ExpandMacroHoverProvider } from './commands/expand_macro'; import { ExpandMacroContentProvider } from './commands/expand_macro';
import { HintsUpdater } from './commands/inlay_hints'; import { HintsUpdater } from './commands/inlay_hints';
import { import {
interactivelyStartCargoWatch, interactivelyStartCargoWatch,
@ -98,6 +98,7 @@ export function activate(context: vscode.ExtensionContext) {
] ]
]; ];
const syntaxTreeContentProvider = new SyntaxTreeContentProvider(); const syntaxTreeContentProvider = new SyntaxTreeContentProvider();
const expandMacroContentProvider = new ExpandMacroContentProvider();
// The events below are plain old javascript events, triggered and handled by vscode // The events below are plain old javascript events, triggered and handled by vscode
vscode.window.onDidChangeActiveTextEditor( vscode.window.onDidChangeActiveTextEditor(
@ -110,11 +111,21 @@ export function activate(context: vscode.ExtensionContext) {
syntaxTreeContentProvider syntaxTreeContentProvider
) )
); );
disposeOnDeactivation(
vscode.workspace.registerTextDocumentContentProvider(
'rust-analyzer',
expandMacroContentProvider
)
);
registerCommand( registerCommand(
'rust-analyzer.syntaxTree', 'rust-analyzer.syntaxTree',
commands.syntaxTree.createHandle(syntaxTreeContentProvider) commands.syntaxTree.createHandle(syntaxTreeContentProvider)
); );
registerCommand(
'rust-analyzer.expandMacro',
commands.expandMacro.createHandle(expandMacroContentProvider)
);
vscode.workspace.onDidChangeTextDocument( vscode.workspace.onDidChangeTextDocument(
events.changeTextDocument.createHandler(syntaxTreeContentProvider), events.changeTextDocument.createHandler(syntaxTreeContentProvider),
@ -122,15 +133,6 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions context.subscriptions
); );
const expandMacroContentProvider = new ExpandMacroHoverProvider();
disposeOnDeactivation(
vscode.languages.registerHoverProvider(
'rust',
expandMacroContentProvider
)
);
const startServer = () => Server.start(allNotifications); const startServer = () => Server.start(allNotifications);
const reloadCommand = () => reloadServer(startServer); const reloadCommand = () => reloadServer(startServer);