Skip to content

Commit f28f783

Browse files
author
Le Tien Tai
committed
Extract code to parseClass method
The reason is regex for `class` keyword is much simpler than `def` and `async`.
1 parent b3d37f8 commit f28f783

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

autoload/pydocstring.vim

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,30 @@ function! s:readtmpl(type)
4444
return tmpl
4545
endfunction
4646

47+
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.
51+
let header = substitute(a:line, '\s\|(.*\|:', '', 'g')
52+
let parse = {'type': 'class', 'header': header, 'args': ''}
53+
return parse
54+
endfunction
55+
4756
function! s:parse(line)
4857
let str = substitute(a:line, '\\', '', 'g')
4958
let type = ''
59+
60+
if str =~ s:regexs['class']
61+
let str = substitute(str, s:regexs['class'], '', '')
62+
return s:parseClass(str)
63+
endif
64+
5065
if str =~ s:regexs['def']
5166
let str = substitute(str, s:regexs['def'], '', '')
5267
let type = 'def'
5368
elseif str =~ s:regexs['async']
5469
let str = substitute(str, s:regexs['async'], '', '')
5570
let type = 'def'
56-
elseif str =~ s:regexs['class']
57-
let str = substitute(str, s:regexs['class'], '', '')
58-
let type = 'class'
5971
else
6072
return 0
6173
endif
@@ -85,6 +97,7 @@ function! s:builddocstring(strs, indent, nested_indent)
8597
let type = a:strs['type']
8698
let prefix = a:strs['header']
8799
let args = a:strs['args']
100+
88101
let tmpl = ''
89102
if len(args) > 0 && type == 'def'
90103
let docstrings = []

test/basic.vader

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,31 @@ Expect python:
6464
#-------------------------------------------------------------------------------
6565

6666

67+
Given python (class Foo no extends):
68+
class Foo:
69+
pass
70+
71+
Execute:
72+
Pydocstring
73+
74+
Expect python:
75+
class Foo:
76+
"""Foo"""
77+
pass
78+
79+
Given python (class Foo extends multiple classes):
80+
class Foo(ClassA, ClassB, ClassC):
81+
pass
82+
83+
Execute:
84+
Pydocstring
85+
86+
Expect python:
87+
class Foo(ClassA, ClassB, ClassC):
88+
"""Foo"""
89+
pass
90+
91+
6792
Given python (class Foo):
6893
class Foo(object):
6994
def foo(self):

0 commit comments

Comments
 (0)