Skip to content

Commit

Permalink
Merge #7068
Browse files Browse the repository at this point in the history
7068: Add VSCode command to view the hir of a function body r=theotherphil a=theotherphil

Will fix #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.

![image](https://user-images.githubusercontent.com/1974256/103236081-bb58f700-493b-11eb-9d12-55ae1b870f8f.png)


Co-authored-by: Phil Ellison <phil.j.ellison@gmail.com>
  • Loading branch information
bors[bot] and theotherphil authored Jan 3, 2021
2 parents 520b8a5 + ee7c3f7 commit 1cc73d6
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 2 deletions.
15 changes: 14 additions & 1 deletion crates/hir/src/code_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions crates/ide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
}
Expand Down
25 changes: 25 additions & 0 deletions crates/ide/src/view_hir.rs
Original file line number Diff line number Diff line change
@@ -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))
}
10 changes: 10 additions & 0 deletions crates/rust-analyzer/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions crates/rust-analyzer/src/lsp_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions crates/rust-analyzer/src/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions docs/dev/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 12 additions & 1 deletion docs/dev/lsp-extensions.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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`
Expand Down
9 changes: 9 additions & 0 deletions editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -1006,6 +1011,10 @@
"command": "rust-analyzer.syntaxTree",
"when": "inRustProject"
},
{
"command": "rust-analyzer.viewHir",
"when": "inRustProject"
},
{
"command": "rust-analyzer.expandMacro",
"when": "inRustProject"
Expand Down
55 changes: 55 additions & 0 deletions editors/code/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down
1 change: 1 addition & 0 deletions editors/code/src/lsp_ext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions editors/code/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 1cc73d6

Please sign in to comment.