Giter Club home page Giter Club logo

vim-foldfunctions's Introduction

vim-foldfunctions

Find top-level functions (from a naive parsing POV, not actual language syntax), and make them the only folds in the file.

In supported files, overrides the default foldmethod with the included expr handling approach, which depends on sane indentation (functions start and end on the same indentation level).

Please report cases in the issues tab but realize that I'm not going to bend the plugin over backward to support every edge case for every language.

Screenshot

Supported FileTypes

  • ruby
  • php
  • javascript
  • vim
  • cpp

Usage

Just load it! In the languages it supports, it will enable itself and set the appropriate foldlevel and foldnestmax. Otherwise it will leave your global options alone. My .vimrc looks like this:

" Defaults for unsupported FileTypes
set foldlevel=1
set foldmethod=syntax

" Global settings for all folding
set foldminlines=3
set foldcolumn=1

set foldtext=MyFoldText() 
function! MyFoldText()
	let n = v:foldend - v:foldstart + 1 
	let line = getline(v:foldstart)
	let sub = substitute(line, '^\s*', '', 'g')
	return "+-- " . sub . " --(" . n . " lines)" . v:folddashes
endfunction

" Mappings
" Space to toggle folds
nnoremap <Space> za
" <leader>Space to focus on current fold
nnoremap <leader><Space> zMzv:set foldminlines=1<cr>

Re-running

An occasional issue with iterative fold parsing like this is that it doesn't know when the "last line" state which it tracks is still the line it thinks it was. If it detects a change it will re-orient itself but some conditions will prevent it from detecting that.

You could reload the file to re-parse, or you could trigger the FileType autocmds automatically, like such:

" t in quickfix to open in new tab
autocmd FileType qf nnoremap <silent> <buffer> t <C-W><Enter><C-W>T

... becomes ...

" t in quickfix to open in new tab (and re-run folding)
autocmd FileType qf nnoremap <silent> <buffer> t <C-W><Enter><C-W>T :doauto FileType<CR>

Why not use syntax rules?

Because they're slow, especially for large files. I'm fine with having a few edge cases that this doesn't catch at the expense of faster startup times.

For example, checking if a given line is a comment using this snippet: map(synstack(a:lnum, col([a:lnum, '$'])), "synIDattr(v:val, 'name')") slows down the entire plugin 8-fold vs my naive regex

vim-foldfunctions's People

Contributors

chrisjohnson avatar cmm-cjohnson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

vim-foldfunctions's Issues

Incorrectly sets foldmethod to manual

Edit: FastFold is at fault

NVIM v0.2.1-dev
installed from some ubuntu ppa on a debian machine
neovim-0.2.0ubuntu1+git201707261936+3549+26~ubuntu15.04.1


using a vimrc from this commit.
stagnation/dotfiles@f6cce80

.vimrc:
:g/foldmethod
1 " vim: foldmethod=marker foldlevel=0 foldcolumn=3
195 set foldmethod=syntax
638 autocmd FileType vim setlocal foldmethod=marker
642 autocmd FileType cpp setlocal foldmethod=syntax

line 195 is inside
if has('vim_starting')
set foldmethod=syntax

The behavior is the same if I remove the if and use a toplevel set foldmethod=syntax


in a python buffer.

:set foldmethod=syntax

:verbose set foldmethod
foldmethod=manual
Last set from ~/.local/share/nvim/plugged/FastFold/plugin/fastfold.vim


using a reproducable DEBUG.vimrc

set foldmethod=syntax

let plug_location = '~/.local/share/nvim/plugged'
call plug#begin(plug_location)
Plug 'Konfekt/FastFold' " don't know if this is relevant - leaving it here in case it does some fold magic.
Plug 'https://github.com/chrisjohnson/vim-foldfunctions'
call plug#end()

<<<EOF

vim -u DEBUG.vimrc file.py
Exhibits the same problem

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.