Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: VSCode command to visualise the hir_def::Exprs within a function #7061

Closed
theotherphil opened this issue Dec 28, 2020 · 1 comment · Fixed by #7068
Closed

Feature: VSCode command to visualise the hir_def::Exprs within a function #7061

theotherphil opened this issue Dec 28, 2020 · 1 comment · Fixed by #7068
Labels
A-vscode vscode plugin issues S-actionable Someone could pick this issue up and work on it right now

Comments

@theotherphil
Copy link
Contributor

Like the Syntax Tree command, but at the hir level. This is useful for getting an idea of how all the parts fit together if you're a new contributor (e.g. me!).

Hack I'm using locally to approximate this:

    // function whose contents I want to inspect
    fn bar() {
        // LOOK FOR THIS
        let m = [1, 2, 3]
            .iter()
            .filter_map(|x| if *x == 2 { Some (4) } else { None })
            .next();
    }
      // code added within ExprValidator::validate_body

      // first method I found to isolate output to a specific function I was interested in
      let body_syntax_ptr = source_map.expr_syntax(body.body_expr).expect("Unable to find expr_syntax (?!)");
      let root = body_syntax_ptr.file_syntax(db.upcast());
      let body_syntax = body_syntax_ptr.value.syntax_node_ptr().to_node(&root);
      let rendered_syntax = format!("{:#?}", body_syntax);
      if rendered_syntax.contains("LOOK FOR THIS") {

        // the output I'd like to be able to see via the new command
        eprintln!("body is {:?}", rendered_syntax);
        for (id, expr) in body.exprs.iter() {
            eprintln!("{:?}: {:?}", id, expr);
        }
    }

Example output (exact format obviously not important):

Idx::(0): Literal(Int(0, None))
Idx::(1): Literal(Int(0, None))
Idx::(2): Literal(Int(0, None))
Idx::(3): Array(ElementList([Idx::(0), Idx::(1), Idx::(2)]))
Idx::(4): MethodCall { receiver: Idx::(3), method_name: Name(Text("iter")), args: [], generic_args: None }
Idx::(5): Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("Some"))] }, generic_args: [None] })
Idx::(6): Literal(Int(0, None))
Idx::(7): Call { callee: Idx::(5), args: [Idx::(6)] }
Idx::(8): Block { statements: [], tail: Some(Idx::(7)), label: None }
Idx::(9): Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("None"))] }, generic_args: [None] })
Idx::(10): Block { statements: [], tail: Some(Idx::(9)), label: None }
Idx::(11): Path(Path { type_anchor: None, mod_path: ModPath { kind: Plain, segments: [Name(Text("x"))] }, generic_args: [None] })
Idx::(12): UnaryOp { expr: Idx::(11), op: Deref }
Idx::(13): Literal(Int(0, None))
Idx::(14): BinaryOp { lhs: Idx::(12), rhs: Idx::(13), op: Some(CmpOp(Eq { negated: false })) }
Idx::(15): If { condition: Idx::(14), then_branch: Idx::(8), else_branch: Some(Idx::(10)) }
Idx::(16): Lambda { args: [Idx::(1)], arg_types: [None], ret_type: None, body: Idx::(15) }
Idx::(17): MethodCall { receiver: Idx::(4), method_name: Name(Text("filter_map")), args: [Idx::(16)], generic_args: None }
Idx::(18): MethodCall { receiver: Idx::(17), method_name: Name(Text("next")), args: [], generic_args: None }
Idx::(19): Block { statements: [Let { pat: Idx::(0), type_ref: None, initializer: Some(Idx::(18)) }], tail: None, label: None }

@lnicola lnicola added A-vscode vscode plugin issues S-actionable Someone could pick this issue up and work on it right now labels Dec 28, 2020
@theotherphil
Copy link
Contributor Author

I'll have a go at adding this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-vscode vscode plugin issues S-actionable Someone could pick this issue up and work on it right now
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants