Skip to content

Commit a707f64

Browse files
author
Le Tien Tai
committed
Extract method to parse 'def' and 'async'
There's 2 small fixes: - Extends regex search for the line end with a comment. - Update insert position for the line end with a comment. Add test cases for above cases.
1 parent f28f783 commit a707f64

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

autoload/pydocstring.vim

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,19 @@ function! s:parseClass(line)
5353
return parse
5454
endfunction
5555

56+
function! s:parseFunc(type, line)
57+
let header = substitute(a:line, '\s\|(.*\|:', '', 'g')
58+
59+
let argsStr = substitute(a:line, '\s\|.*(\|).*', '', 'g')
60+
let args = split(argsStr, ',')
61+
62+
let parse = {'type': a:type, 'header': header, 'args': args}
63+
return parse
64+
endfunction
65+
5666
function! s:parse(line)
5767
let str = substitute(a:line, '\\', '', 'g')
68+
let str = substitute(a:line, '#.*$', '', 'g')
5869
let type = ''
5970

6071
if str =~ s:regexs['class']
@@ -71,18 +82,8 @@ function! s:parse(line)
7182
else
7283
return 0
7384
endif
74-
let str = substitute(str, '\s\|):\|)\s:', '', 'g')
75-
76-
let strs = split(str, '(')
77-
let header = strs[0]
78-
let args = []
79-
if len(strs) > 1
80-
let args = split(strs[1], ',')
81-
end
8285

83-
let parse = {'type': type, 'header': header, 'args': args}
84-
85-
return parse
86+
return s:parseFunc(type, str)
8687
endfunction
8788

8889
" Vim Script does not support lambda function...
@@ -172,7 +173,7 @@ function! pydocstring#insert()
172173
let indent = matchstr(line, '^\(\s*\)')
173174

174175
let startpos = line('.')
175-
let insertpos = search('\:\+$')
176+
let insertpos = search('\:\(\s*#.*\)*$')
176177
let lines = join(getline(startpos, insertpos))
177178

178179
let docstring = s:parse(lines)

test/basic.vader

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,23 @@ Expect python:
4545
"""
4646
pass
4747

48+
Given python (def foo with 2 params and a comment block):
49+
def foo(arg1, arg2): # Something fun (just, for, test)
50+
pass
51+
52+
Execute:
53+
Pydocstring
54+
55+
Expect python:
56+
def foo(arg1, arg2): # Something fun (just, for, test)
57+
"""foo
58+
59+
:param arg1:
60+
:param arg2:
61+
"""
62+
pass
63+
64+
4865
Given python (def foo with variadic params):
4966
def foo(*arg):
5067
pass

0 commit comments

Comments
 (0)