How to collapse specific `if` statements using IDA Python?

In the IDA decompiler view, you can collapse an if statement by right-clicking and selecting “Collapse Item” while the cursor is on it. However, I want to collapse all specific if statements using IDA Python. While I can traverse all if nodes with ctree_visitor_t, I’m unsure how to implement the actual collapse. I’ve come across ida_hexrays.CIT_COLLAPSED, but I’m not sure how to use it. Additionally, vdui.collapse_item only collapses the item under the cursor, which doesn’t solve my issue.

Here’s the approximate code of collapse_item()

  citem_locator_t loc(item.e);
  int32 iflags = cfunc->get_user_iflags(loc);
  if ( hide )
    iflags |= CIT_COLLAPSED;
  else
    iflags &= ~CIT_COLLAPSED;
  cfunc->set_user_iflags(loc, iflags);
  cfunc->save_user_iflags();

Hopefully you can implement a Python version using this.

2 Likes

Thank you very much!!