feat(ide): expand folding coverage and fix selection in preprocessor regions - #325
Merged
Conversation
- Fold function/task bodies (Subroutine) at file, module, and generate block level, recursing into their statement arenas like blocks. - Recurse into generate blocks: instances, subroutines, assigns, and declarations inside generate now fold individually. - Fold runs of consecutive single-line items into one group fold: package imports (Imports), continuous assigns, and declarations. - Fold multi-line argument lists and concatenations/assignment patterns syntactically, like rust-analyzer's ArgList/Array folds. - Rewrite comment folding as a single syntax walk over token trivia; drop the memchr scan and the ineffective visited_ranges set. - Remove FoldingConfig from the ide API (line_fold_only is purely a client-capability concern at the LSP layer) and the never-populated collapsed_text field.
Selection inside a preprocessor directive used to degenerate to the bare cursor range: directive trivia is zero-width in the syntax tree, so no range could be found. Selection inside an inactive `ifdef branch was impossible altogether, since the disabled code is absent from the tree. Recover both from the preprocessor trace, whose root buffer offsets are the file's own offsets: - directive keyword/name ranges give exact selection steps for `ifdef/`define/`include/... lines; - disabled ranges make the whole inactive branch selectable, which is the natural expand-selection unit for dead code. Also drop the duplicate selection range that appeared for trivia-only files (the comment range and the trivia-to-token range coincided).
Add a rust-analyzer style fixture harness for folding ranges (<fold kind>...</fold> tags) and cover every fold kind: comment runs, block comments, regions, module/port-list split, subroutines (module- and file-level), instances (single and multi), generate blocks, import/assign/declaration groups, pseudo regions, argument lists, concatenations, and config declarations. While writing the region fixtures a pre-existing ordering bug in RegionTreeBuilder::stage surfaced: the closing token's trivia was scanned only after open regions had been force-closed, so an endregion marker attached to endmodule/EOF was silently lost and the region stretched to the end of the container. Scan the trivia first.
collect_item_groups folded a lone multi-line item (e.g. a declaration recovered from a parse error) as a one-element group, duplicating the item fold produced by collect_folds. Require at least two items per group.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Four commits improving folding and selection, driven by the review goals of correctness, cleanliness, and efficiency:
Expand folding coverage (
crates/ide/src/folding_ranges.rs, LSP layer)Subroutine) at file, module, and generate-block level, recursing into their statement arenas like blocks.generatenow fold individually.Imports), continuous assigns, and declarations.visited_rangesset.FoldingConfigfrom the ide API (line_fold_onlyis purely a client-capability concern at the LSP layer) and the never-populatedcollapsed_textfield.Selection inside preprocessor directives and inactive ifdef branches
disabled_rangesfor inactive branch bodies.Fold fixture matrix + region fix
<fold kind>fixture harness covering every fold kind.RegionTreeBuilder::stage: the closing token's trivia was scanned only after open regions were force-closed, so anendregionmarker attached toendmodule/EOF was lost and the region stretched to the end of the container.Single-item group fold fix
collect_item_groupsfolded a lone multi-line item as a one-element group, duplicating the item fold; require at least two items per group.Validation
cargo test --workspacegreen (540+ tests, including 18 new folding fixtures and an extended selection matrix).cargo fmt --checkclean for all touched files.Follow-up
Opened separately:
librarydeclarations in.svfiles (library-map syntax) recover into misleading multi-line declaration folds (see issue).