Skip to content

✨ Code Folding #43

Description

@gaetgu

Description

Code folding allows developers to collapse and expand sections of code (such as functions, classes, conditionals, and comments) to reduce visual clutter and focus on the parts they’re working on. This feature is particularly useful when navigating large files or deeply nested structures.

Proposed Behavior

  • Enable folding for code blocks such as:
    • Functions and methods
    • Classes and structs
    • Loops and conditionals
    • Multi-line comments and regions
  • Display a vertical folding ribbon in the gutter next to line numbers, where users can click to collapse or expand blocks.
  • Collapsed blocks should show an inline indicator with an expand toggle.
  • Optionally, include editor commands for fold all / unfold all.

Implementation Notes

  • Can be powered by the existing Tree-sitter syntax trees to determine foldable ranges based on code structure.
  • Should maintain fold state across file/tab switches and ideally across sessions.
  • Folding ribbon interactions should be smooth and intuitive, with hover states and visual cues.
  • Should add animation for folding and unfolding to make transitions feel natural and responsive.

Benefits

  • Reduces visual noise and helps maintain focus while editing.
  • Makes it easier to navigate and manage large files.
  • Provides a cleaner overview of structural code layout.

Additional Context

The folding ribbon provides a familiar and discoverable UI for interacting with folded sections and complements keyboard or menu-based folding commands.

Screenshots

Screen.Recording.2022-04-24.at.8.32.40.AM.mov
Additional Screenshots Screen Shot 2022-04-24 at 8 30 03 AM

Hover:
Screen Shot 2022-04-24 at 8 31 04 AM

Clicked (folded code):
Screen Shot 2022-04-24 at 8 31 40 AM

dark and light mode:
imgonline-com-ua-twotoone-0btV6nX2OfPxYH

In Progress TODOs

  • Fold Ribbon
    • Draw nested folds
    • Draw adjacent folds with a horizontal line (instead of overlapping rounded edges)
    • Draw folded ranges
  • Fold Model
    • Generate folds from a provider for a document
    • Update folds as a document is edited
  • Fold Providers
    • Line indent provider
    • TreeSitter provider
  • Perform Folding
    • Draw text attachment for fold range
    • Animate folded text

Activity

  1. gaetgu commented on Apr 26, 2022

    @gaetgu
    Author

    I have been messing around with CoreGraphics a little bit, and it looks like the ribbon is a gray (in light mode), transparent line with a width of 7 (not 6 or 8, which I find strange) and a round cap. They also have a very fine white stroke around the edge. My experiments are not that great and I can't quite get them to play nice with SwiftUI views, but I thought I would leave that basic information here in case it helps.

  2. nanashili commented on Apr 27, 2022

    @nanashili

    I have been messing around with CoreGraphics a little bit, and it looks like the ribbon is a gray (in light mode), transparent line with a width of 7 (not 6 or 8, which I find strange) and a round cap. They also have a very fine white stroke around the edge. My experiments are not that great and I can't quite get them to play nice with SwiftUI views, but I thought I would leave that basic information here in case it helps.

    Apple likes using odd numbers in everything

  3. austincondiff commented on Apr 27, 2022

    @austincondiff
    Collaborator

    I never understood this. I typically like designing in multiples of 2 (2, 4, 8, 12, 16, 20, 24, 32, 40, 48, 56, 64, 72, 80, 96, 104, etc.) because they can always divide into two without falling on a subpixel. This is especially helpful when dealing with responsive layouts and 1x and 2x densities. I think Apple chooses to design in odd numbers so that it can always have a center pixel.

  4. deleted a comment from stale on Jun 26, 2022
  5. changed the title [-]✨ Add Code Folding Ribbon[/-] [+]✨ Code Folding Ribbon[/+] on Jun 28, 2022
  6. austincondiff commented on Jan 25, 2023

    @austincondiff
    Collaborator

    This may require #127 to be complete in order to begin working on this.

  7. inlinecoder commented on Jan 9, 2025

    @inlinecoder

    My 5 cents: while this ribbon looks great in XCode... well, it just looks great and brings close to zero value.
    What does make life easier and is actually helpful is code blocks indents.

    Hopefully, this feature won't implement XCodes' direction blindly. 🤞

    UPD 5 mins later: I was referring to #65

    Image
  8. 3 remaining items

  9. moved this from 📋 Todo to 🏃‍♂️ In Progress in CodeEdit Projecton Apr 25, 2025
  10. added a commit that references this issue on May 9, 2025
  11. thecoolwinter commented on Jun 3, 2025

    @thecoolwinter
    Collaborator

    Updated the todo items with the changes in #331. Very close to being done here

  12. uuip commented on Jul 16, 2025

    @uuip

    I've been waiting for this feature for a long time, as I often need to open, format, and collapse JSON files. Looking forward to the author's great work.

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions