Skip to content

Add syntax-aware enclosing definitions and folded output - #3998

Open
Matei02355 wants to merge 3 commits into
sharkdp:masterfrom
Matei02355:enclosing-scopes-2228-20260907
Open

Add syntax-aware enclosing definitions and folded output#3998
Matei02355 wants to merge 3 commits into
sharkdp:masterfrom
Matei02355:enclosing-scopes-2228-20260907

Conversation

@Matei02355

@Matei02355 Matei02355 commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Add --function-context/-W N to select and highlight the enclosing function, method or type, and --fold to collapse complete block interiors, multiline comments and consecutive imports/includes. Expose both operations through PrettyPrinter.

Index Syntect definition-name scopes and block punctuation before rendering, so nested control blocks and braces in comments or strings do not incorrectly establish function boundaries. Select the innermost complete function/type for each requested line; namespace-contained globals and unrecognized/incomplete constructs fall back to the selected line. Folding keeps opening and closing block lines and combines nested omissions. Existing line numbers, syntax coloring, headers and snip separators remain available.

Both modes read the complete input and are mutually exclusive with streaming mode, explicit line ranges and Git diff mode. The index covers brace-delimited definitions rather than a compiler AST; indentation-only definitions are documented as unsupported. Plain piped output contains retained source lines, while decorated snip output marks omissions. Preserve real file metadata and independently index each input. Fix UTF-16BE first-line initialization, which otherwise consumes two physical lines and shifts the rendered selection.

Validation: all 493 all-feature tests passed (7 ignored), including 20 new tests covering C, C++, Java, Objective-C, Rust, nested definitions, multiline signatures, strings/comments, globals, prototypes, incomplete constructs, repeated selections, numbering, snips, multiple files, automatic syntax detection, UTF-16LE/BE, CRLF, coloring, folding and library usage. All 8 default-feature help tests, all-target/all-feature Clippy with warnings denied, the minimal library build, formatting, whitespace checks and Bash/Zsh completion syntax passed. A subsequent adjacent-comment regression confirms that code between comments on a shared line remains visible; all 7 structural unit tests and 13 structural integration tests passed again, followed by all-target/all-feature Clippy. README, manual, help and four completions are updated.

Fixes #2228.
Fixes #3005.

@Matei02355
Matei02355 marked this pull request as ready for review September 7, 2026 18:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Can we show folded output for language (c / java) files ? Show enclosing context for highlight

1 participant