eddiebergman / nvim-treesitter-pyfold

Some sane Python folding with nvim-treesitter
33 stars 3 forks source link
folding nvim python treesitter

nvim-treesitter-pyfold

Folding is a great way to declutter the screen from irrelevant context but overfolding can be equally annoying. Hence, a lua based module for nvim-treesitter, setting some sane folding defaults and provides an optional foldtext.

Installation

Using your favourite package manager

# vim-plug
Plug 'nvim-treesitter-pyfold'

# Packer
require('packer').setup {
    ...
    use 'eddiebergman/nvim-treesitter-pyfold'
    ...
}

Once your package manager has installed the package, in your nvim-treesitter setup, you simply enable the package with:

require('nvim-treesitter.configs').setup {
    ...
    pyfold = {
        enable = true
        custom_foldtext = true -- Sets provided foldtext on window where module is active
    }
    ...
}

If you havn't already, to use treesitter folding, you have to set the following two lines in your .vimrc at some point.

set foldmethod=expr
set foldexpr=nvim_treesitter#fold_expr()

For debuging issues, make these two lines are set using set foldmethod and set foldexpr to verify these are set.

Example

Classes

Classes folding

Functions / Methods

Functions example Multline signature Functions Based on Docs present

Dicts, Lists, Tuples

Dicts, Lists, Tuples folding

TODO

Desired

-- imports



The C part of the python tree-sitter parser doesn't want to fold any 
'queryable' that only lives on one line, even if you group multiple of them
together in query. Please let me know if you know of any workaround for this!