diff --git a/CHANGES.rst b/CHANGES.rst index 5d465e2..48f44f5 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 diff --git a/autoload/pydocstring.vim b/autoload/pydocstring.vim index 891ccb9..584c726 100644 --- a/autoload/pydocstring.vim +++ b/autoload/pydocstring.vim @@ -1,6 +1,6 @@ " Insert Docstring. " Author: Shinya Ohyanagi -" 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. @@ -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 @@ -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... @@ -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 @@ -124,25 +124,25 @@ 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 @@ -150,7 +150,7 @@ function! s:builddocstring(strs, indent, nested_indent) 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 @@ -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 @@ -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) diff --git a/doc/pydocstring.txt b/doc/pydocstring.txt index 2c86260..fdd494e 100644 --- a/doc/pydocstring.txt +++ b/doc/pydocstring.txt @@ -1,6 +1,6 @@ *pydocstring.txt* Generate Python docstring to your Python code. -Version: 0.1.5 +Version: 0.1.6 Author: Shinya Ohynagi Repository: http://github.com/heavenshell/vim-pydocstring/ License: BSD, see LICENSE for more details. @@ -141,12 +141,13 @@ Template variables. |{{_lf_}}| Assign line break. |{{_indent_}}| Assign indent. |{{_nested_indent_}}| Assign indent. - |{{_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 diff --git a/ftplugin/python/pydocstring.vim b/ftplugin/python/pydocstring.vim index 5fc8043..fe548e7 100644 --- a/ftplugin/python/pydocstring.vim +++ b/ftplugin/python/pydocstring.vim @@ -1,6 +1,6 @@ " File: pydocstring.vim " Author: Shinya Ohyanagi -" 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. diff --git a/template/pydocstring/multi.txt b/template/pydocstring/multi.txt index 89235d2..2ad35c7 100644 --- a/template/pydocstring/multi.txt +++ b/template/pydocstring/multi.txt @@ -1,5 +1,5 @@ """{{_header_}} :param {{_args_}}: -:rtype: {{_returnType_}} +:rtype: {{_return_type_}} """ diff --git a/test/issue19.vader b/test/issue_nested_indent.vader similarity index 100% rename from test/issue19.vader rename to test/issue_nested_indent.vader diff --git a/test/issue_return_type.vader b/test/issue_return_type.vader new file mode 100644 index 0000000..a824f62 --- /dev/null +++ b/test/issue_return_type.vader @@ -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 + diff --git a/test/run.sh b/test/run.sh index 2b4ff5a..7b93061 100755 --- a/test/run.sh +++ b/test/run.sh @@ -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 diff --git a/test/templates/custom-templates/multi.txt b/test/templates/custom-templates/multi.txt index 7899447..3c9fd5b 100644 --- a/test/templates/custom-templates/multi.txt +++ b/test/templates/custom-templates/multi.txt @@ -1,5 +1,5 @@ """{{_header_}} {{_args_}}:{{_lf_}}{{_indent_}} the ... -return ({{_returnType_}}): +return ({{_return_type_}}): """ diff --git a/test/templates/numpy/multi.txt b/test/templates/numpy/multi.txt index be6f10b..149e743 100644 --- a/test/templates/numpy/multi.txt +++ b/test/templates/numpy/multi.txt @@ -7,5 +7,5 @@ {{_indent_}}Returns {{_indent_}}------- -returnVar : {{_returnType_}} +returnVar : {{_return_type_}} """ diff --git a/test/templates/return_type/arg.txt b/test/templates/return_type/arg.txt new file mode 100644 index 0000000..46fa580 --- /dev/null +++ b/test/templates/return_type/arg.txt @@ -0,0 +1 @@ +{{_name_}}:{{_lf_}}{{_indent_}}:type {{_name_}}: {{_type_}} diff --git a/test/templates/return_type/comment.txt b/test/templates/return_type/comment.txt new file mode 100644 index 0000000..1e0ca6a --- /dev/null +++ b/test/templates/return_type/comment.txt @@ -0,0 +1 @@ +# diff --git a/test/templates/return_type/multi.txt b/test/templates/return_type/multi.txt new file mode 100644 index 0000000..89235d2 --- /dev/null +++ b/test/templates/return_type/multi.txt @@ -0,0 +1,5 @@ +"""{{_header_}} + +:param {{_args_}}: +:rtype: {{_returnType_}} +""" diff --git a/test/templates/return_type/oneline.txt b/test/templates/return_type/oneline.txt new file mode 100644 index 0000000..3a0cf0d --- /dev/null +++ b/test/templates/return_type/oneline.txt @@ -0,0 +1 @@ +"""{{_header_}}"""