Skip to content

Commit fda10da

Browse files
committed
improve handling of comments before functions/methods
1 parent 039a552 commit fda10da

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

py/pycompiler.vim

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,29 @@ function s:PythonCompiler.compile_function(node)
330330
if !empty(rlist) && rlist[-1] == '...'
331331
let rlist[-1] = '*a000'
332332
endif
333+
334+
" Find start of preceding comment (block).
335+
let comment_start = 0
336+
let len_lines = len(self.lines)
337+
if len_lines
338+
while get(self.lines, comment_start - 1, '') =~# '^\s*#'
339+
let comment_start -= 1
340+
endwhile
341+
if comment_start != 0
342+
let comment_start = len_lines + comment_start
343+
endif
344+
endif
345+
333346
if left =~ '^\(VimLParser\|ExprTokenizer\|ExprParser\|LvalueParser\|StringReader\|Compiler\|RegexpParser\)\.'
334347
let left = matchstr(left, '\.\zs.*')
335348
if left == 'new'
336349
return
337350
endif
338-
call self.emptyline()
351+
if comment_start
352+
call insert(self.lines, '', comment_start)
353+
else
354+
call self.emptyline()
355+
endif
339356
call insert(rlist, 'self')
340357
call self.incindent(' ')
341358
call self.out('def %s(%s):', left, join(rlist, ', '))
@@ -344,8 +361,13 @@ function s:PythonCompiler.compile_function(node)
344361
call self.decindent()
345362
call self.decindent()
346363
else
347-
call self.emptyline()
348-
call self.emptyline()
364+
if comment_start
365+
call insert(self.lines, '', comment_start)
366+
call insert(self.lines, '', comment_start)
367+
else
368+
call self.emptyline()
369+
call self.emptyline()
370+
endif
349371
call self.out('def %s(%s):', left, join(rlist, ', '))
350372
call self.incindent(' ')
351373
call self.compile_body(a:node.body)

0 commit comments

Comments
 (0)