mirror of
https://github.com/dense-analysis/ale
synced 2024-12-28 17:12:23 +00:00
Add a fixer for Python for automatically adding blank lines before control statements
This commit is contained in:
parent
4526018344
commit
1e72a7a130
@ -2,6 +2,11 @@
|
||||
" Description: A registry of functions for fixing things.
|
||||
|
||||
let s:default_registry = {
|
||||
\ 'add_blank_lines_for_python_control_statements': {
|
||||
\ 'function': 'ale#handlers#python#AddLinesBeforeControlStatements',
|
||||
\ 'suggested_filetypes': ['python'],
|
||||
\ 'description': 'Add blank lines before control statements.',
|
||||
\ },
|
||||
\ 'autopep8': {
|
||||
\ 'function': 'ale#handlers#python#AutoPEP8',
|
||||
\ 'suggested_filetypes': ['python'],
|
||||
|
@ -36,6 +36,26 @@ function! ale#handlers#python#HandlePEP8Format(buffer, lines) abort
|
||||
return l:output
|
||||
endfunction
|
||||
|
||||
" Add blank lines before control statements.
|
||||
function! ale#handlers#python#AddLinesBeforeControlStatements(buffer, lines) abort
|
||||
let l:new_lines = []
|
||||
let l:last_indent_size = 0
|
||||
|
||||
for l:line in a:lines
|
||||
let l:indent_size = len(matchstr(l:line, '^ *'))
|
||||
|
||||
if l:indent_size <= l:last_indent_size
|
||||
\&& match(l:line, '\v^ *(return|if|for|while|break|continue)') >= 0
|
||||
call add(l:new_lines, '')
|
||||
endif
|
||||
|
||||
call add(l:new_lines, l:line)
|
||||
let l:last_indent_size = l:indent_size
|
||||
endfor
|
||||
|
||||
return l:new_lines
|
||||
endfunction
|
||||
|
||||
function! ale#handlers#python#AutoPEP8(buffer, lines) abort
|
||||
return {
|
||||
\ 'command': 'autopep8 -'
|
||||
|
85
test/fixers/test_python_add_blank_lines_fixer.vader
Normal file
85
test/fixers/test_python_add_blank_lines_fixer.vader
Normal file
@ -0,0 +1,85 @@
|
||||
Before:
|
||||
Save g:ale_fixers
|
||||
|
||||
After:
|
||||
Restore
|
||||
|
||||
Given python(Some Python without blank lines):
|
||||
def foo():
|
||||
return 1
|
||||
|
||||
|
||||
def bar():
|
||||
return 1
|
||||
return 4
|
||||
|
||||
|
||||
def bar():
|
||||
if x:
|
||||
pass
|
||||
for l in x:
|
||||
pass
|
||||
for l in x:
|
||||
pass
|
||||
break
|
||||
continue
|
||||
elif x:
|
||||
pass
|
||||
while x:
|
||||
pass
|
||||
while x:
|
||||
pass
|
||||
else:
|
||||
pass
|
||||
if x:
|
||||
pass
|
||||
elif x:
|
||||
pass
|
||||
else:
|
||||
pass
|
||||
|
||||
Execute(Blank lines should be added appropriately):
|
||||
let g:ale_fixers = {'python': ['ale#handlers#python#AddLinesBeforeControlStatements']}
|
||||
ALEFix
|
||||
|
||||
Expect python(Newlines should be added):
|
||||
def foo():
|
||||
return 1
|
||||
|
||||
|
||||
def bar():
|
||||
return 1
|
||||
|
||||
return 4
|
||||
|
||||
|
||||
def bar():
|
||||
if x:
|
||||
pass
|
||||
|
||||
for l in x:
|
||||
pass
|
||||
|
||||
for l in x:
|
||||
pass
|
||||
|
||||
break
|
||||
|
||||
continue
|
||||
elif x:
|
||||
pass
|
||||
|
||||
while x:
|
||||
pass
|
||||
|
||||
while x:
|
||||
pass
|
||||
else:
|
||||
pass
|
||||
|
||||
if x:
|
||||
pass
|
||||
elif x:
|
||||
pass
|
||||
else:
|
||||
pass
|
Loading…
Reference in New Issue
Block a user