Folding ranges

textDocument/foldingRange for Pine blocks, type/enum/function bodies, and //#region comments.

This page

Abstract

textDocument/foldingRange folds indented Pine blocks so editors can collapse if / for / while / switch / once, FunctionDef / TypeDef / EnumDef, and //#region … //#endregion pairs. Empty or unparsed documents return [].

Conceptual model

Diagram

Rendering…

Control blocks are expressions (If, ForTo, ForIn, While, Switch, Once) and often sit under Expr. The walker visits the inner node. Parser end_lineno can land on the next statement, so the handler prefers the indented body when source lines are available (else at the header indent continues an if). Single-line constructs are skipped.

Interface surface

  • Method: textDocument/foldingRange
  • Capability: folding_range_provider=True
  • Handler: features/folding.py → handle_folding_range (receives version-matched tree=doc.valid_ast)

Each range uses 0-based start_line / end_line. Region comment pairs set kind: "region".

ConstructFolded
if / else if / elsekeyword line through the last indented body line
for … to / for … insame
while / switch / oncesame
FunctionDef / TypeDef / EnumDefdeclaration through last body line
//#region … //#endregionstart through end marker (# region / # endregion too)

Returns [] for empty source, parse failure, or tree=None.

Internals

PathRole
src/pynescript/langserver/features/folding.pyAST walk + indent clamp + region scan
src/pynescript/langserver/config.pyfolding_range_provider=True
src/pynescript/langserver/server.pyTEXT_DOCUMENT_FOLDING_RANGE handler

Tests: tests/test_lsp_features.py.

See also