Fold consecutive single-line comments without breaking VS Code native folding.
Why this extension exists
An earlier comment-folding extension returned only comment ranges. In VS Code, once any folding provider is registered for a language, the indentation fallback is switched off, so Python functions, classes, if/for blocks and similar native folds disappeared until that extension was uninstalled. This extension takes a different approach: one provider returns the complete set of ranges — structural folding (indentation / braces / tags / #region) computed by the extension itself, plus comment folding — merged with VS Code's own merge rules applied up front. Native folding is therefore reproduced, not hoped for.
Install
code --install-extension vscode-comment-folder-1.1.7.vsix
Or in VS Code: Extensions → … → Install from VSIX, then choose the .vsix file. No change to editor.foldingStrategy is needed, and no language server (Pylance, TypeScript, and so on) needs to be disabled.
Features
- Fold consecutive single-line comments of 2 or more lines (configurable via
codeCommentFolder.minimumLines, default 2), multi-line block comments (/* … */, <!-- … -->), and consecutive single-line block comments.
- Any indentation level, multiple independent comment blocks, and comments inside functions and classes.
- Blank lines, indentation changes, and code lines end a block by default; see Blank-line rules below.
- Comment markers by language ID, not by symbol alone:
#, //, --, ;, plus block comments (/* … */, <!-- … -->) and tag-based folding for HTML/XML (see SUPPORTED_LANGUAGES.md in the source tree, 50 language IDs).
- Symbols inside strings, triple-quoted strings, block comments, and URLs are never treated as comments.
#region … #endregion folds as a Region range.
- Commands:
VSCode Comment Folder MM: Fold All Comment Blocks, VSCode Comment Folder MM: Unfold All Comment Blocks, and VSCode Comment Folder MM: Show Folding Ranges for This File (lists the ranges for the current file in the VSCode Comment Folder MM output channel, 1-based line numbers — useful when a comment does not fold as expected).
Blank-line rules
Default codeCommentFolder.blankLinesInsideBlock = 0: a blank line ends a comment block.
# a
# b
# c
# d
This is two independent blocks (lines 1–2 and 4–5), not one large block. Set the value to 1 to tolerate one blank line inside a block; the folded range still ends at the last comment line, and two consecutive blank lines still split a block. Set it to 2 to tolerate up to two blank lines.
Folding interaction and styling: who controls what
- This extension controls: which lines can fold, the range start/end, and the kind. Comment blocks use
FoldingRangeKind.Comment, so VS Code's built-in "Fold All Block Comments" also folds them. Structural ranges have no kind; #region ranges use Region.
- VS Code controls: the gutter chevron, the
... shown after a folded line, hover highlighting, and folding shortcuts. An extension cannot restyle those; they come from the editor theme. To always show folding controls, set "editor.showFoldingControls": "always" in VS Code settings.
- Interaction: click the gutter chevron; use the folding shortcuts (
Ctrl+K Ctrl+[ / Ctrl+K Ctrl+] on Windows/Linux, Cmd+K Cmd+[ / Cmd+K Cmd+] on macOS); or use the commands above.
- One platform limitation: if
editor.foldingStrategy is set to "indentation", VS Code does not call any folding provider, so comment folding does not appear. Keep the default "auto".
Custom languages
"codeCommentFolder.customLanguages": {
"myLang": { "lineComment": "#", "mode": "indent" }
}
mode is "indent" for indentation-based languages, "brace" for brace-based languages, or "tag" for tag-based languages (HTML/XML). blockComment adds block-comment folding, e.g. "blockComment": ["/*", "*/"], and brackets folds [ … ] spans. Custom languages are registered only for that language ID.
Tests
test/folding.test.ts (Node's built-in node:test, 33 tests) covers tests A–D from the original specification — Python, JavaScript, Java, Shell/YAML/Ruby — plus edge cases (strings/URLs, blank lines, different indents, triple-quoted strings, #region, unsupported languages), HTML/XML tag mode, block comments, JSON/JSONC, and the YAML dialect language IDs. Run npm test. Results are in TEST_RESULTS.md in the source tree.
Architecture (short version)
src/folding.ts: pure core, no vscode import, unit-testable. One scan computes structural ranges plus comment ranges, then applies VS Code's merge rules up front (same start: comment wins; partial overlap: the later range is dropped; nested ranges are kept).
src/extension.ts: thin adapter that converts ranges to vscode.FoldingRange objects, caches by document version, and registers the provider only for explicitly supported languages — never '*'.
src/languageRules.ts: language ID → comment marker / structural mode map.
Known limitations
The core is a heuristic scanner (indentation / braces plus a string-state machine), not a full parser, so extreme syntax (for example braces inside template strings) may differ slightly from a language server's ranges. The VS Code API does not allow one extension to safely read another provider's ranges; that is a platform limitation, not something this extension can work around. See ANALYSIS.md in the source tree for the full analysis.
License
MIT