Skip to content

Commit e372138

Browse files
committed
Support function return type hint
1 parent 25b3b4e commit e372138

File tree

5 files changed

+63
-33
lines changed

5 files changed

+63
-33
lines changed

autoload/pydocstring.vim

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ if !exists('g:pydocstring_ignore_args_pattern')
2424
endif
2525

2626
let s:regexs = {
27-
\ 'def': '^def\s\|^\s*def\s',
28-
\ 'class': '^class\s\|^\s*class\s',
29-
\ 'async': '^async\s*def\s\|^\s*async\sdef\s'
30-
\ }
27+
\ 'def': '^def\s\|^\s*def\s',
28+
\ 'class': '^class\s\|^\s*class\s',
29+
\ 'async': '^async\s*def\s\|^\s*async\sdef\s'
30+
\ }
3131

3232
function! s:readtmpl(type)
3333
let tmpldir = g:pydocstring_templates_dir
@@ -45,9 +45,9 @@ function! s:readtmpl(type)
4545
endfunction
4646

4747
function! s:parseClass(line)
48-
" For class definition, we just simply need to extract the class name. We can
49-
" do that by just delete every white spaces and the whole parenthesics if
50-
" existed.
48+
" For class definition, we just simply need to extract the class name. We can
49+
" do that by just delete every white spaces and the whole parenthesics if
50+
" existed.
5151
let header = substitute(a:line, '\s\|(.*\|:', '', 'g')
5252
let parse = {'type': 'class', 'header': header, 'args': '', 'returnType': ''}
5353
return parse
@@ -153,21 +153,24 @@ function! s:builddocstring(strs, indent, nested_indent)
153153
for line in lines
154154
if line =~ '{{_header_}}'
155155
let header = substitute(line, '{{_header_}}', prefix, '')
156-
let header = substitute(header, '{{_lf_}}', '\n', '')
157156
call add(docstrings, a:indent . header)
157+
call add(docstrings, '' )
158158
elseif line =~ '{{_args_}}'
159-
for arg in args
160-
let arg = substitute(arg, '=.*$', '', '')
161-
if arg =~ g:pydocstring_ignore_args_pattern
162-
continue
163-
endif
164-
let template = line
165-
let template = substitute(template, '{{_args_}}', arg, 'g')
166-
let template = substitute(template, '{{_lf_}}', '\n', '')
167-
let template = substitute(template, '{{_indent_}}', a:indent, 'g')
168-
let template = substitute(template, '{{_nested_indent_}}', a:nested_indent, 'g')
169-
call add(docstrings, a:indent . template)
170-
endfor
159+
if len(args) != 0
160+
for arg in args
161+
let arg = substitute(arg, '=.*$', '', '')
162+
if arg =~ g:pydocstring_ignore_args_pattern
163+
continue
164+
endif
165+
let template = line
166+
let template = substitute(template, '{{_args_}}', arg, 'g')
167+
let template = substitute(template, '{{_lf_}}', '\n', '')
168+
let template = substitute(template, '{{_indent_}}', a:indent, 'g')
169+
let template = substitute(template, '{{_nested_indent_}}', a:nested_indent, 'g')
170+
call add(docstrings, a:indent . template)
171+
endfor
172+
call add(docstrings, '' )
173+
endif
171174
elseif line =~ '{{_indent_}}'
172175
let arg = substitute(line, '{{_indent_}}', a:indent, 'g')
173176
call add(docstrings, arg)
@@ -176,20 +179,16 @@ function! s:builddocstring(strs, indent, nested_indent)
176179
let rt = substitute(line, '{{_returnType_}}', returnType, '')
177180
call add(docstrings, a:indent . rt)
178181
else
179-
" call remove(docstrings, -1)
182+
call remove(docstrings, -1)
180183
endif
181184
elseif line == '"""'
182185
call add(docstrings, a:indent . line)
183186
else
184187
call add(docstrings, line)
185188
endif
186-
187-
echo string(docstrings)
188-
189189
endfor
190190
let tmpl = substitute(join(docstrings, "\n"), "\n$", '', '')
191191

192-
echom string(tmpl)
193192
return tmpl
194193
endfunction
195194

template/pydocstring/multi.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
"""{{_header_}}
2-
{{_lf_}}
32
:param {{_args_}}:
4-
{{_lf_}}
53
return ({{_returnType_}}):
64
"""

test/run-single-test-file.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ if hash nvim 2>/dev/null ; then
1616
fi
1717

1818
# Open vim with readonly mode just to execute all *.vader tests.
19-
$VIM_EXE -Nu minimal_vimrc -R "+Vader! $1"
19+
$VIM_EXE -Nu minimal_vimrc -R "+Vader $1"

test/test-template/multi.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
"""{{_header_}}
2-
3-
{{_arg_}}:{{_lf_}}{{_indent_}} {{_arg_}} is
2+
{{_args_}}:{{_lf_}}{{_indent_}} {{_args_}} is
3+
return ({{_returnType_}}):
44
"""

test/type-hint.vader

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,48 @@
11
# vim:set et sw=4 ts=4 tw=79:
22

3-
Given python (Simple def foo return str):
4-
def foo(sample_var):
3+
Given python (def foo no arg return str):
4+
def foo() -> str:
55
pass
66
Execute:
77
Pydocstring
88

99
Expect python:
10-
def foo(sample_var):
10+
def foo() -> str:
11+
"""foo
12+
13+
return (str):
14+
"""
15+
pass
16+
17+
Given python (def foo with 1 arg return str):
18+
def foo(sample_var) -> str:
19+
pass
20+
Execute:
21+
Pydocstring
22+
23+
Expect python:
24+
def foo(sample_var) -> str:
1125
"""foo
1226

1327
:param sample_var:
28+
29+
return (str):
30+
"""
31+
pass
32+
33+
Given python (def foo 2 args return str):
34+
def foo(arg1, arg2) -> str:
35+
pass
36+
Execute:
37+
Pydocstring
38+
39+
Expect python:
40+
def foo(arg1, arg2) -> str:
41+
"""foo
42+
43+
:param arg1:
44+
:param arg2:
45+
46+
return (str):
1447
"""
1548
pass

0 commit comments

Comments
 (0)