mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 05:15:04 +00:00
Use DocumentProvider instead of Hover
This commit is contained in:
parent
80fe467ce8
commit
d16cc223e1
3 changed files with 75 additions and 33 deletions
|
@ -2,47 +2,82 @@ import * as vscode from 'vscode';
|
|||
import { Position, TextDocumentIdentifier } from 'vscode-languageclient';
|
||||
import { Server } from '../server';
|
||||
|
||||
interface ExpandedMacro {
|
||||
name: string,
|
||||
expansion: string,
|
||||
}
|
||||
export const expandMacroUri = vscode.Uri.parse(
|
||||
'rust-analyzer://expandMacro/[EXPANSION].rs'
|
||||
);
|
||||
|
||||
function code_format(expanded: ExpandedMacro): vscode.MarkdownString {
|
||||
const markdown = new vscode.MarkdownString(
|
||||
`#### Recursive expansion of ${expanded.name}! macro`
|
||||
);
|
||||
markdown.appendCodeblock(expanded.expansion, 'rust');
|
||||
return markdown;
|
||||
}
|
||||
export class ExpandMacroContentProvider
|
||||
implements vscode.TextDocumentContentProvider {
|
||||
public eventEmitter = new vscode.EventEmitter<vscode.Uri>();
|
||||
|
||||
export class ExpandMacroHoverProvider implements vscode.HoverProvider {
|
||||
public provideHover(
|
||||
document: vscode.TextDocument,
|
||||
position: vscode.Position,
|
||||
token: vscode.CancellationToken
|
||||
): Thenable<vscode.Hover | null> | null {
|
||||
public provideTextDocumentContent(
|
||||
uri: vscode.Uri
|
||||
): vscode.ProviderResult<string> {
|
||||
async function handle() {
|
||||
const editor = vscode.window.activeTextEditor;
|
||||
if (editor == null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const position = editor.selection.active;
|
||||
const request: MacroExpandParams = {
|
||||
textDocument: { uri: document.uri.toString() },
|
||||
textDocument: { uri: editor.document.uri.toString() },
|
||||
position
|
||||
};
|
||||
const result = await Server.client.sendRequest<ExpandedMacro>(
|
||||
const expanded = await Server.client.sendRequest<ExpandedMacro>(
|
||||
'rust-analyzer/expandMacro',
|
||||
request
|
||||
);
|
||||
if (result != null) {
|
||||
const formated = code_format(result);
|
||||
return new vscode.Hover(formated);
|
||||
|
||||
if (expanded == null) {
|
||||
return 'Not available';
|
||||
}
|
||||
|
||||
return null;
|
||||
return code_format(expanded);
|
||||
}
|
||||
|
||||
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 {
|
||||
textDocument: TextDocumentIdentifier;
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue