]> git.lizzy.rs Git - rust.git/commitdiff
Address review suggestion, fix tidy tests
authorPhil Ellison <phil.j.ellison@gmail.com>
Fri, 1 Jan 2021 19:25:18 +0000 (19:25 +0000)
committerPhil Ellison <phil.j.ellison@gmail.com>
Fri, 1 Jan 2021 19:25:18 +0000 (19:25 +0000)
crates/hir/src/code_model.rs
crates/ide/src/view_hir.rs
docs/dev/README.md
docs/dev/lsp-extensions.md

index 9b78944c6eecf5b40db615ac491e4edea8c270d5..f68299d3a1210dc6aeb96dda63fb6dbfe6bcb2bd 100644 (file)
@@ -1,5 +1,5 @@
 //! FIXME: write short doc here
-use std::{iter, sync::Arc};
+use std::{fmt::Write, iter, sync::Arc};
 
 use arrayvec::ArrayVec;
 use base_db::{CrateDisplayName, CrateId, Edition, FileId};
@@ -729,8 +729,7 @@ pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
 
 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
 pub struct Function {
-    // DO NOT MERGE: this was previously pub(crate)
-    pub id: FunctionId,
+    pub(crate) id: FunctionId,
 }
 
 impl Function {
@@ -798,6 +797,19 @@ pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) {
     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();
+        writeln!(&mut result, "HIR expressions in the body of `{}`:", self.name(db)).unwrap();
+        for (id, expr) in body.exprs.iter() {
+            writeln!(&mut result, "{:?}: {:?}", id, expr).unwrap();
+        }
+
+        result
+    }
 }
 
 // Note: logically, this belongs to `hir_ty`, but we are not using it there yet.
index e48f2cfe02675522679c2dbf5443668342200efe..cfcfb7cfbc00504cae8f8c223df4b462c953f50e 100644 (file)
@@ -1,11 +1,9 @@
 use hir::{Function, Semantics};
-use hir::db::DefDatabase;
 use ide_db::base_db::FilePosition;
 use ide_db::RootDatabase;
-use syntax::{AstNode, algo::find_node_at_offset, ast};
-use std::fmt::Write;
+use syntax::{algo::find_node_at_offset, ast, AstNode};
 
-// Feature: View hir
+// Feature: View Hir
 //
 // |===
 // | Editor  | Action Name
@@ -20,20 +18,8 @@ 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 = find_node_at_offset::<ast::Fn>(source_file.syntax(), position.offset)?;
 
     let function: Function = sema.to_def(&function)?;
-    let body = db.body(function.id.into());
-
-    let mut result = String::new();
-    writeln!(&mut result, "== Body expressions ==").ok()?;
-
-    for (id, expr) in body.exprs.iter() {
-        writeln!(&mut result, "{:?}: {:?}", id, expr).ok()?;
-    }
-
-    Some(result)
-}
\ No newline at end of file
+    Some(function.debug_hir(db))
+}
index 4a2f9feb36912e10197ff4beeed1efef1e32566c..55527bab0aae83fb2f88b6c43da92281a6deeb8f 100644 (file)
@@ -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.
index 8c01db07c8496519960288f4947623b938a2a6ab..78d86f060a67a20825f976b5d7d4ba0779ff7c10 100644 (file)
@@ -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`