ide/
view_hir.rs

1use hir::Semantics;
2use ide_db::{FilePosition, RootDatabase};
3use syntax::AstNode;
4
5// Feature: View Hir
6//
7// | Editor  | Action Name |
8// |---------|--------------|
9// | VS Code | **rust-analyzer: View Hir**
10//
11// ![View Hir](https://user-images.githubusercontent.com/48062697/113065588-068bdb80-91b1-11eb-9a78-0b4ef1e972fb.gif)
12pub(crate) fn view_hir(db: &RootDatabase, position: FilePosition) -> String {
13    (|| {
14        let sema = Semantics::new(db);
15        let source_file = sema.parse_guess_edition(position.file_id);
16        sema.debug_hir_at(source_file.syntax().token_at_offset(position.offset).next()?)
17    })()
18    .unwrap_or_else(|| "Not inside a lowerable item".to_owned())
19}