-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpython.vim
59 lines (53 loc) · 1.53 KB
/
python.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
" Vim folding file
" Language: Python (docstring)
" Author: Eric Chiang, Milly (module)
" Last Change: 20 Jan 2018
" Version: 1.1
if exists('g:loaded_python_docstring')
finish
endif
let g:loaded_python_docstring = 1
if !has('pythonx')
echomsg "Error: Docstring requires vim compiled with +python or +python3"
finish
endif
function! PyDocHide()
setlocal foldmethod=manual
pythonx << EOF
import vim
import ast
import re
try:
lines = list(vim.current.buffer)
root = ast.parse("\n".join(lines))
for node in ast.walk(root):
if not isinstance(node, (ast.Module, ast.FunctionDef, ast.ClassDef)):
continue
if ast.get_docstring(node) is None:
continue
first_child = node.body[0]
if not isinstance(first_child, ast.Expr):
continue
end = first_child.lineno
if '"""' in lines[end - 1]:
bracket = '"""'
elif "'''" in lines[end - 1]:
bracket = "'''"
else:
continue
if re.search(bracket + '.*' + bracket, lines[end - 1]):
start = end
else:
start = node.lineno if hasattr(node, 'lineno') else 1
for i, line in enumerate(lines[end-2 : start-1 : -1]):
if bracket in line:
start = end - i - 1
break
else:
continue
vim.command("%d,%dfold" % (start, end))
except Exception as e:
print("Error: %s" % (e,))
EOF
endfunction
command! PyDocHide call PyDocHide()