aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhil Ellison <[email protected]>2021-01-01 19:25:18 +0000
committerPhil Ellison <[email protected]>2021-01-01 19:25:18 +0000
commitdb53db804604574337f80a614565fb964403a654 (patch)
tree91b5ef245f803183d33f3d41511330e77d59c3c9
parent077592a12fd982de3e69572a4c738dd4468617f9 (diff)
Address review suggestion, fix tidy tests
-rw-r--r--crates/hir/src/code_model.rs18
-rw-r--r--crates/ide/src/view_hir.rs24
-rw-r--r--docs/dev/README.md2
-rw-r--r--docs/dev/lsp-extensions.md13
4 files changed, 34 insertions, 23 deletions
diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs
index 9b78944c6..f68299d3a 100644
--- a/crates/hir/src/code_model.rs
+++ b/crates/hir/src/code_model.rs
@@ -1,5 +1,5 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2use std::{iter, sync::Arc}; 2use std::{fmt::Write, iter, sync::Arc};
3 3
4use arrayvec::ArrayVec; 4use arrayvec::ArrayVec;
5use base_db::{CrateDisplayName, CrateId, Edition, FileId}; 5use base_db::{CrateDisplayName, CrateId, Edition, FileId};
@@ -729,8 +729,7 @@ impl DefWithBody {
729 729
730#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 730#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
731pub struct Function { 731pub struct Function {
732 // DO NOT MERGE: this was previously pub(crate) 732 pub(crate) id: FunctionId,
733 pub id: FunctionId,
734} 733}
735 734
736impl Function { 735impl Function {
@@ -798,6 +797,19 @@ impl Function {
798 pub fn has_body(self, db: &dyn HirDatabase) -> bool { 797 pub fn has_body(self, db: &dyn HirDatabase) -> bool {
799 db.function_data(self.id).has_body 798 db.function_data(self.id).has_body
800 } 799 }
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 }
801} 813}
802 814
803// Note: logically, this belongs to `hir_ty`, but we are not using it there yet. 815// Note: logically, this belongs to `hir_ty`, but we are not using it there yet.
diff --git a/crates/ide/src/view_hir.rs b/crates/ide/src/view_hir.rs
index e48f2cfe0..cfcfb7cfb 100644
--- a/crates/ide/src/view_hir.rs
+++ b/crates/ide/src/view_hir.rs
@@ -1,11 +1,9 @@
1use hir::{Function, Semantics}; 1use hir::{Function, Semantics};
2use hir::db::DefDatabase;
3use ide_db::base_db::FilePosition; 2use ide_db::base_db::FilePosition;
4use ide_db::RootDatabase; 3use ide_db::RootDatabase;
5use syntax::{AstNode, algo::find_node_at_offset, ast}; 4use syntax::{algo::find_node_at_offset, ast, AstNode};
6use std::fmt::Write;
7 5
8// Feature: View hir 6// Feature: View Hir
9// 7//
10// |=== 8// |===
11// | Editor | Action Name 9// | Editor | Action Name
@@ -20,20 +18,8 @@ fn body_hir(db: &RootDatabase, position: FilePosition) -> Option<String> {
20 let sema = Semantics::new(db); 18 let sema = Semantics::new(db);
21 let source_file = sema.parse(position.file_id); 19 let source_file = sema.parse(position.file_id);
22 20
23 let function = find_node_at_offset::<ast::Fn>( 21 let function = find_node_at_offset::<ast::Fn>(source_file.syntax(), position.offset)?;
24 source_file.syntax(),
25 position.offset,
26 )?;
27 22
28 let function: Function = sema.to_def(&function)?; 23 let function: Function = sema.to_def(&function)?;
29 let body = db.body(function.id.into()); 24 Some(function.debug_hir(db))
30 25}
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} \ No newline at end of file
diff --git a/docs/dev/README.md b/docs/dev/README.md
index 4a2f9feb3..55527bab0 100644
--- a/docs/dev/README.md
+++ b/docs/dev/README.md
@@ -227,6 +227,8 @@ There are also two VS Code commands which might be of interest:
227 227
228* `Rust Analyzer: Syntax Tree` shows syntax tree of the current file/selection. 228* `Rust Analyzer: Syntax Tree` shows syntax tree of the current file/selection.
229 229
230* `Rust Analyzer: View Hir` shows the HIR expressions within the function containing the cursor.
231
230 You can hover over syntax nodes in the opened text file to see the appropriate 232 You can hover over syntax nodes in the opened text file to see the appropriate
231 rust code that it refers to and the rust editor will also highlight the proper 233 rust code that it refers to and the rust editor will also highlight the proper
232 text range. 234 text range.
diff --git a/docs/dev/lsp-extensions.md b/docs/dev/lsp-extensions.md
index 8c01db07c..78d86f060 100644
--- a/docs/dev/lsp-extensions.md
+++ b/docs/dev/lsp-extensions.md
@@ -1,5 +1,5 @@
1<!--- 1<!---
2lsp_ext.rs hash: 203fdf79b21b5987 2lsp_ext.rs hash: 91f2c62457e0a20f
3 3
4If you need to change the above hash to make the test pass, please check if you 4If you need to change the above hash to make the test pass, please check if you
5need to adjust this doc as well and ping this issue: 5need to adjust this doc as well and ping this issue:
@@ -449,6 +449,17 @@ interface SyntaxTeeParams {
449Returns textual representation of a parse tree for the file/selected region. 449Returns textual representation of a parse tree for the file/selected region.
450Primarily for debugging, but very useful for all people working on rust-analyzer itself. 450Primarily for debugging, but very useful for all people working on rust-analyzer itself.
451 451
452## View Hir
453
454**Method:** `rust-analyzer/viewHir`
455
456**Request:** `TextDocumentPositionParams`
457
458**Response:** `string`
459
460Returns a textual representation of the HIR of the function containing the cursor.
461For debugging or when working on rust-analyzer itself.
462
452## Expand Macro 463## Expand Macro
453 464
454**Method:** `rust-analyzer/expandMacro` 465**Method:** `rust-analyzer/expandMacro`