Skip to content

Fix/rename #37

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Nov 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
Version 0.1.6
-------------
Released on Nov 19th 2017

- Minor changes

- Change function name camelCase to snake_case
- Change variable name camelCase to snake_case

see https://github.com/heavenshell/vim-pydocstring/issues/34

Version 0.1.5
-------------
Released on Nov 18th 2017
Expand Down
46 changes: 23 additions & 23 deletions autoload/pydocstring.vim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
" Insert Docstring.
" Author: Shinya Ohyanagi <sohyanagi@gmail.com>
" Version: 0.1.5
" Version: 0.1.6
" WebPage: http://github.com/heavenshell/vim-pydocstriong/
" Description: Generate Python docstring to your Python script file.
" License: BSD, see LICENSE for more details.
Expand Down Expand Up @@ -48,26 +48,26 @@ function! s:parseClass(line)
" do that by just delete every white spaces and the whole parenthesics if
" existed.
let header = substitute(a:line, '\s\|(.*\|:', '', 'g')
let parse = {'type': 'class', 'header': header, 'args': '', 'returnType': ''}
let parse = {'type': 'class', 'header': header, 'args': '', 'return_type': ''}
return parse
endfunction

function! s:parseFunc(type, line)
function! s:parse_func(type, line)
let header = substitute(a:line, '\s\|(.*\|:', '', 'g')

let argsStr = substitute(a:line, '\s\|.*(\|).*', '', 'g')
let args = split(argsStr, ',')
let args_str = substitute(a:line, '\s\|.*(\|).*', '', 'g')
let args = split(args_str, ',')

let arrowIndex = match(a:line, "->")
if arrowIndex != -1
let substring = strpart(a:line, arrowIndex + 2)
let arrow_index = match(a:line, "->")
if arrow_index != -1
let substring = strpart(a:line, arrow_index + 2)
" issue #28 `\W*` would deleted `.`.
let returnType = substitute(substring, '[^0-9A-Za-z_.]*', '', 'g')
let return_type = substitute(substring, '[^0-9A-Za-z_.]*', '', 'g')
else
let returnType = ''
let return_type = ''
endif

let parse = {'type': a:type, 'header': header, 'args': args, 'returnType': returnType}
let parse = {'type': a:type, 'header': header, 'args': args, 'return_type': return_type}
return parse
endfunction

Expand All @@ -90,7 +90,7 @@ function! s:parse(line)
return 0
endif

return s:parseFunc(type, str)
return s:parse_func(type, str)
endfunction

" Vim Script does not support lambda function...
Expand All @@ -104,7 +104,7 @@ endfunction
" Check if we should show args in the docstring. We won't do that in case:
" - There's no args.
" - There's only one arg that match with g:pydocstring_ignore_args_pattern
function! s:shouldIncludeArgs(args)
function! s:should_include_args(args)
if len(a:args) == 0
return 0
endif
Expand All @@ -124,33 +124,33 @@ endfunction
" g:pydocstring_ignore_args_pattern
"
" Return 1 for True, and 0 for False
function! s:shouldUseOneLineDocString(type, args, returnType)
function! s:should_use_one_line_docstring(type, args, return_type)
if a:type != 'def'
return 1
endif

if a:returnType != ''
if a:return_type != ''
return 0
endif

return !s:shouldIncludeArgs(a:args)
return !s:should_include_args(a:args)
endfunction

function! s:builddocstring(strs, indent, nested_indent)
let type = a:strs['type']
let prefix = a:strs['header']
let args = a:strs['args']
let returnType = a:strs['returnType']
let return_type = a:strs['return_type']

if s:shouldUseOneLineDocString(type, args, returnType)
if s:should_use_one_line_docstring(type, args, return_type)
return s:readoneline(a:indent, prefix)
endif

let tmpl = ''
let docstrings = []
let lines = s:readtmpl('multi')
let has_return_type = 0
if match(lines, '\c{{_returnType_}}') != -1
if match(lines, '\c{{_returnType_}}\|\c{{_return_type_}}') != -1
let has_return_type = 1
endif
for line in lines
Expand Down Expand Up @@ -186,7 +186,7 @@ function! s:builddocstring(strs, indent, nested_indent)
" '''
" {{_header_}}
" :param {{_args_}}:
" :rtype: {{_returnType_}}
" :rtype: {{_return_type_}}
" '''
let template = substitute(template, ':$', '', 'g')
endif
Expand All @@ -203,9 +203,9 @@ function! s:builddocstring(strs, indent, nested_indent)
elseif line =~ '{{_indent_}}'
let arg = substitute(line, '{{_indent_}}', a:indent, 'g')
call add(docstrings, arg)
elseif line =~ '{{_returnType_}}'
if strlen(returnType) != 0
let rt = substitute(line, '{{_returnType_}}', returnType, '')
elseif line =~ '{{_returnType_}}\|{{_return_type_}}'
if strlen(return_type) != 0
let rt = substitute(line, '{{_returnType_}}\|{{_return_type_}}', return_type, '')
call add(docstrings, a:indent . rt)
else
call remove(docstrings, -1)
Expand Down
7 changes: 4 additions & 3 deletions doc/pydocstring.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
*pydocstring.txt* Generate Python docstring to your Python code.

Version: 0.1.5
Version: 0.1.6
Author: Shinya Ohynagi <sohyanagi@gmail.com>
Repository: http://github.com/heavenshell/vim-pydocstring/
License: BSD, see LICENSE for more details.
Expand Down Expand Up @@ -141,12 +141,13 @@ Template variables.
|{{_lf_}}| Assign line break.
|{{_indent_}}| As#dent.
|{{_nested_indent_}}| As#dent.
|{{_returnType_}}| Function or method return type.
|{{_return_type_}}| Function or method return type.
|{{_returnType_}}| Same as |{{_return_type_}}||.
|{{_name_}}| Argument name, should only be used in `arg.txt`
|{{_type_}}| Argument type, should only be used in `arg.txt`

There's some rules for parsing template:
- `{{_header_}`, `{{_args}}` and `{{_returnType_}}` should be on their own
- `{{_header_}`, `{{_args}}` and `{{_return_type_}}` should be on their own
line.
- Pydocstring will insert a emmpty line between each parts, so you don't
need to include empty line in your template. Please see the default template
Expand Down
2 changes: 1 addition & 1 deletion ftplugin/python/pydocstring.vim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
" File: pydocstring.vim
" Author: Shinya Ohyanagi <sohyanagi@gmail.com>
" Version: 0.1.5
" Version: 0.1.6
" WebPage: http://github.com/heavenshell/vim-pydocstriong/
" Description: Generate Python docstring to your Python script file.
" License: BSD, see LICENSE for more details.
Expand Down
2 changes: 1 addition & 1 deletion template/pydocstring/multi.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""{{_header_}}

:param {{_args_}}:
:rtype: {{_returnType_}}
:rtype: {{_return_type_}}
"""
File renamed without changes.
112 changes: 112 additions & 0 deletions test/issue_return_type.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# vim:set et sw=4 ts=4 tw=79:
# Test for {{_returnType_}} not BCBreak.
Execute (Setup template dir):
Save g:pydocstring_templates_dir
let g:pydocstring_templates_dir = './templates/return_type/'

Given python (def foo no arg return str):
def foo() -> str:
pass

Execute:
Pydocstring

Expect python:
def foo() -> str:
"""foo

:rtype: str
"""
pass

Given python (def foo with 1 arg return str):
def foo(sample_var) -> str:
pass
Execute:
Pydocstring

Expect python:
def foo(sample_var) -> str:
"""foo

:param sample_var:

:rtype: str
"""
pass

Given python (def foo 2 args return str):
def foo(arg1, arg2) -> str:
pass
Execute:
Pydocstring

Expect python:
def foo(arg1, arg2) -> str:
"""foo

:param arg1:
:param arg2:

:rtype: str
"""
pass

Given python (def foo 1 arg with type and return int):
def foo(n: int) -> int:
pass

Execute:
Pydocstring

Expect python:
def foo(n: int) -> int:
"""foo

:param n:
:type n: int

:rtype: int
"""
pass

Given python (def foo 2 args with type and return int):
def foo(n: int, m: str) -> int:
pass

Execute:
Pydocstring

Expect python:
def foo(n: int, m: str) -> int:
"""foo

:param n:
:type n: int
:param m:
:type m: str

:rtype: int
"""
pass


Given python (def foo 1 arg with type, 1 none, and return int):
def foo(n: int, arg) -> int:
pass

Execute:
Pydocstring

Expect python:
def foo(n: int, arg) -> int:
"""foo

:param n:
:type n: int
:param arg:

:rtype: int
"""
pass

2 changes: 1 addition & 1 deletion test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# If nvim is available in PATH, then we prefer to use nvim
# since it works better with nodemon
if hash nvim 2>/dev/null ; then
if hash nvim 2>/dev/null ; then
VIM_EXE="nvim"
fi

Expand Down
2 changes: 1 addition & 1 deletion test/templates/custom-templates/multi.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""{{_header_}}

{{_args_}}:{{_lf_}}{{_indent_}} the ...
return ({{_returnType_}}):
return ({{_return_type_}}):
"""
2 changes: 1 addition & 1 deletion test/templates/numpy/multi.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
{{_indent_}}Returns
{{_indent_}}-------

returnVar : {{_returnType_}}
returnVar : {{_return_type_}}
"""
1 change: 1 addition & 0 deletions test/templates/return_type/arg.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{_name_}}:{{_lf_}}{{_indent_}}:type {{_name_}}: {{_type_}}
1 change: 1 addition & 0 deletions test/templates/return_type/comment.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#
5 changes: 5 additions & 0 deletions test/templates/return_type/multi.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""{{_header_}}

:param {{_args_}}:
:rtype: {{_returnType_}}
"""
1 change: 1 addition & 0 deletions test/templates/return_type/oneline.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""{{_header_}}"""