Skip to content

Commit 0fd4e20

Browse files
committed
Support type hint for arguments
1 parent 90a0ab1 commit 0fd4e20

File tree

8 files changed

+102
-4
lines changed

8 files changed

+102
-4
lines changed

autoload/pydocstring.vim

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,15 @@ function! s:builddocstring(strs, indent, nested_indent)
163163
continue
164164
endif
165165
let template = line
166+
167+
if match(arg, ':') != -1
168+
let argTemplate = s:readtmpl('arg')
169+
let argTemplate = join(s:readtmpl('arg'), '')
170+
let argParts = split(arg, ':')
171+
let argTemplate = substitute(argTemplate, '{{_name_}}', argParts[0], '')
172+
let arg = substitute(argTemplate, '{{_type_}}', argParts[1], '')
173+
endif
174+
166175
let template = substitute(template, '{{_args_}}', arg, 'g')
167176
let template = substitute(template, '{{_lf_}}', '\n', '')
168177
let template = substitute(template, '{{_indent_}}', a:indent, 'g')

doc/pydocstring.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ If you don't like default docstring, You can modify docstring template.
132132
One-line docstring.
133133
- multi.txt
134134
Multi-line docstring.
135+
- arg.txt
136+
Template for each argument.
135137

136138
Template variables.
137139
|{{_header_}}| Class, function or method name as a value.
@@ -140,6 +142,8 @@ Template variables.
140142
|{{_indent_}}| Assign indent.
141143
|{{_nested_indent_}}| Assign indent.
142144
|{{_returnType_}}| Function or method return type.
145+
|{{_name_}}| Argument name, should only be used in `arg.txt`
146+
|{{_type_}}| Argument type, should only be used in `arg.txt`
143147

144148
There's some rules for parsing template:
145149
- `{{_header_}`, `{{_args}}` and `{{_returnType_}}` should be on their own

template/pydocstring/arg.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{_name_}} ({{_type_}})

test/custom-template.vader

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Expect python:
2929
"""foo
3030

3131
arg1:
32-
arg1 is
32+
the ...
3333
"""
3434
pass
3535

@@ -46,9 +46,9 @@ Expect python:
4646
"""foo
4747

4848
arg1:
49-
arg1 is
49+
the ...
5050
arg2:
51-
arg2 is
51+
the ...
5252
"""
5353
pass
5454

test/test-template/arg.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{_type_}} {{_name_}}

test/test-template/multi.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
"""{{_header_}}
2-
{{_args_}}:{{_lf_}}{{_indent_}} {{_args_}} is
2+
{{_args_}}:{{_lf_}}{{_indent_}} the ...
33
return ({{_returnType_}}):
44
"""
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# vim:set et sw=4 ts=4 tw=79:
2+
Execute (Setup template dir):
3+
Save g:pydocstring_templates_dir
4+
let g:pydocstring_templates_dir = './test-template/'
5+
6+
Given python (def foo 1 arg with type, 1 none, and return int):
7+
def foo(n: int, arg) -> int:
8+
pass
9+
10+
Execute:
11+
Pydocstring
12+
13+
Expect python:
14+
def foo(n: int, arg) -> int:
15+
"""foo
16+
17+
int n:
18+
the ...
19+
arg:
20+
the ...
21+
22+
return (int):
23+
"""
24+
pass
25+
26+
Execute (Clear pydocstring_templates_dir):
27+
Restore

test/type-hint.vader

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Given python (def foo no arg return str):
44
def foo() -> str:
55
pass
6+
67
Execute:
78
Pydocstring
89

@@ -46,3 +47,58 @@ Expect python:
4647
return (str):
4748
"""
4849
pass
50+
51+
Given python (def foo 1 arg with type and return int):
52+
def foo(n: int) -> int:
53+
pass
54+
55+
Execute:
56+
Pydocstring
57+
58+
Expect python:
59+
def foo(n: int) -> int:
60+
"""foo
61+
62+
:param n (int):
63+
64+
return (int):
65+
"""
66+
pass
67+
68+
Given python (def foo 2 args with type and return int):
69+
def foo(n: int, m: str) -> int:
70+
pass
71+
72+
Execute:
73+
Pydocstring
74+
75+
Expect python:
76+
def foo(n: int, m: str) -> int:
77+
"""foo
78+
79+
:param n (int):
80+
:param m (str):
81+
82+
return (int):
83+
"""
84+
pass
85+
86+
87+
Given python (def foo 1 arg with type, 1 none, and return int):
88+
def foo(n: int, arg) -> int:
89+
pass
90+
91+
Execute:
92+
Pydocstring
93+
94+
Expect python:
95+
def foo(n: int, arg) -> int:
96+
"""foo
97+
98+
:param n (int):
99+
:param arg:
100+
101+
return (int):
102+
"""
103+
pass
104+

0 commit comments

Comments
 (0)