Merge #7068
7068: Add VSCode command to view the hir of a function body r=theotherphil a=theotherphil Will fix https://github.com/rust-analyzer/rust-analyzer/issues/7061. Very rough initial version just to work out where I needed to wire everything up. @matklad would you be happy merging a hir visualiser of some kind? If so, do you have any thoughts on what you'd like it show, and how? I've spent very little time on this thus far, so I'm fine with throwing away the contents of this PR, but I want to avoid taking the time to make this more polished/interactive/useful only to discover that no-one else has any interest in this functionality.  Co-authored-by: Phil Ellison <phil.j.ellison@gmail.com>
This commit is contained in:
commit
1cc73d60bb
12 changed files with 143 additions and 2 deletions
crates
docs/dev
editors/code
|
@ -39,7 +39,7 @@ use hir_ty::{
|
|||
TyDefId, TyKind, TypeCtor,
|
||||
};
|
||||
use rustc_hash::FxHashSet;
|
||||
use stdx::impl_from;
|
||||
use stdx::{format_to, impl_from};
|
||||
use syntax::{
|
||||
ast::{self, AttrsOwner, NameOwner},
|
||||
AstNode, SmolStr,
|
||||
|
@ -797,6 +797,19 @@ impl Function {
|
|||
pub fn has_body(self, db: &dyn HirDatabase) -> bool {
|
||||
db.function_data(self.id).has_body
|
||||
}
|
||||
|
||||
/// A textual representation of the HIR of this function for debugging purposes.
|
||||
pub fn debug_hir(self, db: &dyn HirDatabase) -> String {
|
||||
let body = db.body(self.id.into());
|
||||
|
||||
let mut result = String::new();
|
||||
format_to!(result, "HIR expressions in the body of `{}`:\n", self.name(db));
|
||||
for (id, expr) in body.exprs.iter() {
|
||||
format_to!(result, "{:?}: {:?}\n", id, expr);
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
// Note: logically, this belongs to `hir_ty`, but we are not using it there yet.
|
||||
|
|
|
@ -31,6 +31,7 @@ mod folding_ranges;
|
|||
mod goto_definition;
|
||||
mod goto_implementation;
|
||||
mod goto_type_definition;
|
||||
mod view_hir;
|
||||
mod hover;
|
||||
mod inlay_hints;
|
||||
mod join_lines;
|
||||
|
@ -271,6 +272,10 @@ impl Analysis {
|
|||
self.with_db(|db| syntax_tree::syntax_tree(&db, file_id, text_range))
|
||||
}
|
||||
|
||||
pub fn view_hir(&self, position: FilePosition) -> Cancelable<String> {
|
||||
self.with_db(|db| view_hir::view_hir(&db, position))
|
||||
}
|
||||
|
||||
pub fn expand_macro(&self, position: FilePosition) -> Cancelable<Option<ExpandedMacro>> {
|
||||
self.with_db(|db| expand_macro::expand_macro(db, position))
|
||||
}
|
||||
|
|
25
crates/ide/src/view_hir.rs
Normal file
25
crates/ide/src/view_hir.rs
Normal file
|
@ -0,0 +1,25 @@
|
|||
use hir::{Function, Semantics};
|
||||
use ide_db::base_db::FilePosition;
|
||||
use ide_db::RootDatabase;
|
||||
use syntax::{algo::find_node_at_offset, ast, AstNode};
|
||||
|
||||
// Feature: View Hir
|
||||
//
|
||||
// |===
|
||||
// | Editor | Action Name
|
||||
//
|
||||
// | VS Code | **Rust Analyzer: View Hir**
|
||||
// |===
|
||||
pub(crate) fn view_hir(db: &RootDatabase, position: FilePosition) -> String {
|
||||
body_hir(db, position).unwrap_or("Not inside a function body".to_string())
|
||||
}
|
||||
|
||||
fn body_hir(db: &RootDatabase, position: FilePosition) -> Option<String> {
|
||||
let sema = Semantics::new(db);
|
||||
let source_file = sema.parse(position.file_id);
|
||||
|
||||
let function = find_node_at_offset::<ast::Fn>(source_file.syntax(), position.offset)?;
|
||||
|
||||
let function: Function = sema.to_def(&function)?;
|
||||
Some(function.debug_hir(db))
|
||||
}
|
|
@ -105,6 +105,16 @@ pub(crate) fn handle_syntax_tree(
|
|||
Ok(res)
|
||||
}
|
||||
|
||||
pub(crate) fn handle_view_hir(
|
||||
snap: GlobalStateSnapshot,
|
||||
params: lsp_types::TextDocumentPositionParams,
|
||||
) -> Result<String> {
|
||||
let _p = profile::span("handle_view_hir");
|
||||
let position = from_proto::file_position(&snap, params)?;
|
||||
let res = snap.analysis.view_hir(position)?;
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
pub(crate) fn handle_expand_macro(
|
||||
snap: GlobalStateSnapshot,
|
||||
params: lsp_ext::ExpandMacroParams,
|
||||
|
|
|
@ -53,6 +53,14 @@ pub struct SyntaxTreeParams {
|
|||
pub range: Option<Range>,
|
||||
}
|
||||
|
||||
pub enum ViewHir {}
|
||||
|
||||
impl Request for ViewHir {
|
||||
type Params = lsp_types::TextDocumentPositionParams;
|
||||
type Result = String;
|
||||
const METHOD: &'static str = "rust-analyzer/viewHir";
|
||||
}
|
||||
|
||||
pub enum ExpandMacro {}
|
||||
|
||||
impl Request for ExpandMacro {
|
||||
|
|
|
@ -443,6 +443,7 @@ impl GlobalState {
|
|||
.on_sync::<lsp_ext::MemoryUsage>(|s, p| handlers::handle_memory_usage(s, p))?
|
||||
.on::<lsp_ext::AnalyzerStatus>(handlers::handle_analyzer_status)
|
||||
.on::<lsp_ext::SyntaxTree>(handlers::handle_syntax_tree)
|
||||
.on::<lsp_ext::ViewHir>(handlers::handle_view_hir)
|
||||
.on::<lsp_ext::ExpandMacro>(handlers::handle_expand_macro)
|
||||
.on::<lsp_ext::ParentModule>(handlers::handle_parent_module)
|
||||
.on::<lsp_ext::Runnables>(handlers::handle_runnables)
|
||||
|
|
|
@ -227,6 +227,8 @@ There are also two VS Code commands which might be of interest:
|
|||
|
||||
* `Rust Analyzer: Syntax Tree` shows syntax tree of the current file/selection.
|
||||
|
||||
* `Rust Analyzer: View Hir` shows the HIR expressions within the function containing the cursor.
|
||||
|
||||
You can hover over syntax nodes in the opened text file to see the appropriate
|
||||
rust code that it refers to and the rust editor will also highlight the proper
|
||||
text range.
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<!---
|
||||
lsp_ext.rs hash: 203fdf79b21b5987
|
||||
lsp_ext.rs hash: 91f2c62457e0a20f
|
||||
|
||||
If you need to change the above hash to make the test pass, please check if you
|
||||
need to adjust this doc as well and ping this issue:
|
||||
|
@ -449,6 +449,17 @@ interface SyntaxTeeParams {
|
|||
Returns textual representation of a parse tree for the file/selected region.
|
||||
Primarily for debugging, but very useful for all people working on rust-analyzer itself.
|
||||
|
||||
## View Hir
|
||||
|
||||
**Method:** `rust-analyzer/viewHir`
|
||||
|
||||
**Request:** `TextDocumentPositionParams`
|
||||
|
||||
**Response:** `string`
|
||||
|
||||
Returns a textual representation of the HIR of the function containing the cursor.
|
||||
For debugging or when working on rust-analyzer itself.
|
||||
|
||||
## Expand Macro
|
||||
|
||||
**Method:** `rust-analyzer/expandMacro`
|
||||
|
|
|
@ -103,6 +103,11 @@
|
|||
"title": "Show Syntax Tree",
|
||||
"category": "Rust Analyzer"
|
||||
},
|
||||
{
|
||||
"command": "rust-analyzer.viewHir",
|
||||
"title": "View Hir",
|
||||
"category": "Rust Analyzer"
|
||||
},
|
||||
{
|
||||
"command": "rust-analyzer.expandMacro",
|
||||
"title": "Expand macro recursively",
|
||||
|
@ -1006,6 +1011,10 @@
|
|||
"command": "rust-analyzer.syntaxTree",
|
||||
"when": "inRustProject"
|
||||
},
|
||||
{
|
||||
"command": "rust-analyzer.viewHir",
|
||||
"when": "inRustProject"
|
||||
},
|
||||
{
|
||||
"command": "rust-analyzer.expandMacro",
|
||||
"when": "inRustProject"
|
||||
|
|
|
@ -340,6 +340,61 @@ export function syntaxTree(ctx: Ctx): Cmd {
|
|||
};
|
||||
}
|
||||
|
||||
// Opens the virtual file that will show the HIR of the function containing the cursor position
|
||||
//
|
||||
// The contents of the file come from the `TextDocumentContentProvider`
|
||||
export function viewHir(ctx: Ctx): Cmd {
|
||||
const tdcp = new class implements vscode.TextDocumentContentProvider {
|
||||
readonly uri = vscode.Uri.parse('rust-analyzer://viewHir/hir.txt');
|
||||
readonly eventEmitter = new vscode.EventEmitter<vscode.Uri>();
|
||||
constructor() {
|
||||
vscode.workspace.onDidChangeTextDocument(this.onDidChangeTextDocument, this, ctx.subscriptions);
|
||||
vscode.window.onDidChangeActiveTextEditor(this.onDidChangeActiveTextEditor, this, ctx.subscriptions);
|
||||
}
|
||||
|
||||
private onDidChangeTextDocument(event: vscode.TextDocumentChangeEvent) {
|
||||
if (isRustDocument(event.document)) {
|
||||
// We need to order this after language server updates, but there's no API for that.
|
||||
// Hence, good old sleep().
|
||||
void sleep(10).then(() => this.eventEmitter.fire(this.uri));
|
||||
}
|
||||
}
|
||||
private onDidChangeActiveTextEditor(editor: vscode.TextEditor | undefined) {
|
||||
if (editor && isRustEditor(editor)) {
|
||||
this.eventEmitter.fire(this.uri);
|
||||
}
|
||||
}
|
||||
|
||||
provideTextDocumentContent(_uri: vscode.Uri, ct: vscode.CancellationToken): vscode.ProviderResult<string> {
|
||||
const rustEditor = ctx.activeRustEditor;
|
||||
const client = ctx.client;
|
||||
if (!rustEditor || !client) return '';
|
||||
|
||||
const params = {
|
||||
textDocument: client.code2ProtocolConverter.asTextDocumentIdentifier(rustEditor.document),
|
||||
position: client.code2ProtocolConverter.asPosition(
|
||||
rustEditor.selection.active,
|
||||
),
|
||||
};
|
||||
return client.sendRequest(ra.viewHir, params, ct);
|
||||
}
|
||||
|
||||
get onDidChange(): vscode.Event<vscode.Uri> {
|
||||
return this.eventEmitter.event;
|
||||
}
|
||||
};
|
||||
|
||||
ctx.pushCleanup(vscode.workspace.registerTextDocumentContentProvider('rust-analyzer', tdcp));
|
||||
|
||||
return async () => {
|
||||
const document = await vscode.workspace.openTextDocument(tdcp.uri);
|
||||
tdcp.eventEmitter.fire(tdcp.uri);
|
||||
void await vscode.window.showTextDocument(document, {
|
||||
viewColumn: vscode.ViewColumn.Two,
|
||||
preserveFocus: true
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
// Opens the virtual file that will show the syntax tree
|
||||
//
|
||||
|
|
|
@ -24,6 +24,7 @@ export interface SyntaxTreeParams {
|
|||
}
|
||||
export const syntaxTree = new lc.RequestType<SyntaxTreeParams, string, void>("rust-analyzer/syntaxTree");
|
||||
|
||||
export const viewHir = new lc.RequestType<lc.TextDocumentPositionParams, string, void>("rust-analyzer/viewHir");
|
||||
|
||||
export interface ExpandMacroParams {
|
||||
textDocument: lc.TextDocumentIdentifier;
|
||||
|
|
|
@ -105,6 +105,7 @@ async function tryActivate(context: vscode.ExtensionContext) {
|
|||
ctx.registerCommand('joinLines', commands.joinLines);
|
||||
ctx.registerCommand('parentModule', commands.parentModule);
|
||||
ctx.registerCommand('syntaxTree', commands.syntaxTree);
|
||||
ctx.registerCommand('viewHir', commands.viewHir);
|
||||
ctx.registerCommand('expandMacro', commands.expandMacro);
|
||||
ctx.registerCommand('run', commands.run);
|
||||
ctx.registerCommand('debug', commands.debug);
|
||||
|
|
Loading…
Add table
Reference in a new issue