nvim_python_fold

View repository ↗

A Cursor-aware Neovim plugin for Python folding using tree-sitter. Cycle individual classes and functions through fold states without hunting for fold points.

Usage

Place your cursor on a class, method, or function and run a command. The node cycles through four effective states:

FOLDED      → signature only (docstring shown as fold header)
DOCSTRING   → signature + full docstring visible, body hidden
BLOCK       → full body visible
UNFOLDED    → fully unfolded

States are per-node — unfolding one function doesn’t affect others. At the boundaries, cycling wraps to all nodes. Functions without docstrings skip the DOCSTRING state transparently.

Commands

CommandAction
:PythonFoldStep toward more folded
:PythonUnfoldStep toward more unfolded

How it works

The plugin uses foldmethod=expr with per-node fold levels tracked in a state machine. On each command, cursor.lua finds the innermost foldable node under the cursor, state.lua computes the next level, and fold.lua rebuilds a per-line fold cache using outermost-first processing so nested nodes inherit their parent’s fold correctly.

Requirements

  • Neovim ≥ 0.12
  • Tree-sitter Python parser (:TSInstall python)

License

MIT