Skip to content

Commit db53db8

Browse files
committed
Address review suggestion, fix tidy tests
1 parent 077592a commit db53db8

File tree

4 files changed

+34
-23
lines changed

4 files changed

+34
-23
lines changed

crates/hir/src/code_model.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! FIXME: write short doc here
2-
use std::{iter, sync::Arc};
2+
use std::{fmt::Write, iter, sync::Arc};
33

44
use arrayvec::ArrayVec;
55
use base_db::{CrateDisplayName, CrateId, Edition, FileId};
@@ -729,8 +729,7 @@ impl DefWithBody {
729729

730730
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
731731
pub struct Function {
732-
// DO NOT MERGE: this was previously pub(crate)
733-
pub id: FunctionId,
732+
pub(crate) id: FunctionId,
734733
}
735734

736735
impl Function {
@@ -798,6 +797,19 @@ impl Function {
798797
pub fn has_body(self, db: &dyn HirDatabase) -> bool {
799798
db.function_data(self.id).has_body
800799
}
800+
801+
/// A textual representation of the HIR of this function for debugging purposes.
802+
pub fn debug_hir(self, db: &dyn HirDatabase) -> String {
803+
let body = db.body(self.id.into());
804+
805+
let mut result = String::new();
806+
writeln!(&mut result, "HIR expressions in the body of `{}`:", self.name(db)).unwrap();
807+
for (id, expr) in body.exprs.iter() {
808+
writeln!(&mut result, "{:?}: {:?}", id, expr).unwrap();
809+
}
810+
811+
result
812+
}
801813
}
802814

803815
// Note: logically, this belongs to `hir_ty`, but we are not using it there yet.

crates/ide/src/view_hir.rs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
use hir::{Function, Semantics};
2-
use hir::db::DefDatabase;
32
use ide_db::base_db::FilePosition;
43
use ide_db::RootDatabase;
5-
use syntax::{AstNode, algo::find_node_at_offset, ast};
6-
use std::fmt::Write;
4+
use syntax::{algo::find_node_at_offset, ast, AstNode};
75

8-
// Feature: View hir
6+
// Feature: View Hir
97
//
108
// |===
119
// | Editor | Action Name
@@ -20,20 +18,8 @@ fn body_hir(db: &RootDatabase, position: FilePosition) -> Option<String> {
2018
let sema = Semantics::new(db);
2119
let source_file = sema.parse(position.file_id);
2220

23-
let function = find_node_at_offset::<ast::Fn>(
24-
source_file.syntax(),
25-
position.offset,
26-
)?;
21+
let function = find_node_at_offset::<ast::Fn>(source_file.syntax(), position.offset)?;
2722

2823
let function: Function = sema.to_def(&function)?;
29-
let body = db.body(function.id.into());
30-
31-
let mut result = String::new();
32-
writeln!(&mut result, "== Body expressions ==").ok()?;
33-
34-
for (id, expr) in body.exprs.iter() {
35-
writeln!(&mut result, "{:?}: {:?}", id, expr).ok()?;
36-
}
37-
38-
Some(result)
39-
}
24+
Some(function.debug_hir(db))
25+
}

docs/dev/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ There are also two VS Code commands which might be of interest:
227227

228228
* `Rust Analyzer: Syntax Tree` shows syntax tree of the current file/selection.
229229

230+
* `Rust Analyzer: View Hir` shows the HIR expressions within the function containing the cursor.
231+
230232
You can hover over syntax nodes in the opened text file to see the appropriate
231233
rust code that it refers to and the rust editor will also highlight the proper
232234
text range.

docs/dev/lsp-extensions.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!---
2-
lsp_ext.rs hash: 203fdf79b21b5987
2+
lsp_ext.rs hash: 91f2c62457e0a20f
33
44
If you need to change the above hash to make the test pass, please check if you
55
need to adjust this doc as well and ping this issue:
@@ -449,6 +449,17 @@ interface SyntaxTeeParams {
449449
Returns textual representation of a parse tree for the file/selected region.
450450
Primarily for debugging, but very useful for all people working on rust-analyzer itself.
451451

452+
## View Hir
453+
454+
**Method:** `rust-analyzer/viewHir`
455+
456+
**Request:** `TextDocumentPositionParams`
457+
458+
**Response:** `string`
459+
460+
Returns a textual representation of the HIR of the function containing the cursor.
461+
For debugging or when working on rust-analyzer itself.
462+
452463
## Expand Macro
453464

454465
**Method:** `rust-analyzer/expandMacro`

0 commit comments

Comments
 (0)