Skip to content

Automatically fold inactive regions - #14796

Open
Colen Garoutte-Carson (Colengms) wants to merge 4 commits into
mainfrom
dev/coleng/auto_fold_inactive_regions
Open

Colen Garoutte-Carson (Colengms) wants to merge 4 commits into
mainfrom
dev/coleng/auto_fold_inactive_regions

Conversation

@Colengms

Copy link
Copy Markdown
Contributor

Summary

Adds support for folding inactive preprocessor regions.

  • Adds the C/C++: Fold Inactive Regions command for manually folding inactive regions in the active C, C++, or CUDA file.
  • Adds the opt-in C_Cpp.autoFoldInactiveRegions setting to fold inactive regions after IntelliSense finishes processing a newly opened editor.
  • Keeps folding independent of inactive-region colorization, allowing regions to be folded when C_Cpp.dimInactiveRegions is disabled.
  • Leaves existing behavior unchanged by default.

Implementation

Inactive ranges may arrive across multiple IntelliSense result notifications. This change accumulates them per document and waits for a complete IntelliSense pass before folding. This prevents an incomplete result from selecting a nested folding range instead of the surrounding inactive preprocessor branch.

Manual requests made before IntelliSense results are ready remain pending until a complete result arrives. Cached ranges are invalidated when a document changes, opens, or closes, and automatic folding occurs only once per editor instance.

Testing

  • Added unit coverage for accumulating complete inactive-region results, replacing and deleting cached results, and producing sorted unique folding lines.
  • Added SingleRootProject scenario coverage verifying:
    • Manual folding works with inactive-region dimming disabled.
    • The inactive preprocessor branch is folded instead of a nested function.
    • Inactive regions are automatically folded when the new setting is enabled.
  • TypeScript compilation and ESLint pass.
  • Focused unit tests: 4 passing.
  • SingleRootProject scenario tests: 58 passing.

Fixes #8992

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

URI-scoped folding state is routed through the wrong client in multi-root workspaces, and one-shot folding state has additional gating issues.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 2 Medium severity

Open (2)
What changed in this PR

Adds manual and opt-in automatic folding for inactive preprocessor regions.

Changes:

  • Adds inactive-region caching and folding orchestration.
  • Registers the command and setting with localized descriptions.
  • Adds unit and scenario coverage.
File Description
Extension/​src/​LanguageServer/​inactiveRegions.ts Implements inactive-region storage and line selection.
Extension/​src/​LanguageServer/​client.ts Integrates caching, manual folding, and automatic folding.
Extension/​src/​LanguageServer/​extension.ts Registers the folding command handler.
Extension/​src/​LanguageServer/​settings.ts Exposes the automatic-fold setting.
Extension/​package.json Contributes the command, setting, and command-palette condition.
Extension/​package.nls.json Adds user-facing command and setting text.
Extension/​test/​unit/​inactiveRegions.test.ts Tests region accumulation and line generation.
Extension/​test/​scenarios/​SingleRootProject/​tests/​inactiveFolding.test.ts Tests manual and automatic folding scenarios.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread Extension/src/LanguageServer/client.ts Outdated
Comment thread Extension/src/LanguageServer/client.ts Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🔵 Needs a closer look

Manual folding can target a stale workspace client and incorrectly suppress later automatic folding.

Review effort: Balanced
Findings: None

Resolved since last review (2)
Previously missed (2)

In code that hasn't changed since last review

Medium severity Manual folds incorrectly suppress later automatic folding

Extension/​src/​LanguageServer/​client.ts:3018

A manual fold also marks the editor as already auto-folded. If the user manually folds, unfolds, and then enables autoFoldInactiveRegions, the settings-change path will refuse to auto-fold this editor even though no automatic fold occurred. Only record the editor when autoFold is actually true.

Medium severity Route document command using the current editor URI

Extension/​src/​LanguageServer/​extension.ts:931

Route this document-scoped command from the current editor URI. ClientCollection.didChangeActiveEditor updates ActiveClient only after awaiting the new client's editor-change notification, so invoking this command during a multi-root editor switch can add the pending fold to the previous client; the actual owner receives the ranges and never fulfills that request.

@Colengms
Colen Garoutte-Carson (Colengms) marked this pull request as ready for review September 24, 2026 22:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

The implementation handles result accumulation, lifecycle invalidation, multi-root routing, and configuration behavior with focused coverage.

Review effort: Balanced
Findings: None

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

Labels

None yet

Projects

Status: Pull Request

Development

Successfully merging this pull request may close these issues.

Automatically fold inactive regions

2 participants