Skip to content

Access AstNodeIds of a AstNode's children #3662

@rzvxa

Description

@rzvxa

I need a way to access AstNodeIds of a specific node's children. Let's say we have an AstNode of an IfStatement or SwitchStatement, There are times that we would want to intercept these and infer information about their children to limit a procedure to a specific scope instead of going the other way around using ancestors iterator.

It can be convenient to provide a way to do it without the overhead of a search. I can think of 2 ways, We could either generate a reverse hashmap between ast elements and their IDs Or add the AstNodeId to the ast elements to prevent the hashmap entirely.

Both of these can be feature-gated, but it's a really handy thing to have around and can provide utility even in cases where it wasn't necessary.

If anyone has a better suggestion for this please don't hesitate to pitch in!

Wouldn't it be good if we could do this?

let case_nodes = switch.cases.iter().map(|it| ctx.nodes().get_node(it.ast_node_id()));

Or

let case_nodes = switch.cases.iter().map(|it| ctx.get_ast_node_id(node));

Or even

// notice it isn't exactly the same, we get "children" instead of "cases".
let children = ctx.nodes().children_nodes(switch_node.id())

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory - New feature or request

    Type

    No type

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions