├── example.png ├── README.md └── ftplugin └── python.vim /example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yhat/vim-docstring/HEAD/example.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #vim-docstring 2 | 3 | Python docstrings are great for helping others, but are sometimes too verbose 4 | for a developer. Fold them with ease with this vim plugin. 5 | 6 | ![](./example.png) 7 | 8 | ## Install 9 | 10 | If you use [pathogen](https://github.com/tpope/vim-pathogen) just clone to your bundle. 11 | 12 | ```bash 13 | cd ~/.vim/bundle 14 | git clone git@github.com:yhat/vim-docstring 15 | ``` 16 | 17 | If not, just stick [ftplugin/python.vim](./ftplugin/python.vim) in your 18 | .vim/ftplugin directory somewhere. 19 | 20 | ## Usage 21 | 22 | This vim plugin defines exactly one command which you can auto-load by adding 23 | the following to your .vimrc. 24 | 25 | ```vim 26 | autocmd FileType python PyDocHide 27 | ``` 28 | 29 | Use the following vim commands to manipulate the folded docstrings. 30 | 31 | * `za`: Toggle current fold. 32 | * `zo`: Open the current fold. 33 | * `zc`: Close the current fold. 34 | 35 | See more help on the [vim wiki](http://vim.wikia.com/wiki/Folding#Opening_and_closing_folds). 36 | 37 | To manually redraw the folds use the command `:PyDocHide` 38 | -------------------------------------------------------------------------------- /ftplugin/python.vim: -------------------------------------------------------------------------------- 1 | " Vim folding file 2 | " Language: Python (docstring) 3 | " Author: Eric Chiang, Milly (module) 4 | " Last Change: 20 Jan 2018 5 | " Version: 1.1 6 | 7 | 8 | if exists('g:loaded_python_docstring') 9 | finish 10 | endif 11 | let g:loaded_python_docstring = 1 12 | 13 | if !has('pythonx') 14 | echomsg "Error: Docstring requires vim compiled with +python or +python3" 15 | finish 16 | endif 17 | 18 | function! PyDocHide() 19 | setlocal foldmethod=manual 20 | pythonx << EOF 21 | import vim 22 | import ast 23 | import re 24 | 25 | try: 26 | lines = list(vim.current.buffer) 27 | root = ast.parse("\n".join(lines)) 28 | for node in ast.walk(root): 29 | if not isinstance(node, (ast.Module, ast.FunctionDef, ast.ClassDef)): 30 | continue 31 | if ast.get_docstring(node) is None: 32 | continue 33 | first_child = node.body[0] 34 | if not isinstance(first_child, ast.Expr): 35 | continue 36 | end = first_child.lineno 37 | if '"""' in lines[end - 1]: 38 | bracket = '"""' 39 | elif "'''" in lines[end - 1]: 40 | bracket = "'''" 41 | else: 42 | continue 43 | if re.search(bracket + '.*' + bracket, lines[end - 1]): 44 | start = end 45 | else: 46 | start = node.lineno if hasattr(node, 'lineno') else 1 47 | for i, line in enumerate(lines[end-2 : start-1 : -1]): 48 | if bracket in line: 49 | start = end - i - 1 50 | break 51 | else: 52 | continue 53 | vim.command("%d,%dfold" % (start, end)) 54 | except Exception as e: 55 | print("Error: %s" % (e,)) 56 | EOF 57 | endfunction 58 | 59 | command! PyDocHide call PyDocHide() 60 | --------------------------------------------------------------------------------