Skip to content

Conversation

@LukeSerne
Copy link
Contributor

@LukeSerne LukeSerne commented Oct 3, 2025

Closes #1294

This PR copies over spbu-se#1 to the main repo. So thanks to @Gr-i-niy, @Wall-AF and @KirillSmirnov for writing the code and reviewing the original PR. I had no involvement in that PR - I just applied the PR's commits to the latest master, and made some small adjustments to fix merge conflicts.


This PR increases the left margin of the decompiler view and uses it to display "+" and "-" symbols at the start of code blocks. You can click those to fold/unfold code sections.

EDIT: After using this PR for a while, I noticed it still lacks a few features, which I'm planning to implement myself and adding to this PR. The current list:

  • Line numbers don't update when code is (un)folded
  • The + and - symbols are inconsistent with the icons used for folding in the listing display
  • When a block is folded, the opening and closing brace are directly next to each other, without any indication of the collapsed tokens
  • When (un)folding a block, the cursor remains at the same position, but the tokens have moved, so the selected token is now completely different.
  • There are no keyboard shortcuts to fold/unfold a block of code
  • Code that is folded away cannot be found using "Search" (Ctrl+F)

Before
Screenshot showing decompiler view without PR applied

After
Screenshot showing decompiler view with PR applied, clearly showing icons to fold code in the increased left margin

After (folded)
Screenshot showing decompiler view with PR applied and a folded block, clearly showing icons to fold code in the increased left margin

Copy link

@Wall-AF Wall-AF left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LukeSerne It seems to be missing the keyboard method of activation. Is there a different solution for that?

This is more consistent with the UI used for the folding in the listing
display.
This way, the same tokens will keep the same line number, even if
earlier blocks have been folded.
After every syntax token that is an opening curly brace, an extra syntax
token is created with the text "…". This token is collapsed by default,
and uncollapsed when the other tokens in the block are collapsed and
vice versa.
@LukeSerne
Copy link
Contributor Author

@LukeSerne It seems to be missing the keyboard method of activation. Is there a different solution for that?

@Wall-AF Yeah, I noticed that too. It seems to not have been part of the PR I copied. I edited the opening post with a list of bugs / missing features of this PR and added the keyboard shortcuts to that as well.

I saw that for the keyboard shortcuts in #4264, the solution the Ghidra team eventually ended up using was implemented in dd08e5c by creating separate Actions for them. I think I'll try implementing the keyboard shortcuts in the same way - maybe doing it that way has other advantages over the way you suggested in #1294 that I don't see.

Previously, if there were any off-screen collapsed blocks before the
first visible line, the initial line number would be off, since it
didn't take into account the previous folded blocks. This commit fixes
that.
Now, DecompilerPanel#getLineNumber accounts for skipped lines due to
collapsed blocks. As such, it will now return the line number shown in
the margin at a given y-offset. If you want to get an index for the list
returned by getLines(), just use `pixmap.getIndex(y).intValue()`.
This doesn't quite work 100% of the time, but it's close enough...
@osogi
Copy link

osogi commented Oct 7, 2025

Hi, I partially reviewed the original PR in spbu-se. I wanted to point out that this implementation breaks the search functionality (and likely some other features) because the "text" of the folded part completely disappears. I have a private fix for this, but it's more of a hack since it's bad from both an architectural and performance perspective.

@LukeSerne
Copy link
Contributor Author

Ah yes, you're right. Thanks for reporting it, I added it to the list. Most of this PR is not great from an architectural and performance perspective anyway, so feel free to publish your fix and I can add it to this PR. The implementation that keeps the cursor at the same token in the decompiler is also quite hacky and not too great from a performance point of view.

It seems like the code folding in the listing that was recently merged instead uses an invisible font for the text that is folded away. Maybe this implementation should be more similar to that to break fewer implicit assumptions in the existing code that deals with the decompiler listing. Not sure how doable implementing that is though.

@osogi
Copy link

osogi commented Oct 10, 2025

so feel free to publish your fix and I can add it to this PR

Here you are
https://github.com/osogi/ghidra-sbpu-se/commits/codefolding-search-fix/

I'll be glad if it will be useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Feature: Decompiler Status: Triage Information is being gathered

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Code folding/Indentation marking in DecompilerPanel

6 participants