Skip to content

Conversation

@kv9898
Copy link
Contributor

@kv9898 kv9898 commented Jan 7, 2026

Summary

Resolves #2019.

Implements structured comment headers for code organization, inspired by R's convention. Sections like # Title ---- (4+ trailing delimiters: -, #, =) create hierarchical structure based on # count.

Example:

# Level 1 ----
class person:
    name: str

## Level 2 ----
a = 1

def greeting():
    print("hello, world")

### Level 3 -=-=-----

# Another Level 1 ----
b = 2

## Another Level 2 ====
c = 3
d = 4

# Final Level 1 ####

Outline and folding:
image

Provides:

  • Folding ranges: Sections fold hierarchically from their line to the next same/higher level section
  • Document symbols: Sections appear in outline tree with proper nesting. AST symbols (variables, functions, classes) are nested as children of their preceding comment section, enabling proper code organization in the outline view.

Implementation

  • comment_section.rs: Regex-based parser (^(#+)\s+(.+?)\s+([-#=]{4,})\s*$) extracts sections with level detection
  • folding.rs: Integrates sections as FoldingRangeKind::Region with hierarchical folding support
  • document_symbols.rs: Sequential processing of statements and sections. AST symbols are added as children of the current section context. Uses path-based navigation for robust parent-child relationships in the symbol tree.
  • Safe integer conversions prevent overflow on large files

Test Plan

  • 6 unit tests for parsing (levels, delimiters, edge cases)
  • 2 integration tests for folding (basic + nested hierarchy)
  • 2 integration tests for document symbols (nesting validation, AST children under sections)
  • All existing folding/symbol tests pass (no regressions)
  • Verified proper nesting: variables, functions, and classes appear as children of their preceding comment section

Copilot AI and others added 4 commits January 7, 2026 08:30
- Add comment_section module to parse Python comment sections (# Section ----)
- Integrate comment sections into folding ranges with hierarchical support
- Integrate comment sections into document symbols with nesting
- Add tests for folding ranges with comment sections
- Add tests for document symbols with comment sections

Co-authored-by: kv9898 <[email protected]>
- Fix fragile parent matching in document symbols using path-based approach
- Add safe integer conversion for file lengths to avoid overflow
- Document behavior for extremely long lines in comment sections

Co-authored-by: kv9898 <[email protected]>
- AST symbols (variables, functions, classes) are now children of their preceding comment section
- Symbols between sections are properly nested in the section hierarchy
- Updated test expectations to reflect new nested structure
- Added comprehensive test for the exact user scenario

Co-authored-by: kv9898 <[email protected]>
@meta-cla meta-cla bot added the cla signed label Jan 7, 2026
@kv9898 kv9898 marked this pull request as draft January 7, 2026 13:19
@kv9898 kv9898 marked this pull request as ready for review January 7, 2026 13:29
@github-actions
Copy link

github-actions bot commented Jan 7, 2026

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@stroxler
Copy link
Contributor

stroxler commented Jan 9, 2026

Thanks for the PR!

Assigning to @yangdanny97 because our team chatted about this - we'd like to support the feature which seems useful, but we also want to make sure it doesn't interfere with traditional semantic folding, so we may add decide to a feature flag for this behavior so that it is opt-in

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support section-folding comments in Python (similar to R’s # ---- convention)

3 participants